11import 'dart:convert' ;
22import 'dart:io' ;
3- import 'dart:isolate' ;
43
54import 'package:archive/archive_io.dart' ;
65import 'package:args/command_runner.dart' ;
6+ import 'package:http/http.dart' as http;
77import 'package:path/path.dart' as path;
88import 'package:toml/toml.dart' ;
99
@@ -41,17 +41,6 @@ class PackageCommand extends Command {
4141
4242 try {
4343 final currentPath = Directory .current.path;
44- final packagePath = (await Isolate .resolvePackageUri (
45- Uri .parse ('package:serious_python/.' )))
46- ? .path;
47-
48- if (packagePath == null ) {
49- stdout.writeln ("Cannot resolve 'serious_python' package path." );
50- exit (1 );
51- }
52-
53- final iosDistPath =
54- path.join (Directory (packagePath).parent.absolute.path, "ios" , "dist" );
5544
5645 // source dir
5746 var sourceDirPath = argResults! .rest.first;
@@ -126,10 +115,7 @@ class PackageCommand extends Command {
126115 extraArgs.add ("--pre" );
127116 }
128117
129- final pythonPath =
130- path.join (iosDistPath, "hostpython3" , "bin" , "python" );
131-
132- await runExec (pythonPath, [
118+ await runPython ([
133119 '-m' ,
134120 'pip' ,
135121 'install' ,
@@ -147,7 +133,7 @@ class PackageCommand extends Command {
147133 });
148134
149135 // compile all python code
150- await runExec (pythonPath, ['-m' , 'compileall' , '-b' , tempDir.path]);
136+ await runPython ( ['-m' , 'compileall' , '-b' , tempDir.path]);
151137 }
152138
153139 // remove unnecessary files
@@ -234,4 +220,47 @@ class PackageCommand extends Command {
234220 }
235221 return proc.exitCode;
236222 }
223+
224+ Future runPython (List <String > args,
225+ {Map <String , String >? environment}) async {
226+ var pythonDir =
227+ Directory (path.join (Directory .systemTemp.path, "hostpython3.10" ));
228+
229+ if (! await pythonDir.exists ()) {
230+ stdout
231+ .writeln ("Downloading and extracting Python into ${pythonDir .path }" );
232+
233+ var isArm64 = Platform .version.contains ("arm64" );
234+
235+ String arch = "" ;
236+ if (Platform .isMacOS && ! isArm64) {
237+ arch = 'x86_64-apple-darwin' ;
238+ } else if (Platform .isMacOS && isArm64) {
239+ arch = 'aarch64-apple-darwin' ;
240+ } else if (Platform .isLinux) {
241+ arch = 'x86_64-unknown-linux-gnu' ;
242+ } else if (Platform .isWindows) {
243+ arch = 'x86_64-pc-windows-msvc-static' ;
244+ }
245+
246+ final url =
247+ "https://github.com/indygreg/python-build-standalone/releases/download/20230507/cpython-3.10.11+20230507-$arch -install_only.tar.gz" ;
248+
249+ await pythonDir.create (recursive: true );
250+
251+ // Download the release asset
252+ var response = await http.get (Uri .parse (url));
253+ var archivePath = path.join (pythonDir.path, 'python.tar.gz' );
254+ await File (archivePath).writeAsBytes (response.bodyBytes);
255+
256+ // Extract the archive
257+ await Process .run ('tar' , ['-xzf' , archivePath, '-C' , pythonDir.path]);
258+ } else {
259+ stdout.writeln ("Python has already downloaded to ${pythonDir .path }" );
260+ }
261+
262+ // Run the python executable
263+ var pythonPath = path.join (pythonDir.path, 'python' , 'bin' , 'python3' );
264+ return await runExec (pythonPath, args, environment: environment);
265+ }
237266}
0 commit comments