This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dependencies.sh
executable file
·385 lines (327 loc) · 11.6 KB
/
dependencies.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env sh
# MIT License
#
# Copyright (c) 2022-2023 Carlo Corradini
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Current directory
# shellcheck disable=SC1007
DIRNAME=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
# Load commons
# shellcheck source=../scripts/__commons.sh
. "$DIRNAME/../scripts/__commons.sh"
# ================
# CONFIGURATION
# ================
# Configuration file
CONFIG_FILE="dependencies.config.yaml"
# Root directory
ROOT_DIR="$(readlink -f "$DIRNAME/..")"
# Synchronize flag
SYNC=false
# Synchronize force flag
SYNC_FORCE=false
# ================
# GLOBALS
# ================
# Configuration
CONFIG=
# ================
# FUNCTIONS
# ================
# Show help message
show_help() {
cat << EOF
Usage: $(basename "$0") [--config-file <FILE>] [--help] [--sync] [--sync-force]
$HELP_COMMONS_USAGE
reCluster dependencies script.
Options:
--config-file <FILE> Configuration file
Default: $CONFIG_FILE
Values:
Any valid file
--help Show this help message and exit
--sync Synchronize dependencies
--sync-force Synchronize dependencies replacing assets that are already present
$HELP_COMMONS_OPTIONS
EOF
}
# Assert dependency $1 exists
# @param $1 Dependency name
assert_dep() {
printf '%s\n' "$CONFIG" | jq --exit-status --arg name "$1" 'has($name)' > /dev/null 2>&1 || FATAL "Dependency '$1' does not exists"
}
# Return dependency configuration
# @param $1 Dependency name
dep_config() {
assert_dep "$1"
printf '%s\n' "$CONFIG" | jq --arg name "$1" '.[$name]'
}
# Generate github api url
# @param $1 Dependency name
gen_github_api_url() {
_config=$(dep_config "$1")
_url=$(printf '%s\n' "$_config" | jq --raw-output '.url')
_owner=$(printf '%s\n' "$_url" | sed 's/.*\/\([^ ]*\/[^.]*\).*/\1/' | sed 's#/[^/]*$##') || FATAL "Error reading owner from GitHub URL '$_url'"
_repo=$(printf '%s\n' "$_url" | sed 's/.*\/\([^ ]*\/[^.]*\).*/\1/' | sed 's#^.*/\([^/]*\)$#\1#') || FATAL "Error reading repository from GitHub URL '$_url'"
printf '%s\n' "https://api.github.com/repos/$_owner/$_repo/releases"
}
# Return dependency latest release
# @param $1 Dependency name
find_latest_release() {
_github_api_url=$(gen_github_api_url "$1")
download_print "$_github_api_url/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' || FATAL "Download '$_github_api_url/latest' failed"
}
# Clean dependencies environment
sync_deps_clean() {
git_files "$ROOT_DIR"
_git_files=$RETVAL
set +o noglob
# Clean dependency directories
for _dep_fd in "$DIRNAME"/*; do
_dep_fd_basename=$(basename "$_dep_fd")
if [ -f "$_dep_fd" ]; then
# File
_dep_fd_git_file=$(printf '%s\n' "$_dep_fd" | sed -n -e 's#^.*'"$ROOT_DIR"'/##p')
if ! git_has_file "$_git_files" "$_dep_fd_git_file"; then
# Remove dependency file
INFO "Removing file '$_dep_fd_basename'"
rm -f "$_dep_fd"
else
DEBUG "Keeping file '$_dep_fd_basename'"
fi
elif [ -d "$_dep_fd" ]; then
# Directory
if ! printf '%s\n' "$CONFIG" | jq --exit-status --arg dep "$_dep_fd_basename" 'has($dep)' > /dev/null 2>&1; then
# Remove dependency directory
INFO "Removing dependency directory '$_dep_fd_basename'"
rm -rf "$_dep_fd"
continue
fi
# Dependency configuration
_config=$(dep_config "$_dep_fd_basename")
# Latest release version
_latest_release=$(find_latest_release "$_dep_fd_basename")
# Clean dependency
for _fd in "$DIRNAME/$_dep_fd_basename"/*; do
_fd_basename=$(basename "$_fd")
if [ -f "$_fd" ]; then
# Check file
if ! printf '%s\n' "$_config" | jq --exit-status --arg file "$_fd_basename" '.files | has($file)' > /dev/null 2>&1; then
# Remove file
INFO "Removing '$_dep_fd_basename' file '$_fd_basename'"
rm -f "$_fd"
continue
fi
elif [ -d "$_fd" ]; then
# Skip if 'latest' release and check release directory
if ! { printf '%s\n' "$_config" | jq --exit-status '.releases | any(. == "latest")' > /dev/null 2>&1 && [ "$_fd_basename" = "$_latest_release" ]; } && ! printf '%s\n' "$_config" | jq --exit-status --arg dir "$_fd_basename" '.releases | any(. == $dir)' > /dev/null 2>&1; then
# Remove release directory
INFO "Removing '$_dep_fd_basename' release directory '$_fd_basename'"
rm -rf "$_fd"
continue
fi
# Clean remaining
for _ufd in "$DIRNAME/$_dep_fd_basename/$_fd_basename"/*; do
[ -f "$_ufd" ] || [ -d "$_ufd" ] || continue
_ufd_basename=$(basename "$_ufd")
if [ "$(printf '%s\n' "$_config" | jq --raw-output --arg asset "$_ufd_basename" 'any(.assets[]; ("^" + . + "$") as $keep | $asset | test($keep))')" = false ]; then
# Remove
INFO "Removing '$_dep_fd_basename' release '$_fd_basename' file/directory '$_ufd_basename'"
rm -rf "$_ufd"
continue
fi
done
fi
done
fi
done
set -o noglob
}
# Synchronize dependency release
# @param $1 Dependency name
# @param $2 Release
sync_dep_release() {
_dep_name=$1
_release=$2
_github_api_url=$(gen_github_api_url "$_dep_name")
case "$_release" in
latest) _is_release_latest=true ;;
*) _is_release_latest=false ;;
esac
# If release is 'latest' find tag name
if [ $_is_release_latest = true ]; then
INFO "Finding '$_dep_name' latest release"
_release=$(find_latest_release "$_dep_name")
INFO "Latest '$_dep_name' release is '$_release'"
fi
# Release id
_release_id=$(download_print "$_github_api_url/tags/$_release" | jq --raw-output .id) || FATAL "Download '$_github_api_url/tags/$_release' failed"
DEBUG "'$_dep_name' release id '$_release_id'"
# Release assets
_assets=$(download_print "$_github_api_url/$_release_id/assets" | jq --compact-output 'map({name, "url": .browser_download_url})') || FATAL "Download '$_github_api_url/$_release_id/assets' failed"
DEBUG "'$_dep_name' assets:\n$(printf '%s\n' "$_assets" | jq .)"
# Create release directory if not exists
_release_dir="$(readlink -f "$DIRNAME")/$_dep_name/$_release"
if [ ! -d "$_release_dir" ]; then
INFO "Creating '$_dep_name' directory '$_release_dir'"
mkdir -p "$_release_dir"
fi
# Download assets
while read -r _asset; do
_asset_name=$(printf '%s\n' "$_asset" | jq --raw-output '.name')
_asset_url=$(printf '%s\n' "$_asset" | jq --raw-output .url)
_asset_path="$_release_dir/$_asset_name"
# Skip if not force and asset already exists
if [ "$SYNC_FORCE" = false ] && [ -f "$_asset_path" ]; then
DEBUG "Skipping '$_dep_name' release '$_release' asset '$_asset_name' already exists"
continue
fi
# Skip if ignored
if [ "$(printf '%s\n' "$_config" | jq --raw-output --arg asset "$_asset_name" 'any(.assets[]; ("^" + . + "$") as $keep | $asset | test($keep))')" = false ]; then
DEBUG "Skipping '$_dep_name' release '$_release' asset '$_asset_name' ignored"
continue
fi
# Download
INFO "Downloading '$_dep_name' release '$_release' asset '$_asset_name' into '$_asset_path'"
download "$_asset_path" "$_asset_url"
done << EOF
$(printf '%s\n' "$_assets" | jq --compact-output '.[]')
EOF
}
# Synchronize dependency
# @param $1 Dependency name
sync_dep_files() {
_config=$(dep_config "$1")
_dep_name=$1
# Files
while read -r _file; do
_file_name=$(printf '%s\n' "$_file" | jq --raw-output '.key')
_file_url=$(printf '%s\n' "$_file" | jq --raw-output '.value')
_file_path="$(readlink -f "$DIRNAME")/$_dep_name/$_file_name"
# Skip if not force and file already exists
if [ "$SYNC_FORCE" = false ] && [ -f "$_file_path" ]; then
DEBUG "Skipping '$_dep_name' file '$_file_name' already exists"
continue
fi
# Download
INFO "Downloading '$_dep_name' file '$_file_name' from '$_file_url' into '$_file_path'"
download "$_file_path" "$_file_url"
done << EOF
$(printf '%s\n' "$_config" | jq --compact-output '.files | to_entries[]')
EOF
}
# Synchronize dependency
# @param $1 Dependency name
sync_dep() {
_config=$(dep_config "$1")
_dep_name=$1
# Releases
while read -r _release; do
_release=$(printf '%s\n' "$_release" | jq --raw-output '.')
INFO "Syncing '$_dep_name' release '$_release'"
sync_dep_release "$_dep_name" "$_release"
done << EOF
$(printf '%s\n' "$_config" | jq --compact-output '.releases[]')
EOF
# Files
if printf '%s\n' "$_config" | jq --exit-status 'has("files")' > /dev/null 2>&1; then
INFO "Syncing '$_dep_name' files"
sync_dep_files "$_dep_name"
fi
}
################################################################################################################################
# Parse command line arguments
# @param $@ Arguments
parse_args() {
while [ $# -gt 0 ]; do
# Number of shift
_shifts=1
case $1 in
--config-file)
# Configuration file
parse_args_assert_value "$@"
CONFIG_FILE=$2
_shifts=$2
;;
--help)
# Display help message and exit
show_help
exit 0
;;
--sync)
# Synchronize
SYNC=true
;;
--sync-force)
# Synchronize force
SYNC=true
SYNC_FORCE=true
;;
*)
# Commons
parse_args_commons "$@"
_shifts=$RETVAL
;;
esac
# Shift arguments
while [ "$_shifts" -gt 0 ]; do
shift
_shifts=$((_shifts = _shifts - 1))
done
done
}
# Verify system
verify_system() {
assert_cmd grep
assert_cmd jq
assert_cmd sed
assert_cmd yq
assert_downloader
# Configuration
[ -f "$CONFIG_FILE" ] || FATAL "Configuration file '$CONFIG_FILE' not found"
INFO "Reading configuration file '$CONFIG_FILE'"
CONFIG=$(yq e --output-format=json --no-colors '.' "$CONFIG_FILE") || FATAL "Error reading configuration file '$CONFIG_FILE'"
DEBUG "Configuration:" "$CONFIG"
}
# Synchronize dependency
sync_deps() {
[ "$SYNC" = true ] || return 0
_num_deps=$(printf '%s\n' "$CONFIG" | jq --raw-output '. | length')
INFO "Syncing $_num_deps dependencies"
# Clean environment
sync_deps_clean
# Dependencies
while read -r _dep; do
_dep_name=$(printf '%s\n' "$_dep" | jq --raw-output '.key')
INFO "Syncing '$_dep_name'"
sync_dep "$_dep_name"
done << EOF
$(printf '%s\n' "$CONFIG" | jq --compact-output 'to_entries[]')
EOF
INFO "Successfully synced $_num_deps dependencies"
}
# ================
# MAIN
# ================
{
parse_args "$@"
verify_system
sync_deps
}