|  | 
|  | 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 | + | 
|  | 39 | +    log('dir.current: ${Directory.current.absolute.path}'); | 
|  | 40 | + | 
|  | 41 | +    // Throw if target runtime is unsupported. | 
|  | 42 | +    if (!sdkDownloadUrls.containsKey(targetOs)) { | 
|  | 43 | +      throw Exception('Unsupported target OS: $targetOs. ' | 
|  | 44 | +          'Supported values are: ${sdkDownloadUrls.keys.toSet()}'); | 
|  | 45 | +    } | 
|  | 46 | + | 
|  | 47 | +    buildOutput.addDependencies([ | 
|  | 48 | +      buildConfig.packageRoot.resolve('build.dart'), | 
|  | 49 | +      buildConfig.packageRoot.resolve('sdk_downloads.dart'), | 
|  | 50 | +    ]); | 
|  | 51 | + | 
|  | 52 | +    final modelName = 'libllm_inference_engine'; | 
|  | 53 | +    final Iterable<String> archKeys; | 
|  | 54 | +    if (buildConfig.dryRun) { | 
|  | 55 | +      archKeys = sdkDownloadUrls[targetOs]![modelName]!.keys; | 
|  | 56 | +    } else { | 
|  | 57 | +      archKeys = [buildConfig.targetArchitecture.toString()]; | 
|  | 58 | +    } | 
|  | 59 | +    for (String arch in archKeys) { | 
|  | 60 | +      arch = getArchAlias(arch); | 
|  | 61 | +      log('arch: $arch'); | 
|  | 62 | +      log('sdkDownloadUrls[$targetOs]: ${sdkDownloadUrls[targetOs]}'); | 
|  | 63 | +      log('sdkDownloadUrls[$targetOs][$modelName]: ${sdkDownloadUrls[targetOs]![modelName]}'); | 
|  | 64 | +      log('sdkDownloadUrls[$targetOs][$modelName][$arch]: ${sdkDownloadUrls[targetOs]![modelName]![arch]}'); | 
|  | 65 | + | 
|  | 66 | +      if (!sdkDownloadUrls[targetOs]!['libllm_inference_engine']! | 
|  | 67 | +          .containsKey(arch)) { | 
|  | 68 | +        continue; | 
|  | 69 | +      } | 
|  | 70 | +      final assetUrl = | 
|  | 71 | +          sdkDownloadUrls[targetOs]!['libllm_inference_engine']![arch]!; | 
|  | 72 | + | 
|  | 73 | +      // Take last chunk and drop file extension, e.g., | 
|  | 74 | +      // "path/to/file.dylib" -> "file" | 
|  | 75 | +      final fileName = assetUrl.split('/').last.split('.').first; | 
|  | 76 | + | 
|  | 77 | +      final downloadFileLocation = buildConfig.outputDirectory.resolve( | 
|  | 78 | +        buildConfig.targetOS.dylibFileName(fileName), | 
|  | 79 | +      ); | 
|  | 80 | +      buildOutput.addAsset( | 
|  | 81 | +        NativeCodeAsset( | 
|  | 82 | +          package: 'mediapipe_genai', | 
|  | 83 | +          name: | 
|  | 84 | +              'src/io/third_party/mediapipe/generated/mediapipe_genai_bindings.dart', | 
|  | 85 | +          linkMode: DynamicLoadingBundled(), | 
|  | 86 | +          os: buildConfig.targetOS, | 
|  | 87 | +          architecture: buildConfig.targetArchitecture, | 
|  | 88 | +          file: downloadFileLocation, | 
|  | 89 | +        ), | 
|  | 90 | +      ); | 
|  | 91 | +      if (!buildConfig.dryRun) { | 
|  | 92 | +        log('downloadFileLocation: $downloadFileLocation'); | 
|  | 93 | +        downloadAsset(assetUrl, downloadFileLocation); | 
|  | 94 | +      } | 
|  | 95 | +    } | 
|  | 96 | +  }); | 
|  | 97 | +} | 
|  | 98 | + | 
|  | 99 | +Future<void> downloadAsset(String assetUrl, Uri destinationFile) async { | 
|  | 100 | +  final downloadUri = Uri.parse(assetUrl); | 
|  | 101 | +  final downloadedFile = File(destinationFile.toFilePath()); | 
|  | 102 | +  log('Saving file to ${downloadedFile.absolute.path}'); | 
|  | 103 | + | 
|  | 104 | +  final downloadResponse = await http.get(downloadUri); | 
|  | 105 | +  log('Download response: ${downloadResponse.statusCode}'); | 
|  | 106 | + | 
|  | 107 | +  if (downloadResponse.statusCode == 200) { | 
|  | 108 | +    if (downloadedFile.existsSync()) { | 
|  | 109 | +      downloadedFile.deleteSync(); | 
|  | 110 | +    } | 
|  | 111 | +    downloadedFile.createSync(); | 
|  | 112 | +    log('Saved file to ${downloadedFile.absolute.path}\n'); | 
|  | 113 | +    downloadedFile.writeAsBytes(downloadResponse.bodyBytes); | 
|  | 114 | +  } else { | 
|  | 115 | +    log('${downloadResponse.statusCode} :: ${downloadResponse.body}'); | 
|  | 116 | +    throw Exception( | 
|  | 117 | +        '${downloadResponse.statusCode} :: ${downloadResponse.body}'); | 
|  | 118 | +  } | 
|  | 119 | +} | 
|  | 120 | + | 
|  | 121 | +/// Translates native-assets architecture names into MediaPipe architecture names | 
|  | 122 | +String getArchAlias(String arch) => | 
|  | 123 | +    <String, String>{ | 
|  | 124 | +      'arm': 'arm64', | 
|  | 125 | +    }[arch] ?? | 
|  | 126 | +    arch; | 
0 commit comments