| 1 | #!/usr/bin/env bash |
| 2 | # Build the datapack zip with the same internal layout as the original |
| 3 | # (pack.mcmeta, README.txt and data/ at the zip root; files only, no wrapper). |
| 4 | # |
| 5 | # Minecraft-versie komt uit het bestand minecraft-version in de repo-root. |
| 6 | # Tag: eerste argument, $TAG, of $CI_COMMIT_TAG. Een leidende v wordt |
| 7 | # gestript, zodat de naam death-firework-<mc>-v<tag>.zip nooit 'vv' bevat. |
| 8 | # |
| 9 | # Gebruik: scripts/ci/pack.sh [tag] |
| 10 | # Optioneel: scripts/ci/pack.sh --compare ORIGINEEL.zip [tag] |
| 11 | set -euo pipefail |
| 12 | root="$(cd "$(dirname "$0")/../.." && pwd)" |
| 13 | cd "$root" |
| 14 | |
| 15 | compare="" |
| 16 | if [[ "${1:-}" == "--compare" ]]; then |
| 17 | compare="${2:?origineel zip}" |
| 18 | shift 2 |
| 19 | fi |
| 20 | |
| 21 | tag="${1:-${TAG:-${CI_COMMIT_TAG:-}}}" |
| 22 | [[ -n "$tag" ]] || { echo "geen tag (geef een argument, \$TAG of \$CI_COMMIT_TAG)" >&2; exit 1; } |
| 23 | tag="${tag#v}" |
| 24 | |
| 25 | mc="$(tr -d '[:space:]' < minecraft-version)" |
| 26 | [[ -n "$mc" ]] || { echo "minecraft-version is leeg" >&2; exit 1; } |
| 27 | |
| 28 | out_dir=build/out |
| 29 | mkdir -p "$out_dir" |
| 30 | name="death-firework-${mc}-v${tag}.zip" |
| 31 | out="${out_dir}/${name}" |
| 32 | |
| 33 | python3 - "$out" <<'PY' |
| 34 | import sys, zipfile |
| 35 | from pathlib import Path |
| 36 | |
| 37 | out = Path(sys.argv[1]) |
| 38 | if out.exists(): |
| 39 | out.unlink() |
| 40 | |
| 41 | # Only pack files (no directory entries, no CI/README.md/minecraft-version). |
| 42 | members = [Path("pack.mcmeta"), Path("README.txt")] |
| 43 | members += sorted(p for p in Path("data").rglob("*") if p.is_file()) |
| 44 | missing = [p for p in members if not p.is_file()] |
| 45 | if missing: |
| 46 | raise SystemExit("ontbrekende pack-bestanden: " + ", ".join(map(str, missing))) |
| 47 | |
| 48 | with zipfile.ZipFile(out, "w", compression=zipfile.ZIP_DEFLATED) as zf: |
| 49 | for path in members: |
| 50 | zf.write(path, arcname=path.as_posix()) |
| 51 | |
| 52 | print(f"geschreven {out} ({len(members)} bestanden)") |
| 53 | PY |
| 54 | |
| 55 | if [[ -n "$compare" ]]; then |
| 56 | python3 - "$out" "$compare" <<'PY' |
| 57 | import sys, zipfile |
| 58 | from pathlib import Path |
| 59 | |
| 60 | built = Path(sys.argv[1]) |
| 61 | orig = Path(sys.argv[2]) |
| 62 | |
| 63 | def files(zpath): |
| 64 | with zipfile.ZipFile(zpath) as zf: |
| 65 | return {n: zf.read(n) for n in zf.namelist() if not n.endswith("/")} |
| 66 | |
| 67 | a, b = files(built), files(orig) |
| 68 | only_new, only_old = sorted(set(a) - set(b)), sorted(set(b) - set(a)) |
| 69 | changed = sorted(k for k in set(a) & set(b) if a[k] != b[k]) |
| 70 | if only_new or only_old or changed: |
| 71 | print("zip-structuur of inhoud wijkt af van het origineel:", file=sys.stderr) |
| 72 | for n in only_new: |
| 73 | print(f" extra: {n}", file=sys.stderr) |
| 74 | for n in only_old: |
| 75 | print(f" ontbreekt: {n}", file=sys.stderr) |
| 76 | for n in changed: |
| 77 | print(f" inhoud anders: {n}", file=sys.stderr) |
| 78 | sys.exit(1) |
| 79 | print(f"identiek aan {orig}: {len(a)} bestanden, zelfde paden en inhoud (geen wrapper, pack.mcmeta en data/ in de root).") |
| 80 | PY |
| 81 | fi |
| 82 | |
| 83 | echo "$out" |