update_firebase_spm_dependency.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env bash
  2. # Copyright 2025 Google LLC
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Modify a .xcodeproj to use a specific branch, version, or commit for the
  16. # firebase-ios-sdk SPM dependency.
  17. set -euo pipefail
  18. # Enable trace mode if DEBUG is set to 'true'
  19. if [[ "${DEBUG:-false}" == "true" ]]; then
  20. set -x
  21. fi
  22. # --- Argument parsing ---
  23. if [[ $# -lt 2 ]]; then
  24. echo "Modify a .xcodeproj to use a specific branch, version, or commit for the"
  25. echo "firebase-ios-sdk SPM dependency."
  26. echo ""
  27. echo "Usage: $0 <path_to.xcodeproj> [--version <version> | --revision <revision> | --prerelease | --branch <branch>]"
  28. exit 1
  29. fi
  30. XCODEPROJ_PATH="$1"
  31. shift
  32. MODE="$1"
  33. shift
  34. PBXPROJ_PATH="${XCODEPROJ_PATH}/project.pbxproj"
  35. # Regex matches SemVer `firebase-ios-sdk` dependency in project.pbxproj:
  36. # {
  37. # isa = XCRemoteSwiftPackageReference;
  38. # repositoryURL = "https://github.com/firebase/firebase-ios-sdk.git";
  39. # requirement = {
  40. # kind = upToNextMajorVersion;
  41. # minimumVersion = xx.yy.zz;
  42. # };
  43. # };
  44. REQUIREMENT_REGEX='({'\
  45. '\s*isa = XCRemoteSwiftPackageReference;'\
  46. '\s*repositoryURL = "https://github\.com/firebase/firebase-ios-sdk(?:\.git)?";'\
  47. '\s*requirement = {\s*)kind = upToNextMajorVersion;'\
  48. '\s*minimumVersion = \d+\.\d+\.\d+;'\
  49. '(\s*};'\
  50. '\s*};)'
  51. # Define the replacement regex based on the selected mode.
  52. case "$MODE" in
  53. --version)
  54. if [[ $# -lt 1 ]]; then echo "Error: Missing version for --version"; exit 1; fi
  55. VERSION="$1"
  56. REPLACEMENT_REGEX="\1kind = branch;\n\t\t\t\tbranch = \"$VERSION\";\2"
  57. ;;
  58. --prerelease)
  59. COMMIT_HASH=$(git ls-remote https://github.com/firebase/firebase-ios-sdk.git main | cut -f1)
  60. if [[ -z "$COMMIT_HASH" ]]; then
  61. echo "Error: Failed to get remote revision for main branch."
  62. exit 1
  63. fi
  64. REPLACEMENT_REGEX="\1kind = revision;\n\t\t\t\trevision = \"$COMMIT_HASH\";\2"
  65. ;;
  66. --revision)
  67. if [[ $# -lt 1 ]]; then echo "Error: Missing revision for --revision"; exit 1; fi
  68. REVISION="$1"
  69. REPLACEMENT_REGEX="\1kind = revision;\n\t\t\t\trevision = \"$REVISION\";\2"
  70. ;;
  71. --branch)
  72. if [[ $# -lt 1 ]]; then echo "Error: Missing branch name for --branch"; exit 1; fi
  73. BRANCH_NAME="$1"
  74. REPLACEMENT_REGEX="\1kind = branch;\n\t\t\t\tbranch = \"$BRANCH_NAME\";\2"
  75. ;;
  76. *)
  77. echo "Invalid mode: $MODE"
  78. exit 1
  79. ;;
  80. esac
  81. # Make a temporary backup of the original file.
  82. # This will be used to check if any changes were made.
  83. TEMP_FILE=$(mktemp)
  84. cp "$PBXPROJ_PATH" "$TEMP_FILE"
  85. # Performs the replacement using Perl.
  86. #
  87. # -0777 Enables reading all input in one go (slurp), rather than line-by-line.
  88. # -p Causes Perl to loop through the input line by line.
  89. # -i Edits the file in place.
  90. # -e Provides the expression to execute.
  91. perl -0777 -i -pe "s#$REQUIREMENT_REGEX#$REPLACEMENT_REGEX#g" "$PBXPROJ_PATH" || {
  92. echo "Failed to update the Xcode project's SPM dependency."
  93. exit 1
  94. }
  95. # Silently compare the modified file with the temporary backup.
  96. # If they are the same, cmp will return 0 (success), and the 'if' block will run.
  97. if cmp -s "$PBXPROJ_PATH" "$TEMP_FILE"; then
  98. echo "Failed to find and replace the firebase-ios-sdk dependency. Check the regex pattern and project file structure."
  99. # Restore the original file from the backup
  100. mv "$TEMP_FILE" "$PBXPROJ_PATH"
  101. exit 1
  102. fi
  103. echo "Successfully updated SPM dependency in $PBXPROJ_PATH"
  104. # Point SPM CI to the tip of `main` of
  105. # https://github.com/google/GoogleAppMeasurement so that the release process
  106. # can defer publishing the `GoogleAppMeasurement` tag until after testing.
  107. export FIREBASECI_USE_LATEST_GOOGLEAPPMEASUREMENT=1