-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrom.sh
More file actions
301 lines (272 loc) Β· 10.4 KB
/
rom.sh
File metadata and controls
301 lines (272 loc) Β· 10.4 KB
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
#!/usr/bin/env bash
# Copyright (C) 2022 Muhammad Fadlyas (fadlyas07)
# SPDX-License-Identifier: GPL-3.0-or-later
export DEVICE=
export ROM_NAME=
export BRANCH_MANIFEST=
export ROM_CODENAME=
export TG_TOKEN=
export CHAT_ID=
export GH_TOKEN=
export BUILDTYPE=
export LunchCommand="lunch XX_${DEVICE}-userdebug"
export BuildCommand=
export GitHubUsername="greenforce-project"
export GitHubRepoRelease="android_release"
export GitHubReleaseTag="release"
if [[ "$DEVICE" == "" ]]; then
echo "Environment for 'DEVICE' is empty, please set it by editing script!"
exit 1
elif [[ "$ROM_NAME" == "" ]]; then
echo "Environment for 'ROM_NAME' is empty, please set it by editing script!"
exit 1
elif [[ "$BRANCH_MANIFEST" == "" ]]; then
echo "Environment for 'BRANCH_MANIFEST' is empty, please set it by editing script!"
exit 1
elif [[ "$ROM_CODENAME" == "" ]]; then
echo "Environment for 'ROM_CODENAME' is empty, please set it by editing script!"
exit 1
elif [[ "$TG_TOKEN" == "" ]]; then
echo "Environment for 'TG_TOKEN' is empty, please set it by editing script!"
exit 1
elif [[ "$CHAT_ID" == "" ]]; then
echo "Environment for 'CHAT_ID' is empty, please set it by editing script!"
exit 1
elif [[ "$GH_TOKEN" == "" ]]; then
echo "Environment for 'GH_TOKEN' is empty, please set it by editing script!"
exit 1
elif [[ "$BUILDTYPE" == "" ]]; then
echo "Environment for 'BUILDTYPE' is empty, please set it by editing script!"
exit 1
elif [[ "$LunchCommand" == "" ]]; then
echo "Environment for 'LunchCommand' is empty, please set it by editing script!"
exit 1
elif [[ "$BuildCommand" == "" ]]; then
echo "Environment for 'BuildCommand' is empty, please set it by editing script!"
exit 1
else
echo "Trust me everything's gonna be alright!"
fi
export CDIR=$(pwd)
export OUT="${CDIR}/out/target/product/${DEVICE}"
export DISTRO=$(source /etc/os-release && echo "${PRETTY_NAME}")
if [[ -d "${CDIR}/ccache" ]]; then
CCACHE_DIR="${CDIR}/ccache"
else
CCACHE_DIR="${HOME}/.ccache"
fi
export CCACHE_DIR
export TZ=":Asia/Jakarta"
export TERM=xterm
red=$(tput setaf 1) # red
grn=$(tput setaf 2) # green
blu=$(tput setaf 4) # blue
cya=$(tput setaf 6) # cyan
txtrst=$(tput sgr0) # Reset
timeStart() {
DATELOG=$(date "+%H%M-%d%m%Y")
BUILD_START=$(date +"%s")
DATE=$(date)
}
timeEnd() {
BUILD_END=$(date +"%s")
DIFF=$(($BUILD_END - $BUILD_START))
}
telegram_curl() {
local ACTION=${1}
shift
local HTTP_REQUEST=${1}
shift
if [[ "${HTTP_REQUEST}" != "POST_FILE" ]]; then
curl -s -X "${HTTP_REQUEST}" "https://api.telegram.org/bot$TG_TOKEN/$ACTION" "$@" | jq .
else
curl -s "https://api.telegram.org/bot$TG_TOKEN/$ACTION" "$@" | jq .
fi
}
telegram_main() {
local ACTION=${1}
local HTTP_REQUEST=${2}
local CURL_ARGUMENTS=()
while [[ "${#}" -gt 0 ]]; do
case "${1}" in
--animation | --audio | --document | --photo | --video )
local CURL_ARGUMENTS+=(-F $(echo "${1}" | sed 's/--//')=@"${2}")
shift
;;
--* )
if [[ "$HTTP_REQUEST" != "POST_FILE" ]]; then
local CURL_ARGUMENTS+=(-d $(echo "${1}" | sed 's/--//')="${2}")
else
local CURL_ARGUMENTS+=(-F $(echo "${1}" | sed 's/--//')="${2}")
fi
shift
;;
esac
shift
done
telegram_curl "${ACTION}" "${HTTP_REQUEST}" "${CURL_ARGUMENTS[@]}"
}
telegram_curl_get() {
local ACTION=${1}
shift
telegram_main "${ACTION}" GET "$@"
}
telegram_curl_post() {
local ACTION=${1}
shift
telegram_main "${ACTION}" POST "$@"
}
telegram_curl_post_file() {
local ACTION=${1}
shift
telegram_main "${ACTION}" POST_FILE "$@"
}
tg_send_message() {
telegram_main sendMessage POST "$@"
}
tg_edit_message_text() {
telegram_main editMessageText POST "$@"
}
tg_send_document() {
telegram_main sendDocument POST_FILE "$@"
}
progress() {
echo "BOTLOG: Build tracker process is running..."
sleep 5;
while [ 1 ]; do
if [[ ${retVal} -ne 0 ]]; then
exit ${retVal}
fi
# Get latest percentage
PERCENTAGE=$(cat $BUILDLOG | tail -n 1 | awk '{ print $2 }')
NUMBER=$(echo ${PERCENTAGE} | sed 's/[^0-9]*//g')
# Report percentage to the $CHAT_ID
if [[ "${NUMBER}" != "" ]]; then
if [[ "${NUMBER}" -le "99" ]]; then
if [[ "${NUMBER}" != "${NUMBER_OLD}" ]] && [[ "$NUMBER" != "" ]] && ! cat $BUILDLOG | tail -n 1 | grep "glob" > /dev/null && ! cat $BUILDLOG | tail -n 1 | grep "including" > /dev/null && ! cat $BUILDLOG | tail -n 1 | grep "soong" > /dev/null && ! cat $BUILDLOG | tail -n 1 | grep "finishing" > /dev/null; then
echo -e "BOTLOG: Percentage changed to ${NUMBER}%"
build_message "π οΈ Building... ${NUMBER}%" > /dev/null
fi
NUMBER_OLD=${NUMBER}
fi
if [[ "$NUMBER" -eq "99" ]] && [[ "$NUMBER" != "" ]] && ! cat $BUILDLOG | tail -n 1 | grep "glob" > /dev/null && ! cat $BUILDLOG | tail -n 1 | grep "including" > /dev/null && ! cat $BUILDLOG | tail -n 1 | grep "soong" > /dev/null && ! cat $BUILDLOG | tail -n 1 | grep "finishing" > /dev/null; then
echo "BOTLOG: Build tracker process ended"
break
fi
fi
sleep 5
done
return 0
}
timeStart
build_message() {
if [[ "$CI_MESSAGE_ID" = "" ]]; then
CI_MESSAGE_ID=$(tg_send_message --chat_id "$CHAT_ID" --text "<b>=== Starting Build ${ROM_NAME} ===</b>
<b>Codename:</b> <code>${ROM_CODENAME} (${BRANCH_MANIFEST})</code>
<b>Device:</b> <code>${DEVICE}</code>
<b>Build type:</b> <code>${BUILDTYPE}</code>
<b>Job:</b> <code>$(nproc --all) Paralel processing</code>
<b>Running on:</b> <code>$DISTRO</code>
<b>Started at</b> <code>$DATE</code>
<b>Status:</b> <code>${1}</code>" --parse_mode "html" | jq .result.message_id)
else
tg_edit_message_text --chat_id "$CHAT_ID" --message_id "$CI_MESSAGE_ID" --text "<b>=== Starting Build ${ROM_NAME} ===</b>
<b>Codename:</b> <code>${ROM_CODENAME} (${BRANCH_MANIFEST})</code>
<b>Device:</b> <code>${DEVICE}</code>
<b>Build type:</b> <code>${BUILDTYPE}</code>
<b>Job:</b> <code>$(nproc --all) Paralel processing</code>
<b>Running on:</b> <code>$DISTRO</code>
<b>Started at</b> <code>$DATE</code>
<b>Status:</b> <code>${1}</code>" --parse_mode "html"
fi
}
statusBuild() {
if [[ $retVal -eq 8 ]]; then
build_message "Build Aborted π‘ with Code Exit ${retVal}.
Total time elapsed: $(($DIFF / 60)) minute(s) and $(($DIFF % 60)) seconds."
tg_send_message --chat_id "$CHAT_ID" --text "Build Aborted π with Code Exit ${retVal}.
Sudah kubilang yang teliti π‘"
echo "Build Aborted"
tg_send_document --chat_id "$CHAT_ID" --document "$BUILDLOG" --reply_to_message_id "$CI_MESSAGE_ID"
LOGTRIM="$CDIR/out/log_trimmed.log"
sed -n '/FAILED:/,//p' $BUILDLOG &> $LOGTRIM
tg_send_document --chat_id "$CHAT_ID" --document "$LOGTRIM" --reply_to_message_id "$CI_MESSAGE_ID"
exit $retVal
fi
if [[ $retVal -eq 141 ]]; then
build_message "Build Aborted π with Code Exit ${retVal}, See log.
Total time elapsed: $(($DIFF / 60)) minute(s) and $(($DIFF % 60)) seconds."
tg_send_message --chat_id "$CHAT_ID_SECOND" --text "Build Aborted π with Code Exit ${retVal}."
echo "Build Aborted"
tg_send_document --chat_id "$CHAT_ID" --document "$BUILDLOG" --reply_to_message_id "$CI_MESSAGE_ID"
LOGTRIM="$CDIR/out/log_trimmed.log"
sed -n '/FAILED:/,//p' $BUILDLOG &> $LOGTRIM
tg_send_document --chat_id "$CHAT_ID" --document "$LOGTRIM" --reply_to_message_id "$CI_MESSAGE_ID"
exit $retVal
fi
if [[ $retVal -ne 0 ]]; then
build_message "Build Error π with Code Exit ${retVal}, See log.
Total time elapsed: $(($DIFF / 60)) minute(s) and $(($DIFF % 60)) seconds."
tg_send_message --chat_id "$CHAT_ID_SECOND" --text "Build Error π with Code Exit ${retVal}."
echo "Build Error"
tg_send_document --chat_id "$CHAT_ID" --document "$BUILDLOG" --reply_to_message_id "$CI_MESSAGE_ID"
LOGTRIM="$CDIR/out/log_trimmed.log"
sed -n '/FAILED:/,//p' $BUILDLOG &> $LOGTRIM
tg_send_document --chat_id "$CHAT_ID" --document "$LOGTRIM" --reply_to_message_id "$CI_MESSAGE_ID"
exit $retVal
fi
build_message "Build success β€οΈ"
tg_send_message --chat_id "$CHAT_ID" --text "LOL WTF' Build Success Mate β€οΈ
Congratsss I'm Happy for you!!"
}
echo -e ${blu}"CCACHE is enabled for this build"${txtrst}
export CCACHE_EXEC=$(which ccache)
export USE_CCACHE=1
ccache -M 50G
BUILDLOG="${CDIR}/out/${ROM_NAME}-${DEVICE}-${DATELOG}.log"
build_message "Prepare for build..."
sleep 2
. build/envsetup.sh
build_message "${LunchCommand}"
command "$LunchCommand"
mkfifo reading
tee "$BUILDLOG" < reading &
if [[ -d "$OUT" ]]; then
build_message "Here we go again...π₯"
else
build_message "Staring bro...π₯"
fi
sleep 2
build_message "π οΈ Building..."
progress &
command "$BuildCommand" > reading
retVal=$?
timeEnd
statusBuild
tg_send_document --chat_id "$CHAT_ID" --document "$BUILDLOG" --reply_to_message_id "$CI_MESSAGE_ID"
export FILENAME=$(cd "${OUT}" && find ${ROM_NAME}*${ROM_CODENAME}*${BUILDTYPE}*.zip)
export FILEPATH="${OUT}/${FILENAME}"
if [[ -e "${FILEPATH}" ]]; then
build_message "Build Success β€οΈ"
[[ ! -e "${CDIR}/gh-release" ]] && curl -Lo "${CDIR}/gh-release" https://github.com/fadlyas07/scripts/raw/master/github/github-release
chmod +x "${CDIR}/gh-release"
build_message "Uploading ${FILENAME}..."
build_upload() {
./gh-release upload \
--security-token "${GH_TOKEN}" \
--user "${GitHubUsername}" \
--repo "${GitHubRepoRelease}" \
--tag "${GitHubReleaseTag}" \
--name "${FILENAME}" \
--file "${FILEPATH}" && echo "succes bro!"
}
if [[ $(build_upload) == "succes bro!" ]]; then
LINK=$(echo "https://github.com/greenforce-project/android_release/releases/download/release/${FILENAME}")
build_message "Build Complete!
tg_send_message --chat_id "$CHAT_ID" --reply_to_message_id "$CI_MESSAGE_ID" --text "Link: <code>${LINK}</code>" --parse_mode "html"
else
build_message "Uploading failed..."
fi
else
build_message "Something went wrong to track files..."
fi