-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmakeapk
executable file
·260 lines (211 loc) · 8.34 KB
/
makeapk
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
#!/usr/bin/env bash
#
# Copyright (c) 2017 Projekt Substratum
# This file is part of Substratum.
#
# Substratum is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Substratum is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Substratum. If not, see <http://www.gnu.org/licenses/>.
#################
# DOCUMENTATION #
#################
# This script was designed to be simple to use
#
# Run the following to learn about what the script can do
#
# $ ./makeapk -h
#
# In order to use the release variant, you must set up a keystore.properties
# file according to the following documentation (just step 2):
# https://developer.android.com/studio/publish/app-signing.html#secure-shared-keystore
# Get parameters
while [[ $# -ge 1 ]]; do
case "${1}" in
# Print a simple help menu
"-h"|"--help")
echo
echo "This is a script to assist with building and releasing
Substratum APKs from the command line! Here are the options (default means without any flag):
-f | --folder:
Specify a source location for Substratum
Default is ../substratum
-i | --install:
Installs the generated APK to a device plugged into the computer
Default is no install
-l | --local-changes:
Keeps the script from updating the repo; use this if you have local changes you are testing and don't want them overwritten
Default is off
-m | --move:
Takes a directory as the next parameter
Moves the APK to the specified directory
Default is to the public release folder
-t | --type:
Takes either release or debug
Default is release
-v | --version:
Commit then build with a new version code (takes the numerical value). This will also merge the dev branch into release.
Default is no version bump"
echo
exit ;;
# Specify Substratum source location
"-f"|"--folder")
shift
SUBS_FOLDER=${1} ;;
# Install generated APK
"-i"|"--install")
INSTALL=true ;;
# Don't overwrite local changes
"-l"|"--local")
LOCALCHANGES=true ;;
# Override move location
"-m"|"--move")
shift
MOVE=${1}
[[ "${MOVE}" == */ ]] || MOVE="${MOVE}/" ;;
# Whether to build debug or release
"-t"|"--type")
shift
TYPE=${1} ;;
# Whether or not to bump version before building
"-v"|"--version")
shift
VERSION_CODE=${1} ;;
esac
shift
done
# Release is the default type
[[ -z ${TYPE} ]] && TYPE=release
# Set some values based on type
case ${TYPE} in
"release")
COMMIT_TYPE="Public"
GRADLE_TASK="assembleRelease"
BRANCH=release ;;
"debug")
GRADLE_TASK="assembleDebug"
COMMIT_TYPE="Internal"
BRANCH=dev ;;
esac
# We assume Substratum source is in the same parent folder
[[ -z ${SUBS_FOLDER} ]] && SUBS_FOLDER=../substratum
cd "${SUBS_FOLDER}" || die "Substratum folder doesn't exist!"
# Clean the source folder of any generated APKs (as the user may have had
# them moved into the root of the source from its out location)
find ./ -name "*.apk" -type f -exec rm {} \;
# Always fetch updates
git fetch origin
git checkout ${BRANCH}
# Only update if the -l flag was not set
[[ -z ${LOCALCHANGES} ]] && git reset --hard origin/${BRANCH}
# Bump version if requested (this is so dirty pls no judge)
if [[ -n ${VERSION_CODE} ]]; then
# If this is a release build, first merge the dev branch into release
# This should always be a clean merge; the --no-edit flag is just in case
[[ ${TYPE} = release ]] && git merge --no-edit origin/dev
# Handle hundreds first
case "${VERSION_CODE:0:1}" in
"1") VERSION_STRING="one hundred" ;;
"2") VERSION_STRING="two hundred" ;;
"3") VERSION_STRING="three hundred" ;;
"4") VERSION_STRING="four hundred" ;;
"5") VERSION_STRING="five hundred" ;;
"6") VERSION_STRING="six hundred" ;;
"7") VERSION_STRING="seven hundred" ;;
"8") VERSION_STRING="eight hundred" ;;
"9") VERSION_STRING="nine hundred" ;;
esac
# If the second digit is a 1, we need a separate clause due to the non-uniformity
if [[ ${VERSION_CODE:1:1} -eq 1 ]]; then
case "${VERSION_CODE:2:1}" in
"0") VERSION_STRING+=" ten" ;;
"1") VERSION_STRING+=" eleven" ;;
"2") VERSION_STRING+=" twelve" ;;
"3") VERSION_STRING+=" thirteen" ;;
"4") VERSION_STRING+=" fourteen" ;;
"5") VERSION_STRING+=" fifteen" ;;
"6") VERSION_STRING+=" sixteen" ;;
"7") VERSION_STRING+=" seventeen" ;;
"8") VERSION_STRING+=" eighteen" ;;
"9") VERSION_STRING+=" nineteen" ;;
esac
# Otherwise, we need to handle the second digit then the first
else
case "${VERSION_CODE:1:1}" in
"2") VERSION_STRING+=" twenty" ;;
"3") VERSION_STRING+=" thirty" ;;
"4") VERSION_STRING+=" forty" ;;
"5") VERSION_STRING+=" fifty" ;;
"6") VERSION_STRING+=" sixty" ;;
"7") VERSION_STRING+=" seventy" ;;
"8") VERSION_STRING+=" eighty" ;;
"9") VERSION_STRING+=" ninety" ;;
esac
case "${VERSION_CODE:2:1}" in
"1") VERSION_STRING+=" one" ;;
"2") VERSION_STRING+=" two" ;;
"3") VERSION_STRING+=" three" ;;
"4") VERSION_STRING+=" four" ;;
"5") VERSION_STRING+=" five" ;;
"6") VERSION_STRING+=" six" ;;
"7") VERSION_STRING+=" seven" ;;
"8") VERSION_STRING+=" eight" ;;
"9") VERSION_STRING+=" nine" ;;
esac
fi
# We need to grab the previous version from app/build.gradle for proper substitution
PREV_VERSION_CODE=$(grep -m 2 "versionCode" app/build.gradle | tail -n 1 | sed 's/.* //')
PREV_VERSION_STRING=$(grep -o 'versionName ".*"' app/build.gradle | cut -d '"' -f 2)
# Warn the user if they are downgrading the version code
[[ ${PREV_VERSION_CODE} -gt ${VERSION_CODE} ]] && echo "Previous version code is bigger than the specified one!" && sleep 10
# Replace them in app/build.gradle with the new versions
sed -i "s/${PREV_VERSION_CODE}/${VERSION_CODE}/g" app/build.gradle
sed -i "s/${PREV_VERSION_STRING}/${VERSION_STRING}/g" app/build.gradle
# Commit the result
git add app/build.gradle
git commit -m "${COMMIT_TYPE} release ${VERSION_CODE}"
git push origin
fi
# Build the APK
./gradlew clean ${GRADLE_TASK}
# Get APK out location
APK_OUT=$(ls app/build/outputs/apk/main/${TYPE}/*.apk)
# Don't do anything else in the script if the build failed
if [[ -f ${APK_OUT} ]]; then
# Get the APK name for below
APK_NAME=$(basename "${APK_OUT}")
# Move the APK if it is a release build and tag it
if [[ ${TYPE} = "release" ]]; then
# If the MOVE flag wasn't set above, set it to the default
[[ -z ${MOVE} ]] && MOVE=/home/site/public/APKs
# Copy the APK to wherever it needs to go
cp "${APK_OUT}" "${MOVE}"
# Only tag if there was a version update (there should be but never know)
if [[ -n ${VERSION_CODE} ]] && [[ ${MOVE} = /home/site/public/APKs ]]; then
git tag -a "${VERSION_CODE}" -m "${VERSION_STRING}
${COMMIT_TYPE} release ${VERSION_CODE}
APK location: http://download.projektsubstratum.com/APKs/${APK_NAME}"
# Push the tag
git push origin "${VERSION_CODE}"
fi
# If MOVE was specified (meaning the APK is debug), move it as well
elif [[ -n ${MOVE} ]]; then
cp "${APK_OUT}" "${MOVE}"
else
cp "${APK_OUT}" .
fi
# Install the APK if requested
[[ ${INSTALL} ]] && adb install -r "${MOVE}${APK_NAME}"
echo; echo "Build successful! APK located at: ${MOVE}${APK_NAME}"; echo
# Build failed, wtf?
else
echo "Build failed!"
fi