"""Build from an isolated path; source stays in its original Chinese directory.""" from __future__ import annotations import argparse import os from pathlib import Path import shutil import subprocess import tempfile def main(): parser = argparse.ArgumentParser() parser.add_argument('--work-root', type=Path, required=True) parser.add_argument('--jdk', type=Path, required=True) parser.add_argument('--sdk', type=Path, required=True) parser.add_argument('--gradle', type=Path, help='Optional verified Gradle executable; otherwise use Wrapper') parser.add_argument('tasks', nargs='*', default=[':sharedCore:jvmTest', ':androidApp:assembleDebug']) args = parser.parse_args() # Keep an explicitly supplied ASCII junction spelling. Resolving it first # would expose the Chinese workspace path to AGP despite the alias. root = Path(os.path.abspath(args.work_root)) if not str(root).isascii(): parser.error('work-root must use ASCII characters') if not (args.jdk / 'bin/java.exe').is_file() or not (args.sdk / 'platforms').is_dir(): parser.error('JDK or SDK is missing') root.mkdir(parents=True, exist_ok=True) destination = Path(tempfile.mkdtemp(prefix='qms-build-', dir=root)) source = Path(__file__).resolve().parents[1] / '安卓程序源代码' shutil.copytree(source, destination, dirs_exist_ok=True, ignore=shutil.ignore_patterns('build', '.gradle', '.kotlin', 'local.properties')) environment = os.environ.copy() environment.update(JAVA_HOME=str(args.jdk.resolve()), ANDROID_HOME=str(args.sdk.resolve())) executable = args.gradle.resolve() if args.gradle else destination / 'gradlew.bat' result = subprocess.run([str(executable), '--console', 'plain', *args.tasks], cwd=destination, env=environment) # Keep this exact directory for logs, reports and APK retrieval, even on failure. print(f'Build directory: {destination}', flush=True) return result.returncode if __name__ == '__main__': raise SystemExit(main())