-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotfiles.sh
More file actions
executable file
·321 lines (263 loc) · 10.1 KB
/
Copy pathdotfiles.sh
File metadata and controls
executable file
·321 lines (263 loc) · 10.1 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
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
#!/usr/bin/env bash
DOTFILES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TARGET_DIR="$HOME"
BACKUP_DIR="$HOME/.dotfiles-backup-$(date +%Y%m%d-%H%M%S)"
ENV_FILE="$HOME/.dotfiles-env"
ENVIRONMENTS=(work perso)
ACTION=""
ENVIRONMENT=""
DRY_RUN=false
VERBOSE=false
PACKAGES=0
LINKS_ADDED=0
LINKS_REMOVED=0
BACKED_UP=0
FAILED=0
CURRENT_KIND=""
# ---------------------------------------------------------------- output ----
setup_colors() {
if [[ -t 1 && -z "${NO_COLOR:-}" ]]; then
BOLD=$'\033[1m'; DIM=$'\033[2m'; RED=$'\033[31m'; GREEN=$'\033[32m'
YELLOW=$'\033[33m'; CYAN=$'\033[36m'; RESET=$'\033[0m'
else
BOLD=""; DIM=""; RED=""; GREEN=""; YELLOW=""; CYAN=""; RESET=""
fi
}
tilde() {
echo "${1/#$HOME/~}"
}
plural() {
[[ "$1" -eq 1 ]] && echo "$1 $2" || echo "$1 ${2}s"
}
say() {
printf ' %s\n' "$1"
}
section() {
[[ "$1" == "$CURRENT_KIND" ]] && return
CURRENT_KIND="$1"
printf '\n %s%s%s\n' "$BOLD" "$1" "$RESET"
}
# status icon, package name, then a dim detail column
report() {
local color="$1" icon="$2" name="$3" detail="$4"
printf ' %s%s%s %-16s %s%s%s\n' \
"$color" "$icon" "$RESET" "$name" "$DIM" "$detail" "$RESET"
}
banner() {
local mode=""
$DRY_RUN && mode=" ${YELLOW}(dry run)${RESET}"
printf '\n %sdotfiles%s %s%s → %s%s%s\n' \
"$BOLD" "$RESET" "$DIM" "$(tilde "$DOTFILES_DIR")" "$(tilde "$TARGET_DIR")" "$RESET" "$mode"
printf ' %s%s · %s · %s%s\n' \
"$DIM" "${PLATFORM#\~}" "$ENVIRONMENT" "$ACTION" "$RESET"
}
summary() {
printf '\n %s%s%s\n' "$DIM" "────────────────────────────────────────────" "$RESET"
# only the direction that matches the action; the other one is stow
# re-folding directories, which is churn nobody asked about
local counts="$(plural "$PACKAGES" package)"
if [[ "$ACTION" == "install" && $LINKS_ADDED -gt 0 ]]; then
counts="$counts · $(plural "$LINKS_ADDED" link) added"
elif [[ "$ACTION" == "uninstall" && $LINKS_REMOVED -gt 0 ]]; then
counts="$counts · $(plural "$LINKS_REMOVED" link) removed"
fi
if [[ $FAILED -gt 0 ]]; then
report "$RED" "✖" "$FAILED failed" "$counts"
elif $DRY_RUN; then
# stow -n cannot descend into directories it did not create, so it
# under-reports whenever a package brings a new directory along
[[ $LINKS_ADDED -gt 0 ]] && counts="$counts or more"
report "$YELLOW" "◌" "nothing written" "$counts — rerun without --dry-run"
else
report "$GREEN" "✔" "$ACTION done" "$counts"
fi
if [[ $BACKED_UP -gt 0 ]]; then
if $DRY_RUN; then
report "$YELLOW" "!" "$BACKED_UP to replace" "would move to $(tilde "$BACKUP_DIR")"
else
report "$YELLOW" "!" "$BACKED_UP replaced" "saved to $(tilde "$BACKUP_DIR")"
fi
fi
printf '\n'
}
usage() {
cat <<EOF
${BOLD}dotfiles${RESET} — GNU Stow wrapper
${BOLD}Usage${RESET}
$(basename "$0") install [work|perso] [--dry-run] [--verbose]
$(basename "$0") uninstall [--dry-run] [--verbose]
${BOLD}Environments${RESET}
Packages named ~macos / ~linux / ~windows are stowed only on the matching
platform, and ~work / ~perso only for the chosen environment. The choice is
remembered in $(tilde "$ENV_FILE"), so later runs can omit it.
${BOLD}Options${RESET}
--dry-run, -n show what would change, write nothing
--verbose, -v print every link stow makes
--help, -h this message
EOF
}
# ----------------------------------------------------------------- stow -----
detect_platform() {
case "$(uname -s)" in
Linux*) echo "~linux" ;;
Darwin*) echo "~macos" ;;
CYGWIN*|MINGW32*|MSYS*|MINGW*) echo "~windows" ;;
*) echo "unknown" ;;
esac
}
# Resolve the environment from the argument, falling back to the saved one
resolve_environment() {
case "$1" in
work) echo "work" ;;
perso|personal) echo "perso" ;;
"") cat "$ENV_FILE" 2>/dev/null ;;
*) echo "" ;;
esac
}
# Decide whether a package directory applies here, and how to label it
package_kind() {
case "$1" in
'~macos'|'~linux'|'~windows')
[[ "$1" == "$PLATFORM" ]] && echo "platform" ;;
'~work'|'~perso')
[[ "$1" == "~$ENVIRONMENT" ]] && echo "environment" ;;
*)
echo "common" ;;
esac
}
# Move aside anything stow would refuse to overwrite. Echoes the file count.
backup_conflicts() {
local package="$1" conflicts count=0
conflicts=$(stow -d "$DOTFILES_DIR" -t "$TARGET_DIR" -n "$package" 2>&1 | grep "existing target")
[[ -z "$conflicts" ]] && return 0
while IFS= read -r line; do
[[ "$line" =~ existing\ target\ (.+)\ since\ neither ]] || continue
local conflict_path="${BASH_REMATCH[1]}"
local full_path="$TARGET_DIR/$conflict_path"
[[ -e "$full_path" ]] || continue
count=$((count + 1))
$VERBOSE && say " ${DIM}backup${RESET} $conflict_path"
$DRY_RUN && continue
mkdir -p "$BACKUP_DIR/$(dirname "$conflict_path")"
mv "$full_path" "$BACKUP_DIR/$conflict_path"
done <<< "$conflicts"
echo "$count"
}
# Run stow and describe what actually changed, rather than echoing every link
stow_package() {
local package="$1" kind="$2" mode="$3" backed_up="$4"
local flags=(-d "$DOTFILES_DIR" -t "$TARGET_DIR" -v "$mode")
$DRY_RUN && flags+=(-n)
local output status
output=$(stow "${flags[@]}" "$package" 2>&1)
status=$?
section "$kind"
if [[ $status -ne 0 ]]; then
# name the file that is in the way, not stow's header line
local blockers count reason
blockers=$(echo "$output" | sed -n 's/.*existing target \(.*\) since neither.*/\1/p')
count=$(echo "$blockers" | grep -c '[^[:space:]]')
reason=$(echo "$blockers" | head -1)
[[ $count -gt 1 ]] && reason="$reason (+$((count - 1)) more)"
[[ -z "$reason" ]] && reason=$(echo "$output" | sed -n 's/^stow: ERROR: //p' | head -1)
# a dry run cannot move conflicts aside first, so what stow calls a
# failure here is just work the real run would do
if $DRY_RUN && [[ "${backed_up:-0}" != "0" ]]; then
PACKAGES=$((PACKAGES + 1))
report "$YELLOW" "!" "$package" "would replace $reason"
return 0
fi
FAILED=$((FAILED + 1))
report "$RED" "✖" "$package" "$reason"
$VERBOSE && echo "$output" | sed 's/^/ /'
return
fi
# stow -R re-links everything it already owns; only the difference is news
local linked unlinked added removed kept
linked=$(echo "$output" | sed -n 's/^LINK: \(.*\) =>.*/\1/p' | sort -u)
unlinked=$(echo "$output" | sed -n 's/^UNLINK: //p' | sort -u)
added=$(comm -23 <(echo "$linked") <(echo "$unlinked") | grep -c '[^[:space:]]')
removed=$(comm -13 <(echo "$linked") <(echo "$unlinked") | grep -c '[^[:space:]]')
kept=$(comm -12 <(echo "$linked") <(echo "$unlinked") | grep -c '[^[:space:]]')
PACKAGES=$((PACKAGES + 1))
LINKS_ADDED=$((LINKS_ADDED + added))
LINKS_REMOVED=$((LINKS_REMOVED + removed))
local detail=""
[[ $added -gt 0 ]] && detail="$added added"
[[ $removed -gt 0 ]] && detail="${detail:+$detail, }$removed removed"
[[ $kept -gt 0 && -n "$detail" ]] && detail="$detail, $kept unchanged"
if [[ -n "$backed_up" && "$backed_up" != "0" ]]; then
report "$YELLOW" "✔" "$package" "${detail:-linked} · $backed_up replaced"
elif [[ -z "$detail" ]]; then
report "$DIM" "·" "$package" "up to date"
else
report "$GREEN" "✔" "$package" "$detail"
fi
$VERBOSE && [[ -n "$output" ]] && echo "$output" | sed 's/^/ /'
return 0
}
# Drop the environment packages that were not selected
unstow_other_environments() {
local keep="$1" name
for name in "${ENVIRONMENTS[@]}"; do
[[ "$name" == "$keep" ]] && continue
[[ -d "$DOTFILES_DIR/~$name" ]] || continue
[[ -L "$TARGET_DIR/.gitconfig-local" || -e "$TARGET_DIR/.gitconfig-local" ]] || continue
$DRY_RUN || stow -d "$DOTFILES_DIR" -Dt "$TARGET_DIR" "~$name" 2>/dev/null
done
}
# ------------------------------------------------------------------ main ----
setup_colors
while [[ $# -gt 0 ]]; do
case "$1" in
install|uninstall) ACTION="$1" ;;
-n|--dry-run) DRY_RUN=true ;;
-v|--verbose) VERBOSE=true ;;
-h|--help) usage; exit 0 ;;
-*) printf '\n %s✖%s unknown option: %s\n' "$RED" "$RESET" "$1"; usage; exit 1 ;;
*) ENV_ARG="$1" ;;
esac
shift
done
if [[ -z "$ACTION" ]]; then
usage
exit 1
fi
PLATFORM=$(detect_platform)
ENVIRONMENT=$(resolve_environment "${ENV_ARG:-}")
if [[ "$ACTION" == "install" && -z "$ENVIRONMENT" ]]; then
if [[ -n "${ENV_ARG:-}" ]]; then
printf '\n %s✖%s unknown environment: %s — expected %swork%s or %sperso%s\n' \
"$RED" "$RESET" "$ENV_ARG" "$CYAN" "$RESET" "$CYAN" "$RESET"
else
printf '\n %s✖%s no environment selected — pass %swork%s or %sperso%s the first time\n' \
"$RED" "$RESET" "$CYAN" "$RESET" "$CYAN" "$RESET"
fi
usage
exit 1
fi
cd "$DOTFILES_DIR" || exit 1
banner
if [[ "$ACTION" == "install" ]]; then
unstow_other_environments "$ENVIRONMENT"
for dir in */; do
package="${dir%/}"
kind=$(package_kind "$package")
[[ -z "$kind" ]] && continue
conflicts=$(backup_conflicts "$package")
BACKED_UP=$((BACKED_UP + ${conflicts:-0}))
stow_package "$package" "$kind" "-R" "$conflicts"
done
$DRY_RUN || echo "$ENVIRONMENT" > "$ENV_FILE"
else
for dir in */; do
package="${dir%/}"
kind=$(package_kind "$package")
[[ -z "$kind" ]] && continue
stow_package "$package" "$kind" "-D" ""
done
unstow_other_environments "$ENVIRONMENT"
$DRY_RUN || rm -f "$ENV_FILE"
fi
summary
[[ $FAILED -eq 0 ]]