ReVancedBuilder/build_revanced.sh

243 lines
9.6 KiB
Bash
Raw Normal View History

2022-10-09 17:37:34 -05:00
#!/bin/bash
2022-10-11 22:52:34 -05:00
# Run only one instance of this script at one time
[ "${BKLOCKER}" != "running" ] && exec env BKLOCKER="running" flock -en "/tmp/revanced-builder.lock" "$0" "$@" || :
2022-10-11 20:14:42 -05:00
2022-10-18 21:04:53 -05:00
# Log everything to a logfile inside logs/
2022-10-18 20:50:18 -05:00
log_file="$1/logs/$(date '+%s')"
[ -d "$1" ] && mkdir -p "$1/logs" && exec > >(tee "$log_file") 2>&1
2022-10-11 18:29:50 -05:00
# Get timestamp
timestamp=$(date '+%s')
# Set working directory and current directory
2022-10-11 20:25:44 -05:00
if [ -d "$1" ]; then
WDIR="$1"
else
echo "Working directory not provided"
2023-01-28 12:59:25 -06:00
exit 1
2022-10-11 20:25:44 -05:00
fi
# File containing all patches
patch_file="$WDIR./chosen_patches.txt"
2022-10-13 01:27:01 -05:00
# Returns if $1 is less than $2
ver_less_than() {
2022-11-23 14:52:46 -06:00
# Strip letters from version name
ver1=$(echo $1 | sed 's/[a-zA-Z]*//g')
ver2=$(echo $2 | sed 's/[a-zA-Z]*//g')
[ $(echo $ver1$'\n'$ver2 | sort -V | tail -n1) != $ver1 ] && echo true || echo false
2022-10-13 01:27:01 -05:00
}
# Make sure to work in the script directory
SDIR="$(dirname -- "$( readlink -f -- "$0"; )";)"
cd "$SDIR"
2022-10-09 17:37:34 -05:00
# Get line numbers where included & excluded patches start from.
# We rely on the hardcoded messages to get the line numbers using grep
excluded_start="$(grep -n -m1 'EXCLUDE PATCHES' "$patch_file" | cut -d':' -f1)"
included_start="$(grep -n -m1 'INCLUDE PATCHES' "$patch_file" | cut -d':' -f1)"
# Get everything but hashes from between the EXCLUDE PATCH & INCLUDE PATCH line
# Note: '^[^#[:blank:]]' ignores starting hashes and/or blank characters i.e, whitespace & tab excluding newline
excluded_patches="$(tail -n +$excluded_start $patch_file | head -n "$(( included_start - excluded_start ))" | grep '^[^#[:blank:]]')"
# Get everything but hashes starting from INCLUDE PATCH line until EOF
included_patches="$(tail -n +$included_start $patch_file | grep '^[^#[:blank:]]')"
# Array for storing patches
declare -a patches
# # Artifacts associative array aka dictionary
# declare -A artifacts
2022-10-09 17:37:34 -05:00
# artifacts["revanced-cli.jar"]="revanced/revanced-cli revanced-cli .jar"
# artifacts["revanced-integrations.apk"]="revanced/revanced-integrations app-release-unsigned .apk"
# artifacts["revanced-patches.jar"]="revanced/revanced-patches revanced-patches .jar"
# artifacts["apkeep"]="EFForg/apkeep apkeep-x86_64-unknown-linux-gnu"
# Required artifacts in the format repository-name_filename
artifacts="revanced/revanced-cli:revanced-cli.jar revanced/revanced-integrations:revanced-integrations.apk revanced/revanced-patches:revanced-patches.jar TeamVanced/VancedMicroG:microg.apk"
2022-10-09 17:37:34 -05:00
## Functions
# get_artifact_download_url() {
# # Usage: get_download_url <repo_name> <artifact_name> <file_type>
# local api_url result
# api_url="https://api.github.com/repos/$1/releases/latest"
# # shellcheck disable=SC2086
# result=$(curl -s $api_url | jq ".assets[] | select(.name | contains(\"$2\") and contains(\"$3\") and (contains(\".sig\") | not)) | .browser_download_url")
# echo "${result:1:-1}"
# }
2022-10-09 17:37:34 -05:00
# Function for populating patches array, using a function here reduces redundancy & satisfies DRY principals
populate_patches() {
# Note: <<< defines a 'here-string'. Meaning, it allows reading from variables just like from a file
while read -r patch; do
patches+=("$1 $patch")
done <<< "$2"
}
## Main
# cleanup to fetch new revanced on next run
2022-10-11 20:25:44 -05:00
if [[ "$2" == "clean" ]]; then
2022-10-09 22:10:11 -05:00
rm -f revanced-cli.jar revanced-integrations.apk revanced-patches.jar
2022-10-09 17:37:34 -05:00
exit
fi
2022-10-11 20:25:44 -05:00
if [[ "$2" == "experimental" ]]; then
2022-10-09 17:37:34 -05:00
EXPERIMENTAL="--experimental"
fi
2022-10-11 18:29:50 -05:00
# Set flag to determine if a build should happen or not
flag=false
2022-10-13 23:34:05 -05:00
check_flag=false
2022-10-11 18:29:50 -05:00
2022-10-11 19:42:28 -05:00
# Get inside the working directory
cd "$WDIR"
2022-10-18 19:55:25 -05:00
echo "$(date) | Starting check..."
2022-10-11 19:42:28 -05:00
2022-10-09 17:37:34 -05:00
# Fetch all the dependencies
2023-01-28 12:59:25 -06:00
try=0
while : ; do
try=$(($try+1))
echo $try
[ $try -gt 10 ] && echo "API error!" && exit 2
curl -X 'GET' 'https://releases.revanced.app/tools' -H 'accept: application/json' -o latest_versions.json
cat latest_versions.json | jq -e '.error' >/dev/null || break
echo "API failure, trying again. $((10-$try)) tries left..."
sleep 10
done
for artifact in $artifacts; do
2022-10-11 18:29:50 -05:00
#Check for updates
repo=$(echo $artifact | cut -d ':' -f1)
name=$(echo $artifact | cut -d ':' -f2)
basename=$(echo $repo | cut -d '/' -f2)
2022-10-18 19:55:25 -05:00
echo "Checking $basename"
version_present=$(jq -r ".\"$basename\"" versions.json)
data="$(jq -r ".tools[] | select((.repository == \"$repo\") and (.content_type | contains(\"archive\")))" latest_versions.json)"
version=$(echo "$data" | jq -r '.version')
2022-10-13 01:27:01 -05:00
if [[ $(ver_less_than $version_present $version) == true || ! -f $name || $2 == force ]]; then
2022-10-13 23:19:22 -05:00
if [[ $2 == checkonly ]]; then
2022-10-18 19:55:25 -05:00
echo "[checkonly] $basename has an update ($version_present -> $version)"
2022-10-13 23:34:05 -05:00
check_flag=true
2022-10-13 23:19:22 -05:00
continue
fi
2022-10-18 19:55:25 -05:00
echo "Downloading $name"
2022-10-13 01:27:01 -05:00
[[ $name == microg.apk && -f $name && $2 != force ]] && microg_updated=true
2022-10-09 17:37:34 -05:00
# shellcheck disable=SC2086,SC2046
curl -sLo "$name" "$(echo "$data" | jq -r '.browser_download_url')"
jq ".\"$basename\" = \"$version\"" versions.json > versions.json.tmp && mv versions.json.tmp versions.json
2022-10-18 20:50:18 -05:00
echo "Upgraded $basename from $version_present to $version"
2022-10-11 18:29:50 -05:00
flag=true
2022-10-09 17:37:34 -05:00
fi
done
2022-10-13 01:36:54 -05:00
[[ ! -f com.google.android.youtube.apk || ! -f com.google.android.apps.youtube.music.apk ]] && flag=true
2022-10-11 18:29:50 -05:00
# Exit if no updates happened
2022-10-13 23:19:22 -05:00
if [[ $flag == false && $2 != force ]]; then
2022-10-13 23:34:05 -05:00
if [[ $check_flag == false ]]; then
2022-10-18 19:55:25 -05:00
echo "Nothing to update"
2022-10-13 23:19:22 -05:00
else
2022-10-13 23:34:05 -05:00
"$SDIR/download_apkmirror.sh" "$WDIR" checkonly
2022-10-13 23:19:22 -05:00
fi
2022-10-18 19:55:25 -05:00
echo "--------------------"$'\n'"--------------------"
2022-10-11 18:29:50 -05:00
exit
fi
# Download required apk files
2022-10-13 01:27:01 -05:00
"$SDIR/download_apkmirror.sh" "$WDIR" $2
2022-10-11 18:29:50 -05:00
# # Fetch microG
# chmod +x apkeep
2022-10-09 17:37:34 -05:00
# if [ ! -f "vanced-microG.apk" ]; then
# # Vanced microG 0.2.24.220220
# VMG_VERSION="0.2.24.220220"
2022-10-09 17:37:34 -05:00
2022-10-18 19:55:25 -05:00
# echo "Downloading Vanced microG"
# ./apkeep -a com.mgoogle.android.gms@$VMG_VERSION .
# mv com.mgoogle.android.gms@$VMG_VERSION.apk vanced-microG.apk
# jq ".\"vanced-microG\" = \"$VMG_VERSION\"" versions.json > versions.json.tmp && mv versions.json.tmp versions.json
# fi
2022-10-09 17:37:34 -05:00
# If the variables are NOT empty, call populate_patches with proper arguments
[[ ! -z "$excluded_patches" ]] && populate_patches "-e" "$excluded_patches"
[[ ! -z "$included_patches" ]] && populate_patches "-i" "$included_patches"
echo "************************************"
echo "Building YouTube APK"
echo "************************************"
if [ -f "com.google.android.youtube.apk" ]; then
2022-10-09 17:53:35 -05:00
# echo "Building Root APK"
# java -jar revanced-cli.jar -m revanced-integrations.apk -b revanced-patches.jar --mount \
# -e microg-support ${patches[@]} \
# $EXPERIMENTAL \
2022-10-12 19:00:55 -05:00
# -a com.google.android.youtube.apk -o build/revanced-yt-root.apk
2022-10-18 19:55:25 -05:00
echo "Building Non-root APK"
2022-10-09 17:37:34 -05:00
java -jar revanced-cli.jar -m revanced-integrations.apk -b revanced-patches.jar \
${patches[@]} \
$EXPERIMENTAL \
2022-10-12 19:00:55 -05:00
-a com.google.android.youtube.apk -o revanced-yt-nonroot.apk
2022-10-09 17:37:34 -05:00
else
2022-10-18 19:55:25 -05:00
echo "Cannot find YouTube APK, skipping build"
2022-10-09 17:37:34 -05:00
fi
echo ""
echo "************************************"
echo "Building YouTube Music APK"
echo "************************************"
if [ -f "com.google.android.apps.youtube.music.apk" ]; then
2022-10-09 17:53:35 -05:00
# echo "Building Root APK"
2022-10-13 01:27:01 -05:00
# java -jar revanced-cli.jar -m revanced-integrations.apk -b revanced-patches.jar --mount \
2022-10-09 17:53:35 -05:00
# -e microg-support ${patches[@]} \
# $EXPERIMENTAL \
2022-10-12 19:00:55 -05:00
# -a com.google.android.apps.youtube.music.apk -o build/revanced-ytm-root.apk
2022-10-18 19:55:25 -05:00
echo "Building Non-root APK"
2022-10-13 01:27:01 -05:00
java -jar revanced-cli.jar -m revanced-integrations.apk -b revanced-patches.jar \
2022-10-09 17:37:34 -05:00
${patches[@]} \
$EXPERIMENTAL \
2022-10-12 19:00:55 -05:00
-a com.google.android.apps.youtube.music.apk -o revanced-ytm-nonroot.apk
2022-10-09 17:37:34 -05:00
else
2022-10-18 19:55:25 -05:00
echo "Cannot find YouTube Music APK, skipping build"
2022-10-09 17:37:34 -05:00
fi
2022-10-11 18:29:50 -05:00
2022-10-11 20:14:42 -05:00
# Rename files
2022-10-12 19:00:55 -05:00
mv revanced-yt-nonroot.apk YouTube_ReVanced_nonroot_$timestamp.apk
mv revanced-ytm-nonroot.apk YouTube_Music_ReVanced_nonroot_$timestamp.apk
# mv revanced-yt-root.apk YouTube_ReVanced_root_$timestamp.apk
# mv revanced-ytm-root.apk YouTube_Music_ReVanced_root_$timestamp.apk
2022-10-11 20:14:42 -05:00
2022-10-11 18:29:50 -05:00
# Send telegram message about the new build
2022-10-18 19:55:25 -05:00
echo "Sending messages to telegram"
2022-10-11 21:08:02 -05:00
# telegram-upload uses personal account, hence bypassing 50 MB max upload limit of bots
2022-12-06 20:11:53 -06:00
# channel_address=$(cat channel_address | sed -z '$ s/\n$//')
# /home/sintan/.local/bin/telegram-upload YouTube_ReVanced_nonroot_$timestamp.apk YouTube_Music_ReVanced_nonroot_$timestamp.apk --to "$channel_address" --caption "" && sent=true
2022-10-11 21:08:02 -05:00
# telegram.sh uses bot account, but it supports formatted messages
2022-10-12 18:07:42 -05:00
msg=$(cat versions.json | tail -n+2 | head -n-1 | cut -c3- | sed "s/\"//g" | sed "s/,//g" | sed "s/com.google.android.apps.youtube.music/YouTube Music/" \
2022-10-18 19:16:12 -05:00
| sed "s/com.google.android.youtube/YouTube/" | sed "s/VancedMicroG/Vanced microG/" | sed "s/revanced-/ReVanced /g" | sed "s/patches/Patches/" \
| sed "s/cli/CLI/" | sed "s/integrations/Integrations/" | awk 1 ORS=$'\n') # I know, it's a hacky solution
2022-12-06 20:11:53 -06:00
# [ $sent ] &&
2023-01-07 06:09:28 -06:00
./telegram.sh -T "⚙⚙⚙ Build Details ⚙⚙⚙" -M "$msg"$'\n'"Timestamp: $timestamp"$'\n'"⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯"
[ $microg_updated ] && ./telegram.sh -M "_An update of microg was published. Please download it from the link in the pinned message._"
2022-10-11 18:29:50 -05:00
2022-10-18 20:50:18 -05:00
# Do some cleanup, keep only the last 3 build's worth of files and a week worth of logs
2022-10-11 18:29:50 -05:00
mkdir -p archive
2022-10-12 19:00:55 -05:00
mv YouTube_ReVanced_nonroot_$timestamp.apk archive/
mv YouTube_Music_ReVanced_nonroot_$timestamp.apk archive/
2022-10-12 18:30:14 -05:00
find ./archive -maxdepth 1 -type f -printf '%Ts\t%P\n' \
2022-10-12 18:19:38 -05:00
| sort -rn \
2022-10-18 20:10:12 -05:00
| tail -n +7 \
2022-10-12 18:19:38 -05:00
| cut -f2- \
2022-10-18 20:10:12 -05:00
| xargs -r -I {} rm "./archive/{}"
2022-10-18 20:50:18 -05:00
find ./logs -mtime +7 -exec rm {} \;
2022-10-20 21:03:54 -05:00
# Run a custom post script, if available
[ -f post_script.sh ] && ./post_script.sh $timestamp
2022-10-20 21:03:54 -05:00
echo "Done!"$'\n'"************************************"