-
Notifications
You must be signed in to change notification settings - Fork 11
/
build.sh
executable file
·344 lines (290 loc) · 9.95 KB
/
build.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env bash
#
# Copyright (c) 2023 Leanplum Inc. All rights reserved.
#
set -o noglob
set -o nounset
set -o pipefail
set -o errexit
PATH_TO_UNITY_ROOT="/Applications/Unity/Unity.app"
UNITY_HUB="$HOME/Applications/Unity Hub.app/Contents/MacOS/Unity Hub"
# Unity Editor version to be used for building the package.
# It should be passed as an argument.
UNITY_EDITOR_VERSION=""
# Path to JDK to be used by the Android gradle build. Set to "" to use default.
JAVA_VERSION_PATH="/Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home"
XCFRAMEWORKS=("Leanplum" "CleverTapSDK" "SDWebImage")
#######################################
# Gets the latest version of specified repo.
# Globals:
# None
# Arguments:
# Repo to get from
# Returns:
# Latest published version
#######################################
get_latest_version() {
curl --silent "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
grep '"tag_name":' | # Get tag line
sed -E 's/.*"([^"]+)".*/\1/' # Pluck JSON value
}
#######################################
# Downloads the iOS SDK from GitHub releases.
# Globals:
# None
# Arguments:
# The version to download, e.g. "1.2.3"
# Returns:
# None
#######################################
download_ios_sdk() {
local version=$1
local repo=https://github.com/Leanplum/Leanplum-iOS-SDK
echo "Downloading AppleSDK ${version} ..."
if [ -d "/tmp/Leanplum-${version}.xcframework" ]; then
rm -rf "/tmp/Leanplum-${version}.xcframework"
fi
# Download from official git repo.
local destination="/tmp/Leanplum-${version}.zip"
# Download the framework zip.
wget --show-progress -O "$destination" \
"${repo}/releases/download/${version}/Leanplum.zip"
extract_ios_sdk $version
echo "Finished downloading iOS SDK."
}
#######################################
# Extracts Leanplum dynamic xcframework and its dependencies.
# Globals:
# None
# Arguments:
# The version to use, e.g. "1.2.3"
# Returns:
# None
#######################################
extract_ios_sdk()
{
local version=$1
echo "Extracting AppleSDK ..."
rm -rf "/tmp/Leanplum"
rm -rf "/tmp/Leanplum-${version}-dynamic"
unzip -q "/tmp/Leanplum-${version}.zip" -d "/tmp/Leanplum-${version}"
mv "/tmp/Leanplum-${version}/dynamic/" "/tmp/Leanplum-${version}-dynamic"
rm -rf "/tmp/Leanplum-${version}.zip"
rm -rf "/tmp/Leanplum-${version}"
}
#######################################
# Copies Leanplum dynamic xcframework and its dependencies.
# Globals:
# XCFRAMEWORKS
# Arguments:
# The version to use, e.g. "1.2.3"
# Returns:
# None
#######################################
prepare_ios()
{
local version=$1
echo "Preparing iOS dependencies..."
# Remove all xcframeworks
## Find and remove all xcframework directories in the iOS Plugins folder.
## Use -depth option to process each directory's contents before
## the directory itself. This prevents errors when deleting the directory.
find "Leanplum-Unity-SDK/Assets/Plugins/iOS" \
-name "*.xcframework" \
-type d \
-depth \
-exec sh -c 'if [ -d {} ]; then rm -rf {};fi' \;
# Add all xcframeworks for specified version
for i in "${XCFRAMEWORKS[@]}"
do
cp -r "/tmp/Leanplum-$version-dynamic/$i.xcframework" \
"Leanplum-Unity-SDK/Assets/Plugins/iOS/"
done
}
#######################################
# Copies the iOS SDK from project directory. Name should be in format Leanplum-${version}.zip
# Globals:
# None
# Arguments:
# The version to copy.
# Returns:
# None
#######################################
copy_ios_sdk() {
local version=$1
echo "Copying AppleSDK ${version} (Leanplum-${version}.zip)..."
local destination="/tmp/Leanplum-${version}.zip"
if [ -d "/tmp/Leanplum-${version}.zip" ]; then
rm -rf "/tmp/Leanplum-${version}.zip"
fi
cp "Leanplum-${version}.zip" $destination
extract_ios_sdk $version
echo "Finished copying iOS SDK."
}
#######################################
# Replaces a string in a file and checks for success via git status.
# Globals:
# None
# Arguments:
# The path to file.
# The string to replace.
# The new string.
# Returns:
# None
#######################################
replace() {
sed -i '' -e "s|$2|$3|g" "$1"
cd "$(dirname "$1")" # Change to directory containing the file and check for success.
if ! git status --porcelain 2>/dev/null | grep "$(basename "$1")"; then
echo "${R}Error patching file: $1${N}" && exit 1
fi
cd ~- # Change back to original folder.
echo "Updated file: $1"
}
#######################################
# Tries to find Unity installation path through Unity Hub.
# Globals:
# None
# Returns:
# None
#######################################
get_unity_from_hub() {
local path=$("${UNITY_HUB}" -- --headless editors -i | grep "$UNITY_EDITOR_VERSION" | awk '{print $NF}')
if [[ $UNITY_EDITOR_VERSION != "" && $path != "" ]]; then
local delimiter="/"
local array=($(echo $path | tr "$delimiter" " "))
local count=${#array[@]}
# the version is second to last element
array[count-2]=$UNITY_EDITOR_VERSION
local arr=${array[@]}
local str=${arr// //}
path=/$str
fi
echo ${path}
}
#######################################
# Builds the Unity SDK.
# Globals:
# XCFRAMEWORKS
# Arguments:
# None
# Returns:
# None
#######################################
build() {
echo "Building Android SDK..."
# Build Android SDK
cd Leanplum-Android-SDK-Unity/
if [[ $JAVA_VERSION_PATH != "" ]]; then
./gradlew clean assembleRelease -Dorg.gradle.java.home=$JAVA_VERSION_PATH
else
./gradlew clean assembleRelease
fi
CLASSES_JAR=android-unity-wrapper/build/outputs/aar/android-unity-wrapper-release.aar
UNITY_DIR=Leanplum-Unity-SDK/Assets/Plugins/Android
cp "${CLASSES_JAR}" "../${UNITY_DIR}/com.leanplum.unity-wrapper-$UNITY_VERSION_STRING.aar"
cd ../
echo "Exporting Unity SDK Package..."
pwd
if [ -f "$PATH_TO_UNITY_ROOT" ]; then
echo "$PATH_TO_UNITY_ROOT exist"
else
PATH_TO_UNITY_ROOT=$(get_unity_from_hub)
echo "Getting Unity from Unity HUB"
fi
PATH_TO_UNITY="$PATH_TO_UNITY_ROOT/Contents/MacOS/Unity"
PATH_TO_PROJECT="$(pwd)/Leanplum-Unity-SDK"
PATH_TO_EXPORT="$(pwd)/Leanplum-Unity-Package"
echo $PATH_TO_UNITY
export OUT_PKG="Leanplum_Unity-$UNITY_VERSION_STRING.unitypackage"
$PATH_TO_UNITY -gvh_disable -quit -nographics -batchmode -projectPath "$PATH_TO_PROJECT" -executeMethod Leanplum.Private.PackageExporter.ExportPackage -logfile
export UNITY_BINARY="$PATH_TO_PROJECT/$OUT_PKG"
mv $UNITY_BINARY $PATH_TO_EXPORT
echo "Done"
}
#######################################
# Configures the Unity SDK build and starts the build.
# Globals:
# None
# Arguments:
# None
# Returns:
# None
#######################################
main() {
APPLE_COPY=false
for i in "$@"; do
case $i in
--apple-sdk-version=*)
APPLE_SDK_VERSION="${i#*=}"
shift # past argument=value
;;
--android-sdk-version=*)
ANDROID_SDK_VERSION="${i#*=}"
shift # past argument=value
;;
--ct-android-sdk-version=*)
CT_ANDROID_SDK_VERSION="${i#*=}"
shift # past argument=value
;;
--version=*)
UNITY_VERSION="${i#*=}"
shift # past argument=value
;;
--unity-editor-version=*)
UNITY_EDITOR_VERSION="${i#*=}"
shift # past argument=value
;;
--apple-copy)
APPLE_COPY=true
shift
;;
--stacktrace)
set -o xtrace
shift
;;
esac
done
if [[ -z "${UNITY_VERSION+x}" ]]; then
echo "Unity SDK version not specified, using current: ${UNITY_VERSION}"
fi
if [[ -z ${APPLE_SDK_VERSION+x} ]]; then
APPLE_SDK_VERSION=$(get_latest_version "Leanplum/Leanplum-iOS-SDK")
echo "iOS SDK version not specified, using latest: ${APPLE_SDK_VERSION}"
fi
if [[ -z ${ANDROID_SDK_VERSION+x} ]]; then
ANDROID_SDK_VERSION=$(get_latest_version "Leanplum/Leanplum-Android-SDK")
echo "Android SDK version not specified, using latest: ${ANDROID_SDK_VERSION}"
fi
export UNITY_VERSION_STRING=${UNITY_VERSION_STRING:-"$UNITY_VERSION"}
PACKAGE_IMPORTER_PATH="Leanplum-Unity-SDK/Assets/Editor/PackageImporter.cs"
CLEVERTAP_UNITY_VERSION=$(sed -n 's/.*CLEVERTAP_UNITY_VERSION = "\(.*\)";/\1/p' "$PACKAGE_IMPORTER_PATH")
INFO="Building unitypackage with version ${UNITY_VERSION_STRING},
including CleverTapUnity version ${CLEVERTAP_UNITY_VERSION},
using Leanplum iOS ${APPLE_SDK_VERSION} and Leanplum Android ${ANDROID_SDK_VERSION}."
echo "$INFO"
sleep 3
if [ "$APPLE_COPY" = true ]; then
copy_ios_sdk $APPLE_SDK_VERSION
else
download_ios_sdk $APPLE_SDK_VERSION
fi
replace "Leanplum-Android-SDK-Unity/android-unity-wrapper/build.gradle" "%LP_VERSION%" $ANDROID_SDK_VERSION
replace "Leanplum-Unity-SDK/Assets/LeanplumSDK/Editor/LeanplumDependencies.xml" "%LP_VERSION%" $ANDROID_SDK_VERSION
replace "Leanplum-Unity-SDK/Assets/Plugins/Android/mainTemplate.gradle" "%LP_VERSION%" $ANDROID_SDK_VERSION
replace "Leanplum-Unity-SDK/Assets/Plugins/Android/mainTemplate.gradle" "%CT_VERSION%" $CT_ANDROID_SDK_VERSION
replace "Leanplum-Android-SDK-Unity/android-unity-wrapper/build.gradle" "%CT_VERSION%" $CT_ANDROID_SDK_VERSION
replace "Leanplum-Android-SDK-Unity/android-unity-wrapper/build.gradle" "%LP_UNITY_VERSION%" $UNITY_VERSION
awk -v value="\"$UNITY_VERSION\";" '!x{x=sub(/SDK_VERSION =.*/, "SDK_VERSION = "value)}1' "Leanplum-Unity-SDK/Assets/LeanplumSDK/Utilities/Constants.cs" > Constants_tmp.cs \
&& mv -v Constants_tmp.cs "Leanplum-Unity-SDK/Assets/LeanplumSDK/Utilities/Constants.cs"
find Leanplum-Unity-Package -name '*.unitypackage' -delete
find Leanplum-Unity-SDK/Assets/Plugins/ -name '*.aar' -delete
prepare_ios $APPLE_SDK_VERSION
build
git checkout Leanplum-Android-SDK-Unity/
git checkout Leanplum-Unity-SDK/Assets/LeanplumSDK/Editor/LeanplumDependencies.xml
git checkout Leanplum-Unity-SDK/Assets/Plugins/Android/mainTemplate.gradle
echo "Completed ${INFO}"
echo "Done."
}
main "$@"