"""Python adapter to the shared Dart SDK/environment launcher.""" import argparse import json import os from pathlib import Path import shutil import sys from common.process import ROOT, cli, run def host_bash(): if os.name == 'nt': # WSL's system32/bash.exe cannot consume native Windows paths. git = shutil.which('git') if git: for relative in ('bin/bash.exe', 'usr/bin/bash.exe'): candidate = Path(git).resolve().parent.parent / relative if candidate.is_file(): return str(candidate) bash = shutil.which('bash') if bash and Path(bash).resolve().parent != Path(os.environ.get('WINDIR', 'C:/Windows')) / 'System32': return bash return None return shutil.which('bash') def project_dart(root=ROOT, environment=None): environment = os.environ if environment is None else environment ci = any(environment.get(key, '').lower() in {'true', '1', 'yes'} for key in ('CI', 'GITEA_ACTIONS', 'GITHUB_ACTIONS')) if ci: flutter = shutil.which('flutter', path=environment.get('PATH', '')) if not flutter: raise RuntimeError('CI requires a complete Flutter SDK on PATH.') sdk_bin = Path(flutter).resolve().parent else: sdk_bin = Path(root) / '.fvm/flutter_sdk/bin' dart = sdk_bin / 'cache/dart-sdk/bin' / ('dart.exe' if os.name == 'nt' else 'dart') if not dart.is_file(): raise RuntimeError('Project Flutter SDK is missing. Run fvm use first.') return str(dart) def tool_command(tool, arguments=(), *, cwd=None, root=ROOT, environment=None): root = Path(root).resolve() cwd = root if cwd is None else Path(cwd).resolve() return [project_dart(root, environment), str(root / 'tool/project_tools.dart'), '--directory', str(cwd), tool, *map(str, arguments)] def project_environment(*, root=ROOT, environment=None): root = Path(root).resolve() result = run( [project_dart(root, environment), str(root / 'tool/project_tools.dart'), 'environment'], cwd=root, env=environment, check=False, capture_output=True, text=True, encoding='utf-8', timeout=30) if result.returncode: # Diagnostics contain no configuration values; do not expose the JSON pipe. raise RuntimeError(result.stderr.strip() or 'Cannot load project environment.') return json.loads(result.stdout) def run_tool(tool, arguments=(), *, root=ROOT, cwd=None, **kwargs): root = Path(root).resolve() cwd = root if cwd is None else Path(cwd).resolve() command = tool_command(tool, arguments, root=root, cwd=cwd, environment=kwargs.get('env')) return run(command, cwd=cwd, **kwargs) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('tool', choices=['flutter', 'dart', 'exec']) parser.add_argument('arguments', nargs=argparse.REMAINDER) args = parser.parse_args() return run_tool(args.tool, args.arguments, cwd=Path.cwd()).returncode if __name__ == '__main__': sys.exit(cli(main))