Skip to content

Commit 867c217

Browse files
committed
refactor: upstream merge & improve code
1 parent b6f91b1 commit 867c217

File tree

1 file changed

+49
-63
lines changed

1 file changed

+49
-63
lines changed

utils.sh

Lines changed: 49 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
TEMP_DIR="temp"
44
BIN_DIR="bin"
55
BUILD_DIR="build"
6-
76
if [ "${GITHUB_TOKEN-}" ]; then GH_HEADER="Authorization: token ${GITHUB_TOKEN}"; else GH_HEADER=; fi
87

98
toml_prep() {
10-
if [ ! -f "$1" ]; then return 1; fi
11-
if [ "${1##*.}" == toml ]; then
12-
__TOML__=$($TOML --output json --file "$1" .)
13-
elif [ "${1##*.}" == json ]; then
14-
__TOML__=$(cat "$1")
15-
else abort "config extension not supported"; fi
9+
[[ -f "$1" ]] || return 1
10+
case "${1##*.}" in
11+
toml) __TOML__=$("$TOML" --output json --file "$1" .) ;;
12+
json) __TOML__=$(<"$1") ;;
13+
*) abort "config extension not supported" ;;
14+
esac
1615
}
1716
toml_get_table_names() { jq -r -e 'to_entries[] | select(.value | type == "object") | .key' <<<"$__TOML__"; }
1817
toml_get_table_main() { jq -r -e 'to_entries | map(select(.value | type != "object")) | from_entries' <<<"$__TOML__"; }
@@ -47,46 +46,36 @@ abort() {
4746
java() { command java "$@"; }
4847

4948
install_pkg() {
50-
local cmd=$1
51-
local pkg=${2:-$1}
52-
if command -v "$cmd" >/dev/null 2>&1; then
53-
return 0
54-
fi
49+
local cmd=$1 pkg=${2:-$1}
50+
command -v "$cmd" >/dev/null 2>&1 && return 0
5551
pr "Installing $pkg..."
5652

57-
if command -v apt-get >/dev/null 2>&1; then
58-
sudo apt-get install -y "$pkg"
59-
elif command -v dnf >/dev/null 2>&1; then
60-
sudo dnf install -y "$pkg"
61-
elif command -v yum >/dev/null 2>&1; then
62-
sudo yum install -y "$pkg"
63-
elif command -v pacman >/dev/null 2>&1; then
64-
sudo pacman -S --noconfirm "$pkg"
65-
elif command -v apk >/dev/null 2>&1; then
66-
sudo apk add "$pkg"
67-
else
68-
abort "Cannot auto-install $pkg. Please install it manually."
69-
fi
70-
53+
local -a managers=("apt-get:install -y" "dnf:install -y" "yum:install -y" "pacman:-S --noconfirm" "apk:add")
54+
local pm args
55+
for entry in "${managers[@]}"; do
56+
pm="${entry%%:*}" args="${entry#*:}"
57+
if command -v "$pm" >/dev/null 2>&1; then
58+
sudo "$pm" "$args" "$pkg"
59+
command -v "$cmd" >/dev/null 2>&1 || abort "Failed to install $pkg"
60+
return
61+
fi
62+
done
7163
command -v "$cmd" >/dev/null 2>&1 || abort "Failed to install $pkg"
7264
}
7365

7466
get_prebuilts() {
7567
local cli_src=$1 cli_ver=$2 patches_src=$3 patches_ver=$4
76-
pr "Getting prebuilts (${patches_src%/*})" >&2
7768
local cl_dir=${patches_src%/*}
69+
pr "Getting prebuilts (${cl_dir})"
7870
cl_dir=${TEMP_DIR}/${cl_dir,,}-uni
7971
[ -d "$cl_dir" ] || mkdir "$cl_dir"
8072

8173
for src_ver in "$cli_src CLI $cli_ver cli" "$patches_src Patches $patches_ver patches"; do
8274
set -- $src_ver
8375
local src=$1 tag=$2 ver=${3-} fprefix=$4
8476

85-
if [ "$tag" = "CLI" ]; then
86-
local grab_cl=false
87-
elif [ "$tag" = "Patches" ]; then
88-
local grab_cl=true
89-
else abort unreachable; fi
77+
local grab_cl=false
78+
[ "$tag" = "Patches" ] && grab_cl=true
9079

9180
local dir=${src%/*}
9281
dir=${TEMP_DIR}/${dir,,}-uni
@@ -106,25 +95,29 @@ get_prebuilts() {
10695
name_ver="$ver"
10796
fi
10897

109-
local url file tag_name name
98+
local url file tag_name matches count
11099
file=$(find "$dir" -name "*${fprefix}-${name_ver#v}.*" -type f 2>/dev/null)
111100
if [ -z "$file" ]; then
112-
local resp asset name
101+
local resp name
113102
resp=$(gh_req "$uni_rel" -) || return 1
114103
tag_name=$(jq -r '.tag_name' <<<"$resp")
115104
matches=$(jq -e '.assets | map(select(.name | endswith("asc") | not))' <<<"$resp")
116-
if [ "$(jq 'length' <<<"$matches")" -gt 1 ]; then
117-
matches=$(jq -e -r 'map(select(.name | contains("-dev") | not))' <<<"$matches")
105+
count=$(jq 'length' <<<"$matches")
106+
if [ "$count" -gt 1 ]; then
107+
local matches_new
108+
matches_new=$(jq -e -r 'map(select(.name | contains("-dev") | not))' <<<"$matches")
109+
if [ "$(jq 'length' <<<"$matches_new")" -eq 1 ]; then
110+
matches=$matches_new
111+
count=1
112+
fi
118113
fi
119-
if [ "$(jq 'length' <<<"$matches")" -eq 0 ]; then
114+
if [ "$count" -eq 0 ]; then
120115
epr "No asset was found"
121116
return 1
122-
elif [ "$(jq 'length' <<<"$matches")" -ne 1 ]; then
123-
wpr "More than 1 asset was found for this cli release. Falling back to the first one found..."
117+
elif [ "$count" -ne 1 ]; then
118+
wpr "More than 1 asset was found for this release. Falling back to the first one found..."
124119
fi
125-
asset=$(jq -r ".[0]" <<<"$matches")
126-
url=$(jq -r .url <<<"$asset")
127-
name=$(jq -r .name <<<"$asset")
120+
read -r url name < <(jq -r '.[0] | "\(.url)\t\(.name)"' <<<"$matches")
128121
file="${dir}/${name}"
129122
gh_dl "$file" "$url" >&2 || return 1
130123
echo "> ⚙️ » $tag: \`$(cut -d/ -f1 <<<"$src")/${name}\` " >>"${cl_dir}/changelog.md"
@@ -179,10 +172,9 @@ _req() {
179172
fi
180173
}
181174
ua() {
182-
local ver major
175+
local ver
183176
ver=$(curl -sf "https://product-details.mozilla.org/1.0/firefox_versions.json" | jq -re '.LATEST_FIREFOX_VERSION') || ver="148.0"
184-
major=${ver%%.*}
185-
echo "Mozilla/5.0 (X11; Linux x86_64; rv:${major}.0) Gecko/20100101 Firefox/${major}.0"
177+
echo "Mozilla/5.0 (X11; Linux x86_64; rv:${ver%%.*}.0) Gecko/20100101 Firefox/${ver%%.*}.0"
186178
}
187179
req() {
188180
if [ -z "${_UA:-}" ]; then _UA=$(ua); fi
@@ -204,14 +196,13 @@ get_highest_ver() {
204196
if ! semver_validate "$m"; then echo "$m"; else sort -rV <<<"$vers" | head -1; fi
205197
}
206198
semver_validate() {
207-
local a="${1%-*}"
208-
a="${a#v}"
209-
local ac="${a//[.0-9]/}"
210-
[ ${#ac} = 0 ]
199+
local a="${1%-*}"
200+
a="${a#v}"
201+
[ -z "${a//[.0-9]/}" ]
211202
}
212203
get_patch_last_supported_ver() {
213204
local list_patches=$1 pkg_name=$2 inc_sel=$3 _exc_sel=$4 _exclusive=$5 # TODO: resolve using all of these
214-
local op
205+
local op pcount av_apps
215206
if [ "$inc_sel" ]; then
216207
if ! op=$(awk '{$1=$1}1' <<<"$list_patches"); then
217208
epr "list-patches: '$op'"
@@ -281,7 +272,7 @@ dl_apkmirror() {
281272
is_bundle=true
282273
else
283274
if [ "$arch" = "arm-v7a" ]; then arch="armeabi-v7a"; fi
284-
local resp node app_table apkmname dlurl=""
275+
local resp node apkmname dlurl=""
285276
apkmname=$($HTMLQ "h1.marginZero" --text <<<"$__APKMIRROR_RESP__")
286277
apkmname="${apkmname,,}" apkmname="${apkmname// /-}" apkmname="${apkmname//[^a-z0-9-]/}"
287278
url="${url}/${apkmname}-${version//./-}-release/"
@@ -345,7 +336,7 @@ dl_uptodown() {
345336
apparch=('arm64-v8a, armeabi-v7a, x86_64' 'arm64-v8a, armeabi-v7a, x86, x86_64' 'arm64-v8a, armeabi-v7a')
346337
else apparch=("$arch" 'arm64-v8a, armeabi-v7a, x86_64' 'arm64-v8a, armeabi-v7a, x86, x86_64' 'arm64-v8a, armeabi-v7a'); fi
347338

348-
local op resp data_code
339+
local i op resp data_code
349340
data_code=$($HTMLQ "#detail-app-name" --attribute data-code <<<"$__UPTODOWN_RESP__")
350341
local versionURL=""
351342
local is_bundle=false
@@ -565,17 +556,12 @@ build_uni() {
565556
done
566557
patched_apk="${TEMP_DIR}/${app_name_l}-${brand_f}-${version_f}-${arch_f}.apk"
567558

568-
if [ "$arch" = "arm64-v8a" ]; then
569-
patcher_args+=("--striplibs arm64-v8a")
570-
elif [ "$arch" = "arm-v7a" ]; then
571-
patcher_args+=("--striplibs armeabi-v7a")
572-
elif [ "$arch" = "x86" ]; then
573-
patcher_args+=("--striplibs x86")
574-
elif [ "$arch" = "x86_64" ]; then
575-
patcher_args+=("--striplibs x86_64")
576-
else
577-
patcher_args+=("--striplibs arm64-v8a,armeabi-v7a")
578-
fi
559+
case "$arch" in
560+
"arm-v7a") libs="armeabi-v7a" ;;
561+
"arm64-v8a"|"x86"|"x86_64") libs="$arch" ;;
562+
*) libs="arm64-v8a,armeabi-v7a" ;;
563+
esac
564+
patcher_args+=("--striplibs $libs")
579565
local stock_apk_input
580566
if [ -f "${stock_apk}.apkm" ]; then stock_apk_input="${stock_apk}.apkm"; else stock_apk_input="$stock_apk"; fi
581567
if [ "${NORB:-}" != true ] || [ ! -f "$patched_apk" ]; then

0 commit comments

Comments
 (0)