-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoladots.sh
More file actions
executable file
·346 lines (301 loc) · 11.5 KB
/
moladots.sh
File metadata and controls
executable file
·346 lines (301 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env bash
# (.dot)file (man)ager
IFS=$'\n'
VERSION="v0.1.0"
# check if tput exists
if ! command -v tput &> /dev/null; then
# tput could not be found
BOLD=""
RESET=""
FG_SKYBLUE=""
FG_ORANGE=""
BG_AQUA=""
FG_BLACK=""
FG_ORANGE=""
UL=""
RUL=""
else
BOLD=$(tput bold)
RESET=$(tput sgr0)
FG_SKYBLUE=$(tput setaf 106)
FG_AQUA=$(tput setaf 45)
BG_AQUA=$(tput setab 45)
FG_BLACK=$(tput setaf 16)
FG_ORANGE=$(tput setaf 178)
UL=$(tput smul)
RUL=$(tput rmul)
fi
logo() {
# print moladots logo
printf "${BOLD}${FG_SKYBLUE}%s\n" ""
echo "███╗ ███╗ ██████╗ ██╗ █████╗ ██╗ ██╗ █████╗ ██████╗ ███████╗██╗ ██╗";
echo "████╗ ████║██╔═══██╗██║ ██╔══██╗╚██╗ ██╔╝██╔══██╗██╔══██╗██╔════╝██║ ██║";
echo "██╔████╔██║██║ ██║██║ ███████║ ╚████╔╝ ███████║██║ ██║█████╗ ██║ ██║";
echo "██║╚██╔╝██║██║ ██║██║ ██╔══██║ ╚██╔╝ ██╔══██║██║ ██║██╔══╝ ╚██╗ ██╔╝";
echo "██║ ╚═╝ ██║╚██████╔╝███████╗██║ ██║ ██║ ██║ ██║██████╔╝███████╗ ╚████╔╝ ";
echo "╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═════╝ ╚══════╝ ╚═══╝ ";
echo " ";
printf "${RESET}\n%s" ""
}
## install apt dependencies
install_apt() {
which $1 &> /dev/null
if [ $? -ne 0 ]; then
echo "** Installing: ${1}... 👇"
sudo apt install -y $1
echo "** Successfully installed $1 👍 "
else
echo "** ¡Already Installed ${1}! 👌 "
fi
}
git_safe() {
# check if git exists
install_apt git git-flow
if ! command -v git &> /dev/null; then
printf "%s\n\n" "${BOLD}${FG_SKYBLUE} dotfiles-mola ${RESET}"
echo "Can't work without Git 😞"
exit 1
fi
}
# function called by trap
catch_ctrl+c() {
goodbye
exit
}
trap 'catch_ctrl+c' SIGINT
repo_check(){
# check if dotfile repo is present inside MOLADOTS_DEST
MOLADOTS_REPO_NAME=$(basename "${MOLADOTS_REPO}")
# all paths are relative to HOME
if [[ -d ${MOLADOTS_DEST} ]]; then
printf "\n%s\n" "Found ${BOLD}${MOLADOTS_REPO_NAME}${RESET} as dotfile repo in ${BOLD}${MOLADOTS_DEST}/${RESET}"
else
printf "\n\n%s\n" "[❌] ${BOLD}${MOLADOTS_REPO_NAME}${RESET} not present inside path ${BOLD}${MOLADOTS_DEST}${RESET}"
read -p "Should I clone it ? [Y/n]: " -n 1 -r USER_INPUT
USER_INPUT=${USER_INPUT:-y}
case $USER_INPUT in
[y/Y]* ) clone_dotrepo "$MOLADOTS_DEST" "$MOLADOTS_REPO" ;;
[n/N]* ) printf "\n%s" "${BOLD}${MOLADOTS_REPO_NAME}${RESET} not found";;
* ) printf "\n%s\n" "[❌] Invalid Input 🙄, Try Again";;
esac
fi
}
find_dotfiles() {
printf "\n"
# while read -r value; do
# dotfiles+=($value)
# done < <( find "${HOME}" -maxdepth 1 -name ".*" -type f )
readarray -t dotfiles < <( find "${MOLADOTS_DEST}/programs" -maxdepth 1 -name ".*" -type f )
printf '%s\n' "${dotfiles[@]}"
}
add_env() {
[[ "$MOLADOTS_DEST" && "$MOLADOTS_REPO" ]] && return
# export environment variables
printf "\n%s\n" "Exporting env variables MOLADOTS_DEST & MOLADOTS_REPO ..."
current_shell=$(basename "$SHELL")
if [[ $current_shell == "zsh" ]]; then
echo "export MOLADOTS_REPO=$1 MOLADOTS_DEST=$2" >> "$HOME"/.zshrc
elif [[ $current_shell == "bash" ]]; then
# assume we have a fallback to bash
echo "export MOLADOTS_REPO=$1 MOLADOTS_DEST=$2" >> "$HOME"/.bashrc
else
echo "Couldn't export ${BOLD}MOLADOTS_REPO=$1${RESET} and ${BOLD}MOLADOTS_DEST=$2${RESET}"
echo "Consider exporting them manually."
exit 1
fi
printf "\n%s" "Configuration for SHELL: ${BOLD}$current_shell${RESET} has been updated."
}
goodbye() {
#printf "\a\n\n%s\n" "${BOLD}Thanks for using moladots 🖖.${RESET}"
printf "%s\n" "for more updates.${RESET}"
printf "%s\n" "${BOLD}Report Bugs 🐛 @ ${UL}https://github.com/molayadev/moladots/issues${RUL}${RESET}"
cowsay -f molayadev "${FG_AQUA} Thanks for using moladots 🖖. ${RESET}"
}
dot_pull() {
# pull changes (if any) from the remote repo
printf "\n%s\n" "${BOLD}Pulling dotfiles ...${RESET}"
dot_repo="${MOLADOTS_DEST}"
printf "\n%s\n" "Pulling changes in $dot_repo"
GET_BRANCH=$(git remote show origin | awk '/HEAD/ {print $3}')
printf "\n%s\n" "Pulling from ${BOLD}${GET_BRANCH}"
git -C "$dot_repo" pull origin "${GET_BRANCH}"
exit 0;
}
diff_check() {
[[ -z $1 ]] && local file_arr
# dotfiles in repository
readarray -t dotfiles_repo < <( find "${MOLADOTS_DEST}" -maxdepth 1 -name ".*" -type f )
# check length here ?
for i in "diff_check${!dotfiles_repo[@]}"
do
dotfile_name=$(basename "${dotfiles_repo[$i]}")
# compare the HOME version of dotfile to that of repo
diff=$(diff -u --suppress-common-lines --color=always "${dotfiles_repo[$i]}" "${MOLADOTS_DEST}/programs")
if [[ $diff != "" ]]; then
if [[ $1 == "show" ]]; then
printf "\n\n%s" "Running diff between ${BOLD} ${FG_ORANGE}${MOLADOTS_DEST}${RESET} and "
printf "%s\n" "${BOLD}${FG_ORANGE}${dotfiles_repo[$i]}${RESET}"
printf "%s\n\n" "$diff"
fi
file_arr+=("${dotfile_name}")
fi
done
[ ${#file_arr} == 0 ] && printf "\n%s\n" "${BOLD}No Changes in dotfiles.${RESET}";return
}
show_diff_check() {
diff_check "show"
}
dot_push() {
diff_check
if [[ ${#file_arr} != 0 ]]; then
printf "\n%s\n" "${BOLD}Following dotfiles changed${RESET}"
for file in "${file_arr[@]}"; do
echo "$file"
cp "${HOME}/$file" "${MOLADOTS_DEST}"
done
dot_repo="${MOLADOTS_DEST})"
git -C "$dot_repo" add -A
echo "${BOLD}Enter Commit Message (Ctrl + d to save): ${RESET}"
commit=$(</dev/stdin)
printf "\n"
git -C "$dot_repo" commit -m "$commit"
# Run Git Push
git -C "$dot_repo" push
else
return
fi
}
clone_dotrepo (){
# clone the repo in the destination directory
MOLADOTS_DEST=$1
MOLADOTS_REPO=$2
if git -C "${MOLADOTS_DEST}" clone "${MOLADOTS_REPO}"; then
if [[ $MOLADOTS_REPO && $MOLADOTS_DEST ]]; then
add_env "$MOLADOTS_REPO" "$MOLADOTS_DEST"
fi
printf "\n%s" "[✔️ ] moladots successfully configured"
else
# invalid arguments to exit, Repository Not Found
printf "\n%s" "[❌] $MOLADOTS_REPO Unavailable. Exiting !"
exit 1
fi
}
initial_setup() {
printf "\n\n%s\n" "First time use 🔥, Set Up ${BOLD}moladots${RESET}"
printf "%s\n" "...................................."
read -p "➤ Enter dotfiles repository URL : " -r MOLADOTS_REPO
read -p "➤ Where should I clone ${BOLD}$(basename "${MOLADOTS_REPO}")${RESET} (${HOME}/..): " -r MOLADOTS_DEST
MOLADOTS_DEST=${MOLADOTS_DEST:-$HOME}
if [[ -d "$MOLADOTS_DEST" ]]; then
printf "\n%s\r\n" "${BOLD}Calling 📞 Git ... ${RESET}"
clone_dotrepo "$MOLADOTS_DEST" "$MOLADOTS_REPO"
printf "\n%s\n" "Open a new terminal or source your shell config"
else
printf "\n%s" "[❌]${BOLD}$MOLADOTS_DEST${RESET} Not a Valid directory"
exit 1
fi
}
manage() {
while :
do
printf "\n%s" "[${BOLD}0${RESET}] Install Development Tools"
printf "\n%s" "[${BOLD}1${RESET}] Show diff"
printf "\n%s" "[${BOLD}2${RESET}] Push changed dotfiles"
printf "\n%s" "[${BOLD}3${RESET}] Pull latest changes"
printf "\n%s" "[${BOLD}4${RESET}] List all dotfiles"
printf "\n%s\n" "[${BOLD}q/Q${RESET}] Quit Session"
read -p "What do you want me to do ? [${BOLD}0${RESET}]: " -r USER_INPUT
# Default choice is [1], See Parameter Expansion
USER_INPUT=${USER_INPUT:0}
case $USER_INPUT in
[0]* ) devtools_menu;;
[1]* ) show_diff_check;;
[2]* ) dot_push;;
[3]* ) dot_pull;;
[4]* ) find_dotfiles;;
[q/Q]* ) goodbye
exit;;
* ) printf "\n%s\n" "[❌]Invalid Input 🙄, Try Again";;
esac
done
}
devtools_menu() {
while :
do
printf "\n\t%s" "[X] ******* ${FG_AQUA}Development Tools${RESET} ******"
#printf "\n\t%s" "[${BOLD}b/B${RESET}] [All ${FG_ORANGE}BACK${RESET}] Install Backend Dev Tools"
#printf "\n\t%s" "[${BOLD}f/F${RESET}] [All ${FG_SKYBLUE}FRONT${RESET}] Install Frontend Dev Tools"
#printf "\n\t%s" "[${BOLD}d/D${RESET}] [All ${FG_AQUA}DEV${RESET}] Install Common Dev Tools"
printf "\n\t%s" "[${BOLD}s/S${RESET}] [${FG_ORANGE}BACK${RESET}] Install SDKMAN"
printf "\n\t%s" "[${BOLD}j/J${RESET}] [${FG_ORANGE}BACK${RESET}] Install Java JDK"
printf "\n\t%s" "[${BOLD}i/I${RESET}] [${FG_ORANGE}BACK${RESET}] Install IntelliJ Community"
printf "\n\t%s" "[${BOLD}m/M${RESET}] [${FG_ORANGE}BACK${RESET}] Install Maven"
printf "\n\t%s" "[${BOLD}a/A${RESET}] [${FG_ORANGE}BACK${RESET}] Install Android Studio"
printf "\n\t%s" "[${BOLD}v/V${RESET}] [${FG_SKYBLUE}FRONT${RESET}] Install VsCode"
printf "\n\t%s" "[${BOLD}n/N${RESET}] [${FG_SKYBLUE}FRONT${RESET}] Install NVM (Stable Node)"
#printf "\n\t%s" "[${BOLD}e/E${RESET}] [${FG_AQUA}DEV${RESET}] Install STS Eclipse"
printf "\n\t%s" "[${BOLD}z/Z${RESET}] [${FG_AQUA}DEV${RESET}] Install Shell Zsh"
printf "\n\t%s" "[${BOLD}p/P${RESET}] [${FG_AQUA}DEV${RESET}] Install Postman"
printf "\n\t%s" "[${BOLD}0${RESET}] [${FG_SKYBLUE}FRONT${RESET}] Install Angular CLI (*NVM)"
printf "\n\t%s" "[${BOLD}1${RESET}] [${FG_SKYBLUE}FRONT${RESET}] Install Ionic CLI (*NVM)"
printf "\n%s\n" "[${BOLD}q/Q${RESET}] Quit Session"
read -p "What do you want me to install ? [${BOLD}0${RESET}]: " -r USER_INPUT
# Default choice is [1], See Parameter Expansion
USER_INPUT=${USER_INPUT:b}
case $USER_INPUT in
[b/B]* ) ;;
[f/F]* ) ;;
[d/D]* ) ;;
[s/S]* ) bash "${MOLADOTS_DEST}/programs/install_sdkman.sh";;
[j/J]* ) bash "${MOLADOTS_DEST}/programs/install_jdk.sh";;
[i/I]* ) bash "${MOLADOTS_DEST}/programs/install_ideacom.sh";;
[m/M]* ) bash "${MOLADOTS_DEST}/programs/install_maven.sh";;
[a/A]* ) bash "${MOLADOTS_DEST}/programs/install_android_studio.sh";;
[v/V]* ) bash "${MOLADOTS_DEST}/programs/install_code.sh";;
[n/N]* ) bash "${MOLADOTS_DEST}/programs/install_nvm.sh" n;;
[z/Z]* ) bash "${MOLADOTS_DEST}/programs/install_zsh.sh";;
[p/P]* ) bash "${MOLADOTS_DEST}/programs/install_postman.sh";;
[0]* ) bash "${MOLADOTS_DEST}/programs/install_nvm.sh" a;;
[1]* ) bash "${MOLADOTS_DEST}/programs/install_nvm.sh" i;;
[q/Q]* ) goodbye
exit;;
* ) printf "\n%s\n" "[❌]Invalid Input 🙄, Try Again";;
esac
done
}
intro() {
BOSS_NAME=$LOGNAME
printf "\n\a%s" "Hi ${BOLD}${FG_ORANGE}$BOSS_NAME${RESET} 👋"
logo
}
init_check() {
# Check wether its a first time use or not
if [[ -z ${MOLADOTS_REPO} && -z ${MOLADOTS_DEST} ]]; then
sudo apt update
echo "** Adding tput command 🎨"
install_apt ncurses-bin
echo "** Adding cowsay command 🐄"
install_apt cowsay
sudo cp ${MOLADOTS_DEST}/assets/cows/* /usr/share/cowsay/cows/
# Trying install git before use it.
git_safe
initial_setup
goodbye
else
repo_check
manage
fi
}
if [[ $1 == "version" || $1 == "--version" || $1 == "-v" ]]; then
if [[ -d "$HOME/moladots" ]]; then
latest_tag=$(git -C "$HOME/moladots" describe --tags --abbrev=0)
latest_tag_push=$(git -C "$HOME/moladots" log -1 --format=%ai "${latest_tag}")
echo "${BOLD}moladots ${latest_tag} ${RESET}"
echo "Released on: ${BOLD}${latest_tag_push}${RESET}"
else
echo "${BOLD}moladots ${VERSION}${RESET}"
fi
exit
fi
intro
init_check