Files
OrionToolbar/patches/oriontoolbar_settings_logs_patch.py
2026-10-23 09:50:39 +01:00

76 lines
3.1 KiB
Python

#!/usr/bin/env python3
from pathlib import Path
from datetime import datetime
import shutil
root = Path('/Volumes/Developer/Applications/OrionToolbar')
menu = root/'OrionToolbar/OrionMenuView.swift'
build = root/'scripts/build.sh'
test = root/'scripts/test.sh'
release = root/'scripts/release.sh'
for p in (menu,build,test,release):
if not p.is_file():
raise SystemExit(f'Missing file: {p}')
changes = {}
s = menu.read_text()
old = ''' private func showSettings() {
openSettings()
NSApplication.shared.activate(ignoringOtherApps: true)
}'''
new = ''' private func showSettings() {
// Activate before opening the Settings scene so it is not left behind
// the menu-bar popover or another application.
NSApp.activate(ignoringOtherApps: true)
openSettings()
// The Settings window is created asynchronously by SwiftUI.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
NSApp.activate(ignoringOtherApps: true)
if let window = NSApp.windows.first(where: {
$0.title.localizedCaseInsensitiveContains("settings") &&
$0.canBecomeKey
}) {
window.makeKeyAndOrderFront(nil)
}
}
}'''
if old not in s:
raise SystemExit('Settings function does not match expected source; no files changed.')
changes[menu]=s.replace(old,new,1)
s=build.read_text()
s=s.replace('mkdir -p "$ROOT/build-logs"','mkdir -p "$DERIVED/build-logs"').replace('LOG="$ROOT/build-logs/','LOG="$DERIVED/build-logs/')
if 'LOG="$DERIVED/build-logs/' not in s:
raise SystemExit('Build log path not recognized; no files changed.')
changes[build]=s
s=test.read_text()
s=s.replace('mkdir -p "$ROOT/build-logs"','mkdir -p "$ROOT/.build/build-logs"').replace('LOG="$ROOT/build-logs/','LOG="$ROOT/.build/build-logs/')
# Existing test script refers to a nonexistent capitalized Scripts folder.
s=s.replace('"$ROOT/Scripts/build.sh"','"$ROOT/scripts/build.sh"')
if 'LOG="$ROOT/.build/build-logs/' not in s:
raise SystemExit('Test log path not recognized; no files changed.')
changes[test]=s
s=release.read_text()
if 'LOG="$ROOT/.build/build-logs/' not in s:
raise SystemExit('Release log path not recognized; no files changed.')
changes[release]=s
stamp=datetime.now().strftime('%Y%m%d-%H%M%S')
for path,content in changes.items():
if path.read_text()==content:
print('Unchanged:',path.relative_to(root));continue
backup=path.with_name(f'{path.name}.bak-{stamp}')
shutil.copy2(path,backup)
path.write_text(content)
print('Updated:',path.relative_to(root),'Backup:',backup.name)
log_dir=root/'.build/build-logs'
log_dir.mkdir(parents=True,exist_ok=True)
old_dir=root/'build-logs'
if old_dir.is_dir():
for file in old_dir.iterdir():
if file.is_file() and file.suffix=='.log':
destination=log_dir/file.name
if destination.exists():
destination=log_dir/(file.stem+'-migrated-'+stamp+file.suffix)
shutil.move(str(file),str(destination))
if not any(old_dir.iterdir()):
old_dir.rmdir()
print('Build logs:',log_dir)