-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild-pr-preview.sh
More file actions
executable file
·105 lines (90 loc) · 3.76 KB
/
Copy pathbuild-pr-preview.sh
File metadata and controls
executable file
·105 lines (90 loc) · 3.76 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
#!/usr/bin/env bash
# Build an installable Community Applications preview for a pull request.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
PR_NUMBER="${1:?usage: $0 <pr-number> <git-sha> [output-directory]}"
GIT_SHA="${2:?usage: $0 <pr-number> <git-sha> [output-directory]}"
OUTPUT_DIR="${3:-$ROOT/dist/pr-preview}"
SOURCE_DIR="$ROOT/source/community.applications"
PLUGIN_TEMPLATE="$ROOT/plugins/community.applications.plg"
SHORT_SHA="${GIT_SHA:0:7}"
COMMIT_TIMESTAMP="$(git -C "$ROOT" show -s --format=%ct "$GIT_SHA")"
VERSION_DATE="$(python3 - "$COMMIT_TIMESTAMP" <<'PY'
from datetime import datetime, timezone
import sys
print(datetime.fromtimestamp(int(sys.argv[1]), timezone.utc).strftime("%Y.%m.%d"))
PY
)"
VERSION="${VERSION_DATE}-pr${PR_NUMBER}-${SHORT_SHA}"
PACKAGE="community.applications-${VERSION}-x86_64-1.txz"
BASE_URL="https://raw.githubusercontent.com/unraid/community.applications/pr-previews/pr/${PR_NUMBER}"
if [[ ! "$PR_NUMBER" =~ ^[0-9]+$ ]]; then
echo "PR number must be numeric: $PR_NUMBER" >&2
exit 1
fi
if [[ ! -d "$SOURCE_DIR" || ! -f "$PLUGIN_TEMPLATE" ]]; then
echo "Run this script from a complete community.applications checkout." >&2
exit 1
fi
rm -rf "$OUTPUT_DIR"
mkdir -p "$OUTPUT_DIR"
STAGING="$(mktemp -d -t ca-pr-preview.XXXXXX)"
trap 'rm -rf "$STAGING"' EXIT
# `source/.` copies the directory contents on both GNU and BSD cp. A trailing
# slash alone behaves differently on Ubuntu and previously nested the entire
# package below ./community.applications/ in CI-built previews.
COPYFILE_DISABLE=1 cp -R "$SOURCE_DIR/." "$STAGING/"
find "$STAGING" \( -name '.DS_Store' -o -name '._*' -o -name 'sftp-config.json' \) -delete
find "$STAGING" -name '.claude' -type d -prune -exec rm -rf {} + 2>/dev/null || true
chmod -R 0755 "$STAGING"
python3 "$ROOT/scripts/create-reproducible-tar.py" \
"$STAGING" "$OUTPUT_DIR/$PACKAGE" "$COMMIT_TIMESTAMP"
tar -tf "$OUTPUT_DIR/$PACKAGE" > "$STAGING/package-contents.txt"
if ! grep -Fxq './usr/local/emhttp/plugins/community.applications/Apps.page' "$STAGING/package-contents.txt"; then
echo "Preview package has an invalid root layout." >&2
exit 1
fi
if command -v md5sum >/dev/null 2>&1; then
MD5="$(md5sum "$OUTPUT_DIR/$PACKAGE" | awk '{print $1}')"
else
MD5="$(md5 -q "$OUTPUT_DIR/$PACKAGE")"
fi
python3 - "$PLUGIN_TEMPLATE" "$OUTPUT_DIR/community.applications.plg" "$VERSION" "$MD5" "$BASE_URL/$PACKAGE" "$BASE_URL/community.applications.plg" <<'PY'
from pathlib import Path
import re
import sys
source, destination, version, md5, package_url, plugin_url = sys.argv[1:]
text = Path(source).read_text(encoding="utf-8")
# Keep `name` canonical because WebGUI uses it to locate the plugin's UI
# directory. `pluginURL` must point at the stable per-PR installer so WebGUI
# update checks follow new preview builds; CA recognizes that bounded URL as an
# alias of its canonical app-feed template.
replacements = {
"version": version,
"md5": md5,
"pluginURL": plugin_url,
}
for entity, value in replacements.items():
text, count = re.subn(
rf'(<!ENTITY\s+{entity}\s+")[^"]*(">)',
rf'\g<1>{value}\g<2>',
text,
count=1,
)
if count != 1:
raise SystemExit(f"could not replace {entity} entity")
text, count = re.subn(
r'(<FILE Name="/boot/config/plugins/&name;/&name;-&version;-x86_64-1\.txz" Run="upgradepkg --install-new --reinstall">\s*<URL>)[^<]*(</URL>)',
rf'\g<1>{package_url}\g<2>',
text,
count=1,
)
if count != 1:
raise SystemExit("could not replace package URL")
Path(destination).write_text(text, encoding="utf-8")
PY
cat > "$OUTPUT_DIR/preview.json" <<EOF
{"pr":${PR_NUMBER},"sha":"${GIT_SHA}","version":"${VERSION}","package":"${PACKAGE}"}
EOF
echo "Built $OUTPUT_DIR/community.applications.plg"
echo "Installer: $BASE_URL/community.applications.plg"