create-update-zip.sh 2.8 KB

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