Skip to content

Commit 1212ec9

Browse files
committed
install: cleanup
1 parent 4c22dd0 commit 1212ec9

File tree

2 files changed

+59
-71
lines changed

2 files changed

+59
-71
lines changed

install.sh

+59-71
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# bash-it installer
33

44
# Show how to use this installer
5-
function _bash-it_show_usage() {
5+
function _bash-it-install-help() {
66
echo -e "\n$0 : Install bash-it"
77
echo -e "Usage:\n$0 [arguments] \n"
88
echo "Arguments:"
@@ -15,83 +15,63 @@ function _bash-it_show_usage() {
1515
exit 0
1616
}
1717

18-
# enable a thing
19-
function _bash-it_load_one() {
20-
file_type=$1
21-
file_to_enable=$2
22-
mkdir -p "$BASH_IT/${file_type}/enabled"
23-
24-
dest="${BASH_IT}/${file_type}/enabled/${file_to_enable}"
25-
if [ ! -e "${dest}" ]; then
26-
ln -sf "../available/${file_to_enable}" "${dest}"
27-
else
28-
echo "File ${dest} exists, skipping"
29-
fi
30-
}
31-
3218
# Interactively enable several things
33-
function _bash-it_load_some() {
19+
function _bash-it-install-enable() {
20+
local file_type single_type enable_func file_name just_the_name RESP
3421
file_type=$1
3522
single_type=$(echo "$file_type" | sed -e "s/aliases$/alias/g" | sed -e "s/plugins$/plugin/g")
3623
enable_func="_enable-$single_type"
37-
[ -d "$BASH_IT/$file_type/enabled" ] || mkdir "$BASH_IT/$file_type/enabled"
38-
for path in "$BASH_IT/${file_type}/available/"[^_]*; do
39-
file_name=$(basename "$path")
24+
for path in "${BASH_IT?}/${file_type}/available/"[^_]*; do
25+
file_name="${path##*/}"
4026
while true; do
41-
just_the_name="${file_name%%.*}"
27+
just_the_name="${file_name%.${file_type}.bash}"
4228
read -r -e -n 1 -p "Would you like to enable the $just_the_name $file_type? [y/N] " RESP
4329
case $RESP in
4430
[yY])
45-
$enable_func "$just_the_name"
31+
"$enable_func" "$just_the_name"
4632
break
4733
;;
4834
[nN] | "")
4935
break
5036
;;
5137
*)
52-
echo -e "\033[91mPlease choose y or n.\033[m"
38+
echo -e "${echo_orange:-}Please choose y or n.${echo_normal:-}"
5339
;;
5440
esac
5541
done
5642
done
5743
}
5844

