Suggestions for API
diff --git a/pkgs/native_assets_cli/example/native_add_library/build.dart b/pkgs/native_assets_cli/example/native_add_library/build.dart
index ae39cdc..014410f 100644
--- a/pkgs/native_assets_cli/example/native_add_library/build.dart
+++ b/pkgs/native_assets_cli/example/native_add_library/build.dart
@@ -2,38 +2,22 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-import 'package:logging/logging.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
-import 'package:native_toolchain_c/native_toolchain_c.dart';
const packageName = 'native_add_library';
/// Implements the protocol from `package:native_assets_cli` by building
/// the C code in `src/` and reporting what native assets it built.
-void main(List<String> args) async {
- // Parse the build configuration passed to this CLI from Dart or Flutter.
- final buildConfig = await BuildConfig.fromArgs(args);
- final buildOutput = BuildOutput();
+void main(List<String> args) async =>
+ await BuildState.build(args, (buildState) async {
+ const somefile = 'some_file';
- // Configure `package:native_toolchain_c` to build the C code for us.
- final cbuilder = CBuilder.library(
- name: packageName,
- assetId: 'package:$packageName/${packageName}.dart',
- sources: [
- 'src/$packageName.c',
- ],
- );
- await cbuilder.run(
- buildConfig: buildConfig,
- // `package:native_toolchain_c` will output the dynamic or static libraries it built,
- // what files it accessed (for caching the build), etc.
- buildOutput: buildOutput,
- logger: Logger('')
- ..level = Level.ALL
- ..onRecord.listen((record) => print(record.message)),
- );
+ final path = buildState.getDylibName(somefile);
- // Write the output according to the native assets protocol so that Dart or
- // Flutter can find the native assets produced by this script.
- await buildOutput.writeToFile(outDir: buildConfig.outDir);
-}
+ // Download/Build the asset to path
+
+ buildState.addAsset(
+ id: 'src/$somefile.dart',
+ path: AssetAbsolutePath(path),
+ );
+ });
diff --git a/pkgs/native_assets_cli/lib/src/api/build_config.dart b/pkgs/native_assets_cli/lib/src/api/build_config.dart
index fabbb68..844d2b2 100644
--- a/pkgs/native_assets_cli/lib/src/api/build_config.dart
+++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart
@@ -196,6 +196,8 @@
environment: environment,
workingDirectory: workingDirectory,
);
+
+ Uri getDylibName(String name);
}
abstract class CCompilerConfig {
diff --git a/pkgs/native_assets_cli/lib/src/api/build_state.dart b/pkgs/native_assets_cli/lib/src/api/build_state.dart
index ba295ca..7c87308 100644
--- a/pkgs/native_assets_cli/lib/src/api/build_state.dart
+++ b/pkgs/native_assets_cli/lib/src/api/build_state.dart
@@ -5,6 +5,8 @@
import 'asset.dart';
import 'build_config.dart';
import 'build_output.dart';
+import 'link_mode.dart';
+import 'target.dart';
final class BuildState {
/// Run a native assets build.
@@ -82,27 +84,51 @@
await output.writeToFile(outDir: config.outDir);
}
- final BuildConfig config;
- final BuildOutput output;
+ final BuildConfig _config;
+ final BuildOutput _output;
BuildState._({
- required this.config,
- required this.output,
- });
-
- /// Adds assets to build output.
- ///
- /// See [BuildOutput.addAssets] for more info.
- void addAssets(Iterable<Asset> assets) => output.addAssets(assets);
+ required BuildConfig config,
+ required BuildOutput output,
+ }) : _output = output,
+ _config = config;
/// Adds dependencies to build output.
///
/// See [BuildOutput.addDependencies] for more info.
void addDependencies(Iterable<Uri> dependencies) =>
- output.addDependencies(dependencies);
+ _output.addDependencies(dependencies);
/// Adds metadata to build output.
///
/// See [BuildOutput.addMetadata] for more info.
- void addMetadata(String key, Object value) => output.addMetadata(key, value);
+ void addMetadata(String key, Object value) => _output.addMetadata(key, value);
+
+ /// Adds asset to build output.
+ ///
+ /// See [BuildOutput.addAssets] for more info.
+ void addAsset({required String id, required AssetPath path}) {
+ if (_config.dryRun) {
+ _output.addAssets([
+ Asset(
+ id: 'package:config.packageName$id',
+ linkMode: LinkMode.dynamic,
+ target: _config.target,
+ path: path,
+ )
+ ]);
+ } else {
+ _output.addAssets(Target.values
+ .where((target) => target.os == _config.targetOs)
+ .map((target) => Asset(
+ id: 'package:config.packageName$id',
+ linkMode: LinkMode.dynamic,
+ target: target,
+ path: path,
+ ))
+ .toList());
+ }
+ }
+
+ Uri getDylibName(String somefile) => _config.getDylibName(somefile);
}
diff --git a/pkgs/native_assets_cli/lib/src/model/build_config.dart b/pkgs/native_assets_cli/lib/src/model/build_config.dart
index 842db9a..8810df1 100644
--- a/pkgs/native_assets_cli/lib/src/model/build_config.dart
+++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart
@@ -592,6 +592,10 @@
can _only_ depend on OS.''');
}
}
+
+ @override
+ Uri getDylibName(String name) =>
+ outDir.resolve(targetOs.dylibFileName(packageName));
}
class CCompilerConfig implements api.CCompilerConfig {