Skip to content

Cleanup

Cleanup #11

name: Build package versions
on:
push:
branches:
- main
workflow_dispatch:
permissions:
contents: write
concurrency:
group: build-zips-${{ github.ref }}
cancel-in-progress: false
jobs:
build:
name: 📦 Build package versions
runs-on: ubuntu-latest
steps:
- name: ⬇️ Checkout
uses: actions/checkout@v4
- name: 🛠️ Build version ZIPs
shell: bash
run: |
set -euo pipefail
shopt -s nullglob
MAIN_VERSION="1.0.1"
mkdir -p .versions
json_file() {
if [[ -f "$1/module.json" ]]; then echo "$1/module.json"; return; fi
if [[ -f "$1/system.json" ]]; then echo "$1/system.json"; return; fi
echo ""
}
patch_main_manifest() {
local dir="$1" pkg="$2"
local jf; jf="$(json_file "$dir")" || true
[[ -n "$jf" ]] || return 0
local base; base="$(basename "$jf")"
export NEW_MAIN_VERSION="$MAIN_VERSION"
export PKG_NAME="$pkg"
export JSON_FILE_BASENAME="$base"
local tmp="${jf}.tmp"
jq -S '
.version = env.NEW_MAIN_VERSION
| .download = ("https://raw.githubusercontent.com/" + env.GITHUB_REPOSITORY + "/main/" + env.PKG_NAME + "/" + env.PKG_NAME + ".zip")
| .manifest = ("https://raw.githubusercontent.com/" + env.GITHUB_REPOSITORY + "/main/" + env.PKG_NAME + "/" + env.JSON_FILE_BASENAME)
' "$jf" > "$tmp"
if ! cmp -s "$tmp" "$jf"; then mv "$tmp" "$jf"; else rm -f "$tmp"; fi
}
patch_version_manifest() {
local dir="$1" pkg="$2" suffix="$3" ver="$4"
local jf; jf="$(json_file "$dir")" || true
[[ -n "$jf" ]] || return 0
local base; base="$(basename "$jf")"
export NEW_VERSION="$ver"
export PKG_NAME="$pkg"
export SUFFIX="$suffix"
export JSON_FILE_BASENAME="$base"
local tmp="${jf}.tmp"
jq -S '
.version = env.NEW_VERSION
| .download = ("https://raw.githubusercontent.com/" + env.GITHUB_REPOSITORY + "/main/.versions/" + env.PKG_NAME + "-" + env.SUFFIX + "/" + env.PKG_NAME + ".zip")
| .manifest = ("https://raw.githubusercontent.com/" + env.GITHUB_REPOSITORY + "/main/.versions/" + env.PKG_NAME + "-" + env.SUFFIX + "/" + env.JSON_FILE_BASENAME)
' "$jf" > "$tmp"
mv "$tmp" "$jf"
}
build_zip() {
local src="$1" out="$2"
local tmp="${out}.tmp"
local parent; parent="$(dirname "$out")"
mkdir -p "$parent"
(
cd "$src"
zip -rqX "$tmp" . -x "*.zip" -x ".versions/*"
)
if [[ ! -f "$out" ]] || ! cmp -s "$tmp" "$out"; then
mv "$tmp" "$out"
echo "Updated ZIP: $out"
return 0
else
rm -f "$tmp"
return 1
fi
}
rsync_copy() {
local from="$1" to="$2"
rm -rf "$to"
mkdir -p "$to"
rsync -a --delete --exclude '*.zip' --exclude '.versions/' "$from/" "$to/"
}
changed=0
for d in */ ; do
case "$d" in
.*/|.git/|.github/|.vscode/|.versions/) continue ;;
esac
d="${d%/}"
pkg="$d"
patch_main_manifest "$d" "$pkg" || true
if build_zip "$d" "$d/$pkg.zip"; then changed=1; fi
for suffix ver in v0 0.0.1 v2 2.0.1; do
out=".versions/${pkg}-${suffix}"
tmp="${out}.tmp"
rsync_copy "$d" "$tmp"
patch_version_manifest "$tmp" "$pkg" "$suffix" "$ver"
if ! diff -qr "$tmp" "$out" >/dev/null 2>&1; then
rm -rf "$out"
mv "$tmp" "$out"
echo "Updated version dir: $out"
changed=1
else
rm -rf "$tmp"
fi
if build_zip "$out" "$out/$pkg.zip"; then changed=1; fi
done
done
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add */*.zip */module.json */system.json .versions/
if git diff --cached --quiet; then
echo "No changes to commit."
else
git commit -m "chore: Build packages & versioned copies (automated)"
git push
fi