-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·296 lines (253 loc) · 7.26 KB
/
install.sh
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
#!/usr/bin/env bash
set -euo pipefail
export IFS=$'\n\t'
DEBUG_ENABLED=${DEBUG_ENABLED:-"false"}
is_debug_enabled() {
[[ "${DEBUG_ENABLED}" == "true" ]]
}
log_debug() {
if is_debug_enabled; then
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')] [debug]: $*"
fi
}
log_error() {
echo "❌ $*"
}
#######################################
# Extract the inline dotfiles statements
# that may exists in the given file.
#
# Globals:
# None
# Arguments:
# full file path
# result - variable that will hold
# the returned value
# Returns:
# None
#######################################
extract_inline_statements() {
local path=${1:-}
local -n __result=${2:-}
local parsed_statements=
statements=$(sed -nr '/#( )?dotfiles( )?:( )?/p' "$path")
if [[ ! -z "$statements" ]]; then
for st in $statements; do
statement_content=$(echo "$st" | sed -rn 's/.*#( )?dotfiles( )?:( )?/\3/pi')
#TODO check if $statement_content already has src
if [[ -z "$parsed_statements" ]]; then
parsed_statements="src=$path $statement_content"
else
parsed_statements="$parsed_statements\nsrc=$path $statement_content"
fi
__result=$parsed_statements
done
return
fi
# shellcheck disable=SC2034
__result=
}
#######################################
# Process the dotfiles statement that
# looks like:
# src=file dst=/destination/directory_or_file execBefore=ls execAfter=ls
#
# Globals:
# None
# Arguments:
# statement
# base_path (optional) it will be used if statament does not contain src
# Returns:
# None
#######################################
process_statement() {
local statement=${1}
local base_path=${2}
log_debug "Evaluating the statement \"$statement\"."
# it is necessary to declare the variables first to avoid "unbound variable" errors
evaluated=$(
eval "declare -A info
info[src]=; src=;
info[dst]=; dst=;
info[execBefore]=; execBefore=;
info[execAfter]=; execAfter=;
$statement
if [[ ! -z "\$src" ]]; then
if [[ ! -z \"$base_path\" ]]; then
info[src]=$base_path/\$src
else
info[src]=\$src
fi
fi
info[dst]=\$dst
info[execBefore]=\$execBefore
info[execAfter]=\$execAfter
typeset -p info"
)
eval "$evaluated"
if [[ -z "${info[src]}" ]]; then
log_debug "Skipping the statement because it has no \"src\" field."
return
fi
if [[ -d "${info[src]}" ]]; then
src_symbol='/'
else
src_symbol=''
fi
if is_debug_enabled; then
log_debug "Processing ${info[src]}:"
elif [[ $(basename "${info[src]}") == '.' ]]; then
printf "%-20.20s" "$(basename "$(dirname "${info[src]}")")${src_symbol}"
else
printf "%-20.20s" "$(basename "${info[src]}")${src_symbol}"
fi
if [[ ! -z "${info[execBefore]}" ]]; then
log_debug "Executing \"execBefore\": ${info[execBefore]}"
eval "${info[execBefore]}" # this command must return 0, otherwise the script will stop
fi
create_link "${info[src]}" "${info[dst]}" msg
echo "$msg"
if [[ ! -z "${info[execAfter]}" ]]; then
log_debug "Executing \"execAfter\": ${info[execAfter]}"
eval "${info[execAfter]}" # this command must return 0, otherwise the script will stop
fi
if is_debug_enabled; then
echo
fi
}
#######################################
# Creates a link between origin and
# destination
#
# Globals:
# None
# Arguments:
# origin - full file path
# destination - full file path
# result - variable that will hold
# the returned value
# force_fail - yes or no if it must fail
# if destination already exists
# Returns:
# None
#######################################
create_link() {
local origin=${1}
local destination=${2:-}
local -n __result=${3:-}
local force_fail=${4:-}
log_debug "Creating link $origin ➡️ $destination."
if [[ $(basename "$origin") == "." ]]; then
# origin is the directory where the .dotfilesrc is in
origin=$(dirname "$origin")
fi
if [[ -z "$destination" ]]; then
__result=$(log_error "destination is empty")
return
fi
if [[ $destination == */ ]]; then # destination is a directory
destination="${destination}$(basename "${origin}")"
src_symbol='/'
else # destination is a full path to a file
# remove lst / in order to be able to check if it exists (in case it is a link)
destination=${destination%/}
src_symbol=''
fi
if [[ -L "$destination" ]]; then
log_debug "Destination exists and it is a symbolic link, so it will be removed."
rm -f "$destination"
fi
if [[ -e "$destination" ]]; then
log_debug "Destination $destination already exists"
if [[ "$force_fail" == "yes" ]]; then
__result=$(log_error "destination '$destination' already exists")
return
fi
if [[ ! -d "$origin" ]]; then
log_error "file $destination already exists"
exit 1
fi
pushd "$origin" >/dev/null
for i in **; do
create_link "$origin/$i" "$destination/$i" foo "yes"
done
popd >/dev/null
__result="✅ files linked within ${destination}${src_symbol}"
return
fi
# TODO check if parent dir exists
ln -s "$origin" "$destination"
__result="✅ linked to ${destination}${src_symbol}"
}
process_dotfilesrc_files() {
local full_path=${1}
log_debug "Searching .dotfilesrc files in $full_path"
shopt -s dotglob globstar nullglob
for item in "$full_path"/**/.dotfilesrc; do
log_debug "Analysing dotfilesrc file at $item"
local info=
while IFS= read -r line; do
process_statement "$line" "$full_path"
done <"$item"
done
}
process_inline_statements() {
local full_path=${1}
log_debug "Searching in-line statements in files under $full_path"
shopt -s dotglob globstar nullglob
local destination
for item in "$full_path"/**; do
if [[ ! -f "$item" ]]; then
continue
fi
extract_inline_statements "$item" statements
if [[ -z "$statements" ]]; then
continue
fi
# if statements contain multiple lines, printf will make this loop work
for statement in $(printf "$statements"); do
if is_debug_enabled; then
log_debug "Processing in-line statement \"$statement\" from file \"$item\"."
else
printf "%-20.20s" "$(basename "$item") "
fi
process_statement "$statement" ""
if is_debug_enabled; then
echo
fi
done
done
}
main() {
declare INSTALLATION_DIR
# shellcheck disable=SC2128
if [ ! -z "$BASH_SOURCE" ]; then FILE="${BASH_SOURCE[0]}"; else FILE="$0"; fi
INSTALLATION_DIR=$(
exec 2>/dev/null
cd -- "$(dirname "$FILE")"
unset PWD
/usr/bin/pwd || /bin/pwd || pwd
)
for arg in "$@"; do
case ${arg} in
--debug)
DEBUG_ENABLED="true"
;;
--verbose-debug)
DEBUG_ENABLED="true"
set -x
;;
esac
done
for directory in config icons bin; do
process_dotfilesrc_files "$INSTALLATION_DIR/$directory"
process_inline_statements "$INSTALLATION_DIR/$directory"
done
log_debug "Successfully finished"
}
# set default values for some XDG environment variables, if not set
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
main "$@"
echo "done"