5945
# Back up existing profile
60-
function _bash-it_backup() {
61-
test -w "$HOME/$CONFIG_FILE" \
62-
&& cp -aL "$HOME/$CONFIG_FILE" "$HOME/$CONFIG_FILE.bak" \
63-
&& echo -e "\033[0;32mYour original $CONFIG_FILE has been backed up to $CONFIG_FILE.bak\033[0m"
46+
function _bash-it-install-backup-config() {
47+
test -w "${HOME?}/${CONFIG_FILE?}" \
48+
&& cp -aL "${HOME?}/${CONFIG_FILE?}" "${HOME?}/${CONFIG_FILE?}.bak" \
49+
&& echo -e "${echo_green:-}Your original ${CONFIG_FILE?} has been backed up to ${CONFIG_FILE?}.bak${echo_normal:-}"
6450
}
6551

6652
# Back up existing profile and create new one for bash-it
67-
function _bash-it_backup_new() {
68-
_bash-it_backup
69-
sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" > "$HOME/$CONFIG_FILE"
70-
echo -e "\033[0;32mCopied the template $CONFIG_FILE into ~/$CONFIG_FILE, edit this file to customize bash-it\033[0m"
53+
function _bash-it-install-backup-new() {
54+
_bash-it-install-backup-config
55+
sed "s|{{BASH_IT}}|${BASH_IT?}|" "${BASH_IT?}/template/bashrc.template.bash" > "${HOME?}/${CONFIG_FILE?}"
56+
echo -e "${echo_green:-}Copied the template ${CONFIG_FILE?} into ~/${CONFIG_FILE?}, edit this file to customize bash-it${echo_normal:-}"
7157
}
7258

7359
# Back up existing profile and append bash-it templates at the end
74-
function _bash-it_backup_append() {
75-
local profile_strings=('if [[ -f ~/.profile ]]; then' 'source ~/.profile' 'fi' 'if [[ -f ~/.bashrc ]]; then' 'source ~/.bashrc' 'fi')
76-
_bash-it_backup
77-
(sed "s|{{BASH_IT}}|$BASH_IT|" "$BASH_IT/template/bash_profile.template.bash" | tail -n +2) >> "$HOME/$CONFIG_FILE"
78-
if [[ ! -f ~/.bash_profile ]]; then
79-
printf '%s\n\t%s\n%s\n%s\n\t%s\n%s\n' "${profile_strings[@]}" > ~/.bash_profile
80-
else
81-
printf '\e[0;33m%s\n\t%s\n\t\t%s\n\t%s\n\t%s\n\t\t%s\n\t%s\n\e[0m' "You may need to update your ~/.bash_profile (or ~/.profile) to source your ~/.bashrc:" "${profile_strings[@]}"
82-
fi
83-
echo -e "\033[0;32mBash-it template has been added to your $CONFIG_FILE\033[0m"
60+
function _bash-it-install-backup-append() {
61+
_bash-it-install-backup-config
62+
(sed "s|{{BASH_IT}}|${BASH_IT?}|" "${BASH_IT?}/template/bashrc.template.bash" | tail -n +2) >> "${HOME?}/${CONFIG_FILE?}"
63+
echo -e "${echo_green:-}Bash-it template has been added to your ${CONFIG_FILE?}${echo_normal:-}"
8464
}
8565

86-
function _bash-it_check_for_backup() {
87-
if ! [[ -e "$HOME/$BACKUP_FILE" ]]; then
66+
function _bash-it-install-backup-check() {
67+
if ! [[ -e "${HOME?}/$BACKUP_FILE" ]]; then
8868
return
8969
fi
90-
echo -e "\033[0;33mBackup file already exists. Make sure to backup your .bashrc before running this installation.\033[0m" >&2
70+
echo -e "${echo_yellow:-}Backup file already exists. Make sure to backup your .bashrc before running this installation.${echo_normal:-}" >&2
9171

9272
if [[ -z "${overwrite_backup}" ]]; then
9373
while [[ -z "${silent}" ]]; do
94-
read -e -n 1 -r -p "Would you like to overwrite the existing backup? This will delete your existing backup file ($HOME/$BACKUP_FILE) [y/N] " RESP
74+
read -e -n 1 -r -p "Would you like to overwrite the existing backup? This will delete your existing backup file (${HOME?}/$BACKUP_FILE) [y/N] " RESP
9575
case $RESP in
9676
[yY])
9777
overwrite_backup=true
@@ -101,28 +81,28 @@ function _bash-it_check_for_backup() {
10181
break
10282
;;
10383
*)
104-
echo -e "\033[91mPlease choose y or n.\033[m"
84+
echo -e "${echo_orange:-}Please choose y or n.${echo_normal:-}"
10585
;;
10686
esac
10787
done
10888
fi
10989
if [[ -z "${overwrite_backup}" ]]; then
110-
echo -e "\033[91mInstallation aborted. Please come back soon!\033[m"
90+
echo -e "${echo_orange:-}Installation aborted. Please come back soon!${echo_normal:-}"
11191
if [[ -n "${silent}" ]]; then
112-
echo -e "\033[91mUse \"-f\" flag to force overwrite of backup.\033[m"
92+
echo -e "${echo_orange:-}Use \"-f\" flag to force overwrite of backup.${echo_normal:-}"
11393
fi
11494
exit 1
11595
else
116-
echo -e "\033[0;32mOverwriting backup...\033[m"
96+
echo -e "${echo_green:-}Overwriting backup...${echo_normal:-}"
11797
fi
11898
}
11999

120-
function _bash-it_modify_config_files() {
121-
_bash-it_check_for_backup
100+
function _bash-it-install-modify-config() {
101+
_bash-it-install-backup-check
122102

123103
if [[ -z "${silent}" ]]; then
124104
while [[ -z "${append_to_config}" ]]; do
125-
read -e -n 1 -r -p "Would you like to keep your $CONFIG_FILE and append bash-it templates at the end? [y/N] " choice
105+
read -e -n 1 -r -p "Would you like to keep your ${CONFIG_FILE?} and append bash-it templates at the end? [y/N] " choice
126106
case $choice in
127107
[yY])
128108
append_to_config=true
@@ -132,17 +112,24 @@ function _bash-it_modify_config_files() {
132112
break
133113
;;
134114
*)
135-
echo -e "\033[91mPlease choose y or n.\033[m"
115+
echo -e "${echo_orange:-}Please choose y or n.${echo_normal:-}"
136116
;;
137117
esac
138118
done
139119
fi
140-
if [[ -n "${append_to_config}" ]]; then
120+
if [[ -n "${append_to_config:-}" ]]; then
141121
# backup/append
142-
_bash-it_backup_append
122+
_bash-it-install-backup-append
143123
else
144124
# backup/new by default
145-
_bash-it_backup_new
125+
_bash-it-install-backup-new
126+
fi
127+
local choice profile_string=$'if [[ -s ~/.profile ]]; then\n\tsource ~/.profile\nfi\nif [[ -s ~/.bashrc ]]; then\n\tsource ~/.bashrc\nfi'
128+
if [[ ! -f ~/.bash_profile ]]; then
129+
printf '%s\n' "${profile_string}" > ~/.bash_profile
130+
else
131+
printf "${echo_yellow:-}%s${echo_normal:-}" "You may need to update your ~/.bash_profile (or ~/.profile) to source your ~/.bashrc:"
132+
printf '%s\n' "${profile_string}"
146133
fi
147134
}
148135

@@ -163,7 +150,7 @@ OPTIND=1
163150
while getopts "hsinaf" opt; do
164151
case "$opt" in
165152
"h")
166-
_bash-it_show_usage
153+
_bash-it-install-help
167154
exit 0
168155
;;
169156
"s") silent=true ;;
@@ -172,7 +159,7 @@ while getopts "hsinaf" opt; do
172159
"a") append_to_config=true ;;
173160
"f") overwrite_backup=true ;;
174161
"?")
175-
_bash-it_show_usage >&2
162+
_bash-it-install-help >&2
176163
exit 1
177164
;;
178165
esac
@@ -181,52 +168,53 @@ done
181168
shift $((OPTIND - 1))
182169

