-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.sh
executable file
·115 lines (96 loc) · 2.46 KB
/
init.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
#!/usr/bin/env bash
set -euo | IFS=$'\n\t'
source libshell
__dirname="$(Path_dirname "${BASH_SOURCE[0]}")"
################################################
# main logic
################################################
declare -gA configFiles=(
["shell/.bash_config"]=""
["shell/.zsh_config"]=""
["prettier/.prettierrc.js"]=""
["editorconfig/.editorconfig"]=""
["eslint/.eslintrc.js"]=""
["stylelint/.stylelintrc.js"]=""
["typescript/tsconfig.json"]=""
["Vim/.vimrc"]=""
["./Vim/init.vim"]="$HOME/.config/nvim/init.vim"
["[copy]:git/.gitconfig"]=""
["[copy]:npm/.npmrc"]=""
# prefix with [copy]: to copy file rather than link
# ["[copy]:./Vim/init.vim"]=""
)
install() {
local file=""
local control=""
local key=""
for key in "${!configFiles[@]}"; do
control="$(String_stripEnd "$key" "]:*" 1)"
file="$(String_stripStart "$key" "*]:")"
local src=""
src="$(Path_resolve "$__dirname" "$file")"
local dest="${configFiles[$key]}"
if String_isEmpty "$dest"; then
dest="$HOME/$(Path_basename "$file")"
else
local destDir=""
destDir="$(Path_dirname "$dest")"
if ! File_isDir "$destDir"; then
mkdir -p "$destDir"
fi
fi
if File_isExist "$dest"; then
IO_warn "Already exists: $dest"
else
if String_includes "$control" "copy"; then
IO_info "Copy $file to $dest"
cp -r "$src" "$dest"
else
IO_info "Link $file to $dest"
ln -s "$src" "$dest"
fi
fi
done
IO_success "Done!"
}
uninstall() {
local file=""
local key=""
for key in "${!configFiles[@]}"; do
file="$(String_stripStart "$key" "*]:")"
local dest="${configFiles[$key]}"
if String_isEmpty "$dest"; then
dest="$HOME/$(Path_basename "$file")"
fi
if File_isExist "$dest"; then
if File_isSymlink "$dest"; then
IO_info "Removing link: $dest"
rm "$dest"
else
IO_warn "$dest is not a link, delete manually"
fi
else
IO_warn "Not found: $dest"
fi
done
IO_success "Done!"
}
################################################
# handle arguments
################################################
Args_define "-i --install" "Install the config"
Args_define "-u --uninstall" "Uninstall the config"
Args_define "-h --help" "Show help"
Args_parse "$@"
if Args_has "-i"; then
install
exit 0
fi
if Args_has "-u"; then
uninstall
exit 0
fi
if Args_has "-h" || (($# == 0)); then
Args_help
exit 0
fi