-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap.sh
executable file
·300 lines (272 loc) · 7.33 KB
/
bootstrap.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
292
293
294
295
296
297
298
299
300
#!/bin/bash
# install with:
# > curl -fsSL https://raw.githubusercontent.com/davidjenni/dotfiles/main/bootstrap.sh | bash
originGitHub='https://github.com/davidjenni/dotfiles.git'
dotPath=$HOME/dotfiles
scriptDir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
shopt -s nocasematch
function have {
hash "$1" >&/dev/null
}
function ensureLocalGit {
if have git; then
echo "git is installed"
return
fi
echo "No local git is installed"
# TODO: Install temp copy of git
}
function ensureGitNames {
prompt=$1
username=$(git config --global user.name)
if [ -z $username ] ; then
defUsername="$USER@$(hostname -s)"
else
defUsername=$username
fi
if [ -z $prompt ] ; then
read -p "Enter git username (default: $defUsername): " username
fi
if [ -z $username ] ; then
username=$defUsername
fi
git config --global user.name "$username"
email=$(git config --global user.email)
if [ -z $email ] ; then
defEmail="[email protected]"
else
defEmail=$email
fi
if [ -z $prompt ] ; then
read -p "Enter git email (default: $defEmail): " email
fi
if [ -z $email ] ; then
email=$defEmail
fi
git config --global user.email "$email"
}
function writeGitConfig {
local var configGitIni=$1
cat $configGitIni \
| grep -v "^\s*#" \
| while IFS='=' read -r key value; do \
# echo "git config --global $key $value"
git config --global $key "$value"
done
}
function initBrew {
case `uname` in
'Darwin') eval "$(/opt/homebrew/bin/brew shellenv)" ;;
'Linux') eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" ;;
esac
}
function ensureBrew {
if have brew; then
echo "$(brew --version) is installed"
# skip update for now, causes errors in CI:
# brew update
return
fi
case `uname` in
'Darwin')
xcode-select --install
;;
'Linux')
sudo apt-get install -y build-essential procps curl file git
;;
esac
echo "Installing Homebrew..."
# https://brew.sh/
# https://docs.brew.sh/Homebrew-on-Linux
NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
initBrew
}
function cloneDotFiles {
echo "Cloning $originGitHub -> $dotPath"
read -p "OK to proceed with clone of repo? [Y/n] " answer
answer=${answer:-Y}
if [[ $answer != "Y" ]]; then
echo "Aborting clone."
exit 4
fi
echo "Cloning dotfiles..."
# setup config for git: username, email, etc.
ensureGitNames
git clone $originGitHub $dotPath
}
function installApps {
echo "Installing apps via brew..."
local var apps=(
7zip
bat
dust
fd
fish
fzf
git-delta
helix
less
lsd
lua-language-server
neofetch
neovim
ripgrep
starship
tmux
tre-command
tokei
zoxide
xz
)
local var casks=(
alacritty
font-jetbrains-mono-nerd-font
)
local var _apps=${apps[*]}
echo ">> brew install $_apps"
initBrew
brew install $_apps
if [ $? -ne 0 ] ; then
echo "Failed to install apps via brew"
exit 2
fi
local var _casks=${casks[*]}
case `uname` in
'Darwin')
brew tap homebrew/cask-fonts
echo ">> brew install --cask $_casks"
brew install --cask $_casks
;;
esac
return $?
}
function copyFile {
local sourceRelPath=$1
local target=$2
if [ -f $target ] ; then
# TODO: add backup story
rm -f $target >&/dev/null
fi
targetDir=$(dirname $target)
mkdir -p $targetDir
sourceFile=$scriptDir/$sourceRelPath
echo " $sourceFile -> $target"
cp $sourceFile $target
}
function copyDir {
local sourceRelPath=$1
local targetDir=$2
if [ -d $targetDir ] ; then
# TODO: add backup story
rm -rf $targetDir >&/dev/null
fi
mkdir -p $targetDir
sourceDir=$scriptDir/$sourceRelPath
echo " $sourceDir -> $targetDir"
cp -R $sourceDir/* $targetDir
}
function setupShellEnv {
echo "Setting up shell environment..."
ensureGitNames noprompt
writeGitConfig $scriptDir/gitconfig.ini
local configDir=$HOME/.config
if [ ! -d "$configDir" ] ; then
mkdir -p $configDir
fi
# neovim
local nvimDir=$configDir/nvim
copyDir nvim $nvimDir
# alacritty
local alacrittyDir=$configDir/alacritty
copyFile alacritty.toml $alacrittyDir/alacritty.toml
# bat: https://github.com/sharkdp/bat#configuration-file
copyFile bat_config $configDir/bat/config
copyFile bash/bash_aliases.sh $HOME/.bash_aliases
copyFile bash/inputrc $HOME/.inputrc
copyFile bash/tmux.conf $HOME/.tmux.conf
# starship.rs:
copyFile starship.toml $configDir/starship.toml
# fish:
local fishConfigDir=$configDir/fish
# rm -f $fishConfigDir/functions/fisher.fish >&/dev/null
# curl -sL https://raw.githubusercontent.com/jorgebucaran/fisher/main/functions/fisher.fish | source && fisher install jorgebucaran/fisher
copyFile fish/config.fish $fishConfigDir/config.fish
copyFile fish/fish_plugins $fishConfigDir/fish_plugins
# copyFile fish/functions/fisher.fish $fishConfigDir/functions
copyFile fish/functions/l.fish $fishConfigDir/functions/l.fish
copyFile fish/functions/la.fish $fishConfigDir/functions/la.fish
copyFile fish/functions/ll.fish $fishConfigDir/functions/ll.fish
copyFile fish/functions/ls.fish $fishConfigDir/functions/ls.fish
# setup ssh to play with 1Password as identity agent:
sshConfig=$HOME/.ssh/config
copyFile ssh/config $sshConfig
sshPerms=u+rwx,go-rwx
chmod $sshPerms $HOME/.ssh
chmod $sshPerms $sshConfig
touch $HOME/.ssh/known_hosts && chmod $sshPerms $HOME/.ssh/known_hosts
case `uname` in
'Darwin')
if [ -d "$HOME/Library/Group Containers/2BUA8C4S2C.com.1password" ] ; then
echo " 1Password socket exists, setting symlink."
mkdir -p $HOME/.1password && chmod $sshPerms $HOME/.1password
ln -s "$HOME/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock" $HOME/.1password/agent.sock
fi
;;
'Linux')
if uname -r | grep -q "WSL"; then
echo "on WSL, let the Windows host OS' 1Password identity agent resolve ssh authN"
# https://developer.1password.com/docs/ssh/integrations/wsl
rm $sshConfig >&/dev/null
git config --global core.sshCommand ssh.exe
fi
;;
esac
}
main() {
case $1 in
"clone")
if [ -x "$dotPath/.git" ] ; then
echo "local git repo already exists, skipping."
main setup
return
fi
cloneDotFiles
if [ $? -ne 0 ] ; then
exit $?
fi
# continue with now-local bootstrap.ps1 from cloned repo:
bash $dotPath/bootstrap.sh setup
exit $?
;;
"setup")
echo "Setting up..."
ensureBrew
main apps
;;
"apps")
installApps
if [ $? -ne 0 ] ; then
exit $?
fi
main env
;;
"env")
setupShellEnv
exit 0
;;
"-h" | "--help")
echo "Usage: $0 {clone|setup|apps|env}"
echo " clone: clone the dotfiles repo and continue with 'setup' etc."
echo " setup: setup package managers, git. Includes 'apps' and 'env'."
echo " apps: install apps via package manager"
echo " env: setups consoles and configurations for git, neovim etc."
exit 9
;;
*)
main clone
;;
esac
echo "Done."
}
echo "Starting bootstrap.sh (in $scriptDir with working dir: $(pwd))..."
main $*