-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·220 lines (185 loc) · 6.1 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
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
readonly BASEDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly PLATFORM=$(uname)
function main_help() {
cat << EOF
Installation help doc
usage: install.sh <command>
commands:
basic Apply basic shell configurations and auto-completion
dev Setup development configurations (git, vim)
tools Installs various tools (gnupg, tree, lpass)
help This help doc
EOF
}
# basic adds the config loader into the existing bashrc and installs bash-completion.
function install_basic() {
echo
echo "=== basic ==="
# Install shell completion.
bash_completion
local bash_config_file
# bash config is stored in .bash_profile in macOS.
if [[ $PLATFORM == "Darwin" ]]; then
bash_config_file=$HOME/.bash_profile
elif [[ $PLATFORM == "Linux" ]]; then
bash_config_file=$HOME/.bashrc
fi
# Add script loader to the default bashrc.
local source_mybashrc
source_mybashrc="source ${BASEDIR}/mybashrc.bash"
if ! grep -F "$source_mybashrc" $bash_config_file; then
echo $source_mybashrc >> $bash_config_file
else
echo "sourcing mybashrc.bash already exists in ${bash_config_file}"
fi
echo
echo "NOTE: If completion isn't enabled automatically, enable it from ~/.bashrc"
}
# package_manager_check checks if a package manager is installed or not.
# If the platform specific package manager is not installed, quits the program.
function package_manager_check() {
if [[ $PLATFORM == "Darwin" ]]; then
if ! hash brew 2> /dev/null; then
echo
echo "brew not found, install brew and rerun the command"
exit
fi
fi
}
# bash_completion installs bash-completion and downloads various completion scripts.
function bash_completion() {
echo
echo "=== bash_completion ==="
package_manager_check
# Install bash-completion
if [[ $PLATFORM == "Darwin" ]]; then
brew install bash-completion
completionPath="$(brew --prefix)/etc/bash_completion.d"
elif [[ $PLATFORM == "Linux" ]]; then
apt-get update
# Ubuntu docker image comes without curl.
apt-get install curl bash-completion
completionPath="/etc/bash_completion.d"
fi
# Install git-completion.
local git_completion_path
git_completion_path=${completionPath}/git-completion.bash
if [[ ! -f $git_completion_path ]]; then
curl -o $git_completion_path https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
else
echo "git-completion.bash exists";
fi
# Add more completion scripts below.
}
# dev installs development tools and configurations.
function install_dev() {
echo
echo "=== dev env setup ==="
local vscode_settings
# Install git, neovim, and set vscode settings path.
if [[ $PLATFORM == "Darwin" ]]; then
brew install git neovim
vscode_settings="$HOME/Library/Application Support/Code/User"
elif [[ $PLATFORM == "Linux" ]]; then
# Install neovim via ppa.
apt-get install software-properties-common --fix-missing
add-apt-repository ppa:neovim-ppa/stable
apt-get update
apt-get install git neovim
vscode_settings=$HOME/.config/Code/User
fi
gitconfig
neovim_config
vimconfig
# If vscode config directory exists, symlink the settings.
if [[ -d $vscode_settings ]]; then
ln -sf ${BASEDIR}/vscode_settings.json "${vscode_settings}/settings.json"
fi
echo
echo "Development environment ready!"
}
function neovim_config() {
echo "Linking neovim config to vimrc"
mkdir -p ~/.config/nvim/
cat > ~/.config/nvim/init.vim << EOF
set runtimepath^=~/.vim runtimepath+=~/.vim/after
let &packpath = &runtimepath
source ~/.vimrc
EOF
vim +PluginInstall +qall
}
# gitconfig creates symlinks of gitconfig and gitignore files in $HOME.
# This would overwrite any existing gitconfig and gitignore.
function gitconfig() {
echo "Setting up gitconfig"
ln -sf ${BASEDIR}/.gitconfig ~/.gitconfig
echo "Setting up gitignore"
ln -sf ${BASEDIR}/.gitignore ~/.gitignore
}
# vimconfig creates symlink of vimrc and installs vundle plugin manager and all
# the plugins.
function vimconfig() {
echo "Setting up vimrc"
ln -sf ${BASEDIR}/.vimrc ~/.vimrc
echo "Installing Vundle";
if [ ! -d ~/.vim/bundle/Vundle.vim ]; then
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
else
echo "Vundle already cloned at ~/.vim/bundle/Vundle.vim"
fi
# Install vim plugins.
echo "Installing vim plugins";
nvim +PluginInstall +qall
}
# install_tools installs various tools for day-to-day needs.
function install_tools() {
# Install gnupg, tree, lpass
if [[ $PLATFORM == "Darwin" ]]; then
brew install gnupg2 tree lastpass-cli --with-pinentry
elif [[ $PLATFORM == "Linux" ]]; then
# lpass has to be built manually.
apt-get install gnupg2 tree
fi
}
function main() {
local confirm
# If there's no argument, print the main help.
if [ $# == 0 ]; then
main_help
exit
fi
# Parse the commandline args
while test $# -gt 0
do
case "$1" in
basic)
echo "Basic shell customization would be applied. Continue?(y/N)"
read confirm
if [ $confirm == "y" ]; then
echo "Applying bash configurations..."
install_basic
fi
;;
dev)
echo "Development configurations would be applied and development tools would be installed. Continue?(y/N)"
read confirm
if [ $confirm == "y" ]; then
echo "Setting up dev env..."
install_dev
fi
;;
tools)
install_tools
;;
*)
main_help
;;
esac
# Shift the argument after use.
shift
done
}
main "$@"