-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkg_build.sh
More file actions
367 lines (334 loc) · 12.1 KB
/
pkg_build.sh
File metadata and controls
367 lines (334 loc) · 12.1 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
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
#!/usr/bin/env bash
set -euo pipefail
CWD="$(pwd)"
plgfile="$CWD/plugins/appdata.cleanup.plus.plg"
xmlfile="$CWD/appdata.cleanup.plus.xml"
source_dir="$CWD/source/appdata.cleanup.plus"
archive_dir="$CWD/archive"
archive_prefix="appdata.cleanup.plus"
release_guard_script="$CWD/scripts/release_guard.sh"
ensure_changes_entry_script="$CWD/scripts/ensure_plg_changes_entry.sh"
version_override="${APPDATA_CLEANUP_PLUS_VERSION_OVERRIDE:-}"
branch_override="${APPDATA_CLEANUP_PLUS_BUILD_BRANCH:-}"
today_version="$(date +"%Y.%m.%d")"
version="${today_version}.01"
dry_run=false
validate_after_build=true
tmpdir=""
cleanup_tmpdir() {
if [ -n "${tmpdir:-}" ] && [ -d "${tmpdir}" ]; then
rm -rf "${tmpdir}"
fi
}
detect_git_branch() {
local detected=""
if command -v git >/dev/null 2>&1 && git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
detected="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)"
if [ -z "$detected" ] || [ "$detected" = "HEAD" ]; then
detected="${GITHUB_REF_NAME:-}"
detected="${detected#refs/heads/}"
fi
fi
printf '%s' "$detected"
}
print_usage() {
cat <<'EOF'
Usage: pkg_build.sh [options]
--branch NAME Force manifest/XML URLs to branch NAME
--dry-run Show the computed version and output paths without writing files
--validate Run scripts/release_guard.sh after build (default)
--no-validate Skip post-build validation
-h, --help Show this help
EOF
}
require_commands() {
local missing=()
local cmd=""
for cmd in "$@"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
missing+=("$cmd")
fi
done
if [ "${#missing[@]}" -gt 0 ]; then
echo "ERROR: Missing required commands: ${missing[*]}" >&2
exit 1
fi
}
plugin_summary_text() {
printf '%s' "Appdata Cleanup Plus finds orphaned appdata folders from removed Docker containers so you can review and delete them."
}
plugin_readme_title_for_branch() {
local target_branch="${1:-}"
if [ "$target_branch" = "dev" ]; then
printf '%s' "**Appdata Cleanup Plus (Dev)**"
return
fi
printf '%s' "**Appdata Cleanup Plus**"
}
plugin_description_for_branch() {
local target_branch="${1:-}"
local description=""
description="$(plugin_summary_text)"
if [ "$target_branch" = "dev" ]; then
description="${description} Dev build: testing channel. Expect preview changes before main."
fi
printf '%s' "$description"
}
apply_branch_channel_messaging() {
local package_root="${1:-}"
local target_branch="${2:-}"
local readme_file="${package_root}/usr/local/emhttp/plugins/appdata.cleanup.plus/README.md"
local summary=""
local title=""
if [ -z "$package_root" ] || [ -z "$target_branch" ]; then
echo "ERROR: apply_branch_channel_messaging requires a package root and branch." >&2
exit 1
fi
summary="$(plugin_summary_text)"
title="$(plugin_readme_title_for_branch "$target_branch")"
if [ -f "$readme_file" ]; then
if [ "$target_branch" = "dev" ]; then
printf '%s\n\n%s\n\n%s\n' "$title" "$summary" "Dev build: testing channel. Expect preview changes before main." > "$readme_file"
else
printf '%s\n\n%s\n' "$title" "$summary" > "$readme_file"
fi
fi
}
normalize_packaged_page_line_endings() {
local package_root="${1:-}"
local page_file="${package_root}/usr/local/emhttp/plugins/appdata.cleanup.plus/AppdataCleanupPlus.page"
if [ -z "$package_root" ]; then
echo "ERROR: normalize_packaged_page_line_endings requires a package root." >&2
exit 1
fi
if [ -f "$page_file" ]; then
perl -0pi -e 's/\r\n/\n/g' "$page_file"
fi
}
ensure_repo_layout() {
if [ ! -f "$plgfile" ]; then
echo "ERROR: Missing plugin manifest: $plgfile" >&2
exit 1
fi
if [ ! -f "$xmlfile" ]; then
echo "ERROR: Missing CA template: $xmlfile" >&2
exit 1
fi
if [ ! -d "$source_dir" ]; then
echo "ERROR: Missing plugin source directory: $source_dir" >&2
exit 1
fi
}
stable_date_part() {
local input="${1:-}"
if [[ "$input" =~ ^([0-9]{4}\.[0-9]{2}\.[0-9]{2})(\.[0-9]+)?$ ]]; then
echo "${BASH_REMATCH[1]}"
return
fi
echo ""
}
normalize_stable_version_for_unraid() {
local input="${1:-}"
if [[ "$input" =~ ^([0-9]{4}\.[0-9]{2}\.[0-9]{2})$ ]]; then
echo "${BASH_REMATCH[1]}.01"
return
fi
if [[ "$input" =~ ^([0-9]{4}\.[0-9]{2}\.[0-9]{2})\.([0-9]+)$ ]]; then
printf '%s.%02d\n' "${BASH_REMATCH[1]}" "$((10#${BASH_REMATCH[2]}))"
return
fi
echo "$input"
}
next_patch_version() {
local input="${1:-}"
if [[ "$input" =~ ^([0-9]{4}\.[0-9]{2}\.[0-9]{2})\.([0-9]+)$ ]]; then
printf '%s.%02d\n' "${BASH_REMATCH[1]}" "$((10#${BASH_REMATCH[2]} + 1))"
return
fi
echo "${input}.01"
}
highest_stable_archive_version_for_date() {
local target_date="${1:-}"
local archive=""
local versions=()
shopt -s nullglob
for archive in "$archive_dir/$archive_prefix-"*-x86_64-1.txz; do
local name="${archive##*/}"
if [[ "$name" =~ ^${archive_prefix}-(.+)-x86_64-1\.txz$ ]]; then
local archive_version="${BASH_REMATCH[1]}"
if [ "$(stable_date_part "$archive_version")" = "$target_date" ]; then
versions+=("$(normalize_stable_version_for_unraid "$archive_version")")
fi
fi
done
shopt -u nullglob
if [ "${#versions[@]}" -eq 0 ]; then
return
fi
printf '%s\n' "${versions[@]}" | sort -V | tail -n1
}
next_stable_version_for_date() {
local target_date="${1:-}"
local highest=""
highest="$(highest_stable_archive_version_for_date "$target_date" || true)"
if [ -z "$highest" ]; then
echo "${target_date}.01"
return
fi
next_patch_version "$highest"
}
rewrite_manifest_branch_metadata() {
local target_file="${1:-}"
local target_branch="${2:-}"
if [ -z "$target_file" ] || [ -z "$target_branch" ]; then
echo "ERROR: rewrite_manifest_branch_metadata requires a file and branch." >&2
exit 1
fi
sed -E -i 's|^<!ENTITY pluginURL ".*">|<!ENTITY pluginURL "https://raw.githubusercontent.com/\&github;/'"$target_branch"'/plugins/\&name;.plg">|' "$target_file"
sed -E -i 's|<URL>https://raw.githubusercontent.com/.*?/archive/.*</URL>|<URL>https://raw.githubusercontent.com/\&github;/'"$target_branch"'/archive/\&name;-\&version;-x86_64-1.txz</URL>|' "$target_file"
}
sync_ca_template_metadata() {
local target_file="${1:-}"
local target_date="${2:-}"
local target_branch="${3:-}"
local target_description=""
if [ -z "$target_file" ] || [ -z "$target_date" ] || [ -z "$target_branch" ]; then
echo "ERROR: sync_ca_template_metadata requires a file, date, and branch." >&2
exit 1
fi
target_description="$(plugin_description_for_branch "$target_branch")"
sed -i "s|<Date>.*</Date>|<Date>${target_date}</Date>|" "$target_file"
sed -i "s|<PluginURL>.*</PluginURL>|<PluginURL>https://raw.githubusercontent.com/alexphillips-dev/Appdata-Cleanup-Plus/${target_branch}/plugins/appdata.cleanup.plus.plg</PluginURL>|" "$target_file"
sed -i "s|<Icon>.*</Icon>|<Icon>https://raw.githubusercontent.com/alexphillips-dev/Appdata-Cleanup-Plus/${target_branch}/source/appdata.cleanup.plus/usr/local/emhttp/plugins/appdata.cleanup.plus/images/appdata.cleanup.plus.png</Icon>|" "$target_file"
sed -i "s|<Beta>.*</Beta>|<Beta>False</Beta>|" "$target_file"
sed -i "s|<Name>.*</Name>|<Name>Appdata Cleanup Plus</Name>|" "$target_file"
perl -0pi -e 's{<Description>\s*.*?\s*</Description>}{<Description>\n'"$target_description"'\n</Description>}s' "$target_file"
}
validate_manifest_branch_matrix() {
local source_file="${1:-}"
local branch_name=""
for branch_name in dev main; do
local probe_file=""
local entity_url=""
local archive_url=""
local expected_entity_url="https://raw.githubusercontent.com/&github;/${branch_name}/plugins/&name;.plg"
local expected_archive_url="https://raw.githubusercontent.com/&github;/${branch_name}/archive/&name;-&version;-x86_64-1.txz"
probe_file="$(mktemp)"
cp "$source_file" "$probe_file"
rewrite_manifest_branch_metadata "$probe_file" "$branch_name"
entity_url="$(grep -m1 '^<!ENTITY pluginURL ' "$probe_file" | sed -E 's/^<!ENTITY pluginURL "([^"]+)".*/\1/' || true)"
archive_url="$(grep -m1 '<URL>' "$probe_file" | sed -E 's|.*<URL>(https://raw.githubusercontent.com/&github;/[^<]*/archive/&name;-&version;-x86_64-1.txz)</URL>.*|\1|' || true)"
rm -f "$probe_file"
if [ "$entity_url" != "$expected_entity_url" ]; then
echo "ERROR: Manifest pluginURL rewrite mismatch for ${branch_name}." >&2
exit 1
fi
if [ "$archive_url" != "$expected_archive_url" ]; then
echo "ERROR: Manifest archive URL rewrite mismatch for ${branch_name}." >&2
exit 1
fi
done
}
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--dry-run)
dry_run=true
;;
--branch)
if [ -z "${2:-}" ]; then
echo "ERROR: --branch requires a branch name." >&2
exit 1
fi
branch_override="${2:-}"
shift
;;
--validate)
validate_after_build=true
;;
--no-validate)
validate_after_build=false
;;
-h|--help)
print_usage
exit 0
;;
*)
echo "ERROR: Unknown argument: ${1}" >&2
print_usage >&2
exit 1
;;
esac
shift
done
trap cleanup_tmpdir EXIT
ensure_repo_layout
require_commands tar sed date awk grep sort head tail mktemp md5sum perl cp mkdir rm
if [ -n "$branch_override" ]; then
branch="$branch_override"
else
detected_branch="$(detect_git_branch)"
if [ "$detected_branch" = "dev" ]; then
branch="dev"
else
branch="main"
fi
fi
if ! [[ "$branch" =~ ^[A-Za-z0-9._/-]+$ ]]; then
echo "ERROR: Invalid branch name: $branch" >&2
exit 1
fi
if [ -n "$version_override" ]; then
version="$(normalize_stable_version_for_unraid "$version_override")"
if [ "$(stable_date_part "$version")" != "$today_version" ]; then
echo "ERROR: APPDATA_CLEANUP_PLUS_VERSION_OVERRIDE must use today's date (${today_version})." >&2
exit 1
fi
else
version="$(next_stable_version_for_date "$today_version")"
fi
filename="$archive_dir/$archive_prefix-$version-x86_64-1.txz"
while [ -f "$filename" ]; do
version="$(next_patch_version "$version")"
filename="$archive_dir/$archive_prefix-$version-x86_64-1.txz"
done
xml_date="${version:0:4}-${version:5:2}-${version:8:2}"
if [ "$dry_run" = true ]; then
echo "Dry run: no files will be written."
echo "Version: $version"
echo "Branch: $branch"
echo "Archive target: $filename"
echo "Manifest: $plgfile"
echo "CA template: $xmlfile"
exit 0
fi
mkdir -p "$archive_dir"
tmpdir="$(mktemp -d)"
package_root="${tmpdir}/package"
mkdir -p "$package_root"
cp -R "${source_dir}/." "$package_root/"
apply_branch_channel_messaging "$package_root" "$branch"
normalize_packaged_page_line_endings "$package_root"
tar --sort=name \
--mtime='UTC 1970-01-01' \
--owner=0 \
--group=0 \
--numeric-owner \
--exclude='./pkg_build.sh' \
-cJf "$filename" \
-C "$package_root" .
md5="$(md5sum "$filename" | awk '{print $1}')"
sed -i "s|<!ENTITY version.*>|<!ENTITY version \"$version\">|" "$plgfile"
sed -i "s|<!ENTITY md5.*>|<!ENTITY md5 \"$md5\">|" "$plgfile"
rewrite_manifest_branch_metadata "$plgfile" "$branch"
validate_manifest_branch_matrix "$plgfile"
sync_ca_template_metadata "$xmlfile" "$xml_date" "$branch"
if [ -f "$ensure_changes_entry_script" ]; then
bash "$ensure_changes_entry_script"
fi
if [ "$validate_after_build" = true ] && [ -f "$release_guard_script" ]; then
bash "$release_guard_script"
fi
echo "Package created: $filename"
echo "Version: $version"
echo "MD5: $md5"
echo "Branch: $branch"