forked from testfairy/command-line-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestfairy-upload.sh
223 lines (176 loc) · 5.8 KB
/
testfairy-upload.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
#!/bin/sh
UPLOADER_VERSION=1.09
# Put your TestFairy API_KEY here. Find it in your TestFairy account settings.
TESTFAIRY_API_KEY=
# Your Keystore, Storepass and Alias, the ones you use to sign your app.
KEYSTORE=
STOREPASS=
ALIAS=
# Tester Groups that will be notified when the app is ready. Setup groups in your TestFairy account testers page.
# This parameter is optional, leave empty if not required
TESTER_GROUPS=
# Should email testers about new version. Set to "off" to disable email notifications.
NOTIFY="on"
# If AUTO_UPDATE is "on" all users will be prompt to update to this build next time they run the app
AUTO_UPDATE="off"
# The maximum recording duration for every test.
MAX_DURATION="10m"
# Is video recording enabled for this build
VIDEO="on"
# Add a TestFairy watermark to the application icon?
ICON_WATERMARK="on"
# Comment text will be included in the email sent to testers
COMMENT=""
# locations of various tools
CURL=curl
ZIP=zip
UNZIP=unzip
KEYTOOL=keytool
ZIPALIGN=zipalign
JARSIGNER=jarsigner
SERVER_ENDPOINT=http://app.testfairy.com
usage() {
echo "Usage: testfairy-upload.sh APK_FILENAME"
echo
}
verify_tools() {
# Windows users: this script requires zip, curl and sed. If not installed please get from http://cygwin.com/
# Check 'zip' tool
${ZIP} -h >/dev/null
if [ $? -ne 0 ]; then
echo "Could not run zip tool, please check settings"
exit 1
fi
# Check 'curl' tool
${CURL} --help >/dev/null
if [ $? -ne 0 ]; then
echo "Could not run curl tool, please check settings"
exit 1
fi
OUTPUT=$( ${JARSIGNER} -help 2>&1 | grep "verify" )
if [ $? -ne 0 ]; then
echo "Could not run jarsigner tool, please check settings"
exit 1
fi
# Check 'zipalign' tool
OUTPUT=$( ${ZIPALIGN} 2>&1 | grep -i "Zip alignment" )
if [ $? -ne 0 ]; then
echo "Could not run zipalign tool, please check settings"
exit 1
fi
OUTPUT=$( ${KEYTOOL} -help 2>&1 | grep "keypasswd" )
if [ $? -ne 0 ]; then
echo "Could not run keytool tool, please check settings"
exit 1
fi
}
verify_settings() {
if [ -z "${TESTFAIRY_API_KEY}" ]; then
usage
echo "Please update API_KEY with your private API key, as noted in the Settings page"
exit 1
fi
if [ -z "${KEYSTORE}" -o -z "${STOREPASS}" -o -z "{$ALIAS}" ]; then
usage
echo "Please update KEYSTORE, STOREPASS and ALIAS with your jar signing credentials"
exit 1
fi
# verify KEYSTORE, STOREPASS and ALIAS at once
OUTPUT=$( ${KEYTOOL} -list -keystore "${KEYSTORE}" -storepass "${STOREPASS}" -alias "${ALIAS}" 2>&1 )
if [ $? -ne 0 ]; then
usage
echo "Please check keystore credentials; keytool failed to verify storepass and alias"
exit 1
fi
}
if [ $# -ne 1 ]; then
usage
exit 1
fi
# before even going on, make sure all tools work
verify_tools
verify_settings
APK_FILENAME=$1
if [ ! -f "${APK_FILENAME}" ]; then
usage
echo "Can't find file: ${APK_FILENAME}"
exit 2
fi
# temporary file paths
DATE=`date`
TMP_FILENAME=.testfairy.upload.apk
INSTRUMENTED_FILENAME=.testfairy.instrumented.zip
ZIPALIGNED_FILENAME=.testfairy.zipalign.apk
TMPDIR_STRIP=.testfairy.tmp
TMPDIR_INSTRUMENTED=.testfairy.instrumented
rm -f "${TMP_FILENAME}" "${ZIPALIGNED_FILENAME}"
rm -rf ${TMPDIR_STRIP} ${TMPDIR_INSTRUMENTED}
#preserve contents under tmp name so we can overwrite the original file
mv ${APK_FILENAME} ${TMP_FILENAME}
#create stripped file as replacement for the original
mkdir ${TMPDIR_STRIP}
${UNZIP} -qd ${TMPDIR_STRIP} ${TMP_FILENAME} AndroidManifest.xml classes*.dex
cd ${TMPDIR_STRIP}
${ZIP} -q ../${APK_FILENAME} *
cd ..
/bin/echo -n "Uploading ${APK_FILENAME} (stripped) to TestFairy.. "
JSON=$( ${CURL} -s ${SERVER_ENDPOINT}/api/upload -F api_key=${TESTFAIRY_API_KEY} -F apk_file="@${APK_FILENAME}" -F icon-watermark="${ICON_WATERMARK}" -F video="${VIDEO}" -F max-duration="${MAX_DURATION}" -F comment="${COMMENT}" -A "TestFairy Command Line Uploader ${UPLOADER_VERSION}" )
URL=$( echo ${JSON} | sed 's/\\\//\//g' | sed -n 's/.*"instrumented_url"\s*:\s*"\([^"]*\)".*/\1/p' )
if [ -z "${URL}" ]; then
echo "FAILED!"
echo
echo "Upload failed, please check your settings"
exit 1
fi
URL="${URL}?api_key=${TESTFAIRY_API_KEY}"
echo "OK!"
/bin/echo -n "Downloading instrumented ZIP.. "
${CURL} -L -o ${INSTRUMENTED_FILENAME} -s ${URL}
if [ ! -f "${INSTRUMENTED_FILENAME}" ]; then
echo "FAILED!"
echo
echo "Could not download ZIP back from server, please contact [email protected]"
exit 1
fi
echo "OK!"
/bin/echo -n "Instrumenting/Patching APK file..."
mkdir ${TMPDIR_INSTRUMENTED}
cd ${TMPDIR_INSTRUMENTED}
${UNZIP} -q ../${INSTRUMENTED_FILENAME}
${ZIP} -0rq ../${TMP_FILENAME} *
if [ $? -ne 0 ]; then
echo "FAILED!"
echo
echo "Could not patch original APK with instrumented binaries"
exit 1
fi
cd ..
echo "OK!"
/bin/echo -n "Re-signing APK file.. "
${ZIP} -qd ${TMP_FILENAME} 'META-INF/*'
${JARSIGNER} -keystore "${KEYSTORE}" -storepass "${STOREPASS}" -digestalg SHA1 -sigalg MD5withRSA ${TMP_FILENAME} "${ALIAS}"
${JARSIGNER} -verify ${TMP_FILENAME} >/dev/null
if [ $? -ne 0 ]; then
echo "FAILED!"
echo
echo "Jarsigner failed to verify, please check parameters and try again"
exit 1
fi
${ZIPALIGN} -f 4 ${TMP_FILENAME} ${ZIPALIGNED_FILENAME}
rm -f ${TMP_FILENAME}
echo "OK!"
/bin/echo -n "Uploading signed APK to TestFairy.. "
JSON=$( ${CURL} -s ${SERVER_ENDPOINT}/api/upload-signed -F api_key=${TESTFAIRY_API_KEY} -F apk_file=@${ZIPALIGNED_FILENAME} -F testers-groups="${TESTER_GROUPS}" -F auto-update="${AUTO_UPDATE}" -F notify="${NOTIFY}")
rm -f ${ZIPALIGNED_FILENAME}
URL=$( echo ${JSON} | sed 's/\\\//\//g' | sed -n 's/.*"build_url"\s*:\s*"\([^"]*\)".*/\1/p' )
if [ -z "$URL" ]; then
echo "FAILED!"
echo
echo "Build uploaded, but no reply from server. Please contact [email protected]"
exit 1
fi
echo "OK!"
echo
echo "Build was successfully uploaded to TestFairy and is available at:"
echo ${URL}
rm -rf ${TMPDIR_STRIP} ${TMPDIR_INSTRUMENTED} ${INSTRUMENTED_FILENAME}