ReVancedBuilder/build_revanced.sh

325 lines
12 KiB
Bash
Raw Permalink Normal View History

2023-03-22 18:28:43 -05:00
#!/usr/bin/env bash
2022-10-09 17:37:34 -05:00
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-11 18:29:50 -05:00
# Get timestamp
2023-08-05 21:49:08 -05:00
timestamp=$(date '+%Y%m%d%H%M%S')
2022-10-11 18:29:50 -05:00
2023-04-23 18:28:58 -05:00
# Log everything to a logfile inside logs/
log_file="$1/logs/$timestamp.log"
2023-04-23 18:28:58 -05:00
[ -d "$1" ] && mkdir -p "$1/logs" && exec > >(tee "$log_file") 2>&1
2022-10-11 18:29:50 -05:00
# 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
2023-03-22 18:28:43 -05:00
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
2023-03-20 12:19:02 -05:00
SDIR="$(dirname -- "$(readlink -f -- "$0")")"
cd "$SDIR"
2022-10-09 17:37:34 -05:00
2023-03-22 18:28:43 -05:00
# Read the settings
if [ -f "$WDIR/build_settings" ]; then
source "$WDIR/build_settings"
else
if [ -f "./build_settings"]; then
cp ./build_settings "$WDIR/build_settings"
source ./build_settings
else
echo "Could not find the build_settings file!"
fi
fi
2023-03-20 12:19:02 -05:00
# Get line numbers where included & excluded patches start from.
2022-10-09 17:37:34 -05:00
# 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
2023-03-20 12:19:02 -05:00
excluded_patches="$(tail -n +$excluded_start $patch_file | head -n "$((included_start - excluded_start))" | grep '^[^#[:blank:]]')"
2022-10-09 17:37:34 -05:00
# 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
# Required artifacts in the format repository-name_filename
2023-03-20 12:19:02 -05:00
artifacts="revanced/revanced-cli:revanced-cli.jar revanced/revanced-integrations:revanced-integrations.apk revanced/revanced-patches:revanced-patches.jar inotia00/VancedMicroG:microg.apk"
2022-10-09 17:37:34 -05:00
## Functions
# 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")
2023-03-20 12:19:02 -05:00
done <<<"$2"
2022-10-09 17:37:34 -05:00
}
## 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..."
if [[ $2 != buildonly ]]; then
2023-08-06 15:55:54 -05:00
# Create a new versions file, if needed
[ -f versions.json ] || echo "{}" >versions.json
cp versions.json versions-new.json
# Fetch all the dependencies
try=0
while :; do
try=$(($try + 1))
[ $try -gt 10 ] && echo "API error!" && exit 2
2023-07-01 22:59:28 -05:00
curl -s -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
#Check for updates
repo=$(echo $artifact | cut -d ':' -f1)
name=$(echo $artifact | cut -d ':' -f2)
basename=$(echo $repo | cut -d '/' -f2)
echo "Checking $basename"
version_present=$(jq -r ".\"$basename\"" versions.json)
2023-07-28 12:02:31 -05:00
[[ "$version_present" == "null" ]] && version_present=0
data="$(jq -r ".tools[] | select((.repository == \"$repo\") and (.content_type | contains(\"archive\")))" latest_versions.json)"
[[ $name == microg.apk ]] && version=$(curl -s "https://api.github.com/repos/$repo/releases/latest" | jq -r '.tag_name') || version=$(echo "$data" | jq -r '.version')
if [[ $(ver_less_than $version_present $version) == true || ! -f $name || $2 == force ]]; then
if [[ $2 == checkonly ]]; then
echo "[checkonly] $basename has an update ($version_present -> $version)"
check_flag=true
continue
fi
echo "Downloading $name"
[[ $name == microg.apk && -f $name && $2 != force ]] && microg_updated=true
# shellcheck disable=SC2086,SC2046
[[ $name == microg.apk ]] && download_link="https://github.com/$repo/releases/latest/download/$name" || download_link="$(echo "$data" | jq -r '.browser_download_url')"
curl -sLo "$name" "$download_link"
2023-08-06 16:10:51 -05:00
jq ".\"$basename\" = \"$version\"" versions-new.json >versions-tmp.json && mv versions-tmp.json versions-new.json
echo "Upgraded $basename from $version_present to $version"
flag=true
2022-10-13 23:19:22 -05:00
fi
done
2022-10-09 17:37:34 -05:00
[[ ! -f com.google.android.youtube.apk || ! -f com.google.android.apps.youtube.music.apk ]] && flag=true
2022-10-13 01:36:54 -05:00
# Exit if no updates happened
if [[ $flag == false && $2 != force ]]; then
if [[ $check_flag == false ]]; then
echo "Nothing to update"
else
2023-07-28 11:55:30 -05:00
"$SDIR/download_apk.sh" "$WDIR" checkonly
fi
echo "--------------------"$'\n'"--------------------"
exit
2022-10-13 23:19:22 -05:00
fi
2022-10-11 18:29:50 -05:00
# Download required apk files
2023-07-28 11:55:30 -05:00
"$SDIR/download_apk.sh" "$WDIR"
fi
2022-10-11 18:29:50 -05:00
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"
2023-04-23 18:52:32 -05:00
# Variable to flag errors
error=0
2023-03-22 18:28:43 -05:00
# Functions for building the APKs
build_yt_nonroot() {
echo "************************************"
echo "Building YouTube APK"
echo "************************************"
if [ -f "com.google.android.youtube.apk" ]; then
echo "Building Non-root APK"
java -jar revanced-cli.jar -m revanced-integrations.apk -b revanced-patches.jar \
${patches[@]} \
$EXPERIMENTAL \
-a com.google.android.youtube.apk -o revanced-yt-nonroot.apk
else
echo "Cannot find YouTube APK, skipping build"
fi
echo ""
echo "************************************"
# Rename files
2023-04-23 18:52:32 -05:00
mv revanced-yt-nonroot.apk YouTube_ReVanced_nonroot_$timestamp.apk || error=1
2023-03-22 18:28:43 -05:00
}
build_yt_root() {
echo "************************************"
echo "Building YouTube APK"
echo "************************************"
if [ -f "com.google.android.youtube.apk" ]; then
echo "Building Root APK"
java -jar revanced-cli.jar -m revanced-integrations.apk -b revanced-patches.jar --mount \
-e microg-support ${patches[@]} \
$EXPERIMENTAL \
-a com.google.android.youtube.apk -o revanced-yt-root.apk
else
echo "Cannot find YouTube APK, skipping build"
fi
echo ""
echo "************************************"
# Rename files
2023-04-23 18:52:32 -05:00
mv revanced-yt-root.apk YouTube_ReVanced_root_$timestamp.apk || error=1
2023-03-22 18:28:43 -05:00
}
build_ytm_nonroot() {
echo "Building YouTube Music APK"
echo "************************************"
if [ -f "com.google.android.apps.youtube.music.apk" ]; then
echo "Building Non-root APK"
java -jar revanced-cli.jar -m revanced-integrations.apk -b revanced-patches.jar \
${patches[@]} \
$EXPERIMENTAL \
-a com.google.android.apps.youtube.music.apk -o revanced-ytm-nonroot.apk
else
echo "Cannot find YouTube Music APK, skipping build"
fi
2022-10-11 18:29:50 -05:00
2023-03-22 18:28:43 -05:00
# Rename files
2023-04-23 18:52:32 -05:00
mv revanced-ytm-nonroot.apk YouTube_Music_ReVanced_nonroot_$timestamp.apk || error=1
2023-03-22 18:28:43 -05:00
}
build_ytm_root() {
echo "Building YouTube Music APK"
echo "************************************"
if [ -f "com.google.android.apps.youtube.music.apk" ]; then
echo "Building Root APK"
java -jar revanced-cli.jar -m revanced-integrations.apk -b revanced-patches.jar --mount \
-e microg-support ${patches[@]} \
$EXPERIMENTAL \
-a com.google.android.apps.youtube.music.apk -o revanced-ytm-root.apk
else
echo "Cannot find YouTube Music APK, skipping build"
fi
# Rename files
2023-04-23 18:52:32 -05:00
mv revanced-ytm-root.apk YouTube_Music_ReVanced_root_$timestamp.apk || error=1
2023-03-22 18:28:43 -05:00
}
2023-07-02 00:20:08 -05:00
telegram_send_msg() {
# telegram.sh uses bot account, but it supports formatted messages
[[ "$TELEGRAM_TOKEN" == "" || "$TELEGRAM_CHAT" == "" ]] && echo "Please provide valid channel address in the settings!"
./telegram.sh -t "$TELEGRAM_TOKEN" -c "$TELEGRAM_CHAT" -T "⚙⚙⚙ Build Details ⚙⚙⚙" -M "$1"$'\n'"Timestamp: $timestamp"$'\n'"⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯"
}
gotify_send_msg() {
curl -s -X POST "$GOTIFY_URL/message?token=$GOTIFY_TOKEN" \
-F "title=⚙⚙⚙ Build Details ⚙⚙⚙" -F "message=$1" -F "priority=5"
}
ntfy_send_msg() {
curl -s -H "Icon: https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Revanced-logo-round.svg/240px-Revanced-logo-round.svg.png" \
-H "Title: ⚙⚙⚙ ReVanced Build ⚙⚙⚙" \
-d "$1" \
"$NTFY_URL/$NTFY_TOPIC"
}
2023-03-22 18:28:43 -05:00
# Check the config and build accordingly
$YT_NONROOT && build_yt_nonroot
$YT_ROOT && build_yt_root
$YTM_NONROOT && build_ytm_nonroot
$YTM_ROOT && build_ytm_root
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-11 21:08:02 -05:00
2023-04-23 18:52:32 -05:00
if [ $error == 1 ]; then
echo "There was an error while building!"
msg="There was an error during the build process! Please take a look at the logs."$'\n'"Timestamp: $timestamp"
2023-07-02 00:20:08 -05:00
$TG_NOTIFICATIONS && telegram_send_msg "$msg"
$GOTIFY_NOTIFICATIONS && gotify_send_msg "$msg"
$NTFY_NOTIFICATIONS && ntfy_send_msg "$msg"
2023-08-08 15:25:33 -05:00
[[ $2 != buildonly ]] && mv versions-new.json versions-fail.json || rm versions-new.json
2023-04-23 18:52:32 -05:00
exit 4
2023-08-08 15:25:33 -05:00
else
2023-08-09 20:58:48 -05:00
mv versions.json versions-old.json
2023-08-08 15:25:33 -05:00
mv versions-new.json versions.json
2023-04-23 18:52:32 -05:00
fi
2023-03-22 18:28:43 -05:00
if $TG_UPLOAD; then
2023-04-02 00:15:37 -05:00
echo "Uploading to telegram"
2023-03-22 18:28:43 -05:00
# telegram-upload uses personal account, hence bypassing 50 MB max upload limit of bots
[ "$CHANNEL_ADDRESS" == "" ] && echo "Please provide valid channel address in the settings!"
/home/sintan/.local/bin/telegram-upload YouTube_ReVanced_nonroot_$timestamp.apk YouTube_Music_ReVanced_nonroot_$timestamp.apk --to "$CHANNEL_ADDRESS" --caption "" && sent=true
fi
2022-10-11 21:08:02 -05:00
# Create the message to be sent
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/" |
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
2023-03-22 18:28:43 -05:00
if $TG_NOTIFICATIONS; then
2023-04-02 00:15:37 -05:00
echo "Sending messages to telegram"
2023-07-02 00:20:08 -05:00
telegram_send_msg "$msg"
[ $microg_updated ] && telegram_send_msg "_An update of microg was published._"
2023-03-22 18:28:43 -05:00
fi
2022-10-11 18:29:50 -05:00
if $GOTIFY_NOTIFICATIONS; then
echo "Sending messages to Gotify"
MESSAGE="$msg"$'\n'"Timestamp: $timestamp"
2023-07-02 00:20:08 -05:00
gotify_send_msg "$MESSAGE"
[ $microg_updated ] && gotify_send_msg "An update of microg was published."
fi
2023-06-23 19:51:02 -05:00
if $NTFY_NOTIFICATIONS; then
echo "Sending messages to ntfy.sh"
MESSAGE="$msg"$'\n'"Timestamp: $timestamp"
2023-07-02 00:20:08 -05:00
ntfy_send_msg "$MESSAGE"
[ $microg_updated ] && ntfy_send_msg "An update of microg was published."
2023-06-23 19:51:02 -05:00
fi
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
2023-03-22 18:28:43 -05:00
mv *ReVanced_*_$timestamp.apk archive/
2023-03-20 12:19:02 -05:00
find ./archive -maxdepth 1 -type f -printf '%Ts\t%P\n' |
sort -rn |
tail -n +7 |
cut -f2- |
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
2023-03-20 12:19:02 -05:00
echo "Done!"$'\n'"************************************"