|
| 1 | +import 'dart:io'; |
| 2 | +import 'package:native_assets_cli/native_assets_cli.dart'; |
| 3 | +import 'package:http/http.dart' as http; |
| 4 | +import 'package:path/path.dart' as path; |
| 5 | + |
| 6 | +import '../sdk_downloads.dart'; |
| 7 | + |
| 8 | +late File logFile; |
| 9 | + |
| 10 | +final logs = <(DateTime, String)>[]; |
| 11 | +void log(String msg) { |
| 12 | + logs.add((DateTime.now(), msg)); |
| 13 | + if (!logFile.parent.existsSync()) { |
| 14 | + logFile.parent.createSync(); |
| 15 | + } |
| 16 | + |
| 17 | + if (logFile.existsSync()) { |
| 18 | + logFile.deleteSync(); |
| 19 | + } |
| 20 | + logFile.createSync(); |
| 21 | + logFile.writeAsStringSync(logs |
| 22 | + .map<String>((rec) => '[${rec.$1.toIso8601String()}] ${rec.$2}') |
| 23 | + .toList() |
| 24 | + .join('\n\n')); |
| 25 | +} |
| 26 | + |
| 27 | +Future<void> main(List<String> args) async { |
| 28 | + await build(args, (buildConfig, buildOutput) async { |
| 29 | + logFile = File( |
| 30 | + path.joinAll([ |
| 31 | + Directory.current.path, // root dir of app using `mediapipe-task-xyz` |
| 32 | + 'build/${buildConfig.dryRun ? "dryrun" : "live-run"}-build-log.txt', |
| 33 | + ]), |
| 34 | + ); |
| 35 | + |
| 36 | + log(args.join(' ')); |
| 37 | + final String targetOS = buildConfig.targetOS.toString(); |
| 38 | + log('targetOS: $targetOS'); |
| 39 | + |
| 40 | + log('dir.current: ${Directory.current.absolute.path}'); |
| 41 | + |
| 42 | + // Throw if target runtime is unsupported. |
| 43 | + if (!sdkDownloadUrls.containsKey(targetOS)) { |
| 44 | + throw Exception('Unsupported target OS: $targetOS. ' |
| 45 | + 'Supported values are: ${sdkDownloadUrls.keys.toSet()}'); |
| 46 | + } |
| 47 | + |
| 48 | + buildOutput.addDependencies([ |
| 49 | + buildConfig.packageRoot.resolve('build.dart'), |
| 50 | + buildConfig.packageRoot.resolve('sdk_downloads.dart'), |
| 51 | + ]); |
| 52 | + |
| 53 | + final downloadFileLocation = buildConfig.outputDirectory.resolve( |
| 54 | + buildConfig.targetOS.dylibFileName('text'), |
| 55 | + ); |
| 56 | + log('downloadFileLocation: $downloadFileLocation'); |
| 57 | + buildOutput.addAsset( |
| 58 | + NativeCodeAsset( |
| 59 | + package: 'mediapipe_text', |
| 60 | + name: |
| 61 | + 'src/io/third_party/mediapipe/generated/mediapipe_text_bindings.dart', |
| 62 | + linkMode: DynamicLoadingBundled(), |
| 63 | + os: buildConfig.targetOS, |
| 64 | + architecture: buildConfig.targetArchitecture, |
| 65 | + file: downloadFileLocation, |
| 66 | + ), |
| 67 | + ); |
| 68 | + if (!buildConfig.dryRun) { |
| 69 | + final arch = buildConfig.targetArchitecture.toString(); |
| 70 | + final assetUrl = sdkDownloadUrls[targetOS]!['libtext']![arch]!; |
| 71 | + downloadAsset(assetUrl, downloadFileLocation); |
| 72 | + } |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +Future<void> downloadAsset(String assetUrl, Uri destinationFile) async { |
| 77 | + final downloadUri = Uri.parse(assetUrl); |
| 78 | + final downloadedFile = File(destinationFile.toFilePath()); |
| 79 | + |
| 80 | + log('Downloading file from $downloadUri'); |
| 81 | + |
| 82 | + final downloadResponse = await http.get(downloadUri); |
| 83 | + log('Download response: ${downloadResponse.statusCode}'); |
| 84 | + |
| 85 | + if (downloadResponse.statusCode == 200) { |
| 86 | + if (downloadedFile.existsSync()) { |
| 87 | + downloadedFile.deleteSync(); |
| 88 | + } |
| 89 | + downloadedFile.createSync(); |
| 90 | + log('Saved file to ${downloadedFile.absolute.path}\n'); |
| 91 | + downloadedFile.writeAsBytes(downloadResponse.bodyBytes); |
| 92 | + } else { |
| 93 | + log('${downloadResponse.statusCode} :: ${downloadResponse.body}'); |
| 94 | + throw Exception( |
| 95 | + '${downloadResponse.statusCode} :: ${downloadResponse.body}'); |
| 96 | + } |
| 97 | +} |
0 commit comments