-
Notifications
You must be signed in to change notification settings - Fork 49
/
process-latest-version.sh
executable file
·238 lines (205 loc) · 8.51 KB
/
process-latest-version.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash
set -o pipefail
# Update pod repo to ensure we retrieve the latest version.
echo "Updating pods..."
pod repo list
pod repo add-cdn trunk "https://cdn.cocoapods.org/"
pod repo update
pod spec which Firebase
# Uncomment for testing purposes:
#GITHUB_TOKEN=your-token-here
#GITHUB_REPOSITORY=invertase/firestore-ios-sdk-frameworks
FIREBASE_GITHUB_REPOSITORY=firebase/firebase-ios-sdk
LATEST_FIREBASE_PODSPEC=$(pod spec which Firebase)
LATEST_FIREBASE_VERSION=$(python -c 'import json,sys; print(json.loads(sys.stdin.read())["version"])' <"$LATEST_FIREBASE_PODSPEC")
# -------------------
# Functions
# -------------------
# Creates a new GitHub release
# ARGS:
# 1: Name of the release (becomes the release title on GitHub)
# 2: Markdown body of the release
# 3: Release git tag
create_github_release() {
local response=''
local created=''
local release_name=$1
local release_body=$2
local release_tag=$3
local body='{
"tag_name": "%s",
"target_commitish": "master",
"name": "%s",
"body": %s,
"draft": false,
"prerelease": false
}'
# shellcheck disable=SC2059
body=$(printf "$body" "$release_tag" "$release_name" "$release_body")
response=$(curl --request POST \
--url https://api.github.com/repos/${GITHUB_REPOSITORY}/releases \
--header "Authorization: Bearer $GITHUB_TOKEN" \
--header 'Content-Type: application/json' \
--data "$body" \
-s)
created=$(echo "$response" | python -c "import sys, json; data = json.load(sys.stdin); print(data.get('id', sys.stdin))")
if [ "$created" != "$response" ]; then
echo "Release created successfully!"
else
printf "Release failed to create; "
printf "\n%s\n" "$body"
printf "\n%s\n" "$response"
exit 1
fi
}
# Gets the JSON contents of a GitHub release
# ARGS:
# 1: GitHub Repository
# 2: Release tag name
get_github_release_by_tag() {
local github_repository=$1
local release_tag=$2
local response=''
local release_id=''
response=$(curl --request GET \
--url "https://api.github.com/repos/${github_repository}/releases/tags/${release_tag}" \
--header "Authorization: Bearer $GITHUB_TOKEN" \
--header 'Content-Type: application/json' \
-s)
release_id=$(echo "$response" | python -c "import sys, json; data = json.load(sys.stdin); print(data.get('id', 'Not Found'))")
if [ "$release_id" != "Not Found" ]; then
echo "$response"
else
response_message=$(echo "$response" | python -c "import sys, json; data = json.load(sys.stdin); print(data.get('message'))")
if [ "$response_message" != "Not Found" ]; then
echo "Failed to query release '$release_name' -> GitHub API request failed with response: $response_message"
echo "$response"
exit 1
fi
fi
}
# -------------------
# Main Script
# -------------------
# Ensure that the GITHUB_TOKEN env variable is defined
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Missing required GITHUB_TOKEN env variable. Set this on the workflow action or on your local environment."
exit 1
fi
# Ensure that the GITHUB_REPOSITORY env variable is defined
if [[ -z "$GITHUB_REPOSITORY" ]]; then
echo "Missing required GITHUB_REPOSITORY env variable. Set this on the workflow action or on your local environment."
exit 1
fi
echo "The latest Firebase pod version is $LATEST_FIREBASE_VERSION, checking if a frameworks release tag exists."
# Check if the framework release already exists, exit if it does.
framework_repo_release=$(get_github_release_by_tag "$GITHUB_REPOSITORY" "$LATEST_FIREBASE_VERSION")
if [[ -n "$framework_repo_release" ]]; then
echo "Tag for this release already exists, exiting early."
exit 0
fi
firebase_firestore_version=$(python -c 'import json,sys; print(next((x for x in json.loads(sys.stdin.read())["subspecs"] if x["name"] == "Firestore"), None)["dependencies"]["FirebaseFirestore"][0])' <"$LATEST_FIREBASE_PODSPEC")
# Sanity check we actually got the subspec version value as it should look something like `~> 1.15.0`
if [[ "$firebase_firestore_version" != '~>'* ]]; then
echo ""
echo "Output of firebase_firestore_version:"
echo "$firebase_firestore_version"
echo ""
echo "Error: could not retrieve FirebaseFirestore subspec version from the Firebase spec."
exit 1
fi
# Remove `~> ` prefix
# shellcheck disable=SC2001
firebase_firestore_version=$(echo "$firebase_firestore_version" | sed 's/\~\> //g')
echo "Found FirebaseFirestore subspec with version '$firebase_firestore_version'"
echo "A frameworks tag for Firebase pod version $LATEST_FIREBASE_VERSION does not yet exist, creating it..."
# Get the release information from the Firebase iOS SDK repository, so we can extract the asset zip to download.
firebase_ios_repo_release=$(get_github_release_by_tag "$FIREBASE_GITHUB_REPOSITORY" "CocoaPods-$LATEST_FIREBASE_VERSION")
if [[ -z "$firebase_ios_repo_release" ]]; then
echo "Error: could not find a release with the tag CocoaPods-$LATEST_FIREBASE_VERSION on the $FIREBASE_GITHUB_REPOSITORY repository."
exit 1
fi
# Check the release actually has any assets (sometimes the don't)
if [[ "$firebase_ios_repo_release" != *".zip"* ]]; then
echo ""
echo ""
echo "$firebase_ios_repo_release"
echo ""
echo "Error: the Firebase release above doesn't seem to have any assets :("
exit 1
fi
firebase_release_archive=$(echo "$firebase_ios_repo_release" | python -c 'import json,sys; print(json.loads(sys.stdin.read())["assets"][0]["browser_download_url"])')
echo "Found archive asset, extracting direct download url from url $firebase_release_archive ..."
redirect_url_html=$(curl -sS --fail "$firebase_release_archive")
redirect_url=$(echo "$redirect_url_html" | cut -d'"' -f 2)
if [[ "$redirect_url" != 'https://'* ]]; then
echo ""
echo "Redirect response HTML:"
echo "$redirect_url_html"
echo ""
echo "Attempted extraction URL:"
echo "$redirect_url"
echo ""
echo "Error: could not extract download url from GitHub asset redirect response."
exit 1
fi
# A quick and dirty URL unescape
# shellcheck disable=SC2001
redirect_url=$(echo "$redirect_url" | sed 's/\&\;/\&/g')
# Create .tmp directory to download and extract binaries from
mkdir -p .tmp
# Cleanup previous output if it exists
#rm -f .tmp/Firebase.zip
rm -rf .tmp/Firebase
# Download the zip
echo ""
echo "Extracted asset redirect URL:"
echo "$redirect_url"
echo ""
echo "Downloading asset..."
echo ""
curl --fail "$redirect_url" > .tmp/Firebase.zip
echo "Download successful, extracting archive..."
unzip -q .tmp/Firebase.zip 'Firebase/FirebaseFirestore/*' -d .tmp/
# Make sure xcframework exists in extracted folder
if [ ! -d ".tmp/Firebase/FirebaseFirestore/FirebaseFirestore.xcframework" ]; then
echo ""
echo "Contents of .tmp/:"
ls -la .tmp/ || true
echo ""
echo "Contents of .tmp/Firebase:"
ls -la .tmp/Firebase || true
echo ""
echo "Contents of .tmp/Firebase/FirebaseFirestore:"
echo ""
ls -la .tmp/Firebase/FirebaseFirestore || true
echo "Error: archive extraction may have failed as FirebaseFirestore.xcframework output could not be found."
exit 1
fi
echo "Archive successfully extracted, updating frameworks in repository..."
# Remove repository copy
rm -rf FirebaseFirestore
# Copy new changes from .tmp
rsync -a .tmp/Firebase/FirebaseFirestore .
# Make sure xcframework exists in repository frameworks
if [ ! -d "FirebaseFirestore/FirebaseFirestore.xcframework" ]; then
echo "Error: updating frameworks in repository may have failed as the FirebaseFirestore.xcframework could not be found."
exit 1
fi
rm -rf .tmp
# Update FirebaseFirestore.podspec with new version
updated_version_line="firebase_firestore_version = '$firebase_firestore_version'"
updated_podspec_contents=$(sed "1s/.*/$updated_version_line/" FirebaseFirestore.podspec)
echo "$updated_podspec_contents" >FirebaseFirestore.podspec
# Update Readme with new version
new_version_added_line="<!--NEW_VERSION_PLACEHOLDER-->¬ - [$LATEST_FIREBASE_VERSION](https:\/\/github.com\/invertase\/firestore-ios-sdk-frameworks\/releases\/tag\/$LATEST_FIREBASE_VERSION)"
updated_readme_contents=$(sed -e "s/<!--NEW_VERSION_PLACEHOLDER-->.*/$new_version_added_line/" README.md | tr '¬' '\n')
echo "$updated_readme_contents" >README.md
# Commit changes and make a release
git add .
git commit -m "release: $LATEST_FIREBASE_VERSION"
git tag -a "$LATEST_FIREBASE_VERSION" -m "$LATEST_FIREBASE_VERSION"
git push origin master --follow-tags
create_github_release "$LATEST_FIREBASE_VERSION" "\"[View Firebase iOS SDK Release](https://github.com/firebase/firebase-ios-sdk/releases/tag/CocoaPods-$LATEST_FIREBASE_VERSION)\"" "$LATEST_FIREBASE_VERSION"
echo ""
echo "Release $LATEST_FIREBASE_VERSION complete."