forked from zthxxx/jovial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer.sh
executable file
·208 lines (155 loc) · 5.7 KB
/
installer.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
#!/usr/bin/env bash
# Usage
# $ cat installer.sh | sudo -E bash -s $USER
#
# use sudo to make this script has access, and use '-E' for preserve most env variables;
# use '-s $USER' to pass "real target user" to this install script
COLOR_RED=`tput setaf 9`
COLOR_GREEN=`tput setaf 10`
COLOR_YELLOW=`tput setaf 11`
SGR_RESET=`tput sgr 0`
# bash strict mode (https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425)
set -xo pipefail
S_USER="$USER"
S_HOME="$HOME"
# set the real target user by params
if [[ -n $1 ]]; then
S_USER="$1"
S_HOME=`sudo -u "${S_USER}" -i echo '$HOME'`
fi
# setup oh-my-zsh env variables
if [[ -z "${ZSH}" ]]; then
# If ZSH is not defined, use the current script's directory.
ZSH="${S_HOME}/.oh-my-zsh"
fi
# Set ZSH_CUSTOM to the path where your custom config files
ZSH_CUSTOM="${ZSH}/custom"
log.info() {
set +x
echo -e "\n${COLOR_YELLOW} $@ ${SGR_RESET}\n"
set -x
}
log.success() {
set +x
echo -e "\n${COLOR_GREEN} $@ ${SGR_RESET}\n"
set -x
}
log.error() {
set +x
echo -e "\n${COLOR_RED} $@ ${SGR_RESET}\n" >&2
set -x
}
is-command() { command -v $@ &> /dev/null; }
# it's same as `realpath <file>`, but `realpath` is GNU only and not builtin
prel-realpath() {
perl -MCwd -e 'print Cwd::realpath($ARGV[0]),qq<\n>' $1
}
install-via-manager() {
local package="$1"
log.info "[jovial] install package: ${package}"
if is-command brew; then
sudo -Eu ${S_USER} brew install ${package}
elif is-command apt; then
apt install -y ${package}
elif is-command apt-get; then
apt-get install -y ${package}
elif is-command yum; then
yum install -y ${package}
elif is-command pacman; then
pacman -S --noconfirm --needed ${package}
fi
}
install.packages() {
local packages=( $@ )
log.info "[jovial] install packages: ${packages[@]}"
local package
for package in ${packages[@]}; do
install-via-manager ${package}
done
}
install.zsh() {
log.info "[jovial] detect whether installed zsh"
if [[ "${SHELL}" =~ '/zsh$' ]]; then
log.success "[jovial] default shell is zsh, skip to install"
return 0
fi
if is-command zsh || install.packages zsh; then
log.info "[jovial] switch default login shell to zsh"
chsh -s `command -v zsh` ${S_USER}
return 0
else
log.error "[ERROR][jovial] cannot find or install zsh, please install zsh manually"
return 1
fi
}
install.ohmyzsh() {
log.info "[jovial] detect whether installed oh-my-zsh"
if [[ -d ${ZSH} && -d ${ZSH_CUSTOM} ]]; then
log.success "[jovial] oh-my-zsh detected, skip to install"
return 0
fi
log.info "[jovial] this theme base on oh-my-zsh, now will install it"
if ! is-command git; then
install.packages git
fi
# https://ohmyz.sh/#install
curl -sSL -H 'Cache-Control: no-cache' https://github.com/ohmyzsh/ohmyzsh/raw/master/tools/install.sh | sudo -Eu ${S_USER} sh
}
install.zsh-plugins() {
log.info "[jovial] install zsh plugins"
local plugin_dir="${ZSH_CUSTOM}/plugins"
if ! is-command git; then
install.packages git
fi
install.packages autojump terminal-notifier source-highlight
if [[ ! -d ${plugin_dir}/zsh-autosuggestions ]]; then
log.info "[jovial] install plugin zsh-autosuggestions"
sudo -Eu ${S_USER} git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions.git "${plugin_dir}/zsh-autosuggestions"
fi
if [[ ! -d ${plugin_dir}/zsh-syntax-highlighting ]]; then
log.info "[jovial] install plugin zsh-syntax-highlighting"
sudo -Eu ${S_USER} git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting.git "${plugin_dir}/zsh-syntax-highlighting"
fi
log.info "[jovial] install plugin zsh-history-enquirer"
curl -sSL -H 'Cache-Control: no-cache' https://github.com/zthxxx/zsh-history-enquirer/raw/master/scripts/installer.zsh | sudo -Eu ${S_USER} zsh
log.info "[jovial] setup oh-my-zsh plugins in ~/.zshrc"
local plugins=(
git
autojump
urltools
bgnotify
zsh-autosuggestions
zsh-syntax-highlighting
zsh-history-enquirer
jovial
)
local plugin_str="${plugins[@]}"
plugin_str="\n ${plugin_str// /\\n }\n"
perl -0i -pe "s/^plugins=\(.*?\) *$/plugins=(${plugin_str})/gms" $(prel-realpath "${S_HOME}/.zshrc")
}
preference-zsh() {
log.info "[jovial] preference zsh in ~/.zshrc"
if is-command brew; then
perl -i -pe "s/.*HOMEBREW_NO_AUTO_UPDATE.*//gms" $(prel-realpath "${S_HOME}/.zshrc")
echo "export HOMEBREW_NO_AUTO_UPDATE=true" >> "${S_HOME}/.zshrc"
fi
install.zsh-plugins
}
install.theme() {
log.info "[jovial] install theme 'jovial'"
local theme_name="jovial"
local git_prefix="https://github.com/zthxxx/${theme_name}/raw/master"
local theme_remote="${git_prefix}/${theme_name}.zsh-theme"
local plugin_remote="${git_prefix}/${theme_name}.plugin.zsh"
local custom_dir="${ZSH_CUSTOM:-"${S_HOME}/.oh-my-zsh/custom"}"
sudo -Eu ${S_USER} mkdir -p "${custom_dir}/themes" "${custom_dir}/plugins/${theme_name}"
local theme_local="${custom_dir}/themes/${theme_name}.zsh-theme"
local plugin_local="${custom_dir}/plugins/${theme_name}/${theme_name}.plugin.zsh"
sudo -Eu ${S_USER} curl -sSL -H 'Cache-Control: no-cache' "${theme_remote}" -o "${theme_local}"
sudo -Eu ${S_USER} curl -sSL -H 'Cache-Control: no-cache' "${plugin_remote}" -o "${plugin_local}"
perl -i -pe "s/^ZSH_THEME=.*/ZSH_THEME=\"${theme_name}\"/g" $(prel-realpath "${S_HOME}/.zshrc")
}
(install.zsh && install.ohmyzsh) || exit 1
install.theme
preference-zsh
log.success "[jovial] installed"