-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup
More file actions
executable file
·189 lines (169 loc) · 5.19 KB
/
Copy pathsetup
File metadata and controls
executable file
·189 lines (169 loc) · 5.19 KB
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
#!/usr/bin/env bash
#
# setup: provision a machine from this dotfiles repo.
#
# Usage:
# ./setup install everything
# ./setup --dry print what would happen, change nothing
# ./setup nvim tmux stow only these packages, still runs the install steps
#
# Safe to run more than once. Every step checks before acting.
set -eo pipefail
DOTFILES_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
BACKUP_DIR="$HOME/.dotfiles-backup/$(date +%Y-%m-%d-%H%M%S)"
DEFAULT_PACKAGES="bin git nvim tmux vim zsh"
PACKAGES=""
DRY=0
while [[ $# -gt 0 ]]; do
case "$1" in
--dry|--dry-run) DRY=1 ;;
-h|--help) sed -n '3,11p' "$0"; exit 0 ;;
-*) echo "unknown option: $1" >&2; exit 1 ;;
*) PACKAGES="$PACKAGES $1" ;;
esac
shift
done
[[ -z "$PACKAGES" ]] && PACKAGES="$DEFAULT_PACKAGES"
log() {
if [[ $DRY == 1 ]]; then
echo "[DRY_RUN]: $*"
else
echo "$*"
fi
}
execute() {
log "run: $*"
[[ $DRY == 1 ]] && return 0
"$@"
}
have() {
command -v "$1" >/dev/null 2>&1
}
# Move a real file or directory out of the way so stow can link in its place.
# Symlinks are left alone: those are already ours.
backup_if_real() {
local target="$1"
[[ -e "$target" && ! -L "$target" ]] || return 0
log "backing up $target"
[[ $DRY == 1 ]] && return 0
mkdir -p "$BACKUP_DIR"
mv "$target" "$BACKUP_DIR/"
}
install_homebrew() {
if have brew; then log "homebrew already installed"; return 0; fi
if [[ $DRY == 1 ]]; then log "would install homebrew"; return 0; fi
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
}
# Homebrew is not on PATH yet in this process: PATH was built when the script
# started, and .zprofile has not been stowed. shellenv fixes it for this run.
load_brew_env() {
local prefix
for prefix in /opt/homebrew /usr/local; do
if [[ -x "$prefix/bin/brew" ]]; then
log "loading shellenv from $prefix"
eval "$("$prefix/bin/brew" shellenv)"
return 0
fi
done
if [[ $DRY == 1 ]]; then
log "brew not found, would load shellenv after install"
return 0
fi
echo "brew not found after install, aborting" >&2
exit 1
}
brew_bundle() {
if [[ ! -f "$DOTFILES_DIR/Brewfile" ]]; then
log "no Brewfile in $DOTFILES_DIR, skipping"
log "create one with: brew bundle dump --file=$DOTFILES_DIR/Brewfile"
return 0
fi
execute brew bundle install --file="$DOTFILES_DIR/Brewfile"
}
# --unattended keeps the installer from launching zsh and from running chsh.
# --keep-zshrc keeps it from writing its own .zshrc, which would collide with
# the one stow is about to link.
install_oh_my_zsh() {
if [[ -d "$HOME/.oh-my-zsh" ]]; then
log "oh-my-zsh already installed"
return 0
fi
if [[ $DRY == 1 ]]; then
log "would install oh-my-zsh"
return 0
fi
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" \
"" --unattended --keep-zshrc
}
install_tpm() {
if [[ -d "$HOME/.tmux/plugins/tpm" ]]; then
log "tmux-tpm already installed"
return 0
fi
execute git clone https://github.com/tmux-plugins/tpm "$HOME/.tmux/plugins/tpm"
}
# vim/.vimrc calls plug#begin, so vim-plug itself has to exist before vim
# is ever opened. Neovim's lazy.nvim bootstraps itself from init.lua; vim-plug
# has no such self-install, so it's fetched here instead.
install_vim_plug() {
if [[ -f "$HOME/.vim/autoload/plug.vim" ]]; then
log "vim-plug already installed"
return 0
fi
execute curl -fLo "$HOME/.vim/autoload/plug.vim" --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
}
clear_conflicts() {
local f
for f in "$HOME/.zshrc" "$HOME/.zprofile" "$HOME/.zshenv" \
"$HOME/.tmux.conf" "$HOME/.gitconfig" "$HOME/.vimrc"; do
backup_if_real "$f"
done
log "backups, if any, are in $BACKUP_DIR"
}
run_stow() {
if ! have stow; then
log "stow not found, install it with: brew install stow"
return 1
fi
local pkg
for pkg in $PACKAGES; do
if [[ ! -d "$DOTFILES_DIR/$pkg" ]]; then
log "no package named $pkg, skipping"
continue
fi
if [[ $DRY == 1 ]]; then
(cd "$DOTFILES_DIR" && stow -n -v "$pkg")
else
execute stow -v -d "$DOTFILES_DIR" -t "$HOME" "$pkg"
fi
done
}
post_install() {
if [[ -x "$HOME/.tmux/plugins/tpm/bin/install_plugins" ]]; then
execute "$HOME/.tmux/plugins/tpm/bin/install_plugins"
fi
if have nvim; then
execute nvim --headless "+Lazy! sync" +qa
fi
if have vim; then
execute vim -es -u "$HOME/.vimrc" -i NONE "+PlugInstall --sync" +qa
fi
echo
log "done. open a new terminal to pick up the new shell config."
}
main() {
[[ $DRY == 1 ]] && echo "----------------- DRY RUN -------------------"
log "dotfiles: $DOTFILES_DIR"
log "packages: $PACKAGES"
install_homebrew
load_brew_env
brew_bundle
install_oh_my_zsh
install_tpm
install_vim_plug
clear_conflicts
run_stow
post_install
}
main