create-update-zip.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. die() {
  4. printf 'Error: %s\n' "$*" >&2
  5. exit 1
  6. }
  7. require_command() {
  8. command -v "$1" >/dev/null 2>&1 || die "Required command not found: $1"
  9. }
  10. is_excluded_path() {
  11. case "$1" in
  12. .gitignore|config.php|data|data/*|update-server|update-server/*|.codex|.codex/*|build|build/*|scripts|scripts/*|backup-server|backup-server/*)
  13. return 0
  14. ;;
  15. esac
  16. return 1
  17. }
  18. read_current_version() {
  19. sed -nE 's/.*define\(["'\'']APP_VERSION["'\''],[[:space:]]*["'\'']([^"'\'']+)["'\'']\).*/\1/p' "$version_file" | head -n 1
  20. }
  21. write_current_version() {
  22. local version="$1"
  23. NEW_APP_VERSION="$version" perl -0pi -e '
  24. BEGIN { $version = $ENV{"NEW_APP_VERSION"}; $updated = 0; }
  25. $updated += s/define\(["'\'']APP_VERSION["'\''],\s*["'\''][^"'\'']+["'\'']\);/define("APP_VERSION", "$version");/g;
  26. END { exit($updated > 0 ? 0 : 2); }
  27. ' "$version_file" || die "Version string could not be updated in $version_file"
  28. }
  29. sha256_file() {
  30. local file="$1"
  31. if command -v shasum >/dev/null 2>&1; then
  32. shasum -a 256 "$file" | awk '{print $1}'
  33. elif command -v sha256sum >/dev/null 2>&1; then
  34. sha256sum "$file" | awk '{print $1}'
  35. else
  36. die "Required command not found: shasum or sha256sum"
  37. fi
  38. }
  39. file_size() {
  40. local file="$1"
  41. if stat -f '%z' "$file" >/dev/null 2>&1; then
  42. stat -f '%z' "$file"
  43. else
  44. stat -c '%s' "$file"
  45. fi
  46. }
  47. require_command git
  48. require_command zip
  49. require_command sed
  50. require_command awk
  51. require_command perl
  52. repo_root="$(git rev-parse --show-toplevel 2>/dev/null)" || die "Not inside a git repository."
  53. current_dir="$(pwd -P)"
  54. repo_root_real="$(cd "$repo_root" && pwd -P)"
  55. [[ "$current_dir" == "$repo_root_real" ]] || die "Run this script from the repository root: $repo_root_real"
  56. [[ "$#" -le 1 ]] || die "Usage: $0 [vX.Y.Z]"
  57. version_file='includes/version.php'
  58. [[ -f "$version_file" ]] || die "Version file not found: $version_file"
  59. current_version="$(read_current_version)"
  60. [[ -n "$current_version" ]] || die "Current version could not be read from $version_file"
  61. if [[ "$#" -eq 1 ]]; then
  62. version="$1"
  63. else
  64. printf 'Current version: %s\n' "$current_version"
  65. printf 'New version (vX.Y.Z): '
  66. IFS= read -r version || die "No version provided."
  67. fi
  68. [[ -n "$version" ]] || die "No version provided."
  69. [[ "$version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "Version must use the format vX.Y.Z."
  70. write_current_version "$version"
  71. output_dir='build/updates'
  72. output_file="${output_dir}/psa-orderform-${version}.zip"
  73. tmp_file_list="$(mktemp)"
  74. cleanup() {
  75. rm -f "$tmp_file_list"
  76. }
  77. trap cleanup EXIT
  78. while IFS= read -r path; do
  79. [[ -n "$path" ]] || continue
  80. if is_excluded_path "$path"; then
  81. continue
  82. fi
  83. [[ -f "$path" ]] || continue
  84. printf '%s\n' "$path" >> "$tmp_file_list"
  85. done < <(git ls-files)
  86. file_count="$(wc -l < "$tmp_file_list" | tr -d '[:space:]')"
  87. [[ "$file_count" -gt 0 ]] || die "No files selected for the update archive."
  88. mkdir -p "$output_dir"
  89. rm -f "$output_file"
  90. zip -q -@ "$output_file" < "$tmp_file_list"
  91. [[ -f "$output_file" ]] || die "ZIP archive was not created."
  92. checksum="$(sha256_file "$output_file")"
  93. size="$(file_size "$output_file")"
  94. printf 'Update ZIP created\n'
  95. printf 'Version: %s\n' "$version"
  96. printf 'Version file: %s\n' "$version_file"
  97. printf 'Path: %s\n' "$output_file"
  98. printf 'Files: %s\n' "$file_count"
  99. printf 'SHA-256: %s\n' "$checksum"
  100. printf 'Size: %s bytes\n' "$size"