forked from rom-builder/BUILDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_utils.sh
388 lines (344 loc) · 12.3 KB
/
_utils.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/bin/bash
telegram_send_message() {
# use environment variables
local token=$TG_TOKEN
local chat=$TG_CHAT
local message=$1
local disable_web_page_preview=$2
if [ -z "$token" ] || [ -z "$chat" ]; then
return
fi
if [ -z "$message" ]; then
echo "No message passed. Aborting."
return
fi
local send_message_response=$(curl -s "https://api.telegram.org/bot$token/sendMessage" -d chat_id="$chat" -d text="$message" -d parse_mode=MARKDOWN -d disable_web_page_preview="$disable_web_page_preview")
if [ "$(echo "$send_message_response" | jq -r '.ok')" == "true" ]; then
echo "Message sent to Telegram."
else
echo "Error sending message to Telegram."
fi
}
telegram_send_file() {
# use environment variables
local token=$TG_TOKEN
local chat=$TG_CHAT
local file=$1
local caption=$2
if [ -z "$token" ] || [ -z "$chat" ]; then
return
fi
if [ -z "$file" ]; then
echo "No file passed. Aborting."
exit 1
fi
local send_file_response=$(curl -s "https://api.telegram.org/bot$token/sendDocument" -F chat_id="$chat" -F document=@"$file" -F caption="$caption")
if [ "$(echo "$send_file_response" | jq -r '.ok')" == "true" ]; then
echo "File $file sent to Telegram."
else
echo "Error sending file $file to Telegram."
fi
}
update_tg() {
local message="$1"
telegram_send_message "*$message*" true
}
logt() {
local message="$1"
update_tg "$message"
echo "$message"
}
resolve_dependencies() {
# Remove repo if it exists as it is outdated
sudo apt-get remove -y repo || apt-get remove -y repo
local packages=('git-core' 'gnupg' 'flex' 'bison' 'build-essential' 'zip' 'curl' 'zlib1g-dev' 'libc6-dev-i386' 'libncurses5' 'lib32ncurses5-dev' 'x11proto-core-dev' 'libx11-dev' 'lib32z1-dev' 'libgl1-mesa-dev' 'libxml2-utils' 'xsltproc' 'unzip' 'openssl' 'libssl-dev' 'fontconfig' 'jq' 'openjdk-8-jdk' 'gperf' 'python-is-python3' 'ccache' 'sshpass')
echo "Updating package lists..."
sudo apt-get update -y || apt-get update -y
echo "Installing dependencies..."
sudo apt-get install -y "${packages[@]}" || apt-get install -y "${packages[@]}"
# Download latest repo from Google Storage
sudo curl https://storage.googleapis.com/git-repo-downloads/repo -o /usr/local/bin/repo && sudo chmod a+x /usr/local/bin/repo
export USE_CCACHE=1
export CCACHE_EXEC=$(which ccache)
echo "Dependencies check complete."
}
git_setup() {
local name="$1"
local email="$2"
echo "Setting up git with email: $email and name: $name"
git config --global user.email "$email"
git config --global user.name "$name"
git config --global pull.rebase true
}
git_clone() {
# take arguments as -r repo -d dir -b branch
while [[ "$#" -gt 0 ]]; do
case $1 in
-r|--repo) repo="$2"; shift ;;
-d|--dir) dir="$2"; shift ;;
-b|--branch) branch="$2"; shift ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
# Check if the directory exists
if [ -d "$dir" ]; then
echo "Already cloned $repo into $dir."
echo "Pulling latest changes..."
(
cd "$dir" || exit
git pull
)
return
else # Clone the repo into the directory
# If branch is not null
if [ "$branch" != "null" ]; then
echo "Cloning $repo into $dir with branch $branch..."
git clone "$repo" -b "$branch" "$dir"
else
echo "Cloning $repo into $dir..."
git clone "$repo" "$dir"
fi
fi
# Clear variables
unset repo dir branch
}
git_clone_json() {
local json_file="$1"
local before_sync="$2"
# Check if the file exists
if [ ! -f "$json_file" ]; then
logt "File $json_file does not exist. Aborting."
exit 1
fi
if [ "$before_sync" == "true" ]; then
echo "Pulling repos required before sync..."
else
echo "Pulling repos required after sync..."
fi
for repo in $(jq -r '.repos[].repo' $json_file); do
before_sync_repo=$(jq -r --arg repo "$repo" '.repos[] | select(.repo == $repo) | .before_sync' $json_file)
# if before_sync is true then clone only the repos with before_sync set to true
if [ "$before_sync" == "true" ]; then
if [ "$before_sync_repo" != "true" ]; then
continue
fi
fi
# if before_sync is false or null then skip the repos with before_sync set to true
if [ -z "$before_sync" ] || [ "$before_sync" == "false" ]; then
if [ "$before_sync_repo" == "true" ]; then
continue
fi
fi
dir=$(jq -r --arg repo "$repo" '.repos[] | select(.repo == $repo) | .dir' $json_file)
branch=$(jq -r --arg repo "$repo" '.repos[] | select(.repo == $repo) | .branch' $json_file)
pre_command=$(jq -r --arg repo "$repo" '.repos[] | select(.repo == $repo) | .pre_command' $json_file)
post_command=$(jq -r --arg repo "$repo" '.repos[] | select(.repo == $repo) | .post_command' $json_file)
# If pre_command is not null
if [ "$pre_command" != "null" ]; then
echo "Running pre_commands for $repo..."
eval "$pre_command"
fi
# if branch is not specified
if [ "$branch" == "null" ]; then
echo "Repo: $repo Dir: $dir"
git_clone -r "$repo" -d "$dir"
else
echo "Repo: $repo Dir: $dir Branch: $branch"
git_clone -r "$repo" -d "$dir" -b "$branch"
fi
# If post_commands is not null
if [ "$post_command" != "null" ]; then
echo "Running post_commands for $repo..."
eval "$post_command"
fi
done
}
clean_build() {
local dir="$1"
echo "Cleaning build directory: $dir"
if [ -d "$dir" ]; then
rm -rf "$dir"
else
echo "Build directory does not exist."
fi
}
# Create a function to check if github repo exists or not
check_github_repo() {
local repo=$1
local token=$2
local response=$(curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bearer $token" https://api.github.com/repos/$repo)
if [[ $response -eq 200 ]]; then
return 0 # true
else
return 1 # false
fi
}
# Create a function to create a github repo
create_github_repo() {
local repo_name=$1
local token=$2
local repo=$(echo $repo_name | cut -d'/' -f2)
local org=$(echo $repo_name | cut -d'/' -f1)
local data="{\"name\":\"$repo\",\"auto_init\":true,\"private\":false, \"has_issues\": false, \"has_projects\": false, \"has_wiki\": false, \"description\": \"Automated build releases for $DEVICE-$ROM_NAME\"}"
# Spit owner and repo from owner/repo
local response=$(curl -s -H "Authorization: Bearer $token" -d "$data" https://api.github.com/orgs/$org/repos)
local status=$(echo $response | jq -r '.message')
if [[ "$status" == "null" ]]; then
echo "Repository $repo_name created successfully."
return 0 # true
else
echo "Error creating repository: $status"
return 1 # false
fi
}
github_release() {
while [[ "$#" -gt 0 ]]; do
case $1 in
-tk|--token) token="$2"; shift ;;
-r|--repo) repo="$2"; shift ;;
-tg|--tag) tag="$2"; shift ;;
-p|--pattern) pattern="$2"; shift ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
# check if all variables are set
required_vars=(token repo tag pattern)
for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
echo "Variable $var is not set. Aborting."
return
fi
done
# Check if $RELEASE_OUT_DIR is set
if [ -z "$RELEASE_OUT_DIR" ]; then
logt "RELEASE_OUT_DIR is not set. Aborting upload."
exit 1
fi
# Check if files exist
echo "Checking if files exist..."
if [ -z "$(ls -A $RELEASE_OUT_DIR | grep -E "$pattern")" ]; then
echo $(ls -A $RELEASE_OUT_DIR | grep -E "$pattern")
update_tg "Build had no files to upload."
echo "No files found matching pattern $pattern. Aborting upload."
return
else
echo "Files found matching pattern $pattern."
fi
# Check if github repo exists
if ! check_github_repo "$repo" "$token"; then
echo "Github repo $repo does not exist. Creating..."
if create_github_repo "$repo" "$token"; then
echo "Github: $repo created successfully."
else
logt "Failed to create github repo $repo. Aborting upload."
return
fi
fi
# Create the release
echo "Creating release $tag..."
local release_response=$(curl -s -H "Authorization: Bearer $token" "https://api.github.com/repos/$repo/releases" -d "{\"tag_name\":\"$tag\",\"name\":\"$ROM_NAME $tag\",\"body\":\"Release version **$tag**\n\nDevice: $DEVICE\nROM: $ROM_NAME\n\n***Happy flashing!***\",\"generate_release_notes\":false}")
local release_url=$(echo $release_response | jq -r '.html_url')
if [ -z "$release_url" ]; then
logt "Release URL is null. Some error occured when creating the release. Aborting upload."
echo "Release response: $release_response"
return
else
local release_id=$(echo $release_response | jq -r '.id')
echo "Release URL: $release_url"
fi
telegram_send_message "Created [Release]($release_url)" true
echo "Release created at $release_url"
# Upload each file that matches the pattern
for file in $(ls -A $RELEASE_OUT_DIR | grep -E "$pattern"); do
# if SF_UPLOAD and file is zip
if [ "$SF_UPLOAD" == "true" ] && [[ "$file" == *.zip ]]; then
echo "Uploading $file to SourceForge..."
(sourceforge_upload -f "$RELEASE_OUT_DIR/$file" -v $tag)
fi
echo "Uploading $file to Github..."
file_release=$(curl -s -H "Authorization: Bearer $token" -H "Content-Type: application/octet-stream" -T "$RELEASE_OUT_DIR/$file" "https://uploads.github.com/repos/$repo/releases/$release_id/assets?name=$file")
file_url=$(echo $file_release | jq -r '.browser_download_url')
# if file_url is null or empty
if [ -z "$file_url" ]; then
logt "File URL is null. Some error occured when uploading the file. Aborting upload."
echo "File release response: $file_release"
return
else
echo "GitHub URL: $file_url"
telegram_send_message "GitHub: [$file]($file_url)" true
fi
done
telegram_send_message "Uploaded files to [release $tag in $repo]($release_url)" true
echo "Uploaded files to release $tag in $repo"
}
compute_build_time() {
local start_time="$1"
local end_time="$2"
local build_time=$((end_time - start_time))
local hours=$((build_time / 3600))
local minutes=$((build_time % 3600 / 60))
local seconds=$((build_time % 60))
if [ "$hours" -gt 0 ]; then
echo "$hours h $minutes m $seconds s"
elif [ "$minutes" -gt 0 ]; then
echo "$minutes m $seconds s"
else
echo "$seconds s"
fi
}
remove_ota_package() {
# if REMOVE_OTA_PACKAGE is not set or is false, return
if [ -z "$REMOVE_OTA_PACKAGE" ] || [ "$REMOVE_OTA_PACKAGE" == "false" ]; then
echo "REMOVE_OTA_PACKAGE is not set or is false. Skipping removal of OTA package."
return
fi
# Remove OTA package if exists with name *_$DEVICE-ota-*.$USER.zip
local ota_package=$(ls $RELEASE_OUT_DIR/*_$DEVICE-ota-*.$USER.zip)
if [ -n "$ota_package" ]; then
base_filename=$(basename $ota_package)
echo "Removing OTA package $base_filename"
rm -f $ota_package
else
echo "No OTA package found to remove."
fi
}
# Function to upload release files to sourceforge
sourceforge_upload() {
# Check if $SF_UPLOAD is set to true
if [ -z "$SF_UPLOAD" ] || [ "$SF_UPLOAD" == "false" ]; then
echo "SF_UPLOAD is not set or is false. Skipping upload to sourceforge."
return
fi
# Check if $SF_USER, $SF_HOST, $SF_PASS, $SF_PROJECT and $SF_DIR are set
local required_sf_vars=(SF_USER SF_HOST SF_PASS SF_PROJECT SF_DIR SF_PATH)
for var in "${required_sf_vars[@]}"; do
if [ -z "${!var}" ]; then
logt "$var is not set. Aborting upload to sourceforge."
return
fi
done
while [[ "$#" -gt 0 ]]; do
case $1 in
-f|--file) file="$2"; shift ;;
-v|--version) version="$2"; shift ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
# Upload file to sourceforge
local file_name=$(basename $file)
echo "Uploading $file_name to Sourceforge..."
sshpass -p "$SF_PASS" scp -o StrictHostKeyChecking=no $file $SF_USER@$SF_HOST:$SF_PATH
if [ $? -eq 0 ]; then
# Get download URL
local download_url="https://sourceforge.net/projects/$SF_PROJECT/files/$SF_DIR/$file_name/download"
echo "Sourceforge download URL: $download_url"
telegram_send_message "Sourceforge: [$file_name]($download_url)" true
else
logt "Failed to upload $file to sourceforge."
fi
}
# Export functions
export -f resolve_dependencies git_setup git_clone git_clone_json clean_build github_release telegram_send_message telegram_send_file update_tg logt