-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeploy
More file actions
executable file
·282 lines (247 loc) · 7.25 KB
/
deploy
File metadata and controls
executable file
·282 lines (247 loc) · 7.25 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
#!/usr/bin/env zsh
# Dotfile manager
#
# By Henrik Lissner <henrik@lissner.net>
# https://github.com/hlissner/dotfiles
#
# Requires: git, zsh
source "${0:A:h}/env"
SEP='.'
# Helpers for output formatting
function echo-debug { echo "$@" | sed -e "s%$HOME%~%g" -e "s%$DOTFILES%.%g"; }
function echo-list { printf "\n\r\033[2K🔸 \033[0;35m\033[1m%s\033[0m\n" "$*"; }
function echo-note { printf "\n\r\033[2K👉 \033[0;35m\033[1m%s\033[0m\n" "$*"; }
function echo-ask { printf "\n\r\033[2K☝️ \033[0;36m\033[1m%s\033[0m" "$*"; }
function echo-alert { printf "\n\r\033[2K⚠️ \033[0;33m\033[1m%s\033[0m\n" "$*"; }
function echo-info { printf "\n\r\033[2Kℹ️ \033[0;34m%s\033[0m\n" "$*"; }
function echo-ok { printf "\r\033[2K✅ \033[0;32m%s\033[0m\n" "$*"; }
function echo-fail { printf "\r\033[2K❌ \033[0;31m%s\033[0m\n" "$*"; }
function _exec {
if [[ -n $DRYRUN ]]; then
echo-debug "$@"
return 0
fi
"$@"
}
function _topic-do {
if [[ -e "$1/_init" ]]; then
if [[ ! -x "$1/_init" ]]; then
echo-fail "$1's init script isn't executable"
return 1
fi
export TOPIC=$1
pushd -q $1
_exec ./_init $2
local retcode=$?
popd -q
return $retcode
fi
}
function _topics {
local topics; topics=( "$DOTFILES_DATA"/*.topic(N) )
echo ${${${topics[@]#$DOTFILES_DATA/}%.topic}/$SEP//}
}
# Functions for topic initialization
function topic-enabled-p { [[ -L "$(topic-path $1)" ]] }
function topic-path { echo "$DOTFILES_DATA/${1/\//$SEP}.topic" }
function mklink {
if [[ $1 == '-s' ]]; then
local dosudo=1
shift
fi
local flags="-svf"
local dest=${@[-1]}
local srcs; srcs=( ${@:1:-1} )
# Create destination directory if it doesn't exist
if [[ $dest == */ && ! -d $dest ]]; then
mkdir -p $dest
elif [[ ! -d ${dest%/*} ]]; then
mkdir -p ${dest%/*}
fi
# Loop through source links
for lk in ${srcs[@]}; do
local src
case $lk in
/*) src=$lk ;; # Absolute path
.) src="$(topic-path $TOPIC)" ;; # Relative path from topic
*) src="$(topic-path $TOPIC)/$lk" ;; # Other cases
esac
# If source is a directory, remove destination if it is a symlink
if [[ -d $src ]]; then
if [[ $dest != */ && -d $dest && -L $dest ]]; then
${dosudo:+sudo} rm -fv $dest
fi
fi
# Create the symbolic link
_exec ${dosudo:+sudo} ln $flags $src $dest
done
}
function init {
if [[ $1 == "deps" ]]; then
[[ -n "${deps[*]}" ]] && echo "${deps[@]}"
return 0
fi
declare -f $1 >/dev/null || return 1
echo-info "$TOPIC.init.$1: start"
$1
local retval=$?
if (( retval )); then
echo-fail "$TOPIC.init.$1: failed"
else
echo-ok "$TOPIC.init.$1: done"
fi
return $retval
}
# Track topics being processed to detect circular deps
typeset -gA _PROCESSING_TOPICS
# Functions to initialize, link, and update topics
function topic-init {
local _needs_reload=0
for topic in $@; do
# Reset op on every iteration so it doesn't bleed from a previous topic
local op=''
# Check if the topic directory exists
if [[ ! -d $topic ]]; then
echo-fail "$topic doesn't exist!"
continue
fi
# Resolve dependencies before processing
if [[ -z ${_PROCESSING_TOPICS[$topic]} ]]; then
_PROCESSING_TOPICS[$topic]=1
local topic_deps=( $(cd $topic && ./_init deps 2>/dev/null) )
if (( ${#topic_deps} )); then
local missing_deps=()
for dep in $topic_deps; do
topic-enabled-p $dep || missing_deps+=($dep)
done
if (( ${#missing_deps} )); then
echo-list "$topic: installing dependencies: ${missing_deps[*]}"
topic-init $missing_deps
fi
fi
fi
echo-ok "$topic"
# Set the paths for the topic
local tpath="$DOTFILES/$topic"
local tsympath="$(topic-path $topic)"
# If the topic has a bin directory, add it to PATH temporarily
if [[ -d "$topic/bin" ]]; then
export path; path=( $tpath/bin $path )
echo-ok "$topic: added $topic/bin to PATH, temporarily"
fi
# If LINK is set or the topic is not enabled, create symlinks and link the topic
if [[ -n $LINK ]] || ! topic-enabled-p $topic; then
op=install
mklink $tpath $tsympath
_topic-do $topic link
fi
# If INHIBIT_INIT is not set, run the initialization or update script
if [[ -z $INHIBIT_INIT ]]; then
_topic-do $topic ${FORCE_OP:-${op:-update}}
local retval=$?
# If the install operation fails, remove the topic
if [[ $op == install && $retval != 0 ]]; then
topic-remove $topic
else
_needs_reload=1
fi
fi
done
if (( _needs_reload )); then
NEEDS_RELOAD=1
fi
}
function topic-remove {
for topic in $@; do
if [[ ! -d $topic ]]; then
echo-fail "$topic doesn't exist"
continue
elif ! topic-enabled-p $topic; then
echo-fail "$topic isn't enabled"
continue
fi
[[ -z $INHIBIT_INIT ]] && _topic-do $topic clean
_exec rm -f "$(topic-path $topic)" && \
echo-ok "Removed $topic" || \
echo-fail "Couldn't remove $topic"
done
}
function clean {
local paths; paths=( $XDG_CACHE_HOME $XDG_CONFIG_HOME $XDG_DATA_HOME $XDG_BIN_HOME $DOTFILES_DATA )
local links; links=( ~/.*(-@DN) ${^paths}/**(-@DN))
if (( ${#links[@]} > 0 )); then
echo-info "Removing dead dotfile symlinks..."
for link in "${links[@]}"; do
[[ -e $link ]] || _exec rm -fv "$link"
done
else
echo-ok "No dead symlinks in HOME"
fi
# Remove empty dotfile folders
local dirs; dirs=( ${^paths}/**(N/^F) ~/.*(N/^F) )
if (( ${#dirs[@]} > 0 )); then
echo-info "Removing empty dotfile directories..."
for dir in "${dirs[@]}"; do
_exec rmdir "$dir" && echo-ok "Deleted ${dir/$HOME/~}"
done
else
echo-ok "No empty directories in HOME"
fi
}
function _dot {
while getopts acdfhlLit opt; do
case $opt in
a) TARGETS=( $(_topics) );;
c) CLEAN=1 ;;
d) REMOVE=1 ;;
L) LIST=1 ;;
f) export LINK=1 ;;
l) export LINK=1 ;&
i) export INHIBIT_INIT=1 ;;
t) export DRYRUN=1 ;;
h) cat <<EOL
Usage: dot [-acdlLit] [TOPIC...]
-a Target all enabled topics (ignores TOPIC args)
-c Afterwards, remove dead symlinks & empty
dot-directories in HOME (Can be used alone)
-d Disable & unlink topic(s)
-f Force install & link
-l Only enable & relink topic(s) (implies -i)
-L List enabled topics
-i Do not run install/update/clean init scripts
-t Do a test run; do not actually do anything
EOL
exit ;;
*) >&2 echo-fail "Aborted."
exit 1 ;;
esac
done
shift $((OPTIND-1))
# Print help if no options or topics were provided
if [[ $# -eq 0 && -z $CLEAN && -z $LIST && -z $REMOVE ]]; then
set -- -h
# recursively re-call _dot to display help
_dot "$@"
return
fi
if [[ -n $LIST ]]; then
_topics | tr ' ' $'\n'
else
pushd -qL "$DOTFILES"
if [[ -n $REMOVE ]]; then
topic-remove ${TARGETS:-$@}
else
topic-init ${TARGETS:-$@}
fi
[[ -n $CLEAN ]] && clean
popd -q
fi
if (( NEEDS_RELOAD )) && [[ -z $DRYRUN ]]; then
echo-note "Reloading shell..."
exec $SHELL -l
fi
}
## Bootstrap
if [[ $ZSH_EVAL_CONTEXT != *:file ]]; then
_dot "$@"
fi