-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy
executable file
·229 lines (201 loc) · 5.97 KB
/
deploy
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
#!/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 {
${DRYRUN:+echo-debug} $@
return ${DRYRUN:+1}
}
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
popd -q
return $?
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 {
declare -f $1 >/dev/null || return 1
echo-info "$TOPIC.init.$1: start"
$1
local retval=$?
echo-ok "$TOPIC.init.$1: done"
return $retval
}
# Functions to initialize, link, and update topics
function topic-init {
for topic in $@; do
# Check if the topic directory exists
if [[ ! -d $topic ]]; then
echo-fail "$topic doesn't exist!"
continue
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
local 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}}
# If the install operation fails, remove the topic
if [[ $op == install && $? != 0 ]]; then
topic-remove $topic
else
# Reload the shell if everything goes well
exec "$SHELL" -l
fi
fi
done
}
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[@]} > 1 ]]; 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[@]} > 1 ]]; 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 acdefhlLit 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: ${0:A:t} [-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))
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
}
## Bootstrap
if [[ $ZSH_EVAL_CONTEXT != *:file ]]; then
_dot "$@"
fi