183170
if [[ -n "${silent}" && -n "${interactive}" ]]; then
184-
echo -e "\033[91mOptions --silent and --interactive are mutually exclusive. Please choose one or the other.\033[m"
171+
echo -e "${echo_orange:-}Options --silent and --interactive are mutually exclusive. Please choose one or the other.${echo_normal:-}"
185172
exit 1
186173
fi
187174

188175
if [[ -n "${no_modify_config}" && -n "${append_to_config}" ]]; then
189-
echo -e "\033[91mOptions --no-modify-config and --append-to-config are mutually exclusive. Please choose one or the other.\033[m"
176+
echo -e "${echo_orange:-}Options --no-modify-config and --append-to-config are mutually exclusive. Please choose one or the other.${echo_normal:-}"
190177
exit 1
191178
fi
192179

193180
BASH_IT="$(cd "${BASH_SOURCE%/*}" && pwd)"
194181

195-
CONFIG_FILE=.bashrc
182+
CONFIG_FILE=".bashrc"
196183

197-
BACKUP_FILE=$CONFIG_FILE.bak
184+
BACKUP_FILE="${CONFIG_FILE?}.bak"
198185
echo "Installing bash-it"
199186
if [[ -z "${no_modify_config}" ]]; then
200-
_bash-it_modify_config_files
187+
_bash-it-install-modify-config
201188
fi
202189

203190
# Disable auto-reload in case its enabled
204191
export BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE=''
205192
# Load dependencies for enabling components
206-
# shellcheck disable=SC1090
193+
# shellcheck source-path=SCRIPTPATH/vendor/github.com/erichs/composure
207194
source "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
208195
cite _about _param _example _group _author _version
209196
# shellcheck source-path=SCRIPTDIR/lib
210-
source "$BASH_IT/lib/utilities.bash"
211-
# shellcheck source-path=SCRIPTDIR/lib
212197
source "${BASH_IT}/lib/log.bash"
213198
# shellcheck source-path=SCRIPTDIR/lib
214-
source "$BASH_IT/lib/helpers.bash"
199+
source "${BASH_IT?}/lib/utilities.bash"
200+
# shellcheck source-path=SCRIPTDIR/lib
201+
source "${BASH_IT?}/lib/helpers.bash"
202+
# shellcheck source-path=SCRIPTDIR/themes
203+
source "${BASH_IT?}/themes/colors.theme.bash"
215204

216205
if [[ -n $interactive && -z "${silent}" ]]; then
217206
for type in "aliases" "plugins" "completion"; do
218-
echo -e "\033[0;32mEnabling ${type}\033[0m"
219-
_bash-it_load_some "$type"
207+
echo -e "${echo_green:-}Enabling ${type}${echo_normal:-}"
208+
_bash-it-install-enable "$type"
220209
done
221210
else
222211
echo ""
223212
_bash-it-profile-load "default"
224213
fi
225214

226215
echo ""
227-
echo -e "\033[0;32mInstallation finished successfully! Enjoy bash-it!\033[0m"
228-
# shellcheck disable=SC2086
229-
echo -e "\033[0;32mTo start using it, open a new tab or 'source "~/$CONFIG_FILE"'.\033[0m"
216+
echo -e "${echo_green:-}Installation finished successfully! Enjoy bash-it!${echo_normal:-}"
217+
echo -e "${echo_green:-}To start using it, open a new tab or 'source ~/${CONFIG_FILE?}'.${echo_normal:-}"
230218
echo ""
231219
echo "To show the available aliases/completions/plugins, type one of the following:"
232220
echo " bash-it show aliases"
File renamed without changes.

0 commit comments

Comments
 (0)