-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·232 lines (194 loc) · 4.64 KB
/
install.sh
File metadata and controls
executable file
·232 lines (194 loc) · 4.64 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
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
#!/usr/bin/env bash
# vim: foldmethod=marker
set -euo pipefail
# Configuration {{{
DOTFILES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TARGET_DIR="${TARGET_DIR:-$HOME}"
COMMON_PACKAGES=(
agents
bash
bat
dircolors
editorconfig
fish
git
neovim
tmux
vim
)
MACOS_PACKAGES=(
kitty
linearmouse
macos
)
WSL_PACKAGES=(
wsl
)
# }}}
# Command-line parsing {{{
DRY_RUN=false
VERBOSE=false
RESTOW=true # restow = re-link (recommended). Set false to only stow new.
EXTRA_PACKAGES=()
usage() {
cat << EOF
Usage: ./install.sh [options]
Options:
--dry-run Show stow actions without applying
--verbose Verbose stow output
--no-restow Use stow (not --restow)
--package <name> Also stow <name> (repeatable)
-h, --help Show help
Packages installed by default:
Common: ${COMMON_PACKAGES[*]}
macOS: ${MACOS_PACKAGES[*]}
WSL: ${WSL_PACKAGES[*]}
Env overrides:
TARGET_DIR=/path/to/target (default: \$HOME)
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run)
DRY_RUN=true
shift
;;
--verbose)
VERBOSE=true
shift
;;
--no-restow)
RESTOW=false
shift
;;
--package)
[[ $# -gt 1 ]] || {
echo "--package requires an argument"
usage
exit 1
}
EXTRA_PACKAGES+=("$2")
shift 2
;;
-h | --help)
usage
exit 0
;;
*)
echo "Unknown arg: $1"
usage
exit 1
;;
esac
done
# }}}
# Helpers {{{
log() { printf "\033[1;34m==>\033[0m %s\n" "$*" >&2; }
warn() { printf "\033[1;33m!!\033[0m %s\n" "$*" >&2; }
err() { printf "\033[1;31mxx\033[0m %s\n" "$*" >&2; }
have() { command -v "$1" > /dev/null 2>&1; }
is_macos() { [[ "$(uname -s)" == "Darwin" ]]; }
is_wsl() {
# Works for WSL1/WSL2
grep -qiE "(microsoft|wsl)" /proc/version 2> /dev/null
}
# }}}
# Platform detection {{{
PLATFORM="unknown"
if is_macos; then
PLATFORM="macos"
elif is_wsl; then
PLATFORM="wsl"
else
PLATFORM="linux"
fi
# }}}
# Dotfile package selection {{{
packages_for_platform() {
local -a pkgs=()
pkgs+=("${COMMON_PACKAGES[@]}")
case "$PLATFORM" in
macos) pkgs+=("${MACOS_PACKAGES[@]}") ;;
wsl) pkgs+=("${WSL_PACKAGES[@]}") ;;
linux) ;;
esac
if [[ "${#EXTRA_PACKAGES[@]}" -gt 0 ]]; then
pkgs+=("${EXTRA_PACKAGES[@]}")
fi
# Filter to only directories that exist to avoid stow errors.
local -a existing=()
local p
for p in "${pkgs[@]}"; do
if [[ -d "$DOTFILES_DIR/$p" ]]; then
existing+=("$p")
else
warn "Package '$p' not found (no directory at $DOTFILES_DIR/$p); skipping."
fi
done
printf "%s\n" "${existing[@]}"
}
# }}}
# Stow argument builder {{{
build_stow_args() {
STOW_ARGS=(--dir "$DOTFILES_DIR" --target "$TARGET_DIR")
# Prefer deterministic behavior.
STOW_ARGS+=(--no-folding)
if $VERBOSE; then
STOW_ARGS+=(-v)
fi
if $DRY_RUN; then
STOW_ARGS+=(--simulate)
fi
if $RESTOW; then
# Restow re-applies links cleanly if you rename/move things.
STOW_ARGS+=(--restow)
fi
}
# }}}
# Main {{{
main() {
log "Platform: $PLATFORM"
log "Dotfiles: $DOTFILES_DIR"
log "Target: $TARGET_DIR"
if ! have stow; then
err "GNU stow is required but not found on PATH."
warn "Install it first (e.g. brew install stow or apt-get install stow) then re-run."
exit 1
fi
local -a PKGS=()
while IFS= read -r pkg; do
PKGS+=("$pkg")
done < <(packages_for_platform)
if [[ "${#PKGS[@]}" -eq 0 ]]; then
err "No stow packages selected (or none exist)."
exit 1
fi
log "Will stow packages:"
for p in "${PKGS[@]}"; do
printf ' - %s\n' "$p"
done
local -a STOW_ARGS=()
local cmd
build_stow_args
printf -v cmd '%q ' stow "${STOW_ARGS[@]}" "${PKGS[@]}"
log "Running: ${cmd% }"
stow "${STOW_ARGS[@]}" "${PKGS[@]}"
log "Dotfiles installed via stow."
post_install "${PKGS[@]}"
}
# Post-install tasks {{{
post_install() {
local -a pkgs=("$@")
# bat caches themes and syntaxes in a binary; rebuild after stowing.
if printf '%s\n' "${pkgs[@]}" | grep -qx 'bat' && have bat; then
if $DRY_RUN; then
log "Would run: bat cache --build"
else
log "Rebuilding bat cache..."
bat cache --build
fi
fi
}
# }}}
main
# }}}