-
Notifications
You must be signed in to change notification settings - Fork 25
/
install.sh
executable file
·291 lines (248 loc) · 7.22 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
#!/bin/sh
#############################################################
# Set development environment on Linux/macOS/Cygwin quickly.
# Author: Vincent Zhang <[email protected]>
# URL: https://github.com/seagle0128/dotfiles
#############################################################
# Variables
DOTFILES=$HOME/.dotfiles
EMACSD=$HOME/.emacs.d
TMUX=$HOME/.tmux
ZSH=$HOME/.zinit
# Get OS informatio
OS=`uname -s`
OSREV=`uname -r`
OSARCH=`uname -m`
# Only enable exit-on-error after the non-critical colorization stuff,
# which may fail on systems lacking tput or terminfo
# set -e
# Use colors, but only if connected to a terminal, and that terminal
# supports them.
if command -v tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
BOLD="$(tput bold)"
NORMAL="$(tput sgr0)"
else
RED=""
GREEN=""
YELLOW=""
BLUE=""
BOLD=""
NORMAL=""
fi
# Functions
is_mac()
{
[ "$OS" = "Darwin" ]
}
is_cygwin()
{
[ "$OSTYPE" = "cygwin" ]
}
is_linux()
{
[ "$OS" = "Linux" ]
}
is_debian() {
command -v apt-get >/dev/null 2>&1
}
is_arch() {
command -v pacman >/dev/null 2>&1
}
is_x86_64() {
[ $(uname -m) = "x86_64" ]
}
is_arm64() {
[ $(uname -m) = "arm64" ]
}
sync_repo() {
local repo_uri="$1"
local repo_path="$2"
local repo_branch="$3"
if [ -z "$repo_branch" ]; then
repo_branch="master"
fi
if [ ! -e "$repo_path" ]; then
mkdir -p "$repo_path"
git clone --depth 1 --branch $repo_branch "https://github.com/$repo_uri.git" "$repo_path"
else
cd "$repo_path" && git pull --rebase --stat origin $repo_branch; cd - >/dev/null
fi
}
install_package() {
if ! command -v ${1} >/dev/null 2>&1; then
if is_mac; then
brew -q install ${1}
elif is_debian; then
sudo apt-get install -y ${1}
elif is_arch; then
pacman -Ssu --noconfirm ${1}
elif is_cygwin; then
apt-cyg install -y ${1}
fi
else
if is_mac; then
brew upgrade -q ${1}
elif is_debian; then
sudo apt-get upgrade -y ${1}
elif is_arch; then
pacman -Ssu --noconfirm ${1}
elif is_cygwin; then
apt-cyg upgrade -y ${1}
fi
fi
}
clean_dotfiles() {
confs="
.gemrc
.gitconfig
.markdownlintrc
.npmrc
.tmux.conf
.vimrc
.zshenv
.zshrc
.zshrc.local
starship.toml
"
for c in ${confs}; do
[ -f $HOME/${c} ] && mv $HOME/${c} $HOME/${c}.bak
done
if [ -f $HOME/.config/starship.toml ]; then
mv $HOME/.config/starship.toml $HOME/.config/starship.toml.bak
fi
[ -d $EMACSD ] && mv $EMACSD $EMACSD.bak
rm -rf $ZSH $TMUX $DOTFILES
rm -f $HOME/.gitignore_global
rm -f $HOME/.tmux.conf
}
YES=0
NO=1
promote_yn() {
eval ${2}=$NO
read -p "$1 [y/N]: " yn
case $yn in
[Yy]* ) eval ${2}=$YES;;
[Nn]*|'' ) eval ${2}=$NO;;
*) eval ${2}=$NO;;
esac
}
# Clean or not?
if [ -d $ZSH ] || [ -d $TMUX ] || [ -d $EMACSD ]; then
promote_yn "${YELLOW}Do you want to reset all configurations?${NORMAL}" "continue"
if [ $continue -eq $YES ]; then
clean_dotfiles
fi
fi
# Generate locale
if is_linux; then
locale -a | grep en_US.utf8 > /dev/null || localedef -v -c -i en_US -f UTF-8 en_US.UTF-8
fi
# Install Brew/apt-cyg
if is_mac && ! command -v brew >/dev/null 2>&1; then
printf "${GREEN}▓▒░ Installing Homebrew...${NORMAL}\n"
# Install homebrew
# Use mirror
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_INSTALL_FROM_API=1
# /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/Homebrew/install@HEAD/install.sh)"
if is_arm64; then
echo >> $HOME/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> $HOME/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
# Tap cask-upgrade
brew tap buo/cask-upgrade
# Install GNU utilities
brew install coreutils
elif is_cygwin && ! command -v apt-cyg >/dev/null 2>&1; then
printf "${GREEN}▓▒░ Installing Apt-Cyg...${NORMAL}\n"
APT_CYG=/usr/local/bin/apt-cyg
curl -fsSL https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg > $APT_CYG
chmod +x $APT_CYG
fi
# Check git
if ! command -v git >/dev/null 2>&1; then
printf "${GREEN}▓▒░ Installing git...${NORMAL}\n"
install_package git
fi
# Check curl
if ! command -v curl >/dev/null 2>&1; then
printf "${GREEN}▓▒░ Installing curl...${NORMAL}\n"
install_package curl
fi
# Check zsh
if ! command -v zsh >/dev/null 2>&1; then
printf "${GREEN}▓▒░ Installing zsh...${NORMAL}\n"
install_package zsh
fi
# ZSH plugin manager
printf "${GREEN}▓▒░ Installing Zinit...${NORMAL}\n"
sh -c "$(curl -fsSL https://git.io/zinit-install)"
# Dotfiles
printf "${GREEN}▓▒░ Installing Dotfiles...${NORMAL}\n"
sync_repo seagle0128/dotfiles $DOTFILES
chmod +x $DOTFILES/install.sh
chmod +x $DOTFILES/install_brew_cask.sh
chmod +x $DOTFILES/install_go.sh
ln -sf $DOTFILES/.zshenv $HOME/.zshenv
ln -sf $DOTFILES/.zshrc $HOME/.zshrc
ln -sf $DOTFILES/.vimrc $HOME/.vimrc
ln -sf $DOTFILES/.tmux.conf.local $HOME/.tmux.conf.local
ln -sf $DOTFILES/.markdownlintrc $HOME/.markdownlintrc
ln -sf $DOTFILES/starship.toml $HOME/.config/starship.toml
cp -u $DOTFILES/.npmrc $HOME/.npmrc
cp -u $DOTFILES/.gemrc $HOME/.gemrc
mkdir -p $HOME/.cargo && cp -u $DOTFILES/cargo.toml $HOME/.cargo/config.toml
cp -u $DOTFILES/.zshrc.local $HOME/.zshrc.local
mkdir -p $HOME/.pip; cp -u $DOTFILES/.pip.conf $HOME/.pip/pip.conf
ln -sf $DOTFILES/.gitignore_global $HOME/.gitignore_global
ln -sf $DOTFILES/.gitconfig_global $HOME/.gitconfig_global
if is_mac; then
cp -u $DOTFILES/.gitconfig_macOS $HOME/.gitconfig
elif is_cygwin; then
cp -u $DOTFILES/.gitconfig_cygwin $HOME/.gitconfig
else
cp -u $DOTFILES/.gitconfig_linux $HOME/.gitconfig
fi
if is_cygwin; then
ln -sf $DOTFILES/.minttyrc $HOME/.minttyrc
fi
# Emacs Configurations
printf "${GREEN}▓▒░ Installing Centaur Emacs...${NORMAL}\n"
sync_repo seagle0128/.emacs.d $EMACSD
# Oh My Tmux
printf "${GREEN}▓▒░ Installing Oh My Tmux...${NORMAL}\n"
sync_repo gpakosz/.tmux $TMUX
ln -sf $TMUX/.tmux.conf $HOME/.tmux.conf
# Packages
printf "${GREEN}▓▒░ Installing packages...${NORMAL}\n"
if is_mac; then
./install_brew.sh
elif is_arch; then
./install_arch.sh
elif is_debian; then
./install_debian.sh
else
printf "Noting to install!"
fi
# Entering zsh
printf "${GREEN}▓▒░ Done. Enjoy!${NORMAL}\n"
if command -v zsh >/dev/null 2>&1; then
if is_cygwin && [ "$SHELL" != "$(which zsh)" ]; then
chsh -s $(which zsh)
printf "${GREEN} You need to logout and login to enable zsh as the default shell.${NORMAL}\n"
fi
env zsh
else
echo "${RED}Error: zsh is not installed${NORMAL}" >&2
exit 1
fi