-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeploy
executable file
·191 lines (173 loc) · 4.87 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
#!/usr/bin/env zsh
# Dotfile manager
#
# By ztlevi <[email protected]>
# https://github.com/ztlevi/dotty
#
# Requires: git, zsh
source "${0:A:h}/config/env"
local SEP='.'
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=( "$DOTTY_DATA_HOME"/*.topic(N) )
echo ${${${topics[@]#$DOTTY_DATA_HOME/}%.topic}/$SEP//}
}
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
}
# deploy functions
function topic-init {
for topic in $@; do
if [[ ! -d $topic ]]; then
echo-fail "$topic doesn't exist!"
continue
fi
echo-ok "Start deploying $topic"
local tpath="$DOTTY_CONFIG_HOME/$topic"
local tsympath="$(topic-path $topic)"
if [[ -d "$topic/bin" ]]; then
export path; path=( $tpath/bin $path )
echo-ok "$topic: added $topic/bin to PATH, temporarily"
fi
if [[ -n $LINK ]] || ! topic-enabled-p $topic; then
local op=install
fi
# Load the env.zsh for each topic
[[ -f "$tpath/env.zsh" ]] && source "$tpath/env.zsh"
if [[ -z $INHIBIT_INIT ]]; then
# installation starts here because op is install
_topic-do $topic ${FORCE_OP:-${op:-update}}
# Rmoeve if installation failed
if [[ $op == install && $? != 0 ]]; then
topic-remove $topic
fi
fi
# Link in the very end
if [[ -n $LINK ]] || ! topic-enabled-p $topic; then
mklink $tpath $tsympath
_topic-do $topic link
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 $DOTTY_DATA_HOME )
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
}
if ! _is_callable brew; then
echo "homebrew cannot be found! Please edit your zshrc..."
exit 1
fi
## Bootstrap
if [[ ${ZSH_EVAL_CONTEXT} != *:file ]]; then
while getopts acrdehlLitH opt; do
case $opt in
a) TARGETS=( $(_topics) );;
c) CLEAN=1 ;;
r) REINSTALL=1 ;;
d) REMOVE=1 ;;
L) LIST=1 ;;
l) export LINK=1 ;&
i) export INHIBIT_INIT=1 ;;
t) export DRYRUN=1 ;;
H) HELP=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.
-r Reinstall topic(s)
-d Disable & unlink topic(s)
-l Only enable & relink topic(s) (implies -i), LINK and INHIBIT_INIT is 1.
-L List enabled topics
-i Do not run install/update/clean init scripts
-t Do a test run; do not actually do anything
-H Print usage of the topic
EOL
exit ;;
*) >&2 echo-fail "Aborted."
exit 1 ;;
esac
done
shift $((OPTIND-1))
if [[ -n "${LIST}" ]]; then
_topics | tr ' ' $'\n'
else
pushd -qL "${DOTTY_HOME}"
if [[ -n "${CLEAN}" ]]; then
clean
fi
if [[ -n "${REINSTALL}" ]]; then
topic-remove ${TARGETS:-$@}
topic-init ${TARGETS:-$@}
exit 0
fi
if [[ -n "${REMOVE}" ]]; then
topic-remove ${TARGETS:-$@}
exit 0
fi
if [[ -n "${HELP}" ]]; then
for topic in $@; do
exe=cat
if _is_callable bat; then
exe="bat --paging=never"
fi
[[ -f ${DOTTY_HOME}/${topic}/README.md ]] && eval ${exe} ${DOTTY_HOME}/${topic}/README.md
[[ -f ${DOTTY_HOME}/config/${topic}/README.md ]] && eval ${exe} ${DOTTY_HOME}/config/${topic}/README.md
done
exit 0
fi
topic-init ${TARGETS:-$@}
popd -q
fi
fi