forked from worklouder/input-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch-app.py
More file actions
342 lines (271 loc) · 10.9 KB
/
Copy pathpatch-app.py
File metadata and controls
342 lines (271 loc) · 10.9 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env python3
import os
import re
import sys
from pathlib import Path
USAGE = "usage: patch-app.py <unpacked-app-dir> <patch-dir>"
NAVBAR_CLASS_PREFIX = "_navbar_"
NAVBAR_HEIGHT_FALLBACK_PX = 50
NAVBAR_HEIGHT_PLACEHOLDER = "__TITLEBAR_HEIGHT__"
RELEASE_REPO_PLACEHOLDER = "__RELEASE_REPO__"
DEFAULT_RELEASE_REPO = "SilkePilon/input-linux"
PREAMBLE_ALREADY_APPLIED = "enable-transparent-visuals"
EDGE_JS_ALREADY_APPLIED = ".NET runtime not available"
TRAY_ICON_ALREADY_APPLIED = '"win32"?"tray_icon.ico"'
TRAY_GUARD_ALREADY_APPLIED = "|tray_service| failed to create tray"
WINDOW_CHROME_ALREADY_APPLIED = "autoHideMenuBar:!0"
WINDOW_BRIDGE_ALREADY_APPLIED = "inputLinuxWindow"
MAIN_MODULES = (
"linux-window-controls.js",
"linux-app-menu.js",
"linux-udev.js",
"smoke-test.js",
)
failures = []
notes = []
def note(message):
notes.append(message)
print(f" {message}")
def fail(message):
failures.append(message)
print(f" FAILED: {message}", file=sys.stderr)
def read(path):
return path.read_text(encoding="utf-8")
def write(path, text):
path.write_text(text, encoding="utf-8")
def substitute(text, pattern, replacement, description, expected=1):
new_text, count = re.subn(pattern, replacement, text, count=expected)
if count == 0:
fail(f"{description}: pattern not found")
return text
note(f"{description}: patched ({count} site{'s' if count != 1 else ''})")
return new_text
def fragment_marker(tag, stem):
if tag == "head":
return f'<meta name="input-linux-patch" content="{stem}">'
return f'<div hidden data-input-linux-patch="{stem}"></div>'
def prepend_linux_preamble(app_dir, patch_dir):
target = app_dir / "dist-electron/main/index.js"
content = read(target)
if PREAMBLE_ALREADY_APPLIED in content:
note("linux preamble: already present, skipping")
return
preamble = read(patch_dir / "dist-electron/main/index.js").rstrip("\n")
write(target, preamble + "\n" + content)
note("linux preamble: prepended to dist-electron/main/index.js")
def release_repo():
configured = os.environ.get("RELEASE_REPO") or os.environ.get("GITHUB_REPOSITORY")
return configured if configured and "/" in configured else DEFAULT_RELEASE_REPO
def inject_html_fragments(app_dir, patch_dir, navbar_height):
target = app_dir / "dist/index.html"
html = read(target)
for tag in ("head", "body"):
fragments = sorted((patch_dir / "dist").glob(f"index-{tag}-*.html"))
if not fragments:
fail(f"html fragments: no index-{tag}-*.html found in {patch_dir / 'dist'}")
continue
for fragment in fragments:
marker = fragment_marker(tag, fragment.stem)
if marker in html:
note(f"{fragment.name}: already injected, skipping")
continue
if f"</{tag}>" not in html:
fail(f"{fragment.name}: no </{tag}> in dist/index.html")
continue
content = (
read(fragment)
.replace(NAVBAR_HEIGHT_PLACEHOLDER, str(navbar_height))
.replace(RELEASE_REPO_PLACEHOLDER, release_repo())
)
html = html.replace(
f"</{tag}>", f"{marker}\n{content}\n </{tag}>", 1
)
note(f"{fragment.name}: injected before </{tag}>")
write(target, html)
def patch_edge_js(app_dir):
target = app_dir / "node_modules/electron-edge-js/lib/edge.js"
if not target.exists():
fail(f"edge.js: {target} does not exist")
return
content = read(target)
if EDGE_JS_ALREADY_APPLIED in content:
note("edge.js: already patched, skipping")
return
content = substitute(
content,
r"edge\s*=\s*require\(edgeNative\);",
"try {\n"
" edge = require(edgeNative);\n"
" } catch (e) {\n"
" if (process.env.EDGE_DEBUG) {\n"
" console.warn('electron-edge-js: .NET runtime not available, "
"C# interop disabled:', e.message);\n"
" }\n"
" }",
"edge.js require(edgeNative) guard",
)
content = substitute(
content,
r"exports\.func\s*=\s*function\s*\(([^)]*)\)\s*\{",
lambda m: (
f"exports.func = function ({m.group(1)}) {{\n"
" if (!edge) {\n"
" return function (input, callback) {\n"
" if (typeof callback === 'function') {\n"
" callback(new Error('electron-edge-js: .NET runtime not "
"available on this system'), null);\n"
" }\n"
" };\n"
" }"
),
"edge.js exports.func guard",
)
write(target, content)
def patch_tray(app_dir):
target = app_dir / "dist-electron/main/index.js"
content = read(target)
if TRAY_ICON_ALREADY_APPLIED in content.replace(" ", ""):
note("tray icon path: already patched, skipping")
else:
content = substitute(
content,
r'process\.platform\s*===\s*"darwin"\s*\?\s*"tray_icon_Template\.png"'
r'\s*:\s*"tray_icon\.ico"',
'process.platform==="win32"?"tray_icon.ico":"tray_icon_Template.png"',
"tray icon path (use PNG off Windows)",
)
if TRAY_GUARD_ALREADY_APPLIED in content:
note("tray creation guard: already patched, skipping")
else:
content = substitute(
content,
r"(?<![\w.$])([\w$]+)\.get\(\)\.trayService\.createNewTray\(\)",
lambda m: (
"(()=>{try{"
f"{m.group(1)}.get().trayService.createNewTray()"
"}catch(trayErr){console.error('|tray_service| failed to create tray: '"
"+trayErr)}})()"
),
"tray creation guard",
)
write(target, content)
def detect_navbar_height(app_dir):
declared_height = re.compile(
r"\." + re.escape(NAVBAR_CLASS_PREFIX) + r"[\w-]+\s*\{[^}]*?height:\s*(\d+)px"
)
for stylesheet in sorted((app_dir / "dist/assets").glob("*.css")):
match = declared_height.search(read(stylesheet))
if match:
return int(match.group(1))
note(f"navbar height: not found in stylesheets, using {NAVBAR_HEIGHT_FALLBACK_PX}px")
return NAVBAR_HEIGHT_FALLBACK_PX
def patch_window_chrome(app_dir):
target = app_dir / "dist-electron/main/index.js"
content = read(target)
if WINDOW_CHROME_ALREADY_APPLIED in content:
note("window chrome: already patched, skipping")
return
content = substitute(
content,
r'\.\.\.\s*process\.platform\s*===\s*"darwin"\s*\?\s*\{\s*backgroundColor\s*:'
r'\s*"#000000"\s*\}\s*:\s*\{\s*\}',
'...process.platform==="darwin"?{backgroundColor:"#000000"}:{},'
'...(process.platform==="linux"?{'
'backgroundColor:"#00000000",'
"transparent:!0,"
"resizable:!0,"
"autoHideMenuBar:!0,"
"frame:!1"
'}:{})',
"window chrome (frameless window on Linux)",
)
write(target, content)
def bundled_scripts(app_dir):
for source in sorted((app_dir / "dist-electron").rglob("*")):
if source.is_file() and source.suffix in (".js", ".cjs", ".mjs"):
yield source
def rename_preloads(app_dir):
preload_dir = app_dir / "dist-electron/preload"
if not preload_dir.is_dir():
fail(f"preload rename: {preload_dir} does not exist")
return
renamed = []
for esm_preload in sorted(preload_dir.glob("*.mjs")):
commonjs_preload = esm_preload.with_suffix(".cjs")
esm_preload.rename(commonjs_preload)
renamed.append(commonjs_preload.name)
note(f"preload rename: {esm_preload.name} -> {commonjs_preload.name}")
if not renamed and not any(preload_dir.glob("*.cjs")):
fail("preload rename: no preload scripts found at all")
return
for source in bundled_scripts(app_dir):
content = read(source)
repointed = re.sub(r"(preload/[\w.-]+)\.mjs", r"\1.cjs", content)
if repointed != content:
write(source, repointed)
note(f"preload rename: updated references in {source.relative_to(app_dir)}")
stale = [
str(source.relative_to(app_dir))
for source in bundled_scripts(app_dir)
if re.search(r"preload/[\w.-]+\.mjs", read(source))
]
if stale:
fail(f"preload rename: stale .mjs preload references remain in {', '.join(stale)}")
def expose_window_controls(app_dir, patch_dir):
target = app_dir / "dist-electron/preload/preload.cjs"
if not target.exists():
fail("window controls bridge: dist-electron/preload/preload.cjs does not exist")
return
content = read(target)
if WINDOW_BRIDGE_ALREADY_APPLIED in content:
note("window controls bridge: already appended, skipping")
return
bridge = read(patch_dir / "dist-electron/preload/window-controls-preload.cjs")
write(target, content.rstrip("\n") + "\n\n" + bridge)
note("window controls bridge: appended to dist-electron/preload/preload.cjs")
def install_main_modules(app_dir):
target = app_dir / "dist-electron/main/index.js"
content = read(target)
for filename in MAIN_MODULES:
if not (app_dir / "dist-electron/main" / filename).exists():
fail(f"{filename}: was not copied into the app")
continue
if f'"./{filename}"' in content or f"'./{filename}'" in content:
note(f"{filename}: already imported, skipping")
continue
content += f'\nimport "./{filename}";\n'
note(f"{filename}: imported from dist-electron/main/index.js")
write(target, content)
def main(argv):
if len(argv) != 3:
print(USAGE, file=sys.stderr)
return 2
app_dir = Path(argv[1]).resolve()
patch_dir = Path(argv[2]).resolve()
navbar_height = detect_navbar_height(app_dir)
note(f"app header height: {navbar_height}px")
for patch in (
lambda: prepend_linux_preamble(app_dir, patch_dir),
lambda: inject_html_fragments(app_dir, patch_dir, navbar_height),
lambda: patch_edge_js(app_dir),
lambda: patch_tray(app_dir),
lambda: patch_window_chrome(app_dir),
lambda: rename_preloads(app_dir),
lambda: expose_window_controls(app_dir, patch_dir),
lambda: install_main_modules(app_dir),
):
patch()
if failures:
print("", file=sys.stderr)
print(f"✗ {len(failures)} patch(es) failed to apply:", file=sys.stderr)
for failure in failures:
print(f" - {failure}", file=sys.stderr)
print(
" The upstream bundle likely changed shape. Update scripts/patch-app.py.",
file=sys.stderr,
)
return 1
print(f"✓ All patches applied ({len(notes)} steps).")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))