|
14 | 14 | # See the License for the specific language governing permissions and
|
15 | 15 | # limitations under the License.
|
16 | 16 |
|
| 17 | +# Modify a .xcodeproj to use a specific branch, version, or commit for the |
| 18 | +# firebase-ios-sdk SPM dependency. |
17 | 19 |
|
18 |
| -# Modify a .xcodeproj to use a specific branch. |
19 |
| - |
20 |
| -set -xeuo pipefail |
21 |
| - |
22 |
| -SAMPLE=$1 |
23 |
| -SAMPLE_DIR=$(echo "$SAMPLE" | perl -ne 'print lc') |
24 |
| -XCODEPROJ=${SAMPLE_DIR}/${SAMPLE}Example.xcodeproj/project.pbxproj |
25 |
| - |
26 |
| -# Regex matches SemVer `firebase-ios-sdk` dependency in project.pbxproj: |
27 |
| -# { |
28 |
| -# isa = XCRemoteSwiftPackageReference; |
29 |
| -# repositoryURL = "https://github.com/firebase/firebase-ios-sdk.git"; |
30 |
| -# requirement = { |
31 |
| -# kind = upToNextMajorVersion; |
32 |
| -# minimumVersion = xx.yy.zz; |
33 |
| -# }; |
34 |
| -# }; |
35 |
| -REQUIREMENT_REGEX='({'\ |
36 |
| -'\s*isa = XCRemoteSwiftPackageReference;'\ |
37 |
| -'\s*repositoryURL = "https://github\.com/firebase/firebase-ios-sdk\.git";'\ |
38 |
| -'\s*requirement = {\s*)kind = upToNextMajorVersion;'\ |
39 |
| -'\s*minimumVersion = \d+\.\d+\.\d+;'\ |
40 |
| -'(\s*};'\ |
41 |
| -'\s*};)' |
42 |
| - |
43 |
| -# Replaces the minimumVersion requirement with a branch requirement. |
44 |
| -REPLACEMENT_REGEX="\1branch = $BRANCH_NAME;\n\t\t\t\tkind = branch;\2" |
45 |
| - |
46 |
| -# Performs the replacement using Perl. |
47 |
| -# |
48 |
| -# -0777 Enables reading all input in one go (slurp), rather than line-by-line. |
49 |
| -# -p Causes Perl to loop through the input line by line. |
50 |
| -# -i Edits the file in place. |
51 |
| -# -e Provides the expression to execute. |
52 |
| -perl -0777 -i -pe "s#$REQUIREMENT_REGEX#$REPLACEMENT_REGEX#g" "$XCODEPROJ" || { |
53 |
| - echo "Failed to update quickstart's Xcode project to the branch: $BRANCH_NAME" |
| 20 | +set -euo pipefail |
| 21 | + |
| 22 | +# --- Helper functions --- |
| 23 | +usage() { |
| 24 | + echo "Usage: $0 <path_to.xcodeproj> [--version <version> | --revision <revision> | --prerelease]" |
| 25 | + echo "Example: $0 path/to/MyProject.xcodeproj --version 10.24.0" |
| 26 | + exit 1 |
| 27 | +} |
| 28 | + |
| 29 | +# --- Argument parsing --- |
| 30 | +if [[ $# -lt 2 ]]; then |
| 31 | + usage |
| 32 | +fi |
| 33 | + |
| 34 | +XCODEPROJ_PATH="$1" |
| 35 | +shift |
| 36 | +MODE="$1" |
| 37 | +shift |
| 38 | + |
| 39 | +# Validate Xcode project path |
| 40 | +if [[ ! -d "$XCODEPROJ_PATH" || ! "$XCODEPROJ_PATH" == *.xcodeproj ]]; then |
| 41 | + echo "Error: Invalid Xcode project path provided: $XCODEPROJ_PATH" |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | +PBXPROJ_PATH="${XCODEPROJ_PATH}/project.pbxproj" |
| 45 | +if [[ ! -f "$PBXPROJ_PATH" ]]; then |
| 46 | + echo "Error: project.pbxproj not found at ${PBXPROJ_PATH}" |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +case "$MODE" in |
| 51 | + --version) |
| 52 | + if [[ $# -lt 1 ]]; then usage; fi |
| 53 | + VERSION="$1" |
| 54 | + # Release testing: Point to CocoaPods-{VERSION} tag (as a branch) |
| 55 | + export REPLACEMENT_VALUE |
| 56 | + REPLACEMENT_VALUE=$(printf '{\n\t\t\t\tkind = branch;\n\t\t\t\tbranch = "%s";\n\t\t\t}' "$VERSION") |
| 57 | + ;; |
| 58 | + --prerelease) |
| 59 | + # Prerelease testing: Point to the tip of the main branch |
| 60 | + COMMIT_HASH=$(git ls-remote https://github.com/firebase/firebase-ios-sdk.git main | cut -f1) |
| 61 | + if [[ -z "$COMMIT_HASH" ]]; then |
| 62 | + echo "Error: Failed to get remote revision for main branch." |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | + export REPLACEMENT_VALUE |
| 66 | + REPLACEMENT_VALUE=$(printf '{\n\t\t\t\tkind = revision;\n\t\t\t\trevision = "%s";\n\t\t\t}' "$COMMIT_HASH") |
| 67 | + ;; |
| 68 | + --revision) |
| 69 | + if [[ $# -lt 1 ]]; then usage; fi |
| 70 | + REVISION="$1" |
| 71 | + # PR testing: Point to the specific commit hash of the current branch |
| 72 | + export REPLACEMENT_VALUE |
| 73 | + REPLACEMENT_VALUE=$(printf '{\n\t\t\t\tkind = revision;\n\t\t\t\trevision = "%s";\n\t\t\t}' "$REVISION") |
| 74 | + ;; |
| 75 | + *) |
| 76 | + usage |
| 77 | + ;; |
| 78 | +esac |
| 79 | + |
| 80 | +# Read the original content to check for changes later. |
| 81 | +ORIGINAL_CONTENT=$(<"$PBXPROJ_PATH") |
| 82 | + |
| 83 | +# Use perl to perform the replacement. |
| 84 | +# -0777: Slurp the whole file into one string. |
| 85 | +# -i: Edit in-place. |
| 86 | +# -p: Loop over the input. |
| 87 | +# -e: Execute the script. |
| 88 | +# The `e` flag in `s/.../.../ge` evaluates the replacement as a Perl expression. |
| 89 | +# This allows us to use an environment variable for the replacement string, |
| 90 | +# avoiding quoting issues with shell variables. |
| 91 | +perl -0777 -i -pe 's#(repositoryURL = "https://github.com/firebase/firebase-ios-sdk\.git";\s*requirement = )\{[^\}]+\};#$1 . $ENV{"REPLACEMENT_VALUE"} . ";"#ge' "$PBXPROJ_PATH" || { |
| 92 | + echo "Failed to update the Xcode project's SPM dependency." |
54 | 93 | exit 1
|
55 | 94 | }
|
56 | 95 |
|
| 96 | +# Verify that the file was changed. |
| 97 | +UPDATED_CONTENT=$(<"$PBXPROJ_PATH") |
| 98 | +if [[ "$ORIGINAL_CONTENT" == "$UPDATED_CONTENT" ]]; then |
| 99 | + echo "Failed to find and replace the firebase-ios-sdk dependency. Check the regex pattern and project file structure." |
| 100 | + exit 1 |
| 101 | +} |
| 102 | + |
| 103 | +echo "Successfully updated SPM dependency in $PBXPROJ_PATH" |
| 104 | + |
57 | 105 | # Point SPM CI to the tip of `main` of
|
58 | 106 | # https://github.com/google/GoogleAppMeasurement so that the release process
|
59 | 107 | # can defer publishing the `GoogleAppMeasurement` tag until after testing.
|
|
0 commit comments