diff --git a/build.sh b/build.sh index 840188e..58b0a2c 100755 --- a/build.sh +++ b/build.sh @@ -1,9 +1,10 @@ #!/usr/bin/env bash set -euo pipefail +shopt -s nullglob trap "rm -rf temp/*tmp.* temp/*/*tmp.*; exit 130" INT -if [ "${1:-}" = "clean" ]; then +if [ "${1-}" = "clean" ]; then rm -rf temp build logs build.md exit 0 fi @@ -28,14 +29,14 @@ DEF_CLI_SRC=$(toml_get "$main_config_t" cli-source) || DEF_CLI_SRC="j-hc/revance DEF_RV_BRAND=$(toml_get "$main_config_t" rv-brand) || DEF_RV_BRAND="ReVanced" mkdir -p $TEMP_DIR $BUILD_DIR -if [ "${2:-}" = "--config-update" ]; then +if [ "${2-}" = "--config-update" ]; then config_update exit 0 fi : >build.md ENABLE_MAGISK_UPDATE=$(toml_get "$main_config_t" enable-magisk-update) || ENABLE_MAGISK_UPDATE=true -if [ "$ENABLE_MAGISK_UPDATE" = true ] && [ -z "${GITHUB_REPOSITORY:-}" ]; then +if [ "$ENABLE_MAGISK_UPDATE" = true ] && [ -z "${GITHUB_REPOSITORY-}" ]; then pr "You are building locally. Magisk updates will not be enabled." ENABLE_MAGISK_UPDATE=false fi @@ -82,7 +83,7 @@ for table_name in $(toml_get_table_names); do cli_ver=$(toml_get "$t" cli-version) || cli_ver=$DEF_CLI_VER if ! set_prebuilts "$integrations_src" "$patches_src" "$cli_src" "$integrations_ver" "$patches_ver" "$cli_ver"; then - if ! RVP="$(get_rv_prebuilts "$integrations_src" "$patches_src" "$integrations_ver" "$patches_ver" "$cli_src" "$cli_ver")"; then + if ! RVP="$(get_rv_prebuilts "$cli_src" "$cli_ver" "$integrations_src" "$integrations_ver" "$patches_src" "$patches_ver")"; then abort "could not download rv prebuilts" fi read -r rv_cli_jar rv_integrations_apk rv_patches_jar rv_patches_json <<<"$RVP" @@ -104,9 +105,9 @@ for table_name in $(toml_get_table_names); do app_args[rv_brand]=$(toml_get "$t" rv-brand) || app_args[rv_brand]="$DEF_RV_BRAND" app_args[excluded_patches]=$(toml_get "$t" excluded-patches) || app_args[excluded_patches]="" - if [ -n "${app_args[excluded_patches]}" ] && [[ "${app_args[excluded_patches]}" != *'"'* ]]; then abort "patch names inside excluded-patches must be quoted"; fi + if [ -n "${app_args[excluded_patches]}" ] && [[ ${app_args[excluded_patches]} != *'"'* ]]; then abort "patch names inside excluded-patches must be quoted"; fi app_args[included_patches]=$(toml_get "$t" included-patches) || app_args[included_patches]="" - if [ -n "${app_args[included_patches]}" ] && [[ "${app_args[included_patches]}" != *'"'* ]]; then abort "patch names inside included-patches must be quoted"; fi + if [ -n "${app_args[included_patches]}" ] && [[ ${app_args[included_patches]} != *'"'* ]]; then abort "patch names inside included-patches must be quoted"; fi app_args[exclusive_patches]=$(toml_get "$t" exclusive-patches) && vtf "${app_args[exclusive_patches]}" "exclusive-patches" || app_args[exclusive_patches]=false app_args[version]=$(toml_get "$t" version) || app_args[version]="auto" app_args[app_name]=$(toml_get "$t" app-name) || app_args[app_name]=$table_name @@ -134,9 +135,9 @@ for table_name in $(toml_get_table_names); do app_args[archive_dlurl]=${app_args[archive_dlurl]%/} app_args[dl_from]=archive } || app_args[archive_dlurl]="" - if [ -z "${app_args[dl_from]:-}" ]; then abort "ERROR: no 'apkmirror_dlurl', 'uptodown_dlurl' or 'apkmonk_dlurl' option was set for '$table_name'."; fi + if [ -z "${app_args[dl_from]-}" ]; then abort "ERROR: no 'apkmirror_dlurl', 'uptodown_dlurl' or 'apkmonk_dlurl' option was set for '$table_name'."; fi app_args[arch]=$(toml_get "$t" arch) || app_args[arch]="all" - if [ "${app_args[arch]}" != "both" ] && [ "${app_args[arch]}" != "all" ] && [[ "${app_args[arch]}" != "arm64-v8a"* ]] && [[ "${app_args[arch]}" != "arm-v7a"* ]]; then + if [ "${app_args[arch]}" != "both" ] && [ "${app_args[arch]}" != "all" ] && [[ ${app_args[arch]} != "arm64-v8a"* ]] && [[ ${app_args[arch]} != "arm-v7a"* ]]; then abort "wrong arch '${app_args[arch]}' for '$table_name'" fi @@ -169,8 +170,7 @@ if [ -z "$(ls -A1 ${BUILD_DIR})" ]; then abort "All builds failed."; fi log "\nInstall [Microg](https://github.com/ReVanced/GmsCore/releases) for non-root YouTube and YT Music APKs" log "Use [zygisk-detach](https://github.com/j-hc/zygisk-detach) to detach root ReVanced YouTube and YT Music from Play Store" -log "\n[revanced-magisk-module](https://github.com/j-hc/revanced-magisk-module)" -log "\nChangelog:" +log "\n[revanced-magisk-module](https://github.com/j-hc/revanced-magisk-module)\n" log "$(cat $TEMP_DIR/*-rv/changelog.md)" SKIPPED=$(cat $TEMP_DIR/skipped 2>/dev/null || :) diff --git a/utils.sh b/utils.sh index fdd3494..77b4c0b 100755 --- a/utils.sh +++ b/utils.sh @@ -5,13 +5,12 @@ TEMP_DIR="temp" BIN_DIR="bin" BUILD_DIR="build" -if [ "${GITHUB_TOKEN:-}" ]; then GH_HEADER="Authorization: token ${GITHUB_TOKEN}"; else GH_HEADER=; fi +if [ "${GITHUB_TOKEN-}" ]; then GH_HEADER="Authorization: token ${GITHUB_TOKEN}"; else GH_HEADER=; fi NEXT_VER_CODE=${NEXT_VER_CODE:-$(date +'%Y%m%d')} REBUILD=${REBUILD:-false} OS=$(uname -o) # -------------------- json/toml -------------------- -json_get() { grep -o "\"${1}\":[^\"]*\"[^\"]*\"" | sed -E 's/".*".*"(.*)"/\1/'; } toml_prep() { __TOML__=$(tr -d '\t\r' <<<"$1" | tr "'" '"' | grep -o '^[^#]*' | grep -v '^$' | sed -r 's/(\".*\")|\s*/\1/g; 1i []'); } toml_get_table_names() { local tn @@ -31,60 +30,55 @@ toml_get() { pr() { echo -e "\033[0;32m[+] ${1}\033[0m"; } epr() { echo >&2 -e "\033[0;31m[-] ${1}\033[0m" - if [ "${GITHUB_REPOSITORY:-}" ]; then echo -e "::error::utils.sh [-] ${1}\n"; fi + if [ "${GITHUB_REPOSITORY-}" ]; then echo -e "::error::utils.sh [-] ${1}\n"; fi } abort() { - epr "ABORT: ${1:-}" + epr "ABORT: ${1-}" exit 1 } get_rv_prebuilts() { - local integrations_src=$1 patches_src=$2 integrations_ver=$3 patches_ver=$4 cli_src=$5 cli_ver=$6 - local patches_dir=${patches_src%/*} - patches_dir=${TEMP_DIR}/${patches_dir,,}-rv - local integrations_dir=${integrations_src%/*} - integrations_dir=${TEMP_DIR}/${integrations_dir,,}-rv - local cli_dir=${cli_src%/*} - cli_dir=${TEMP_DIR}/${cli_dir,,}-rv - mkdir -p "$patches_dir" "$integrations_dir" "$cli_dir" - + local cli_src=$1 cli_ver=$2 integrations_src=$3 integrations_ver=$4 patches_src=$5 patches_ver=$6 pr "Getting prebuilts (${patches_src%/*})" >&2 - local rv_cli_url rv_integrations_url rv_patches rv_patches_dl rv_patches_url rv_patches_json - - local rv_cli_rel="https://api.github.com/repos/${cli_src}/releases/" - if [ "$cli_ver" ]; then rv_cli_rel+="tags/${cli_ver}"; else rv_cli_rel+="latest"; fi - local rv_integrations_rel="https://api.github.com/repos/${integrations_src}/releases/" - if [ "$integrations_ver" ]; then rv_integrations_rel+="tags/${integrations_ver}"; else rv_integrations_rel+="latest"; fi - local rv_patches_rel="https://api.github.com/repos/${patches_src}/releases/" - if [ "$patches_ver" ]; then rv_patches_rel+="tags/${patches_ver}"; else rv_patches_rel+="latest"; fi - rv_cli_url=$(gh_req "$rv_cli_rel" - | json_get 'browser_download_url' | grep -E '\.jar$') || return 1 - local rv_cli_jar="${cli_dir}/${rv_cli_url##*/}" - echo "CLI: $(cut -d/ -f4 <<<"$rv_cli_url")/$(cut -d/ -f9 <<<"$rv_cli_url") " >"$patches_dir/changelog.md" - - rv_integrations_url=$(gh_req "$rv_integrations_rel" - | json_get 'browser_download_url' | grep -E '\.apk$') || return 1 - local rv_integrations_apk="${integrations_dir}/${rv_integrations_url##*/}" - echo "Integrations: $(cut -d/ -f4 <<<"$rv_integrations_url")/$(cut -d/ -f9 <<<"$rv_integrations_url") " >>"$patches_dir/changelog.md" - - rv_patches=$(gh_req "$rv_patches_rel" -) || return 1 - # rv_patches_changelog=$(json_get 'body' <<<"$rv_patches" | sed 's/\(\\n\)\+/\\n/g') - rv_patches_dl=$(json_get 'browser_download_url' <<<"$rv_patches") - rv_patches_json="${patches_dir}/patches-$(json_get 'tag_name' <<<"$rv_patches").json" - rv_patches_url=$(grep -E '\.jar$' <<<"$rv_patches_dl") - local rv_patches_jar="${patches_dir}/${rv_patches_url##*/}" - [ -f "$rv_patches_jar" ] || REBUILD=true - local nm - nm=$(cut -d/ -f9 <<<"$rv_patches_url") - echo "Patches: $(cut -d/ -f4 <<<"$rv_patches_url")/$nm " >>"$patches_dir/changelog.md" - # shellcheck disable=SC2001 - echo -e "[Changelog](https://github.com/${patches_src}/releases/tag/v$(sed 's/.*-\(.*\)\..*/\1/' <<<"$nm"))\n" >>"$patches_dir/changelog.md" - # echo -e "\n${rv_patches_changelog//# [/### [}\n---" >>"$patches_dir/changelog.md" - - dl_if_dne "$rv_cli_jar" "$rv_cli_url" >&2 || return 1 - dl_if_dne "$rv_integrations_apk" "$rv_integrations_url" >&2 || return 1 - dl_if_dne "$rv_patches_jar" "$rv_patches_url" >&2 || return 1 - dl_if_dne "$rv_patches_json" "$(grep 'json' <<<"$rv_patches_dl")" >&2 || return 1 - - echo "$rv_cli_jar" "$rv_integrations_apk" "$rv_patches_jar" "$rv_patches_json" + for f in "${TEMP_DIR}"/*-rv; do : >"${f}/changelog.md"; done + for src_ver in "$cli_src CLI $cli_ver" "$integrations_src Integrations $integrations_ver" "$patches_src Patches $patches_ver"; do + set -- $src_ver + local src=$1 tag=$2 ver=${3-} ext + if [ "$tag" = "CLI" ] || [ "$tag" = "Patches" ]; then + ext="jar" + elif [ "$tag" = "Integrations" ]; then + ext="apk" + else abort unreachable; fi + local dir=${src%/*} + dir=${TEMP_DIR}/${dir,,}-rv + [ -d "$dir" ] || mkdir "$dir" + + local rv_rel="https://api.github.com/repos/${src}/releases/" + if [ "$ver" ]; then rv_rel+="tags/${ver}"; else rv_rel+="latest"; fi + + local resp asset url name file + resp=$(gh_req "$rv_rel" -) || return 1 + asset=$(jq -e -r ".assets[] | select(.name | endswith(\"$ext\"))" <<<"$resp") || return 1 + url=$(jq -r .url <<<"$asset") + name=$(jq -r .name <<<"$asset") + file="${dir}/${name}" + [ -f "$file" ] || REBUILD=true + + echo "$tag: $(cut -d/ -f5 <<<"$url")/${name} " >>"$dir/changelog.md" + gh_dl "$file" "$url" >&2 || return 1 + echo -n "$file " + if [ "$tag" = "Patches" ]; then + local tag_name + tag_name=$(jq -r '.tag_name' <<<"$resp") + name="patches-${tag_name}.json" + file="${dir}/${name}" + url=$(jq -e -r '.assets[] | select(.name | endswith("json")) | .url' <<<"$resp") || return 1 + gh_dl "$file" "$url" >&2 || return 1 + echo -n "$file " + echo -e "[Changelog](https://github.com/${src}/releases/tag/${tag_name})\n" >>"$dir/changelog.md" + fi + done + echo } get_prebuilts() { @@ -98,10 +92,10 @@ get_prebuilts() { HTMLQ="${BIN_DIR}/htmlq/htmlq-x86_64" fi mkdir -p ${MODULE_TEMPLATE_DIR}/bin/arm64 ${MODULE_TEMPLATE_DIR}/bin/arm ${MODULE_TEMPLATE_DIR}/bin/x86 ${MODULE_TEMPLATE_DIR}/bin/x64 - dl_if_dne "${MODULE_TEMPLATE_DIR}/bin/arm64/cmpr" "https://github.com/j-hc/cmpr/releases/latest/download/cmpr-arm64-v8a" - dl_if_dne "${MODULE_TEMPLATE_DIR}/bin/arm/cmpr" "https://github.com/j-hc/cmpr/releases/latest/download/cmpr-armeabi-v7a" - dl_if_dne "${MODULE_TEMPLATE_DIR}/bin/x86/cmpr" "https://github.com/j-hc/cmpr/releases/latest/download/cmpr-x86" - dl_if_dne "${MODULE_TEMPLATE_DIR}/bin/x64/cmpr" "https://github.com/j-hc/cmpr/releases/latest/download/cmpr-x86_64" + gh_dl "${MODULE_TEMPLATE_DIR}/bin/arm64/cmpr" "https://github.com/j-hc/cmpr/releases/latest/download/cmpr-arm64-v8a" + gh_dl "${MODULE_TEMPLATE_DIR}/bin/arm/cmpr" "https://github.com/j-hc/cmpr/releases/latest/download/cmpr-armeabi-v7a" + gh_dl "${MODULE_TEMPLATE_DIR}/bin/x86/cmpr" "https://github.com/j-hc/cmpr/releases/latest/download/cmpr-x86" + gh_dl "${MODULE_TEMPLATE_DIR}/bin/x64/cmpr" "https://github.com/j-hc/cmpr/releases/latest/download/cmpr-x86_64" } config_update() { @@ -125,7 +119,8 @@ config_update() { fi else sources[$PATCHES_SRC]=0 - if ! last_patches_url=$(gh_req "https://api.github.com/repos/${PATCHES_SRC}/releases/latest" - 2>&1 | json_get 'browser_download_url' | grep -E '\.jar$'); then + if ! last_patches_url=$(gh_req "https://api.github.com/repos/${PATCHES_SRC}/releases/latest" - 2>&1 \ + | jq -e -r '.assets[] | select(.name | endswith("jar")) | .name'); then abort oops fi last_patches=${last_patches_url##*/} @@ -146,21 +141,29 @@ config_update() { } _req() { - if [ "$2" = - ]; then - wget -nv -O "$2" --header="$3" "$1" + local ip="$1" op="$2" + shift 2 + if [ "$op" = - ]; then + wget -nv -O "$op" "$@" "$ip" else local dlp - dlp="$(dirname "$2")/tmp.$(basename "$2")" + dlp="$(dirname "$op")/tmp.$(basename "$op")" if [ -f "$dlp" ]; then while [ -f "$dlp" ]; do sleep 1; done return fi - wget -nv -O "$dlp" --header="$3" "$1" || return 1 - mv -f "$dlp" "$2" + wget -nv -O "$dlp" "$@" "$ip" || return 1 + mv -f "$dlp" "$op" + fi +} +req() { _req "$1" "$2" --header="User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0"; } +gh_req() { _req "$1" "$2" --header="$GH_HEADER"; } +gh_dl() { + if [ ! -f "$1" ]; then + pr "Getting '$1' from '$2'" + _req "$2" "$1" --header="$GH_HEADER" --header="Accept: application/octet-stream" fi } -req() { _req "$1" "$2" "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:108.0) Gecko/20100101 Firefox/108.0"; } -gh_req() { _req "$1" "$2" "$GH_HEADER"; } log() { echo -e "$1 " >>"build.md"; } get_largest_ver() { @@ -180,7 +183,7 @@ get_patch_last_supported_ver() { exc_sel=$(list_args "$3" | sed 's/.*/\.name != &/' | paste -sd '~' | sed 's/~/ and /g' || :) inc_sel=${inc_sel:-false} if [ "$4" = false ]; then inc_sel="${inc_sel} or .use==true"; fi - if ! vs=$(jq -r ".[] + if ! vs=$(jq -e -r ".[] | select(.compatiblePackages // [] | .[] | .name==\"${1}\") | select(${inc_sel}) | select(${exc_sel:-true}) @@ -190,17 +193,6 @@ get_patch_last_supported_ver() { tr -d ' ,\t[]"' <<<"$vs" | sort -u | grep -v '^$' | get_largest_ver || : } -dl_if_dne() { - [ "${DRYRUN:-}" ] && { - : >"$1" - return 0 - } - if [ ! -f "$1" ]; then - pr "Getting '$1' from '$2'" - req "$2" "$1" - fi -} - isoneof() { local i=$1 v shift @@ -212,10 +204,6 @@ isoneof() { dl_apkmirror() { local url=$1 version=${2// /-} output=$3 arch=$4 dpi=$5 apkorbundle=APK if [ "$arch" = "arm-v7a" ]; then arch="armeabi-v7a"; fi - [ "${DRYRUN:-}" ] && { - : >"$output" - return 0 - } local apparch resp node app_table dlurl="" if [ "$arch" = all ]; then apparch=(universal noarch 'arm64-v8a + armeabi-v7a') @@ -228,9 +216,9 @@ dl_apkmirror() { node=$($HTMLQ "div.table-row.headerFont:nth-last-child($n)" -r "span:nth-child(n+3)" <<<"$resp") if [ -z "$node" ]; then break; fi app_table=$($HTMLQ --text --ignore-whitespace <<<"$node") - if [ "$(sed -n 3p <<<"$app_table")" = "$apkorbundle" ] && { [ "$apkorbundle" = BUNDLE ] || - { [ "$apkorbundle" = APK ] && [ "$(sed -n 6p <<<"$app_table")" = "$dpi" ] && - isoneof "$(sed -n 4p <<<"$app_table")" "${apparch[@]}"; }; }; then + if [ "$(sed -n 3p <<<"$app_table")" = "$apkorbundle" ] && { [ "$apkorbundle" = BUNDLE ] \ + || { [ "$apkorbundle" = APK ] && [ "$(sed -n 6p <<<"$app_table")" = "$dpi" ] \ + && isoneof "$(sed -n 4p <<<"$app_table")" "${apparch[@]}"; }; }; then dlurl=$($HTMLQ --base https://www.apkmirror.com --attribute href "div:nth-child(1) > a:nth-child(1)" <<<"$node") break fi @@ -239,7 +227,7 @@ dl_apkmirror() { resp=$(req "$dlurl" -) fi url=$(echo "$resp" | $HTMLQ --base https://www.apkmirror.com --attribute href "a.btn") || return 1 - if [ "$apkorbundle" = BUNDLE ] && [[ "$url" != *"&forcebaseapk=true" ]]; then url="${url}&forcebaseapk=true"; fi + if [ "$apkorbundle" = BUNDLE ] && [[ $url != *"&forcebaseapk=true" ]]; then url="${url}&forcebaseapk=true"; fi url=$(req "$url" - | $HTMLQ --base https://www.apkmirror.com --attribute href "span > a[rel = nofollow]") || return 1 req "$url" "$output" } @@ -318,11 +306,7 @@ patch_apk() { --keystore-entry-password=123456789 --keystore-password=123456789 --signer=jhc --keystore-entry-alias=jhc --options=options.json" if [ "$OS" = Android ]; then cmd+=" --custom-aapt2-binary=${AAPT2}"; fi pr "$cmd" - if [ "${DRYRUN:-}" = true ]; then - cp -f "$stock_input" "$patched_apk" - else - eval "$cmd" - fi + eval "$cmd" [ -f "$patched_apk" ] } @@ -475,7 +459,7 @@ build_rv() { "${app_name} ${args[rv_brand]}" \ "$version" \ "${app_name} ${args[rv_brand]} Magisk module" \ - "https://raw.githubusercontent.com/${GITHUB_REPOSITORY:-}/update/${upj}" \ + "https://raw.githubusercontent.com/${GITHUB_REPOSITORY-}/update/${upj}" \ "$base_template" local module_output="${app_name_l}-${rv_brand_f}-magisk-v${version_f}-${arch_f}.zip"