-
Notifications
You must be signed in to change notification settings - Fork 0
/
INSTALL
executable file
·527 lines (440 loc) · 9.9 KB
/
INSTALL
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/usr/bin/env bash
#
# Dotfiles installer and uninstaller
#
# Set useful shell options.
set -Cueo pipefail
shopt -s extglob
shopt -s nullglob
warning () {
if [[ -t 2 ]]; then
local -r prefix='\033[33m'
local -r suffix='\033[0m'
else
local -r prefix=
local -r suffix=
fi
echo -e "${prefix}${*}${suffix}" >&2
}
error () {
if [[ -t 2 ]]; then
local -r prefix='\033[31m'
local -r suffix='\033[0m'
else
local -r prefix=
local -r suffix=
fi
echo -e "${prefix}${*}${suffix}" >&2
}
# Bare portable realpath.
::realpath () {
if [[ -d "$1" ]]; then
(cd -P "$1" && pwd)
elif [[ "$1" != */* ]]; then
local path
path="$(pwd -P)"
echo "$path/$1"
elif [[ -h "$1" ]]; then
readlink "$1"
else
local base="${1##*/}"
local dir="${1%/*}"
if [[ -d "$dir" ]]; then
(cd -P "$dir" && echo "$PWD/$base")
else
error "::realpath: $1: No such file or directory"
fi
fi
}
parent="$(::realpath "${0%/*}")"
self="${0##*/}"
# Useful ln to create symlink. Pass basename contained destination path
# explicitly.
#
# Good.
# $ symln /path/to/conf ~/.conf
#
# Bad.
# $ symln /path/to/conf ~
symln () {
local src="$1" dst="$2"
if [[ -d "$dst" ]]; then
error "It is a directory: '$dst'"
return 1
fi
mkdir -p "${dst%/*}"
ln -sv "$src" "$dst"
}
is_linux () {
[[ "$OSTYPE" =~ ^linux ]]
}
is_macos () {
[[ "$OSTYPE" =~ ^darwin ]]
}
has () {
type "$1" &>/dev/null
}
has_commands () {
local all=0
for cmd; do
if ! has "$cmd"; then
error "'$cmd is not found on command line."
all=1
fi
done
return "$all"
}
if ! cd "$parent"; then
error 'Fail to change directory to script parent.'
exit 1
fi
help () {
echo -e -n "\
Descriptions:
Dotfiles installer and uninstaller with extra.
Usage:
$self <SUBCOMMAND>
Subcommands:
init Initialize setup
deploy Deploy dotfiles
undeploy Remove deployed dotfiles
rust Install rust tool chain
haskell Install haskell tool chain
lscolor Install ls color config
python-packages
Install python packages with pip
haskell-packages
Install haskell packages with cabal
ghq-binary Install ghq binary from GitHub release
zsh-config Install zsh config
tmux-config Install tmux config
vim-config Install vim config
xmonad-config
Install xmonad config
Options:
-h, --help Show this messages and exit.
"
}
readonly bin_srcpath="${parent}/bin"
readonly bin_dstpath=~/bin
readonly config_srcpath="${parent}/.config"
readonly config_dstpath=~/.config
readonly home_config_srcpath="${parent}/home"
readonly home_config_dstpath=~
# List files in a directory which is specified as an argument. Each files are
# preceded by the directory path.
list_files () {
local src="${1%%*(/)}"
if [[ ! -d "$src" ]]; then
warning "$src is not directory."
return 1
fi
local GLOBIGNORE="$src/*~:$src/*.sw[a-z]"
printf '%s\n' "$src"/*
}
# Make symbolik links from files in source directory to in destination
# directory.
links () {
local -r src_dir="${1%%*(/)}"
local -r dst_dir="${2%%*(/)}"
# validations
if [[ ! "$src_dir" =~ ^/ ]]; then
error "Source directory is not absolute path '$src_dir'"
return 1
fi
if [[ ! -d "$src_dir" ]]; then
error "No such source directory '$src_dir'"
return 1
fi
if [[ ! "$dst_dir" =~ ^/ ]]; then
error "Destination directory is not absolute path '$dst_dir'"
return 1
fi
if [[ ! -d "$dst_dir" ]]; then
error "No such destination directory '$dst_dir'"
return 1
fi
list_files "$src_dir" | while read -r -d $'\n' link_src; do
local f_base="${link_src##*/}"
local link_dst="${dst_dir}/${f_base}"
if [[ -a "${link_dst}" ]]; then
warning "Skip linking from ${link_src} to ${link_dst} because ${f_base} has already existed in ${dst_dir}."
else
command ln -sv "$link_src" "$link_dst"
fi
done
}
has_same_resolved_path () {
local p1 p2
p1="$(::realpath "$1")"
p2="$(::realpath "$2")"
[[ "$p1" == "$p2" ]]
}
# Remove symbolic links from destination directory.
unlinks () {
local -r src_dir="${1%%*(/)}"
local -r dst_dir="${2%%*(/)}"
# validations
if [[ ! "$src_dir" =~ ^/ ]]; then
error "Source directory is not absolute path '$src_dir'"
return 1
fi
if [[ ! -d "$src_dir" ]]; then
error "No such source directory '$src_dir'"
return 1
fi
if [[ ! "$dst_dir" =~ ^/ ]]; then
error "Destination directory is not absolute path '$dst_dir'"
return 1
fi
if [[ ! -d "$dst_dir" ]]; then
error "No such destination directory '$dst_dir'"
return 1
fi
list_files "$src_dir" | while read -r -d $'\n' link_src; do
local f_base="${link_src##*/}"
local link_dst="${dst_dir}/${f_base}"
if [[ ! -h "$link_dst" ]]; then
warning "Skip unlinking $link_dst because no such link."
continue
fi
if ! has_same_resolved_path "$link_src" "$link_dst"; then
warning "Skip unlinking $link_dst because the link is not under control of this repository."
continue
fi
command unlink "$link_dst" && echo "unlink: $link_dst"
done
}
deploy_bins () {
links "$bin_srcpath" "$bin_dstpath"
}
deploy_configs () {
links "$config_srcpath" "$config_dstpath"
}
deploy_home_configs () {
links "$home_config_srcpath" "$home_config_dstpath"
}
undeploy_bins () {
unlinks "$bin_srcpath" "$bin_dstpath"
}
undeploy_configs () {
unlinks "$config_srcpath" "$config_dstpath"
}
undeploy_home_configs () {
unlinks "$home_config_srcpath" "$home_config_dstpath"
}
deploy () {
deploy_bins
deploy_configs
deploy_home_configs
}
undeploy () {
undeploy_bins
undeploy_configs
undeploy_home_configs
}
init () {
mkdir -vp \
~/bin \
~/.config \
~/.cache \
~/.local/share \
;
}
rust () {
# https://rustup.rs/
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
}
haskell () {
# https://www.haskell.org/ghcup/
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
}
lscolor () {
curl -sL https://raw.githubusercontent.com/trapd00r/LS_COLORS/master/LS_COLORS > ~/.dircolors
}
python-packages () {
local -ar packages=(
pipenv
'python-language-server[all]'
flake8
ipdb
tmuxp
kaggle
youtube-dl
vim-vint
# powerline-status
)
pip3 install --user "${packages[@]}"
}
haskell-packages () {
cabal install --overwrite-policy=always ShellCheck
cabal install --overwrite-policy=always --lib xmonad xmonad-contrib X11
cabal install --overwrite-policy=always xmonad
cabal install --overwrite-policy=always xmobar --flags="all_extensions"
}
install_ghq () {
if ! has_commands curl bsdtar grep; then
return 1
fi
local zipfile
if is_linux; then
zipfile='ghq_linux_amd64.zip'
elif is_macos; then
zipfile='ghq_darwin_amd64.zip'
else
error 'This OS is not neither Linux nor macOS, so no prebuild binary on GitHub release.'
return 1
fi
# So bad looking but for compatibility until Bash v4.2. Wanna use lastpipe
# option.
local asset_url
asset_url="$( \
curl -L -H "Accept: application/vnd.github.v3+json" 'https://api.github.com/repos/x-motemen/ghq/releases/latest' \
| grep -E -o "https://.*${zipfile}" \
)"
if [[ -z "$asset_url" ]]; then
error 'The asset url is empty.'
return 1
fi
if [[ ! -d ~/bin ]]; then
warning 'No ~/bin directory exists, so create it to deploy ghq binary.'
mkdir ~/bin
fi
curl -L "$asset_url" | bsdtar -x -f - --include '*/ghq' --strip-components 1 -C ~/bin
if [[ ! -x ~/bin/ghq ]]; then
chmod -v +x ~/bin/ghq
fi
}
zsh-config () {
local -r url='https://github.com/a5ob7r/zsh-config.git'
if ! has_commands git; then
return 1
fi
if has ghq; then
ghq get "$url"
local path
path="$(ghq list --exact --full-path github.com/a5ob7r/zsh-config)"
symln "${path}/.zshrc" ~/.zshrc
else
git clone "$url" ~/zsh-config
symln ~/zsh-config/.zshrc ~/.zshrc
fi
}
tmux-config () {
local -r url='https://github.com/a5ob7r/tmux-config.git'
if ! has_commands git; then
return 1
fi
if has ghq; then
ghq get "$url"
local path
path="$(ghq list --exact --full-path github.com/a5ob7r/tmux-config)"
symln "${path}" ~/.config/tmux
else
git clone "$url" ~/tmux-config
symln ~/tmux-config ~/.config/tmux
fi
}
vim-config () {
local -r url='https://github.com/a5ob7r/vim-config.git'
if ! has_commands git; then
return 1
fi
if has ghq; then
ghq get "$url"
local path
path="$(ghq list --exact --full-path github.com/a5ob7r/vim-config)"
symln "${path}" ~/.vim
else
git clone "$url" ~/vim-config
symln ~/vim-config ~/.vim
fi
}
xmonad-config () {
local -r url='https://github.com/a5ob7r/xmonad-config.git'
if ! has_commands git; then
return 1
fi
if has ghq; then
ghq get "$url"
local path
path="$(ghq list --exact --full-path github.com/a5ob7r/xmonad-config)"
symln "${path}" ~/.xmonad
else
git clone "$url" ~/xmonad-config
symln ~/xmonad-config ~/.xmonad
fi
}
# main
# Argument array
declare -a args=()
# Parse arguments
while (( $# )); do
case "$1" in
--debug )
set -x
shift
;;
-h | --help )
help
exit 0
;;
-* )
error "Invalid option '$1'"
exit 1
;;
* )
args+=("$1")
shift
;;
esac
done
if ! (( ${#args[@]} )); then
help
exit 0
fi
case "${args[0]}" in
init )
init
;;
deploy )
deploy
;;
undeploy )
undeploy
;;
rust )
rust
;;
haskell )
haskell
;;
lscolor )
lscolor
;;
python-packages )
python-packages
;;
haskell-packages )
haskell-packages
;;
ghq-binary )
install_ghq
;;
zsh-config )
zsh-config
;;
tmux-config )
tmux-config
;;
vim-config )
vim-config
;;
xmonad-config )
xmonad-config
;;
* )
error "Invalid subcommand '${args[0]}'"
exit 1
;;
esac