| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #!/usr/bin/env bash
- set -euo pipefail
- die() {
- printf 'Error: %s\n' "$*" >&2
- exit 1
- }
- require_command() {
- command -v "$1" >/dev/null 2>&1 || die "Required command not found: $1"
- }
- is_excluded_path() {
- case "$1" in
- .gitignore|config.php|data|data/*|update-server|update-server/*|.codex|.codex/*|build|build/*|scripts|scripts/*|backup-server|backup-server/*)
- return 0
- ;;
- esac
- return 1
- }
- read_current_version() {
- sed -nE 's/.*define\(["'\'']APP_VERSION["'\''],[[:space:]]*["'\'']([^"'\'']+)["'\'']\).*/\1/p' "$version_file" | head -n 1
- }
- write_current_version() {
- local version="$1"
- local tmp_version_file
- # No "sed -i": GNU requires no argument, BSD/macOS requires one.
- tmp_version_file="$(mktemp)"
- sed -E "s/define\\([\"']APP_VERSION[\"'],[[:space:]]*[\"'][^\"']+[\"']\\);/define(\"APP_VERSION\", \"${version}\");/" \
- "$version_file" > "$tmp_version_file" ||
- { rm -f "$tmp_version_file"; die "Version string could not be updated in $version_file"; }
- cat "$tmp_version_file" > "$version_file" ||
- { rm -f "$tmp_version_file"; die "Version string could not be written to $version_file"; }
- rm -f "$tmp_version_file"
- [[ "$(read_current_version)" == "$version" ]] || die "Version string could not be updated in $version_file"
- }
- sha256_file() {
- local file="$1"
- if command -v sha256sum >/dev/null 2>&1; then
- sha256sum "$file" | awk '{print $1}'
- elif command -v shasum >/dev/null 2>&1; then
- shasum -a 256 "$file" | awk '{print $1}'
- else
- die "Required command not found: sha256sum or shasum"
- fi
- }
- file_size() {
- local file="$1"
- # GNU stat uses -c, BSD/macOS stat uses -f.
- stat -c '%s' "$file" 2>/dev/null || stat -f '%z' "$file"
- }
- require_command git
- require_command zip
- require_command sed
- require_command awk
- repo_root="$(git rev-parse --show-toplevel 2>/dev/null)" || die "Not inside a git repository."
- current_dir="$(pwd -P)"
- repo_root_real="$(cd "$repo_root" && pwd -P)"
- [[ "$current_dir" == "$repo_root_real" ]] || die "Run this script from the repository root: $repo_root_real"
- [[ "$#" -le 1 ]] || die "Usage: $0 [vX.Y.Z]"
- version_file='includes/version.php'
- [[ -f "$version_file" ]] || die "Version file not found: $version_file"
- current_version="$(read_current_version)"
- [[ -n "$current_version" ]] || die "Current version could not be read from $version_file"
- if [[ "$#" -eq 1 ]]; then
- version="$1"
- else
- printf 'Current version: %s\n' "$current_version"
- printf 'New version (vX.Y.Z): '
- IFS= read -r version || die "No version provided."
- fi
- [[ -n "$version" ]] || die "No version provided."
- [[ "$version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "Version must use the format vX.Y.Z."
- write_current_version "$version"
- output_dir='build/updates'
- output_file="${output_dir}/psa-orderform-${version}.zip"
- tmp_file_list="$(mktemp)"
- cleanup() {
- rm -f "$tmp_file_list"
- }
- trap cleanup EXIT
- while IFS= read -r path; do
- [[ -n "$path" ]] || continue
- is_excluded_path "$path" && continue
- [[ -f "$path" ]] || continue
- printf '%s\n' "$path" >> "$tmp_file_list"
- done < <(git ls-files)
- file_count="$(wc -l < "$tmp_file_list" | tr -d '[:space:]')"
- [[ "$file_count" -gt 0 ]] || die "No files selected for the update archive."
- mkdir -p "$output_dir"
- rm -f "$output_file"
- zip -q -@ "$output_file" < "$tmp_file_list"
- [[ -f "$output_file" ]] || die "ZIP archive was not created."
- checksum="$(sha256_file "$output_file")"
- size="$(file_size "$output_file")"
- printf 'Update ZIP created\n'
- printf 'Version: %s\n' "$version"
- printf 'Version file: %s\n' "$version_file"
- printf 'Path: %s\n' "$output_file"
- printf 'Files: %s\n' "$file_count"
- printf 'SHA-256: %s\n' "$checksum"
- printf 'Size: %s bytes\n' "$size"
|