#!/usr/bin/env bash # # Build the release zip. # # ./build.sh -> dist/mc-packwiz-deploy-.zip # ./build.sh -o /tmp -> writes there instead # ./build.sh -v 1.2.0 -> override the version # # Version comes from the latest git tag when there is one, otherwise VERSION # below. A dirty working tree gets a -dirty suffix so a zip built mid-edit is # never mistaken for a tagged release. set -euo pipefail VERSION="0.1.0" NAME="mc-packwiz-deploy" OUTDIR="" HERE="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" # Everything the zip ships. Anything not listed here stays out, so stray notes # or test output in the working tree can't leak into a release. FILES=( README.md deploy.sh mc-service-setup.sh packwiz-setup.sh mc-refresh-restart.sh packwiz-http.py ) die() { echo "error: $*" >&2; exit 1; } info() { echo ">>> $*"; } usage() { awk 'NR>1 && /^#/ {sub(/^# ?/,""); print; next} NR>1 {exit}' "$0"; exit "${1:-0}"; } while [ $# -gt 0 ]; do case "$1" in -o|--outdir) OUTDIR="${2:-}"; shift 2 ;; -v|--version) VERSION="${2:-}"; VERSION_FORCED=1; shift 2 ;; -h|--help) usage 0 ;; *) echo "unknown option: $1" >&2; usage 1 ;; esac done cd "$HERE" command -v zip >/dev/null || die "zip not found — sudo dnf install zip" # ---------------------------------------------------------------- version --- if [ -z "${VERSION_FORCED:-}" ] && git rev-parse --git-dir >/dev/null 2>&1; then tag="$(git describe --tags --abbrev=0 2>/dev/null || true)" [ -n "$tag" ] && VERSION="${tag#v}" if ! git diff --quiet HEAD -- "${FILES[@]}" 2>/dev/null; then VERSION="${VERSION}-dirty" info "working tree has uncommitted changes to shipped files" fi fi [ -n "$OUTDIR" ] || OUTDIR="$HERE/dist" mkdir -p "$OUTDIR" ZIP="$OUTDIR/${NAME}-${VERSION}.zip" # ------------------------------------------------------------------ check --- for f in "${FILES[@]}"; do [ -f "$f" ] || die "missing file: $f" done info "syntax-checking scripts" fail=0 for f in "${FILES[@]}"; do case "$f" in *.sh) bash -n "$f" || { echo " FAILED: $f" >&2; fail=1; } ;; *.py) python3 -m py_compile "$f" || { echo " FAILED: $f" >&2; fail=1; } ;; esac done (( fail )) && die "refusing to build a zip containing scripts that don't parse" # ------------------------------------------------------------------ stage --- # Staged in a temp tree so the zip has a single top-level directory and the # scripts land executable regardless of how git checked them out. STAGE="$(mktemp -d)" trap 'rm -rf "$STAGE"' EXIT DEST="$STAGE/$NAME" mkdir -p "$DEST" for f in "${FILES[@]}"; do case "$f" in *.sh|*.py) install -m 755 "$f" "$DEST/$f" ;; *) install -m 644 "$f" "$DEST/$f" ;; esac done printf '%s\n' "$VERSION" > "$DEST/VERSION" # ------------------------------------------------------------------ build --- rm -f "$ZIP" ( cd "$STAGE" && zip -qr "$ZIP" "$NAME" ) info "built $ZIP" unzip -l "$ZIP" | sed 's/^/ /' if command -v sha256sum >/dev/null; then sha256sum "$ZIP" | tee "$ZIP.sha256" | sed 's/^/ /' fi