-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialize
executable file
·518 lines (463 loc) · 17.3 KB
/
initialize
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/bin/zsh
# Check for command line tools
if [[ "$OSTYPE" == darwin* ]] && (( ! $+commands[xcode-select] )); then
>&2 echo 'initialize: install xcode command line tools manually'
exit 1
fi
#
# Parse options
#
local -a force help no_log output quiet verbose
zparseopts -D \
f=force -force=force \
-help=help \
-no-log=no_log \
o:=output -output:=output \
q=quiet -quiet=quiet \
v=verbose -verbose=verbose
# Display help and detect illegal options
if [[ -n "$help" || -n "$@" ]]; then
if [[ -n "$@" ]] >&2 echo "initialize: illegal option $@"
print -P \
"usage: initialize [-fqv] [-o output] [--help] [--no-log]
%BOPTIONS%b
-f, --force
Run without confirmation or zsh version check.
--help
Display this help menu.
--no-log
Disable all logging (no log file will be created).
-o, --output <output_file>
Specify a filename for the log file. Default is dotfiles-initialize.log
-q, --quiet
Suppress as much output as possible.
-v, --verbose
Print all command outputs to console."
if [[ -n "$@" ]] exit 1
exit
fi
# Show welcome message
if [[ -n "$force" ]]; then
# Version check
local input min_zsh_version='5.0.0'
if ! autoload -Uz is-at-least || ! is-at-least "$min_zsh_version"; then
>&2 print "initialize: old shell detected, minimum supported: $min_zsh_version"
read -q input \?'Continue anyways? [n] '
echo
if [[ "$input" != 'y' ]] exit
unset input
echo
fi
unset min_zsh_version
# Info
clear
print -P "
This script sets up various defaults and configurations as preferred by me.
- %BImport config files%b into zsh
- %BChange default shell%b to /bin/zsh
- %BInstall prezto%b
-- %BImport%b original zsh config files into prezto
-- %BBackup%b original zsh config files
- %BInstall statusline%b prezto theme (el1t/statusline)
-- %BInstall%b custom Powerline-patched Menlo font
-- %BInstall%b zpreztorc from el1t/dotfiles
-- %BInstall%b Solarized Terminal.app profile (Mac only)
- %BInitialize%b vim settings
-- %BInstall%b vundle plugins
-- %BCreate%b vim folder structure
For Macs only:
- %BInstall%b xcode command line tools
- %BAdd \"Recent Applications\"%b stack to dock
- %BSet%b the following %Bdefaults:%b
-- Enable scroll-to-open on dock
-- Enable key repeat
-- Default to expanded save panel
- %BInstall homebrew%b
-- %BBrew install%b %Upython3%u, %Ugit%u, and %Uvim%u
tl;dr: %B%UMacOS%u and %USolarized%u; %Uzsh%u not bash; %Uprezto%u not oh-my-zsh; %Uhomebrew%u not macports%b"
if [[ "$OSTYPE" != darwin* ]] print -P '%F{1}%UWarning%u%f: Partial support for this OS'
if [[ -d ~/.oh-my-zsh/ ]] print -P '%F{1}%UWarning%u%f: oh-my-zsh will be uninstalled'
echo
read -q input\?'Continue? [y] '
echo
if [[ "$input" == 'n' ]] exit
unset input
echo
fi
# Setup logging
setopt LOCAL_OPTIONS POSIX_BUILTINS
local LOGFILE='dotfiles-initialize.log'
if [[ -n "$output" ]] LOGFILE="$output[2]"
unset output
# Echo and log
_initialize_echo_log() {
if [[ -z "$quiet" ]]; then
# Print and log
tee -a "$LOGFILE"
elif [[ -z "$no_log" ]]; then
# Log only
cat >>! "$LOGFILE"
else
# Print only
cat
fi
}
# Log only, unless verbose
_initialize_log() {
if [[ -n "$verbose" ]]; then
_initialize_echo_log
elif [[ -z "$no_log" ]]; then
cat >>! "$LOGFILE"
fi
}
# Log only, always
_initialize_msg() {
if [[ -z "$no_log" ]] cat >>! "$LOGFILE"
}
# Ignore print if quiet flag is set
print() {
if [[ -z "$quiet" ]] command print "$@"
}
echo '
-------------------------------------'"
dotfiles: initialize ($(date))"'
-------------------------------------' | _initialize_msg
#
# Install xcode command line tools
#
if [[ "$OSTYPE" == darwin* ]] && ! pkgutil --pkg-info=com.apple.pkg.CLTools_Executables > /dev/null; then
print -P '%B>>> Installing XCode command line tools%b'
# Attempt headless install, from homebrew installation
echo 'Searching online for the Command Line Tools' | _initialize_echo_log
local placeholder='/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress'
echo "touch \"$placeholder\"" | _initialize_log
touch "$placeholder"
local label=$(softwareupdate -l | grep -B 1 -E "Command Line (Developer|Tools)" | awk -F"*" '/^ +\\*/ {print $2}' | sed 's/^ *//' | head -n1)
echo "sudo softwareupdate -i \"$label\"" | _initialize_echo_log
sudo softwareupdate -i "$label"
echo "rm -f \"$placeholder\"" | _initialize_log
rm -f "$placeholder"
echo 'sudo xcode-select --switch /Library/Developer/CommandLineTools' | _initialize_log
sudo xcode-select --switch /Library/Developer/CommandLineTools
# Check if headless install worked
if ! pkgutil --pkg-info=con.apple.pkg.CLTools_Executables > /dev/null; then
echo 'headless installation failed' | _initialize_msg
echo 'Installing the Command Line Tools (expect a GUI popup):' | _initialize_echo_log
echo 'sudo xcode-select --install' | _initialize_echo_log
sudo xcode-select --install
echo 'sudo xcode-select --switch /Library/Developer/CommandLineTools' | _initialize_echo_log
sudo xcode-select --switch /Library/Developer/CommandLineTools
fi
# sudo xcodebuild -license
fi
#
# Uninstall oh-my-zsh
#
if [[ -d ~/.oh-my-zsh/ ]]; then
print -P '%B>>> Uninstalling oh-my-zsh%b'
echo 'oh-my-zsh detected, uninstalling' | _initialize_msg
local +h ZSH=~/.oh-my-zsh/
echo "source $ZSH/lib/functions.zsh" | _initialize_log
source "$ZSH/lib/functions.zsh"
if (( $+functions[uninstall_oh_my_zsh] )); then
echo 'running function uninstall_oh_my_zsh' | _initialize_msg
uninstall_oh_my_zsh
# Detect if oh-my-zsh uninstall switched to bash
if [[ "${$(sh -c 'echo $SHELL'):t}" != 'zsh' ]]; then
print -P '%B>>> Switching back to zsh%b'
echo 'chsh -s /bin/zsh' | _initialize_log
chsh -s /bin/zsh
# If oh-my-zsh switches to bash, it leaves a dirty .zshrc
if [[ -s ~/.zshrc ]]; then
print -P '%B%F{1}>>> Renaming .zshrc to .zshrc.omz-backup%f%b'
print '(oh-my-zsh uninstall left a dirty .zshrc)'
echo 'oh-my-zsh left dirty .zshrc' | _initialize_msg
echo 'mv ~/.zshrc ~/.zshrc.omz-backup' | _initialize_log
mv ~/.zshrc ~/.zshrc.omz-backup
fi
fi
else
>&2 print -P '%F{1}%BError%b%f: Could not locate uninstall function'
print 'Please uninstall oh-my-zsh manually'
echo 'error: oh-my-zsh is installed but function uninstall_oh_my_zsh does not exist' | _initialize_msg
exit 1
fi
unset ZSH
fi
#
# Vim setup
#
# Create missing directories
local -a vim_dirs
local directory printed
vim_dirs=(autoload backups plugins swaps undo colors)
for directory in $vim_dirs; do
if [[ ! -d ~/.vim/"$directory" ]]; then
if [[ -z "$printed" ]]; then
print -P '%B>>> Creating vim directory structure%b'
printed='y'
fi
echo 'mkdir -p "$directory"' | _initialize_echo_log
mkdir -p ~/.vim/"$directory"
fi
done
if [[ -z "$printed" ]] echo 'no additional vim directories created' | _initialize_msg
unset vim_dirs directory printed
# Install vim-plug
if [[ ! -s ~/.vim/autoload/plug.vim ]]; then
print -P '%B>>> Installing vim-plug%b'
echo 'curl -fsSLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim' | _initialize_echo_log
curl -fsSLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim | _initialize_echo_log
else
echo 'existing vim-plug installation detected' | _initialize_msg
fi
check_and_copy_file() {
if [[ -x ~/"$1" ]]; then
if [[ -z "$force" ]]; then
read -q input\?"Existing $1 detected, overwrite? [y] "
else
input='y'
fi
if [[ "$input" != 'n' ]]; then
echo "mv ~/.$1 ~/.$1_backup" | _initialize_log
mv ~/"$1" ~/"$1_backup"
fi
else
echo "check_and_copy_file: ~/$1 did not exist" | _initialize_msg
fi
if [[ ! -s ~/"$1" ]]; then
print -P "%B>>> Cloning $1%b"
echo "curl -fsSLo ~/.$1 https://raw.githubusercontent.com/el1t/dotfiles/master/$1" | _initialize_echo_log
curl -fsSLo ~/"$1" --create-dirs "https://raw.githubusercontent.com/el1t/dotfiles/master/$1" | _initialize_echo_log
else
echo "check_and_copy_file: skipped cloning ~/$1" | _initialize_msg
fi
}
# Setup .vimrc
check_and_copy_file '.vimrc'
# Install vim-plug plugins
if (( $+commands[vim] )); then
input='y'
print -P '%B>>> Installing vim-plug plugins%b'
echo 'installing vim-plug plugins' | _initialize_msg
echo 'vim +PlugInstall +qall' | _initialize_echo_log
vim +PlugInstall +qall
else
input='n'
echo 'vim not found' | _initialize_msg
if [[ "$OSTYPE" == darwin* ]]; then
print 'Postponing plugin installation until vim is installed'
echo 'vim plugins installation postponed' | _initialize_msg
else
print -P '%F{1}%UWarning%u%f: vim not detected
After installation, please run %Bvim +PlugInstall +qall%b to install plugins manually'
echo 'vim plugins not installed' | _initialize_msg
fi
fi
print -P '%B>>> Installing color themes%b'
curl -fsSLo ~/.vim/colors/solarized.vim \
'https://raw.githubusercontent.com/el1t/dotfiles/master/.vim/colors/solarized.vim'
curl -fsSLo ~/.vim/plugins/vim-airline/autoload/airline/themes/powerline.vim \
'https://raw.githubusercontent.com/el1t/dotfiles/master/.vim/plugins/vim-airline/autoload/airline/themes/powerline.vim'
#
# Setup other config files
#
local -a config_files
local config_file
config_files=('.tmux.conf' '.config/karabiner/assets/complex_modifications/custom.json')
for config_file in $config_files; do
check_and_copy_file "$config_file"
done
#
# Prezto and theme setup
#
print -P '%B>>> Installing prezto and statusline%b'
echo 'deferring install to statusline' | _initialize_msg
echo "zsh -c \"set -- '-f'; \$(curl -fsSL https://raw.githubusercontent.com/el1t/statusline/master/prezto/install)\"" | _initialize_echo_log
# TODO: set matching flags for statusline script
zsh -c "set -- '-f'; $(curl -fsSL https://raw.githubusercontent.com/el1t/statusline/master/prezto/install)"
print -P '%B>>> Importing zsh config files from el1t/dotfiles%b'
check_and_copy_file '.zprezto/runcoms/zpreztorc'
check_and_copy_file '.zprezto/runcoms/zshrc'
#
# Copy over config files into zshrc
#
if [[ "${SHELL:t}" != 'zsh' ]]; then
if [[ -z "$force" ]]; then
read -q input\?'Import config into zsh? (via sourcing originals, possible compatibility issues) [y] '
print
else
input='y'
fi
if [[ "$input" != 'n' ]]; then
unset input
echo 'importing old shell configs into zsh' | _initialize_msg
function import_config {
local input="$HOME/$1"
local output="${ZDOTDIR:-$HOME}/$2"
if [[ -s "$input" && -w "$output" ]]; then
echo "Importing $input to $output" | _initialize_echo_log
print -P "\n#\n# Import from ${SHELL:t}\n#\n\nsource $input" >>! "$output"
fi
}
case "${SHELL:t}" in
(bash)
import_config '.bashrc' '.zshrc'
import_config '.bash_profile' '.zprofile'
import_config '.bash_login' '.zlogin'
import_config '.bash_logout' '.zlogout'
;;
(ksh)
import_config '.kshrc' '.zshrc'
;;
(sh)
import_config '.shrc' '.zshrc'
;;
(*)
print -P "Importing from ${SHELL:t} is unsupported. No files were imported."
echo "${SHELL:t} import is unsupported" | _initialize_msg
;;
esac
else
unset input
echo 'skipped importing old shell configs' | _initialize_msg
fi
print -P '%B>>> Changing default shell to /bin/zsh%b'
echo 'chsh -s /bin/zsh' | _initialize_echo_log
chsh -s /bin/zsh
fi
#
# Mac specifics
#
if [[ "$OSTYPE" == darwin* ]]; then
# Setup defaults
print -P '%B>>> Writing to system settings%b'
echo 'Adding recents folder to dock' | _initialize_echo_log
echo "defaults write com.apple.dock persistent-others -array-add '{ \"tile-data\" = { \"list-type\" = 1; }; \"tile-type\" = \"recents-tile\"; }'" | _initialize_log
defaults write com.apple.dock persistent-others -array-add '{ "tile-data" = { "list-type" = 1; }; "tile-type" = "recents-tile"; }'
echo 'Enabling scroll-to-open' | _initialize_echo_log
echo 'defaults write com.apple.dock scroll-to-open -bool TRUE' | _initialize_log
defaults write com.apple.dock scroll-to-open -bool TRUE
echo 'Enabling key repeat' | _initialize_echo_log
echo 'defaults write -g ApplePressAndHoldEnabled -bool false' | _initialize_log
defaults write -g ApplePressAndHoldEnabled -bool false
echo 'Enabling expanded save panel' | _initialize_echo_log
echo 'defaults write -g NSNavPanelExpandedStateForSaveMode -boolean true' | _initialize_log
defaults write -g NSNavPanelExpandedStateForSaveMode -boolean true
echo 'Enabling debug menu in App Store' | _initialize_echo_log
echo 'defaults write com.apple.appstore ShowDebugMenu -bool true' | _initialize_log
defaults write com.apple.appstore ShowDebugMenu -bool true
echo 'Enabling Airdrop scan all interfaces' | _initialize_echo_log
echo 'defaults write com.apple.NetworkBrowser BrowseAllInterfaces 1' | _initialize_log
defaults write com.apple.NetworkBrowser BrowseAllInterfaces 1
echo 'Enabling develop mode in Safari' | _initialize_echo_log
echo 'defaults write com.apple.Safari IncludeInternalDebugMenu -bool true' | _initialize_log
defaults write com.apple.Safari IncludeInternalDebugMenu -bool true
echo 'defaults write com.apple.Safari IncludeDevelopMenu -bool true' | _initialize_log
defaults write com.apple.Safari IncludeDevelopMenu -bool true
echo 'defaults write com.apple.Safari WebKitDeveloperExtrasEnabledPreferenceKey -bool true' | _initialize_log
defaults write com.apple.Safari WebKitDeveloperExtrasEnabledPreferenceKey -bool true
echo "defaults write com.apple.Safari 'com.apple.Safari.ContentPageGroupIdentifier.WebKit2DeveloperExtrasEnabled' -bool true" | _initialize_log
defaults write com.apple.Safari 'com.apple.Safari.ContentPageGroupIdentifier.WebKit2DeveloperExtrasEnabled' -bool true
echo 'defaults write NSGlobalDomain WebKitDeveloperExtras -bool true' | _initialize_log
defaults write NSGlobalDomain WebKitDeveloperExtras -bool true
print -P '%B>>> Restarting Dock%b'
echo 'killall Dock' | _initialize_echo_log
killall Dock
# Install homebrew
if (( $+commands[brew] )); then
echo 'brew exists, updating instead' | _initialize_msg
# Update instead
print -P '%B>>> Updating brew formulae%b'
echo 'brew update' | _initialize_echo_log
brew update | _initialize_log
print -P '%B>>> Upgrading installed packages%b'
echo 'brew upgrade --all' | _initialize_echo_log
brew upgrade --all | _initialize_log
print -P '%B>>> Cleaning brew cache%b'
echo 'brew cleanup' | _initialize_echo_log
brew cleanup | _initialize_log
echo 'rm -rf "$(brew --cache)"' | _initialize_echo_log
rm -rf "$(brew --cache)"
else
print -P '%B>>> Installing homebrew%b'
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
print -P '%B>>> Installing homebrew packages%b'
echo 'brew install python3 git vim cmake' | _initialize_echo_log
brew install python3 git vim cmake | _initialize_log
# If plugin installation was postponed
if [[ "$input" == 'n' ]]; then
print -P '%B>>> Installing vim-plug plugins%b'
echo 'vim +PlugInstall +qall' | _initialize_echo_log
vim +PlugInstall +qall
fi
# Extra homebrew packages maintain compatibility with .zshrc
if [[ -z "$force" ]]; then
read -q input\?'Install extra homebrew packages? (exa node pngcrush tmux) [y] '
echo
else
input='y'
fi
if [[ "$input" != 'n' ]]; then
print -P '%B>>> Installing extra homebrew packages%b'
echo 'brew install exa node pngcrush tmux' | _initialize_echo_log
brew install exa node pngcrush tmux | _initialize_log
fi
# Finish vim-plug installations
print -P '%B>>> Completing vim plugin installation (~1min)'
print 'YouCompleteMe'
if [[ "$input" != 'n' ]]; then
# Install with javascript support
echo '~/.vim/plugins/YouCompleteMe/install.py --clang-completer --tern-completer' | _initialize_log
~/.vim/plugins/YouCompleteMe/install.py --clang-completer --tern-completer | _initialize_log
else
echo '~/.vim/plugins/YouCompleteMe/install.py --clang-completer' | _initialize_log
~/.vim/plugins/YouCompleteMe/install.py --clang-completer | _initialize_log
fi
print '\nZsh is now the default shell.\n'
if [[ -z "$force" ]]; then
read -q input\?'Restart terminal? (recommended) [y] '
echo
else
input='y'
fi
if [[ "$input" != 'n' ]] killall Terminal
#
# Ubuntu/Debian installations
#
elif (( $+commands[apt-get] )); then
print -P '%B>>> Installing packages'
echo 'sudo apt-get update' | _initialize_echo_log
sudo apt-get update | _initialize_log
echo 'sudo apt-get upgrade' | _initialize_echo_log
sudo apt-get upgrade | _initialize_log
echo 'sudo apt-get install -y build-essential cmake python-dev python3-dev' | _initialize_echo_log
sudo apt-get install -y build-essential cmake python-dev python3-dev | _initialize_log
# Extra apt packages maintain compatibility with .zshrc
if [[ -z "$force" ]]; then
read -q input\?'Install extra apt packages? (node pngcrush tmux) [y] '
echo
else
input='y'
fi
if [[ "$input" != 'n' ]]; then
print -P '%B>>> Installing extra apt packages%b'
# Node 8.x
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
echo 'sudo apt-get install -y nodejs pngcrush tmux' | _initialize_echo_log
sudo apt-get install -y nodejs pngcrush tmux | _initialize_log
fi
# Finish vim-plug installations
print -P '%B>>> Completing vim plugin installation (~1min)'
print 'YouCompleteMe'
if [[ "$input" != 'n' ]]; then
# Install with javascript support
echo '~/.vim/plugins/YouCompleteMe/install.py --clang-completer --tern-completer' | _initialize_log
~/.vim/plugins/YouCompleteMe/install.py --clang-completer --tern-completer | _initialize_log
else
echo '~/.vim/plugins/YouCompleteMe/install.py --clang-completer' | _initialize_log
~/.vim/plugins/YouCompleteMe/install.py --clang-completer | _initialize_log
fi
fi
unset input