Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mediapipe-task-text] Upgrade native_assets_cli #35

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 0 additions & 105 deletions packages/mediapipe-task-text/build.dart

This file was deleted.

97 changes: 97 additions & 0 deletions packages/mediapipe-task-text/hook/build.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'dart:io';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:http/http.dart' as http;
import 'package:path/path.dart' as path;

import '../sdk_downloads.dart';

late File logFile;

final logs = <(DateTime, String)>[];
void log(String msg) {
logs.add((DateTime.now(), msg));
if (!logFile.parent.existsSync()) {
logFile.parent.createSync();
}

if (logFile.existsSync()) {
logFile.deleteSync();
}
logFile.createSync();
logFile.writeAsStringSync(logs
.map<String>((rec) => '[${rec.$1.toIso8601String()}] ${rec.$2}')
.toList()
.join('\n\n'));
}

Future<void> main(List<String> args) async {
await build(args, (buildConfig, buildOutput) async {
logFile = File(
path.joinAll([
Directory.current.path, // root dir of app using `mediapipe-task-xyz`
'build/${buildConfig.dryRun ? "dryrun" : "live-run"}-build-log.txt',
]),
);

log(args.join(' '));
final String targetOS = buildConfig.targetOS.toString();
log('targetOS: $targetOS');

log('dir.current: ${Directory.current.absolute.path}');

// Throw if target runtime is unsupported.
if (!sdkDownloadUrls.containsKey(targetOS)) {
throw Exception('Unsupported target OS: $targetOS. '
'Supported values are: ${sdkDownloadUrls.keys.toSet()}');
}

buildOutput.addDependencies([
buildConfig.packageRoot.resolve('build.dart'),
buildConfig.packageRoot.resolve('sdk_downloads.dart'),
]);

final downloadFileLocation = buildConfig.outputDirectory.resolve(
buildConfig.targetOS.dylibFileName('text'),
);
log('downloadFileLocation: $downloadFileLocation');
buildOutput.addAsset(
NativeCodeAsset(
package: 'mediapipe_text',
name:
'src/io/third_party/mediapipe/generated/mediapipe_text_bindings.dart',
linkMode: DynamicLoadingBundled(),
os: buildConfig.targetOS,
architecture: buildConfig.targetArchitecture,
file: downloadFileLocation,
),
);
if (!buildConfig.dryRun) {
final arch = buildConfig.targetArchitecture.toString();
final assetUrl = sdkDownloadUrls[targetOS]!['libtext']![arch]!;
downloadAsset(assetUrl, downloadFileLocation);
}
});
}

Future<void> downloadAsset(String assetUrl, Uri destinationFile) async {
final downloadUri = Uri.parse(assetUrl);
final downloadedFile = File(destinationFile.toFilePath());

log('Downloading file from $downloadUri');

final downloadResponse = await http.get(downloadUri);
log('Download response: ${downloadResponse.statusCode}');

if (downloadResponse.statusCode == 200) {
if (downloadedFile.existsSync()) {
downloadedFile.deleteSync();
}
downloadedFile.createSync();
log('Saved file to ${downloadedFile.absolute.path}\n');
downloadedFile.writeAsBytes(downloadResponse.bodyBytes);
} else {
log('${downloadResponse.statusCode} :: ${downloadResponse.body}');
throw Exception(
'${downloadResponse.statusCode} :: ${downloadResponse.body}');
}
}
6 changes: 3 additions & 3 deletions packages/mediapipe-task-text/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ dependencies:
http: ^1.1.0
logging: ^1.2.0
mediapipe_core: ^0.0.1
native_assets_cli: ^0.3.0
native_toolchain_c: ^0.3.0
native_assets_cli: ^0.5.4
native_toolchain_c: ^0.4.1
path: ^1.8.3

dev_dependencies:
ffigen: ^9.0.1
ffigen: ^12.0.0
flutter_lints: ^2.0.0
test: ^1.21.0