create-update-zip.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. local tmp_version_file
  24. # No "sed -i": GNU requires no argument, BSD/macOS requires one.
  25. tmp_version_file="$(mktemp)"
  26. sed -E "s/define\\([\"']APP_VERSION[\"'],[[:space:]]*[\"'][^\"']+[\"']\\);/define(\"APP_VERSION\", \"${version}\");/" \
  27. "$version_file" > "$tmp_version_file" ||
  28. { rm -f "$tmp_version_file"; die "Version string could not be updated in $version_file"; }
  29. cat "$tmp_version_file" > "$version_file" ||
  30. { rm -f "$tmp_version_file"; die "Version string could not be written to $version_file"; }
  31. rm -f "$tmp_version_file"
  32. [[ "$(read_current_version)" == "$version" ]] || die "Version string could not be updated in $version_file"
  33. }
  34. sha256_file() {
  35. local file="$1"
  36. if command -v sha256sum >/dev/null 2>&1; then
  37. sha256sum "$file" | awk '{print $1}'
  38. elif command -v shasum >/dev/null 2>&1; then
  39. shasum -a 256 "$file" | awk '{print $1}'
  40. else
  41. die "Required command not found: sha256sum or shasum"
  42. fi
  43. }
  44. file_size() {
  45. local file="$1"
  46. # GNU stat uses -c, BSD/macOS stat uses -f.
  47. stat -c '%s' "$file" 2>/dev/null || stat -f '%z' "$file"
  48. }
  49. require_command git
  50. require_command zip
  51. require_command sed
  52. require_command awk
  53. repo_root="$(git rev-parse --show-toplevel 2>/dev/null)" || die "Not inside a git repository."
  54. current_dir="$(pwd -P)"
  55. repo_root_real="$(cd "$repo_root" && pwd -P)"
  56. [[ "$current_dir" == "$repo_root_real" ]] || die "Run this script from the repository root: $repo_root_real"
  57. [[ "$#" -le 1 ]] || die "Usage: $0 [vX.Y.Z]"
  58. version_file='includes/version.php'
  59. [[ -f "$version_file" ]] || die "Version file not found: $version_file"
  60. current_version="$(read_current_version)"
  61. [[ -n "$current_version" ]] || die "Current version could not be read from $version_file"
  62. if [[ "$#" -eq 1 ]]; then
  63. version="$1"
  64. else
  65. printf 'Current version: %s\n' "$current_version"
  66. printf 'New version (vX.Y.Z): '
  67. IFS= read -r version || die "No version provided."
  68. fi
  69. [[ -n "$version" ]] || die "No version provided."
  70. [[ "$version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "Version must use the format vX.Y.Z."
  71. write_current_version "$version"
  72. output_dir='build/updates'
  73. output_file="${output_dir}/psa-orderform-${version}.zip"
  74. tmp_file_list="$(mktemp)"
  75. cleanup() {
  76. rm -f "$tmp_file_list"
  77. }
  78. trap cleanup EXIT
  79. while IFS= read -r path; do
  80. [[ -n "$path" ]] || continue
  81. is_excluded_path "$path" && continue
  82. [[ -f "$path" ]] || continue
  83. printf '%s\n' "$path" >> "$tmp_file_list"
  84. done < <(git ls-files)
  85. file_count="$(wc -l < "$tmp_file_list" | tr -d '[:space:]')"
  86. [[ "$file_count" -gt 0 ]] || die "No files selected for the update archive."
  87. mkdir -p "$output_dir"
  88. rm -f "$output_file"
  89. zip -q -@ "$output_file" < "$tmp_file_list"
  90. [[ -f "$output_file" ]] || die "ZIP archive was not created."
  91. checksum="$(sha256_file "$output_file")"
  92. size="$(file_size "$output_file")"
  93. printf 'Update ZIP created\n'
  94. printf 'Version: %s\n' "$version"
  95. printf 'Version file: %s\n' "$version_file"
  96. printf 'Path: %s\n' "$output_file"
  97. printf 'Files: %s\n' "$file_count"
  98. printf 'SHA-256: %s\n' "$checksum"
  99. printf 'Size: %s bytes\n' "$size"