2019-06-21 09:03:24 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import shutil
|
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
from ctcconfig import *
|
|
|
|
|
|
|
|
print("Welcome to CutTheCord CI :)")
|
|
|
|
|
|
|
|
BRANCH = sys.argv[1]
|
|
|
|
PATCHES = sys.argv[2:]
|
|
|
|
FORCE = False
|
|
|
|
DEFAULT_PATCHES = ["necessary", "branding", "customversion"]
|
|
|
|
OPTIONAL_PATCHES = ["necessary", "blobs"]
|
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
def xmlpatch(patch_file, workdir, patch_name=""):
|
|
|
|
if not os.path.exists(patch_file):
|
|
|
|
# Allow missing patches
|
|
|
|
return
|
|
|
|
|
|
|
|
print(f"Applying {patch_file}")
|
|
|
|
|
|
|
|
subprocess.run(f"java -cp /opt/ctc/tools/xml-patch.jar com.github.dnault.xmlpatch.BatchPatcher --patch {patch_file} --srcdir {workdir}",
|
|
|
|
shell=True, check=True, cwd=workdir)
|
2019-06-21 09:03:24 +00:00
|
|
|
|
|
|
|
def patch(patch_file, workdir, patch_name=""):
|
|
|
|
if not os.path.exists(patch_file):
|
2021-08-10 18:56:58 +00:00
|
|
|
# Allow missing patches
|
|
|
|
return
|
2019-06-21 09:03:24 +00:00
|
|
|
|
2019-07-11 12:03:23 +00:00
|
|
|
print(f"Applying {patch_file}")
|
|
|
|
|
2019-06-21 09:03:24 +00:00
|
|
|
subprocess.run(f"patch -p1 --no-backup-if-mismatch -i {patch_file}",
|
|
|
|
shell=True, check=True, cwd=workdir)
|
|
|
|
|
|
|
|
|
|
|
|
# Wipe and recreate the working folder
|
|
|
|
if os.path.exists(WORK_APK_PATH):
|
|
|
|
shutil.rmtree(WORK_APK_PATH)
|
|
|
|
|
2020-05-25 00:19:19 +00:00
|
|
|
os.environ["DISTOK_EXTRACTED_DISCORD_PATH"] = WORK_APK_PATH
|
|
|
|
|
2019-06-21 09:03:24 +00:00
|
|
|
os.makedirs(WORK_FOLDER, exist_ok=True)
|
|
|
|
|
2021-08-06 17:07:52 +00:00
|
|
|
with open(os.path.join(REPO_FOLDER, "resources/patchport-state.json")) as f:
|
2020-05-28 12:56:38 +00:00
|
|
|
STATE = json.load(f)
|
|
|
|
VERSION = STATE["versioncode"]
|
2019-06-21 09:03:24 +00:00
|
|
|
|
|
|
|
BASE_APK_PATH = os.path.join(WORK_FOLDER, f"discord-base-{VERSION}")
|
|
|
|
|
|
|
|
# Prepare names of input and output APKs
|
|
|
|
INPUT_FILE = os.path.join(DISTOK_FOLDER, "android",
|
|
|
|
f"{PACKAGE_ID}-{VERSION}.apk")
|
|
|
|
# OUTPUT_FILE = os.path.join(RESULT_FOLDER,
|
|
|
|
# f"cutthecord-{VERSION}-{'_'.join(PATCHES)}.apk")
|
|
|
|
OUTPUT_FILE = os.path.join(RESULT_FOLDER,
|
|
|
|
f"cutthecord-{VERSION}-{BRANCH}.apk")
|
|
|
|
|
|
|
|
# Add necessary patches to the list of patches that will be applied
|
|
|
|
# Important to have this after the output file name generation
|
|
|
|
# otherwise it'll include it, which is not wanted
|
2019-07-11 12:20:03 +00:00
|
|
|
PATCHES = DEFAULT_PATCHES + PATCHES
|
2019-06-21 09:03:24 +00:00
|
|
|
|
|
|
|
print(f"Branch: {BRANCH}, output name: {OUTPUT_FILE}")
|
|
|
|
|
|
|
|
# Check if the version is already patched, if it is exit
|
|
|
|
if not FORCE and os.path.exists(OUTPUT_FILE):
|
|
|
|
print("This version is already patched, bye!")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2019-06-21 09:12:54 +00:00
|
|
|
if DO_GITPULL:
|
|
|
|
# Update cutthecord
|
|
|
|
subprocess.run("git pull", shell=True, cwd=REPO_FOLDER)
|
2019-06-21 09:03:24 +00:00
|
|
|
|
|
|
|
# Extract the APK if it's not already extracted to base cache
|
|
|
|
if not os.path.exists(BASE_APK_PATH):
|
2021-02-21 19:33:49 +00:00
|
|
|
subprocess.run(f"{APKTOOL_BIN} d --no-dummy {INPUT_FILE} -o {BASE_APK_PATH} -f",
|
2019-06-21 09:03:24 +00:00
|
|
|
shell=True,
|
|
|
|
cwd=WORK_FOLDER)
|
|
|
|
|
|
|
|
# Copy the base cache to work on it
|
|
|
|
shutil.copytree(BASE_APK_PATH, WORK_APK_PATH)
|
|
|
|
|
|
|
|
# Go through patches and apply every single one of them
|
|
|
|
for patch_name in PATCHES:
|
2021-08-10 18:56:58 +00:00
|
|
|
pre_script = os.path.join(REPO_FOLDER, "resources/patches", patch_name, f"{VERSION}-pre.sh")
|
2019-10-08 02:17:06 +00:00
|
|
|
if os.path.exists(pre_script):
|
2021-08-10 18:56:58 +00:00
|
|
|
subprocess.run(f"bash {pre_script}", shell=True, cwd=WORK_APK_PATH)
|
|
|
|
pre_script = os.path.join(REPO_FOLDER, "resources/xmlpatches", patch_name, f"{VERSION}-pre.sh")
|
|
|
|
if os.path.exists(pre_script):
|
|
|
|
subprocess.run(f"bash {pre_script}", shell=True, cwd=WORK_APK_PATH)
|
2019-10-08 02:17:06 +00:00
|
|
|
|
2019-06-21 09:03:24 +00:00
|
|
|
# Apply custom emoji patches
|
|
|
|
if patch_name in ["mutant", "blobs"]:
|
|
|
|
print(f"Applying {patch_name} emoji patch")
|
2021-08-10 18:56:58 +00:00
|
|
|
patch_script = os.path.join(REPO_FOLDER, "resources/patches", patch_name, "emojireplace.py")
|
|
|
|
subprocess.run(f"{PYTHON_BIN} {patch_script}", shell=True, cwd=WORK_APK_PATH)
|
2019-06-21 09:03:24 +00:00
|
|
|
|
|
|
|
# Apply custom emoji patches
|
|
|
|
elif patch_name == "customtheme":
|
|
|
|
print(f"Applying splash patch")
|
2021-08-10 18:56:58 +00:00
|
|
|
splash = os.path.join(REPO_FOLDER, "resources/patches", patch_name, "asset_loading.png")
|
|
|
|
patch_script = os.path.join(REPO_FOLDER, "resources/patches", patch_name, "fixsplash.sh")
|
2019-06-21 09:03:24 +00:00
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
subprocess.run(f"bash {patch_script} {splash}", shell=True, cwd=WORK_APK_PATH)
|
2019-06-21 09:03:24 +00:00
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
patch_file = os.path.join(REPO_FOLDER, "resources/patches", patch_name, f"{VERSION}.patch")
|
|
|
|
xml_patch_file = os.path.join(REPO_FOLDER, "resources/xmlpatches", patch_name, f"{VERSION}.xml")
|
2019-06-21 09:03:24 +00:00
|
|
|
|
|
|
|
# Apply custom version patches
|
|
|
|
if patch_name == "customversion":
|
|
|
|
print(f"Applying custom version")
|
2021-08-10 18:56:58 +00:00
|
|
|
patch_script = os.path.join(REPO_FOLDER, "resources/patches", patch_name, "addpatch.py")
|
2019-06-21 09:03:24 +00:00
|
|
|
|
|
|
|
subprocess.run(f"{PYTHON_BIN} {patch_script} {patch_file} "
|
|
|
|
f"{' '.join(PATCHES)}",
|
|
|
|
shell=True,
|
|
|
|
cwd=WORK_APK_PATH)
|
|
|
|
|
|
|
|
patch(patch_file.replace(".patch", "-custom.patch"), WORK_APK_PATH)
|
|
|
|
# Apply branding patches
|
|
|
|
elif patch_name == "branding":
|
|
|
|
print(f"Applying branding icon patch")
|
|
|
|
if BRANCH in ICONS:
|
|
|
|
shutil.copyfile(ICONS[BRANCH],
|
2021-08-10 18:56:58 +00:00
|
|
|
os.path.join(WORK_APK_PATH, "res", "mipmap-xxxhdpi", "logo_debug.png"))
|
2019-06-21 09:03:24 +00:00
|
|
|
elif "default" in ICONS:
|
|
|
|
shutil.copyfile(ICONS["default"],
|
2021-08-10 18:56:58 +00:00
|
|
|
os.path.join(WORK_APK_PATH, "res", "mipmap-xxxhdpi", "logo_debug.png"))
|
2019-06-21 09:03:24 +00:00
|
|
|
|
2019-06-22 14:29:53 +00:00
|
|
|
if BRANCH in DYN_ICONS:
|
|
|
|
shutil.copyfile(DYN_ICONS[BRANCH]["bg"],
|
2021-08-10 18:56:58 +00:00
|
|
|
os.path.join(WORK_APK_PATH, "res", "mipmap-xxxhdpi", "ic_launcher_background.png"))
|
2019-06-22 14:29:53 +00:00
|
|
|
shutil.copyfile(DYN_ICONS[BRANCH]["fg"],
|
2021-08-10 18:56:58 +00:00
|
|
|
os.path.join(WORK_APK_PATH, "res", "mipmap-xxxhdpi", "ic_launcher_foreground.png"))
|
2019-06-22 14:29:53 +00:00
|
|
|
elif "default" in ICONS:
|
|
|
|
shutil.copyfile(DYN_ICONS["default"]["bg"],
|
2021-08-10 18:56:58 +00:00
|
|
|
os.path.join(WORK_APK_PATH, "res", "mipmap-xxxhdpi", "ic_launcher_background.png"))
|
2019-06-22 14:29:53 +00:00
|
|
|
shutil.copyfile(DYN_ICONS["default"]["fg"],
|
2021-08-10 18:56:58 +00:00
|
|
|
os.path.join(WORK_APK_PATH, "res", "mipmap-xxxhdpi", "ic_launcher_foreground.png"))
|
2019-06-22 14:29:53 +00:00
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
patch_script = os.path.join(REPO_FOLDER, "resources/patches", patch_name, "customicon.sh")
|
2019-06-21 09:03:24 +00:00
|
|
|
|
|
|
|
subprocess.run(f"bash {patch_script}", shell=True, cwd=WORK_APK_PATH)
|
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
patch_script = os.path.join(REPO_FOLDER, "resources/patches", patch_name, "customdynamicicon.sh")
|
2019-06-22 14:29:53 +00:00
|
|
|
|
|
|
|
subprocess.run(f"bash {patch_script}", shell=True, cwd=WORK_APK_PATH)
|
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
patch_script = os.path.join(REPO_FOLDER, "resources/patches", patch_name, "addpatch.py")
|
2019-06-21 09:03:24 +00:00
|
|
|
|
|
|
|
# Hell code
|
|
|
|
app_name = APP_NAMES.get(BRANCH, APP_NAMES.get("default", "CutTheCord"))
|
|
|
|
|
|
|
|
subprocess.run(f"{PYTHON_BIN} {patch_script} "
|
|
|
|
f"{patch_file} {app_name} {BRANCH}",
|
|
|
|
shell=True,
|
|
|
|
cwd=WORK_APK_PATH)
|
|
|
|
|
|
|
|
patch(patch_file.replace(".patch", "-custom.patch"), WORK_APK_PATH)
|
|
|
|
elif patch_name in ["bettertm", "bettertmlight"]:
|
|
|
|
print(f"Applying bettertm emoji patch")
|
2021-08-10 18:56:58 +00:00
|
|
|
patch_dir = os.path.join(REPO_FOLDER, "resources/patches", patch_name)
|
|
|
|
patch_script = os.path.join(REPO_FOLDER, "resources/patches", patch_name, "bettertm.sh")
|
|
|
|
subprocess.run(f"bash {patch_script} {patch_dir}", shell=True, cwd=WORK_APK_PATH)
|
2019-06-21 09:03:24 +00:00
|
|
|
# Apply custom ringtone
|
|
|
|
elif patch_name == "customring":
|
|
|
|
print(f"Applying custom ringtone")
|
|
|
|
if BRANCH in RINGTONES:
|
|
|
|
CUSTOM_RINGTONE = RINGTONES[BRANCH]
|
|
|
|
else:
|
|
|
|
CUSTOM_RINGTONE = RINGTONES["default"]
|
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
shutil.copyfile(CUSTOM_RINGTONE, os.path.join(WORK_APK_PATH, "res", "raw", "call_ringing.mp3"))
|
2019-06-21 09:03:24 +00:00
|
|
|
# Apply custom fonts
|
|
|
|
elif patch_name == "customfont":
|
|
|
|
print(f"Applying custom font")
|
|
|
|
if BRANCH in FONTS:
|
|
|
|
fonts = FONTS[BRANCH]
|
|
|
|
else:
|
|
|
|
fonts = FONTS["default"]
|
|
|
|
|
|
|
|
for font in fonts:
|
2021-08-10 18:56:58 +00:00
|
|
|
shutil.copyfile(fonts[font], os.path.join(WORK_APK_PATH, "res", "font", font))
|
2019-06-21 09:03:24 +00:00
|
|
|
# Apply any other patches
|
|
|
|
else:
|
2019-10-08 02:14:07 +00:00
|
|
|
patch(patch_file, WORK_APK_PATH, patch_name)
|
2021-08-10 18:56:58 +00:00
|
|
|
xmlpatch(xml_patch_file, WORK_APK_PATH, patch_name)
|
2019-10-08 02:14:07 +00:00
|
|
|
|
2021-08-10 18:56:58 +00:00
|
|
|
post_script = os.path.join(REPO_FOLDER, "resources/patches", patch_name, f"{VERSION}-post.sh")
|
2019-10-08 02:17:06 +00:00
|
|
|
if os.path.exists(post_script):
|
2021-08-10 18:56:58 +00:00
|
|
|
subprocess.run(f"bash {post_script}", shell=True, cwd=WORK_APK_PATH)
|
|
|
|
post_script = os.path.join(REPO_FOLDER, "resources/xmlpatches", patch_name, f"{VERSION}-post.sh")
|
|
|
|
if os.path.exists(post_script):
|
|
|
|
subprocess.run(f"bash {post_script}", shell=True, cwd=WORK_APK_PATH)
|
2019-10-08 02:08:26 +00:00
|
|
|
|
2019-06-21 09:03:24 +00:00
|
|
|
# Pack the APK
|
|
|
|
subprocess.run(f"{APKTOOL_BIN} b discord",
|
|
|
|
shell=True,
|
|
|
|
cwd=WORK_FOLDER)
|
|
|
|
|
|
|
|
APK_PATH = os.path.join(WORK_FOLDER, "discord",
|
|
|
|
"dist", f"{PACKAGE_ID}-{VERSION}.apk")
|
|
|
|
|
|
|
|
# Sign the APK
|
2019-11-26 18:21:08 +00:00
|
|
|
if DO_APKSIGNER:
|
2019-11-26 22:53:26 +00:00
|
|
|
subprocess.run(f"apksigner sign --ks {KEYSTORE_FILE} "
|
2019-11-26 18:21:08 +00:00
|
|
|
f"--ks-key-alias {KEYSTORE_ALIAS} "
|
2019-11-26 22:53:26 +00:00
|
|
|
f"--ks-pass pass:{KEYSTORE_PASS} "
|
2019-11-26 18:21:08 +00:00
|
|
|
f"{APK_PATH}",
|
|
|
|
shell=True,
|
|
|
|
cwd=WORK_FOLDER)
|
|
|
|
else:
|
|
|
|
subprocess.run(f"jarsigner -storepass {KEYSTORE_PASS} -keystore "
|
|
|
|
f"{KEYSTORE_FILE} {APK_PATH} {KEYSTORE_ALIAS}",
|
|
|
|
shell=True,
|
|
|
|
cwd=WORK_FOLDER)
|
2019-06-21 09:03:24 +00:00
|
|
|
|
|
|
|
# Copy the result file
|
|
|
|
shutil.copyfile(APK_PATH, OUTPUT_FILE)
|
|
|
|
|
|
|
|
if DO_FDROID:
|
|
|
|
# Do fdroid magic
|
|
|
|
subprocess.run(f"fdroid update -c",
|
|
|
|
shell=True,
|
|
|
|
cwd=FDROID_FOLDER)
|
|
|
|
|
|
|
|
# Wipe caches
|
|
|
|
shutil.rmtree(WORK_APK_PATH)
|
|
|
|
print("CutTheCord CI has fulfilled its purpose.")
|