-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruffleapk_setup.py
137 lines (118 loc) · 5.32 KB
/
ruffleapk_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
import sys
import os.path
import re
import shutil
from pathlib import Path
import datetime
import subprocess
def readProperties():
props = {}
with open('ruffleapk.properties', 'r') as f:
for line in f:
if ':' in line and not line.startswith('#'):
name, value = line.split(':', 1)
props[name.strip().upper()] = value.strip()
return props
def patchFile(file, patches):
print(f"-- Processing {file} ...")
lines = []
with open(file, 'r') as f:
for line in f:
for pattern,replacement in patches:
if isinstance(pattern, re.Pattern):
line = pattern.sub(replacement, line)
else:
line = line.replace(pattern, replacement)
lines.append(line.rstrip())
with open(file, 'w') as f:
for line in lines:
f.write(line+"\n")
def patchWith(properties):
print(f"Patching RuffleAPK for: {properties['APP_NAME']}")
SWF_BASENAME = os.path.basename(properties['SWF_FILE'])
patchFile('app/src/main/assets/www/index.html', [
# patch assets/www/index.html <title>{APP_NAME}</title>
(re.compile(r"<title>.*</title>"), f"<title>{properties['APP_NAME']}</title>"),
# patch assets/www/index.html window.swfplayer.load("{SWF_FILE}");
(re.compile(r"window\.swfplayer\.load\(\".*\"\)"), f"window.swfplayer.load(\"{SWF_BASENAME}\")")
])
# copy to assets/www/{SWF_FILE} (remove old swf files)
print(f"-- Copying app/src/main/assets/www/{SWF_BASENAME} ...")
for filename in os.listdir('app/src/main/assets/www'):
if filename.endswith('.swf'):
os.remove('app/src/main/assets/www/'+filename)
shutil.copy2(properties['SWF_FILE'], f"app/src/main/assets/www/{SWF_BASENAME}");
patchFile('app/src/main/res/values/strings.xml', [
# patch res/values/strings.xml <string name="app_name">{APP_NAME}</string>
(re.compile(r"<string name=\"app_name\">.*</string>"), f"<string name=\"app_name\">{properties['APP_NAME']}</string>"),
])
# patch java package to {APP_PACKAGE}
print(f"-- Relocating package to {properties['APP_PACKAGE']} ...")
javabase = Path('app/src/main/java')
oldpath = list(javabase.rglob("MainActivity.java"))[0].parent
newpath = javabase / '/'.join(properties['APP_PACKAGE'].split('.'))
if not oldpath == newpath:
newpath.mkdir(0o777, True, True) # make parents, existing is ok
for file in os.listdir(oldpath):
shutil.move(oldpath / file, newpath)
# get existing package
oldpackage = '.'.join(oldpath.relative_to(javabase).parts)
for file in os.listdir(newpath):
patchFile(newpath / file, [(oldpackage, properties['APP_PACKAGE'])])
code = datetime.datetime.today().strftime('%y%m%d%H') # for uint32 this holds until the year 2099
patchFile('app/build.gradle.kts', [
# patch build.gradle.kts namespace = "{APP_PACKAGE}"
(re.compile(r"\bnamespace *= *\".*\""), f"namespace = \"{properties['APP_PACKAGE']}\""),
# patch build.gradle.kts applicationId = "{APP_PACKAGE}"
(re.compile(r"\bapplicationId *= *\".*\""), f"applicationId = \"{properties['APP_PACKAGE']}\""),
# patch build.gradle.kts versionCode = 1
(re.compile(r"\bversionCode *= *[0-9]+"), f"versionCode = {code}"),
# patch build.gradle.kts versionName = "1.0"
(re.compile(r"\bversionName *= *\".*\""), f"versionName = \"{properties['APP_VERSION']}\""),
])
patchFile('settings.gradle.kts', [
# patch settings.gradle.kts rootProject.name = "{APP_NAME}"
(re.compile(r"\brootProject.name *= *\".*\""), f"rootProject.name = \"{properties['APP_NAME']}\""),
])
# TODO patch images
def invokeBuild():
print("-- Building APK image ...")
ret = subprocess.run(["gradlew", "clean", "build"], shell=True).returncode
if ret == 0:
shutil.copy2('app/build/outputs/apk/release/app-release-unsigned.apk', 'ruffle_unsigned.apk')
print("-- DONE")
else:
print(f"-- Build return with exit code {ret}!")
def main():
properties = readProperties()
if not 'APP_NAME' in properties:
print('APP_NAME is not set')
exit()
if not 'APP_PACKAGE' in properties:
print('APP_PACKAGE is not set')
exit()
if not re.compile(r"[a-zA-Z]\w+(\.[a-zA-Z]\w+)+").fullmatch(properties['APP_PACKAGE']):
print('APP_PACKAGE needs to be a unique reverse-domain-ish')
exit()
if not 'APP_VERSION' in properties:
print('APP_VERSION is not set')
exit()
if not re.compile(r"[0-9]+\.[0-9]+(\.[0-9]+)?").fullmatch(properties['APP_VERSION']):
print('APP_VERSION needs to be X.Y.Z')
exit()
if not 'SWF_FILE' in properties:
print('SWF_FILE is not set')
exit()
if not os.path.isfile(properties['SWF_FILE']):
print(f"SWF_FILE {properties['SWF_FILE']} does not exist, check your path")
exit()
if 'LOGO_FILE' in properties and not os.path.isfile(properties['LOGO_FILE']):
print(f"LOGO_FILE {properties['LOGO_FILE']} is set but does not exist")
exit()
print("NOTE: Logos are currently not processed")
patchWith(properties)
invokeBuild()
if __name__ == "__main__":
os.chdir(sys.path[0])
main()