From d144f81442894f669108699517d4f4a3f7a529f8 Mon Sep 17 00:00:00 2001 From: Martin Kustermann Date: Tue, 1 Oct 2024 09:18:09 +0200 Subject: [PATCH] Rename NativeCodeAsset->CodeAsset and merge {CodeAsset,DataAsset} and {CodeAsset,DataAsset}Impl (#1614) This is the last base refactoring. Building on this the next PRs will gradually introduce more flexibility into the build system (starting with adding custom asset types) and gradually separate components more and more. --- pkgs/native_assets_builder/CHANGELOG.md | 2 +- .../lib/src/build_runner/build_runner.dart | 10 +- .../lib/src/model/build_dry_run_result.dart | 4 +- .../lib/src/model/build_result.dart | 4 +- .../lib/src/model/hook_result.dart | 8 +- .../lib/src/model/link_result.dart | 2 +- .../build_runner_build_dry_run_test.dart | 5 +- .../build_runner_caching_test.dart | 9 +- .../build_runner_failure_test.dart | 6 +- .../test/build_runner/helpers.dart | 4 +- .../test/build_runner/link_caching_test.dart | 2 +- .../test/build_runner/link_test.dart | 9 +- .../packaging_preference_test.dart | 8 +- .../test_data/add_asset_link/hook/link.dart | 4 +- .../test_data/wrong_linker/hook/build.dart | 2 +- .../wrong_namespace_asset/hook/build.dart | 2 +- pkgs/native_assets_cli/CHANGELOG.md | 5 +- .../example/build/local_asset/hook/build.dart | 2 +- .../build/local_asset/test/build_test.dart | 4 +- .../lib/native_assets_cli.dart | 3 +- .../lib/native_assets_cli_internal.dart | 2 +- pkgs/native_assets_cli/lib/src/api/build.dart | 2 +- .../lib/src/api/build_config.dart | 2 +- .../lib/src/api/build_output.dart | 6 +- .../lib/src/api/data_asset.dart | 38 ------ .../lib/src/api/hook_config.dart | 6 +- .../lib/src/api/link_config.dart | 2 +- .../lib/src/api/link_output.dart | 2 +- pkgs/native_assets_cli/lib/src/api/test.dart | 6 +- .../lib/src/{api => }/asset.dart | 57 ++++++-- ...native_code_asset.dart => code_asset.dart} | 121 ++++++++++++++--- .../native_assets_cli/lib/src/data_asset.dart | 87 ++++++++++++ .../native_assets_cli/lib/src/json_utils.dart | 18 ++- pkgs/native_assets_cli/lib/src/link_mode.dart | 10 +- .../lib/src/link_mode_preference.dart | 4 +- .../lib/src/model/asset.dart | 33 ----- .../lib/src/model/build_config.dart | 2 +- .../lib/src/model/data_asset.dart | 65 --------- .../lib/src/model/hook_config.dart | 7 +- .../lib/src/model/hook_output.dart | 37 +++--- .../lib/src/model/link_config.dart | 12 +- .../lib/src/model/native_code_asset.dart | 125 ------------------ .../lib/src/validator/validator.dart | 16 +-- .../test/api/asset_test.dart | 14 +- .../test/api/build_config_test.dart | 6 +- .../test/api/build_output_test.dart | 4 +- .../test/api/link_config_test.dart | 6 +- .../test/model/asset_test.dart | 42 +++--- .../test/model/build_config_test.dart | 2 +- .../test/model/build_output_test.dart | 44 +++--- .../test/model/checksum_test.dart | 4 +- .../test/model/link_config_test.dart | 10 +- .../test/validator/validator_test.dart | 34 ++--- .../lib/src/cbuilder/cbuilder.dart | 2 +- .../lib/src/cbuilder/clinker.dart | 2 +- .../test/clinker/objects_test.dart | 4 +- .../test/clinker/treeshake_helper.dart | 2 +- pkgs/native_toolchain_c/test/helpers.dart | 4 +- 58 files changed, 451 insertions(+), 484 deletions(-) delete mode 100644 pkgs/native_assets_cli/lib/src/api/data_asset.dart rename pkgs/native_assets_cli/lib/src/{api => }/asset.dart (60%) rename pkgs/native_assets_cli/lib/src/{api/native_code_asset.dart => code_asset.dart} (65%) create mode 100644 pkgs/native_assets_cli/lib/src/data_asset.dart delete mode 100644 pkgs/native_assets_cli/lib/src/model/asset.dart delete mode 100644 pkgs/native_assets_cli/lib/src/model/data_asset.dart delete mode 100644 pkgs/native_assets_cli/lib/src/model/native_code_asset.dart diff --git a/pkgs/native_assets_builder/CHANGELOG.md b/pkgs/native_assets_builder/CHANGELOG.md index b5184798e..8909f3e54 100644 --- a/pkgs/native_assets_builder/CHANGELOG.md +++ b/pkgs/native_assets_builder/CHANGELOG.md @@ -10,7 +10,7 @@ dart-lang/native repository to make it clear those are not intended to be used by end-users. - Remove link-dry-run concept as it's unused by Flutter Tools & Dart SDK -- Use unified classes instead of two `{OS,...}` and `{OS,,...}Impl` +- Bump `native_assets_cli` to `0.9.0` ## 0.8.3 diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index faa9a8422..c2e4df213 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -431,17 +431,17 @@ class NativeAssetsBuildRunner { packageLayout!, ), ); - buildOutput = _expandArchsNativeCodeAssets(buildOutput); + buildOutput = _expandArchsCodeAssets(buildOutput); hookResult = hookResult.copyAdd(buildOutput, packageSuccess); } return hookResult; } - HookOutputImpl _expandArchsNativeCodeAssets(HookOutputImpl buildOutput) { - final assets = []; + HookOutputImpl _expandArchsCodeAssets(HookOutputImpl buildOutput) { + final assets = []; for (final asset in buildOutput.assets) { switch (asset) { - case NativeCodeAssetImpl _: + case CodeAsset _: if (asset.architecture != null) { // Backwards compatibility, if an architecture is provided use it. assets.add(asset); @@ -453,7 +453,7 @@ class NativeAssetsBuildRunner { assets.add(asset.copyWith(architecture: architecture)); } } - case DataAssetImpl _: + case DataAsset _: assets.add(asset); } } diff --git a/pkgs/native_assets_builder/lib/src/model/build_dry_run_result.dart b/pkgs/native_assets_builder/lib/src/model/build_dry_run_result.dart index 8ec575e00..1379cf244 100644 --- a/pkgs/native_assets_builder/lib/src/model/build_dry_run_result.dart +++ b/pkgs/native_assets_builder/lib/src/model/build_dry_run_result.dart @@ -10,7 +10,7 @@ import '../../native_assets_builder.dart'; /// the dependency tree of the entry point application. abstract interface class BuildDryRunResult { /// The native assets produced by the hooks, which should be bundled. - List get assets; + List get assets; /// Whether all hooks completed without errors. /// @@ -18,5 +18,5 @@ abstract interface class BuildDryRunResult { bool get success; /// The native assets produced by the hooks, which should be linked. - Map> get assetsForLinking; + Map> get assetsForLinking; } diff --git a/pkgs/native_assets_builder/lib/src/model/build_result.dart b/pkgs/native_assets_builder/lib/src/model/build_result.dart index f2a7244e8..b38680563 100644 --- a/pkgs/native_assets_builder/lib/src/model/build_result.dart +++ b/pkgs/native_assets_builder/lib/src/model/build_result.dart @@ -18,8 +18,8 @@ abstract class BuildResult { bool get success; /// The native assets produced by the hooks, which should be bundled. - List get assets; + List get assets; /// The native assets produced by the hooks, which should be linked. - Map> get assetsForLinking; + Map> get assetsForLinking; } diff --git a/pkgs/native_assets_builder/lib/src/model/hook_result.dart b/pkgs/native_assets_builder/lib/src/model/hook_result.dart index cc7656c0c..3e0644991 100644 --- a/pkgs/native_assets_builder/lib/src/model/hook_result.dart +++ b/pkgs/native_assets_builder/lib/src/model/hook_result.dart @@ -12,11 +12,11 @@ import '../../native_assets_builder.dart'; final class HookResult implements BuildResult, BuildDryRunResult, LinkResult { /// The native assets produced by the hooks, which should be bundled. @override - final List assets; + final List assets; /// The assets produced by the hooks, which should be linked. @override - final Map> assetsForLinking; + final Map> assetsForLinking; /// The files used by the hooks. @override @@ -36,8 +36,8 @@ final class HookResult implements BuildResult, BuildDryRunResult, LinkResult { }); factory HookResult({ - List? assets, - Map>? assetsForLinking, + List? assets, + Map>? assetsForLinking, List? dependencies, bool success = true, }) => diff --git a/pkgs/native_assets_builder/lib/src/model/link_result.dart b/pkgs/native_assets_builder/lib/src/model/link_result.dart index f09270f41..e3f6877f7 100644 --- a/pkgs/native_assets_builder/lib/src/model/link_result.dart +++ b/pkgs/native_assets_builder/lib/src/model/link_result.dart @@ -10,7 +10,7 @@ import '../build_runner/build_runner.dart'; /// the dependency tree of the entry point application. abstract interface class LinkResult { /// The native assets produced by the hooks, which should be bundled. - List get assets; + List get assets; /// The files used by the hooks. List get dependencies; diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_build_dry_run_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_build_dry_run_test.dart index bc7a13bbb..d91cb8961 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_build_dry_run_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_build_dry_run_test.dart @@ -43,10 +43,9 @@ void main() async { final dryRunAsset = dryRunAssets[i]; final buildAsset = result.assets[0]; expect(dryRunAsset.id, buildAsset.id); - // The build runner expands NativeCodeAssets to all architectures. + // The build runner expands CodeAssets to all architectures. expect(buildAsset.file, isNotNull); - if (dryRunAsset is NativeCodeAssetImpl && - buildAsset is NativeCodeAssetImpl) { + if (dryRunAsset is CodeAsset && buildAsset is CodeAsset) { expect(dryRunAsset.architecture, isNotNull); expect(buildAsset.architecture, isNotNull); expect(dryRunAsset.os, buildAsset.os); diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart index 88501a222..d20862d2a 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart @@ -88,8 +88,7 @@ void main() async { { final result = await build(packageUri, logger, dartExecutable); await expectSymbols( - asset: result.assets.single as NativeCodeAssetImpl, - symbols: ['add']); + asset: result.assets.single as CodeAsset, symbols: ['add']); } await copyTestProjects( @@ -100,7 +99,7 @@ void main() async { { final result = await build(packageUri, logger, dartExecutable); await expectSymbols( - asset: result.assets.single as NativeCodeAssetImpl, + asset: result.assets.single as CodeAsset, symbols: ['add', 'subtract'], ); } @@ -136,7 +135,7 @@ void main() async { } logMessages.clear(); await expectSymbols( - asset: result.assets.single as NativeCodeAssetImpl, + asset: result.assets.single as CodeAsset, symbols: ['add'], ); @@ -156,7 +155,7 @@ void main() async { } logMessages.clear(); await expectSymbols( - asset: result.assets.single as NativeCodeAssetImpl, + asset: result.assets.single as CodeAsset, symbols: ['add', 'multiply'], ); } diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_failure_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_failure_test.dart index aa1e030ca..d4939bec6 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_failure_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_failure_test.dart @@ -28,8 +28,7 @@ void main() async { final result = await build(packageUri, logger, dartExecutable); expect(result.assets.length, 1); await expectSymbols( - asset: result.assets.single as NativeCodeAssetImpl, - symbols: ['add']); + asset: result.assets.single as CodeAsset, symbols: ['add']); expect( result.dependencies, [ @@ -72,8 +71,7 @@ void main() async { final result = await build(packageUri, logger, dartExecutable); expect(result.assets.length, 1); await expectSymbols( - asset: result.assets.single as NativeCodeAssetImpl, - symbols: ['add']); + asset: result.assets.single as CodeAsset, symbols: ['add']); expect( result.dependencies, [ diff --git a/pkgs/native_assets_builder/test/build_runner/helpers.dart b/pkgs/native_assets_builder/test/build_runner/helpers.dart index 8f644a3da..ba809a893 100644 --- a/pkgs/native_assets_builder/test/build_runner/helpers.dart +++ b/pkgs/native_assets_builder/test/build_runner/helpers.dart @@ -246,14 +246,14 @@ Future buildDryRun( return result; }); -Future expectAssetsExist(List assets) async { +Future expectAssetsExist(List assets) async { for (final asset in assets) { expect(File.fromUri(asset.file!), exists); } } Future expectSymbols({ - required NativeCodeAssetImpl asset, + required CodeAsset asset, required List symbols, }) async { if (Platform.isLinux) { diff --git a/pkgs/native_assets_builder/test/build_runner/link_caching_test.dart b/pkgs/native_assets_builder/test/build_runner/link_caching_test.dart index 8089e0912..0104ed64c 100644 --- a/pkgs/native_assets_builder/test/build_runner/link_caching_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/link_caching_test.dart @@ -5,7 +5,7 @@ import 'dart:io'; import 'package:native_assets_builder/native_assets_builder.dart'; -import 'package:native_assets_cli/src/api/asset.dart'; +import 'package:native_assets_cli/src/asset.dart'; import 'package:test/test.dart'; import '../helpers.dart'; diff --git a/pkgs/native_assets_builder/test/build_runner/link_test.dart b/pkgs/native_assets_builder/test/build_runner/link_test.dart index 8b3c79aad..d7cc00e33 100644 --- a/pkgs/native_assets_builder/test/build_runner/link_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/link_test.dart @@ -4,8 +4,7 @@ import 'dart:io'; -import 'package:native_assets_cli/native_assets_cli.dart' as cli; -import 'package:native_assets_cli/src/api/asset.dart'; +import 'package:native_assets_cli/src/asset.dart'; import 'package:test/test.dart'; import '../helpers.dart'; @@ -198,11 +197,11 @@ void main() async { capturedLogs: logMessages, ); expect(linkResult.assets.length, 1); - expect(linkResult.assets.first, isA()); + expect(linkResult.assets.first, isA()); }); }, ); } -Iterable _getNames(List assets) => - assets.whereType().map((asset) => asset.name); +Iterable _getNames(List assets) => + assets.whereType().map((asset) => asset.name); diff --git a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart index 02510ff47..2129b4ed5 100644 --- a/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/packaging_preference_test.dart @@ -52,19 +52,19 @@ void main() async { // This package honors preferences. expect( - (resultDynamic.assets.single as NativeCodeAssetImpl).linkMode, + (resultDynamic.assets.single as CodeAsset).linkMode, DynamicLoadingBundled(), ); expect( - (resultPreferDynamic.assets.single as NativeCodeAssetImpl).linkMode, + (resultPreferDynamic.assets.single as CodeAsset).linkMode, DynamicLoadingBundled(), ); expect( - (resultStatic.assets.single as NativeCodeAssetImpl).linkMode, + (resultStatic.assets.single as CodeAsset).linkMode, StaticLinking(), ); expect( - (resultPreferStatic.assets.single as NativeCodeAssetImpl).linkMode, + (resultPreferStatic.assets.single as CodeAsset).linkMode, StaticLinking(), ); }); diff --git a/pkgs/native_assets_builder/test_data/add_asset_link/hook/link.dart b/pkgs/native_assets_builder/test_data/add_asset_link/hook/link.dart index 5cea3b1cd..371081747 100644 --- a/pkgs/native_assets_builder/test_data/add_asset_link/hook/link.dart +++ b/pkgs/native_assets_builder/test_data/add_asset_link/hook/link.dart @@ -6,10 +6,10 @@ import 'package:native_assets_cli/native_assets_cli.dart'; void main(List arguments) async { await link(arguments, (config, output) async { - final builtDylib = config.assets.first as NativeCodeAsset; + final builtDylib = config.assets.first as CodeAsset; output ..addAsset( - NativeCodeAsset( + CodeAsset( package: 'add_asset_link', name: 'dylib_add_link', linkMode: builtDylib.linkMode, diff --git a/pkgs/native_assets_builder/test_data/wrong_linker/hook/build.dart b/pkgs/native_assets_builder/test_data/wrong_linker/hook/build.dart index c91b70d43..b8730f013 100644 --- a/pkgs/native_assets_builder/test_data/wrong_linker/hook/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_linker/hook/build.dart @@ -16,7 +16,7 @@ void main(List arguments) async { } output.addAsset( - NativeCodeAsset( + CodeAsset( package: config.packageName, name: 'foo', file: assetUri, diff --git a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/hook/build.dart b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/hook/build.dart index 1c0c8a664..0cf220b41 100644 --- a/pkgs/native_assets_builder/test_data/wrong_namespace_asset/hook/build.dart +++ b/pkgs/native_assets_builder/test_data/wrong_namespace_asset/hook/build.dart @@ -16,7 +16,7 @@ void main(List arguments) async { } output.addAsset( - NativeCodeAsset( + CodeAsset( package: 'other_package', name: 'foo', file: assetUri, diff --git a/pkgs/native_assets_cli/CHANGELOG.md b/pkgs/native_assets_cli/CHANGELOG.md index 91c891905..3805a1643 100644 --- a/pkgs/native_assets_cli/CHANGELOG.md +++ b/pkgs/native_assets_cli/CHANGELOG.md @@ -17,8 +17,9 @@ - No longer try to resolve uris encoded in `config.json` against any base uri. The `hook/{build,link}.dart` invoker has to ensure the uris it encodes can be opened as-is (i.e. without resolving against any base uri) -- **Breaking change** Use unified classes instead of two `{OS,...}` and - move methods (e.g. from `OS`) to extensions (e.g. `OSLibraryNaming`) +- **Breaking change** Moved some methods to be extension methods. +- Some classes in the `BuildConfig` and `BuildOutput` now expose `fromJson` and + `toJson`. ## 0.8.0 diff --git a/pkgs/native_assets_cli/example/build/local_asset/hook/build.dart b/pkgs/native_assets_cli/example/build/local_asset/hook/build.dart index ee6a83652..19fa40160 100644 --- a/pkgs/native_assets_cli/example/build/local_asset/hook/build.dart +++ b/pkgs/native_assets_cli/example/build/local_asset/hook/build.dart @@ -32,7 +32,7 @@ Future main(List args) async { output.addAsset( // TODO: Change to DataAsset once the Dart/Flutter SDK can consume it. - NativeCodeAsset( + CodeAsset( package: packageName, name: 'asset.txt', file: assetPath, diff --git a/pkgs/native_assets_cli/example/build/local_asset/test/build_test.dart b/pkgs/native_assets_cli/example/build/local_asset/test/build_test.dart index e10c33fd8..0befd2e4a 100644 --- a/pkgs/native_assets_cli/example/build/local_asset/test/build_test.dart +++ b/pkgs/native_assets_cli/example/build/local_asset/test/build_test.dart @@ -14,9 +14,9 @@ void main() { mainMethod: build.main, check: (_, output) { expect(output.assets, isNotEmpty); - expect(output.assets.first, isA()); + expect(output.assets.first, isA()); expect( - (output.assets.first as NativeCodeAsset).id, + (output.assets.first as CodeAsset).id, 'package:local_asset/asset.txt', ); }, diff --git a/pkgs/native_assets_cli/lib/native_assets_cli.dart b/pkgs/native_assets_cli/lib/native_assets_cli.dart index 610159c08..21bc081b6 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli.dart @@ -6,8 +6,6 @@ /// build hook (`hook/build.dart`). library native_assets_cli; -export 'src/api/asset.dart' - show Asset, DataAsset, NativeCodeAsset, OSLibraryNaming; export 'src/api/build.dart' show build; export 'src/api/build_config.dart' show BuildConfig; export 'src/api/build_output.dart' show BuildOutput, LinkOutput; @@ -17,6 +15,7 @@ export 'src/api/link.dart' show link; export 'src/api/link_config.dart' show LinkConfig; export 'src/api/linker.dart' show Linker; export 'src/architecture.dart' show Architecture; +export 'src/asset.dart' show Asset, CodeAsset, DataAsset, OSLibraryNaming; export 'src/build_mode.dart' show BuildMode; export 'src/c_compiler_config.dart' show CCompilerConfig; export 'src/ios_sdk.dart' show IOSSdk; diff --git a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart index fbb4ad9a3..2d1b478c9 100644 --- a/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart +++ b/pkgs/native_assets_cli/lib/native_assets_cli_internal.dart @@ -17,11 +17,11 @@ library native_assets_cli_internal; export 'native_assets_cli.dart' hide build, link; -export 'src/api/asset.dart' show AssetImpl, DataAssetImpl, NativeCodeAssetImpl; export 'src/api/build_config.dart' show BuildConfigImpl; export 'src/api/build_output.dart' show HookOutputImpl; export 'src/api/hook_config.dart' show HookConfigImpl; export 'src/api/link_config.dart' show LinkConfigImpl; +export 'src/asset.dart' show Asset, CodeAsset, DataAsset; export 'src/model/dependencies.dart'; export 'src/model/hook.dart'; export 'src/model/metadata.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index 12b2ffa77..ef7dfd3fa 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -72,7 +72,7 @@ import 'build_output.dart'; /// /// output.addAsset( /// // TODO: Change to DataAsset once the Dart/Flutter SDK can consume it. -/// NativeCodeAsset( +/// CodeAsset( /// package: packageName, /// name: 'asset.txt', /// file: assetPath, 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 557651ca7..c7d880cfb 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_config.dart @@ -10,6 +10,7 @@ import 'package:pub_semver/pub_semver.dart'; import '../architecture.dart'; import '../args_parser.dart'; +import '../asset.dart'; import '../build_mode.dart'; import '../c_compiler_config.dart'; import '../ios_sdk.dart'; @@ -20,7 +21,6 @@ import '../model/metadata.dart'; import '../os.dart'; import '../utils/json.dart'; import '../utils/map.dart'; -import 'asset.dart'; import 'build.dart'; import 'build_output.dart'; import 'deprecation_messages.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/build_output.dart b/pkgs/native_assets_cli/lib/src/api/build_output.dart index 821f97fc3..d15bc4b8e 100644 --- a/pkgs/native_assets_cli/lib/src/api/build_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/build_output.dart @@ -9,6 +9,7 @@ import 'package:collection/collection.dart'; import 'package:pub_semver/pub_semver.dart'; import '../architecture.dart'; +import '../asset.dart'; import '../model/dependencies.dart'; import '../model/metadata.dart'; import '../os.dart'; @@ -16,7 +17,6 @@ import '../utils/datetime.dart'; import '../utils/file.dart'; import '../utils/json.dart'; import '../utils/map.dart'; -import 'asset.dart'; import 'build.dart'; import 'build_config.dart'; import 'builder.dart'; @@ -85,7 +85,7 @@ abstract final class BuildOutput { /// /// The [Asset]s produced by this build or dry-run can be provided to the /// constructor as [assets], or can be added later using [addAsset] and - /// [addAssets]. In dry runs, the [Architecture] for [NativeCodeAsset]s can be + /// [addAssets]. In dry runs, the [Architecture] for [CodeAsset]s can be /// omitted. /// /// The files used by this build must be provided to the constructor as @@ -106,7 +106,7 @@ abstract final class BuildOutput { }) => HookOutputImpl( timestamp: timestamp, - assets: assets?.cast().toList(), + assets: assets?.cast().toList(), dependencies: Dependencies([...?dependencies]), metadata: Metadata({...?metadata}), ); diff --git a/pkgs/native_assets_cli/lib/src/api/data_asset.dart b/pkgs/native_assets_cli/lib/src/api/data_asset.dart deleted file mode 100644 index 4c9459d43..000000000 --- a/pkgs/native_assets_cli/lib/src/api/data_asset.dart +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file -// 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. - -part of 'asset.dart'; - -/// Data bundled with a Dart or Flutter application. -/// -/// A data asset is accessible in a Dart or Flutter application. To retrieve an -/// asset at runtime, the [id] is used. This enables access to the asset -/// irrespective of how and where the application is run. -/// -/// An data asset must provide a [Asset.file]. The Dart and Flutter SDK will -/// bundle this code in the final application. -abstract final class DataAsset implements Asset { - /// Constructs a data asset. - /// - /// The unique [id] of this asset is a uri `package:/` from - /// [package] and [name]. - factory DataAsset({ - required String package, - required String name, - required Uri file, - }) => - DataAssetImpl( - name: name, - package: package, - file: file, - ); - - /// The package which contains this asset. - String get package; - - /// The name of this asset, which must be unique for the package. - String get name; - - static const String type = 'data'; -} diff --git a/pkgs/native_assets_cli/lib/src/api/hook_config.dart b/pkgs/native_assets_cli/lib/src/api/hook_config.dart index 64271462b..de31b34a1 100644 --- a/pkgs/native_assets_cli/lib/src/api/hook_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/hook_config.dart @@ -10,6 +10,7 @@ import 'package:crypto/crypto.dart'; import 'package:pub_semver/pub_semver.dart'; import '../architecture.dart'; +import '../asset.dart'; import '../build_mode.dart'; import '../c_compiler_config.dart'; import '../ios_sdk.dart'; @@ -21,7 +22,6 @@ import '../model/metadata.dart'; import '../model/target.dart'; import '../os.dart'; import '../utils/map.dart'; -import 'asset.dart'; import 'build_config.dart'; import 'link_config.dart'; @@ -143,10 +143,10 @@ abstract class HookConfig { /// The asset types that the invoker of this hook supports. /// /// Currently known values: - /// * [NativeCodeAsset.type] + /// * [CodeAsset.type] /// * [DataAsset.type] Iterable get supportedAssetTypes; - /// The preferred [LinkMode] method for [NativeCodeAsset]s. + /// The preferred [LinkMode] method for [CodeAsset]s. LinkModePreference get linkModePreference; } diff --git a/pkgs/native_assets_cli/lib/src/api/link_config.dart b/pkgs/native_assets_cli/lib/src/api/link_config.dart index 318298046..0353d0512 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_config.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_config.dart @@ -10,6 +10,7 @@ import 'package:pub_semver/pub_semver.dart'; import '../architecture.dart'; import '../args_parser.dart'; +import '../asset.dart'; import '../build_mode.dart'; import '../c_compiler_config.dart'; import '../ios_sdk.dart'; @@ -18,7 +19,6 @@ import '../link_mode_preference.dart'; import '../model/hook.dart'; import '../os.dart'; import '../utils/map.dart'; -import 'asset.dart'; import 'build_config.dart'; import 'hook_config.dart'; diff --git a/pkgs/native_assets_cli/lib/src/api/link_output.dart b/pkgs/native_assets_cli/lib/src/api/link_output.dart index 1f569f262..77b7bb030 100644 --- a/pkgs/native_assets_cli/lib/src/api/link_output.dart +++ b/pkgs/native_assets_cli/lib/src/api/link_output.dart @@ -54,7 +54,7 @@ abstract final class LinkOutput { void addAssets(Iterable assets); factory LinkOutput({ - Iterable? assets, + Iterable? assets, Dependencies? dependencies, DateTime? timestamp, }) => diff --git a/pkgs/native_assets_cli/lib/src/api/test.dart b/pkgs/native_assets_cli/lib/src/api/test.dart index 167a248fd..4efd0dbd0 100644 --- a/pkgs/native_assets_cli/lib/src/api/test.dart +++ b/pkgs/native_assets_cli/lib/src/api/test.dart @@ -9,12 +9,12 @@ import 'package:test/test.dart'; import 'package:yaml/yaml.dart'; import '../architecture.dart'; +import '../asset.dart'; import '../build_mode.dart'; import '../c_compiler_config.dart'; import '../ios_sdk.dart'; import '../link_mode_preference.dart'; import '../os.dart'; -import 'asset.dart'; import 'build_config.dart'; import 'build_output.dart'; @@ -81,8 +81,8 @@ Future testBuildHook({ final file = File.fromUri(asset.file!); expect(await file.exists(), true); } - if (allAssets.any((asset) => asset is NativeCodeAsset)) { - expect(buildConfig.supportedAssetTypes, NativeCodeAsset.type); + if (allAssets.any((asset) => asset is CodeAsset)) { + expect(buildConfig.supportedAssetTypes, CodeAsset.type); } if (allAssets.any((asset) => asset is DataAsset)) { expect(buildConfig.supportedAssetTypes, DataAsset.type); diff --git a/pkgs/native_assets_cli/lib/src/api/asset.dart b/pkgs/native_assets_cli/lib/src/asset.dart similarity index 60% rename from pkgs/native_assets_cli/lib/src/api/asset.dart rename to pkgs/native_assets_cli/lib/src/asset.dart index 95de679b0..ef2385bdc 100644 --- a/pkgs/native_assets_cli/lib/src/api/asset.dart +++ b/pkgs/native_assets_cli/lib/src/asset.dart @@ -2,21 +2,16 @@ // 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:pub_semver/pub_semver.dart'; +import 'api/build_config.dart'; +import 'api/build_output.dart'; +import 'architecture.dart'; +import 'json_utils.dart'; +import 'link_mode.dart'; +import 'os.dart'; +import 'utils/map.dart'; -import '../architecture.dart'; -import '../link_mode.dart'; -import '../os.dart'; -import '../utils/json.dart'; -import '../utils/map.dart'; -import 'build_config.dart'; -import 'build_output.dart'; - -part '../model/asset.dart'; -part '../model/data_asset.dart'; -part '../model/native_code_asset.dart'; +part 'code_asset.dart'; part 'data_asset.dart'; -part 'native_code_asset.dart'; /// Data or code bundled with a Dart or Flutter application. /// @@ -34,7 +29,7 @@ abstract final class Asset { /// different packages. /// /// The default asset id for an asset reference from `lib/src/foo.dart` is - /// `'package:foo/src/foo.dart'`. For example a [NativeCodeAsset] can be accessed + /// `'package:foo/src/foo.dart'`. For example a [CodeAsset] can be accessed /// via `@Native` with the `assetId` argument omitted: /// /// ```dart @@ -63,4 +58,38 @@ abstract final class Asset { /// already present on the target system or an asset already present in Dart /// or Flutter. Uri? get file; + + /// A json representation of this [Asset]. + Map toJson(); + + static List listFromJson(List? list) { + final assets = []; + if (list == null) return assets; + for (var i = 0; i < list.length; ++i) { + final jsonMap = list.mapAt(i); + final type = jsonMap[_typeKey]; + switch (type) { + case CodeAsset.type: + assets.add(CodeAsset.fromJson(jsonMap)); + case DataAsset.type: + assets.add(DataAsset.fromJson(jsonMap)); + default: + // Do nothing, some other launcher might define it's own asset types. + } + } + return assets; + } + + static List> listToJson(Iterable assets) => [ + for (final asset in assets) asset.toJson(), + ]; } + +const _architectureKey = 'architecture'; +const _fileKey = 'file'; +const _idKey = 'id'; +const _linkModeKey = 'link_mode'; +const _nameKey = 'name'; +const _osKey = 'os'; +const _packageKey = 'package'; +const _typeKey = 'type'; diff --git a/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart b/pkgs/native_assets_cli/lib/src/code_asset.dart similarity index 65% rename from pkgs/native_assets_cli/lib/src/api/native_code_asset.dart rename to pkgs/native_assets_cli/lib/src/code_asset.dart index 49353cd36..51789000e 100644 --- a/pkgs/native_assets_cli/lib/src/api/native_code_asset.dart +++ b/pkgs/native_assets_cli/lib/src/code_asset.dart @@ -45,25 +45,26 @@ part of 'asset.dart'; /// "manually", the Dart or Flutter SDK will take care of copying the asset /// [file] from its specified location on the current system into the /// application bundle. -abstract final class NativeCodeAsset implements Asset { +final class CodeAsset implements Asset { + /// The id of this code asset. + @override + final String id; + /// The operating system this asset can run on. - OS get os; + final OS os; /// The architecture this asset can run on. /// /// Not available during a [BuildConfig.dryRun]. - Architecture? get architecture; + final Architecture? architecture; /// The link mode for this native code. /// /// Either dynamic loading or static linking. - LinkMode get linkMode; + final LinkMode linkMode; /// The file to be bundled with the Dart or Flutter application. /// - /// How this file is bundled depends on the kind of asset, represented by a - /// concrete subtype of [Asset], and the SDK (Dart or Flutter). - /// /// If the [linkMode] is [DynamicLoadingBundled], the file name must be /// provided in the [BuildOutput] for [BuildConfig.dryRun]. Supplying a file /// name instead of an absolute path is enough for [BuildConfig.dryRun]. The @@ -73,28 +74,118 @@ abstract final class NativeCodeAsset implements Asset { /// [LookupInExecutable] the file must be omitted in the [BuildOutput] for /// [BuildConfig.dryRun]. @override - Uri? get file; + final Uri? file; /// Constructs a native code asset. /// /// The [id] of this asset is a uri `package:/` from [package] /// and [name]. - factory NativeCodeAsset({ + CodeAsset({ required String package, required String name, required LinkMode linkMode, required OS os, Uri? file, Architecture? architecture, + }) : this._( + id: 'package:$package/$name', + linkMode: linkMode, + os: os, + file: file, + architecture: architecture, + ); + + CodeAsset._({ + required this.id, + required this.linkMode, + required this.os, + required this.file, + required this.architecture, + }) { + if (linkMode is DynamicLoading && + linkMode is! DynamicLoadingBundled && + file != null) { + throw ArgumentError.value( + file, + 'file', + 'Must be null if dynamicLoading is not BundledDylib.', + ); + } + } + + factory CodeAsset.fromJson(Map jsonMap) { + final linkMode = LinkMode.fromJson(jsonMap.map$(_linkModeKey)); + final fileString = jsonMap.optionalString(_fileKey); + final Uri? file; + if (fileString != null) { + file = Uri(path: fileString); + } else { + file = null; + } + final Architecture? architecture; + final os = OS.fromString(jsonMap.string(_osKey)); + final architectureString = jsonMap.optionalString(_architectureKey); + if (architectureString != null) { + architecture = Architecture.fromString(architectureString); + } else { + architecture = null; + } + + return CodeAsset._( + id: jsonMap.string(_idKey), + os: os, + architecture: architecture, + linkMode: linkMode, + file: file, + ); + } + + CodeAsset copyWith({ + LinkMode? linkMode, + String? id, + OS? os, + Architecture? architecture, + Uri? file, }) => - NativeCodeAssetImpl( - id: 'package:$package/$name', - linkMode: linkMode, - os: os, - architecture: architecture, - file: file, + CodeAsset._( + id: id ?? this.id, + linkMode: linkMode ?? this.linkMode, + os: os ?? this.os, + architecture: architecture ?? this.architecture, + file: file ?? this.file, + ); + + @override + bool operator ==(Object other) { + if (other is! CodeAsset) { + return false; + } + return other.id == id && + other.linkMode == linkMode && + other.architecture == architecture && + other.os == os && + other.file == file; + } + + @override + int get hashCode => Object.hash( + id, + linkMode, + architecture, + os, + file, ); + @override + Map toJson() => { + if (architecture != null) _architectureKey: architecture.toString(), + if (file != null) _fileKey: file!.toFilePath(), + _idKey: id, + _linkModeKey: linkMode.toJson(), + _osKey: os.toString(), + _typeKey: CodeAsset.type, + }..sortOnKey(); + static const String type = 'native_code'; } diff --git a/pkgs/native_assets_cli/lib/src/data_asset.dart b/pkgs/native_assets_cli/lib/src/data_asset.dart new file mode 100644 index 000000000..f8493b875 --- /dev/null +++ b/pkgs/native_assets_cli/lib/src/data_asset.dart @@ -0,0 +1,87 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// 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. + +part of 'asset.dart'; + +/// Data bundled with a Dart or Flutter application. +/// +/// A data asset is accessible in a Dart or Flutter application. To retrieve an +/// asset at runtime, the [id] is used. This enables access to the asset +/// irrespective of how and where the application is run. +/// +/// An data asset must provide a [Asset.file]. The Dart and Flutter SDK will +/// bundle this code in the final application. +final class DataAsset extends Asset { + /// The file to be bundled with the Dart or Flutter application. + /// + /// The file can be omitted in the [BuildOutput] for [BuildConfig.dryRun]. + /// + /// The file can also be omitted for asset types which refer to an asset + /// already present on the target system or an asset already present in Dart + /// or Flutter. + @override + final Uri file; + + /// The name of this asset, which must be unique for the package. + final String name; + + /// The package which contains this asset. + final String package; + + /// The identifier for this data asset. + /// + /// An [DataAsset] has a string identifier called "asset id". Dart code that + /// uses an asset references the asset using this asset id. + /// + /// An asset identifier consists of two elements, the `package` and `name`, + /// which together make a library uri `package:/`. The package + /// being part of the identifer prevents name collisions between assets of + /// different packages. + @override + String get id => 'package:$package/$name'; + + DataAsset({ + required this.file, + required this.name, + required this.package, + }); + + /// Constructs a [DataAsset] from a json representation obtained via + /// [DataAsset.toJson]. + factory DataAsset.fromJson(Map jsonMap) => DataAsset( + name: jsonMap.string(_nameKey), + package: jsonMap.string(_packageKey), + file: jsonMap.path(_fileKey), + ); + + @override + bool operator ==(Object other) { + if (other is! DataAsset) { + return false; + } + return other.package == package && + other.file.toFilePath() == file.toFilePath() && + other.name == name; + } + + @override + int get hashCode => Object.hash( + package, + name, + file.toFilePath(), + ); + + @override + Map toJson() => { + _nameKey: name, + _packageKey: package, + _fileKey: file.toFilePath(), + _typeKey: DataAsset.type, + }..sortOnKey(); + + @override + String toString() => 'DataAsset(${toJson()})'; + + static const String type = 'data'; +} diff --git a/pkgs/native_assets_cli/lib/src/json_utils.dart b/pkgs/native_assets_cli/lib/src/json_utils.dart index e64be65f7..e177ff4a1 100644 --- a/pkgs/native_assets_cli/lib/src/json_utils.dart +++ b/pkgs/native_assets_cli/lib/src/json_utils.dart @@ -6,7 +6,7 @@ import 'dart:core'; import 'dart:core' as core; import 'dart:io'; -extension JsonUtils on Map { +extension MapJsonUtils on Map { String string(String key, {Iterable? validValues}) { final value = get(key); if (validValues != null && !validValues.contains(value)) { @@ -43,6 +43,7 @@ extension JsonUtils on Map { List list(String key) => get>(key); List? optionalList(String key) => getOptional>(key); + Map map$(String key) => get>(key); Map? optionalMap(String key) => getOptional>(key); @@ -66,6 +67,21 @@ extension JsonUtils on Map { } } +extension ListJsonUtils on List { + T get(int index) { + final value = this[index]; + if (value == null) { + throw FormatException('No value was provided for index: $index'); + } + if (value is T) return value; + throw FormatException( + 'Unexpected value \'$value\' for index \'.$index\' in config file. ' + 'Expected a $T.'); + } + + Map mapAt(int index) => get>(index); +} + void _throwIfNotExists(String key, Uri value) { final fileSystemEntity = value.fileSystemEntity; if (!fileSystemEntity.existsSync()) { diff --git a/pkgs/native_assets_cli/lib/src/link_mode.dart b/pkgs/native_assets_cli/lib/src/link_mode.dart index f258a4302..53b3295c7 100644 --- a/pkgs/native_assets_cli/lib/src/link_mode.dart +++ b/pkgs/native_assets_cli/lib/src/link_mode.dart @@ -2,10 +2,10 @@ // 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 'api/asset.dart'; import 'api/build_config.dart'; +import 'asset.dart'; -/// The link mode for a [NativeCodeAsset]. +/// The link mode for a [CodeAsset]. /// /// Known linking modes: /// @@ -53,7 +53,7 @@ abstract final class LinkMode { }; } -/// The [NativeCodeAsset] will be loaded at runtime. +/// The [CodeAsset] will be loaded at runtime. /// /// Nothing happens at native code linking time. /// @@ -72,10 +72,10 @@ abstract final class DynamicLoading extends LinkMode { /// looked up in this dynamic library. /// /// An asset with this dynamic loading method must provide a -/// [NativeCodeAsset.file]. The Dart and Flutter SDK will bundle this code in +/// [CodeAsset.file]. The Dart and Flutter SDK will bundle this code in /// the final application. /// -/// During a [BuildConfig.dryRun], the [NativeCodeAsset.file] can be a file name +/// During a [BuildConfig.dryRun], the [CodeAsset.file] can be a file name /// instead of a the full path. The file does not have to exist during a dry /// run. final class DynamicLoadingBundled extends DynamicLoading { diff --git a/pkgs/native_assets_cli/lib/src/link_mode_preference.dart b/pkgs/native_assets_cli/lib/src/link_mode_preference.dart index 8a1eb614f..f1d667515 100644 --- a/pkgs/native_assets_cli/lib/src/link_mode_preference.dart +++ b/pkgs/native_assets_cli/lib/src/link_mode_preference.dart @@ -2,9 +2,9 @@ // 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 'api/asset.dart'; +import 'asset.dart'; -/// The preferred linkMode method for [NativeCodeAsset]s. +/// The preferred linkMode method for [CodeAsset]s. final class LinkModePreference { /// The name for this link mode. final String name; diff --git a/pkgs/native_assets_cli/lib/src/model/asset.dart b/pkgs/native_assets_cli/lib/src/model/asset.dart deleted file mode 100644 index 62e50308a..000000000 --- a/pkgs/native_assets_cli/lib/src/model/asset.dart +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file -// 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. - -part of '../api/asset.dart'; - -abstract final class AssetImpl implements Asset { - Map toJson(Version version); - - static List listFromJson(List? list) { - final assets = []; - if (list == null) return assets; - for (final jsonElement in list) { - final jsonMap = as>(jsonElement); - final type = jsonMap[NativeCodeAssetImpl.typeKey]; - switch (type) { - case NativeCodeAsset.type: - assets.add(NativeCodeAssetImpl.fromJson(jsonMap)); - case DataAsset.type: - assets.add(DataAssetImpl.fromJson(jsonMap)); - default: - // Do nothing, some other launcher might define it's own asset types. - } - } - return assets; - } - - static List> listToJson( - Iterable assets, Version version) => - [ - for (final asset in assets) asset.toJson(version), - ]; -} 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 211613b6b..c7a64f4e5 100644 --- a/pkgs/native_assets_cli/lib/src/model/build_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/build_config.dart @@ -43,7 +43,7 @@ final class BuildConfigImpl extends HookConfigImpl implements BuildConfig { static List _supportedAssetTypesBackwardsCompatibility( Iterable? supportedAssetTypes, ) => - supportedAssetTypes?.toList() ?? [NativeCodeAsset.type]; + supportedAssetTypes?.toList() ?? [CodeAsset.type]; BuildConfigImpl({ required super.outputDirectory, diff --git a/pkgs/native_assets_cli/lib/src/model/data_asset.dart b/pkgs/native_assets_cli/lib/src/model/data_asset.dart deleted file mode 100644 index 6c298147d..000000000 --- a/pkgs/native_assets_cli/lib/src/model/data_asset.dart +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// 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. - -part of '../api/asset.dart'; - -final class DataAssetImpl implements DataAsset, AssetImpl { - @override - final Uri file; - - @override - final String name; - - @override - final String package; - - @override - String get id => 'package:$package/$name'; - - DataAssetImpl({ - required this.file, - required this.name, - required this.package, - }); - - factory DataAssetImpl.fromJson(Map jsonMap) => - DataAssetImpl( - name: get(jsonMap, _nameKey), - package: get(jsonMap, _packageKey), - file: Uri(path: get(jsonMap, _fileKey)), - ); - - @override - bool operator ==(Object other) { - if (other is! DataAssetImpl) { - return false; - } - return other.package == package && - other.file.toFilePath() == file.toFilePath() && - other.name == name; - } - - @override - int get hashCode => Object.hash( - package, - name, - file.toFilePath(), - ); - - @override - Map toJson(Version version) => { - _nameKey: name, - _packageKey: package, - _fileKey: file.toFilePath(), - typeKey: DataAsset.type, - }..sortOnKey(); - - static const typeKey = 'type'; - static const _nameKey = 'name'; - static const _packageKey = 'package'; - static const _fileKey = 'file'; - - @override - String toString() => 'DataAsset(${toJson(BuildOutput.latestVersion)})'; -} diff --git a/pkgs/native_assets_cli/lib/src/model/hook_config.dart b/pkgs/native_assets_cli/lib/src/model/hook_config.dart index 8cba1dbb9..5445e76fd 100644 --- a/pkgs/native_assets_cli/lib/src/model/hook_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/hook_config.dart @@ -367,8 +367,7 @@ abstract class HookConfigImpl implements HookConfig { } static List parseSupportedAssetTypes(Map config) => - config.optionalStringList(supportedAssetTypesKey) ?? - [NativeCodeAsset.type]; + config.optionalStringList(supportedAssetTypesKey) ?? [CodeAsset.type]; static CCompilerConfig parseCCompiler( Map config, bool dryRun) { @@ -491,7 +490,7 @@ can _only_ depend on OS.'''); entry.key, json.encode(entry.value.toJson()), ], - ...supportedAssetTypes ?? [NativeCodeAsset.type], + ...supportedAssetTypes ?? [CodeAsset.type], hook.name, linkingEnabled, ].join('###'); @@ -518,7 +517,7 @@ can _only_ depend on OS.'''); packageName, targetOS.toString(), linkModePreference.toString(), - ...supportedAssetTypes ?? [NativeCodeAsset.type], + ...supportedAssetTypes ?? [CodeAsset.type], hook.name, linkingEnabled, ].join('###'); diff --git a/pkgs/native_assets_cli/lib/src/model/hook_output.dart b/pkgs/native_assets_cli/lib/src/model/hook_output.dart index a87a85be2..ac8060586 100644 --- a/pkgs/native_assets_cli/lib/src/model/hook_output.dart +++ b/pkgs/native_assets_cli/lib/src/model/hook_output.dart @@ -8,15 +8,15 @@ final class HookOutputImpl implements BuildOutput, LinkOutput { @override final DateTime timestamp; - final List _assets; + final List _assets; @override - Iterable get assets => _assets; + Iterable get assets => _assets; - final Map> _assetsForLinking; + final Map> _assetsForLinking; @override - Map> get assetsForLinking => _assetsForLinking; + Map> get assetsForLinking => _assetsForLinking; final Dependencies _dependencies; @@ -29,8 +29,8 @@ final class HookOutputImpl implements BuildOutput, LinkOutput { HookOutputImpl({ DateTime? timestamp, - Iterable? assets, - Map>? assetsForLinking, + Iterable? assets, + Map>? assetsForLinking, Dependencies? dependencies, Metadata? metadata, }) : timestamp = (timestamp ?? DateTime.now()).roundDownToSeconds(), @@ -81,11 +81,11 @@ final class HookOutputImpl implements BuildOutput, LinkOutput { } return HookOutputImpl( timestamp: DateTime.parse(get(jsonMap, _timestampKey)), - assets: AssetImpl.listFromJson(get?>(jsonMap, _assetsKey)), - assetsForLinking: get?>( - jsonMap, _assetsForLinkingKey) - ?.map((packageName, assets) => MapEntry( - packageName, AssetImpl.listFromJson(as>(assets)))), + assets: Asset.listFromJson(get?>(jsonMap, _assetsKey)), + assetsForLinking: + get?>(jsonMap, _assetsForLinkingKey)?.map( + (packageName, assets) => MapEntry( + packageName, Asset.listFromJson(as>(assets)))), dependencies: Dependencies.fromJson(get?>(jsonMap, _dependenciesKey)), metadata: @@ -95,13 +95,12 @@ final class HookOutputImpl implements BuildOutput, LinkOutput { Map toJson(Version version) => { _timestampKey: timestamp.toString(), - if (_assets.isNotEmpty) - _assetsKey: AssetImpl.listToJson(_assets, version), + if (_assets.isNotEmpty) _assetsKey: Asset.listToJson(_assets), if (_assetsForLinking.isNotEmpty) _assetsForLinkingKey: _assetsForLinking.map((packageName, assets) => MapEntry( packageName, - AssetImpl.listToJson(assets, version), + Asset.listToJson(assets), )), if (_dependencies.dependencies.isNotEmpty) _dependenciesKey: _dependencies.toJson(), @@ -155,7 +154,7 @@ final class HookOutputImpl implements BuildOutput, LinkOutput { return false; } return other.timestamp == timestamp && - const ListEquality().equals(other._assets, _assets) && + const ListEquality().equals(other._assets, _assets) && other._dependencies == _dependencies && other.metadata == metadata; } @@ -163,7 +162,7 @@ final class HookOutputImpl implements BuildOutput, LinkOutput { @override int get hashCode => Object.hash( timestamp.hashCode, - const ListEquality().hash(_assets), + const ListEquality().hash(_assets), _dependencies, metadata, ); @@ -182,7 +181,7 @@ final class HookOutputImpl implements BuildOutput, LinkOutput { @override void addAsset(Asset asset, {String? linkInPackage}) { - _getAssetList(linkInPackage).add(asset as AssetImpl); + _getAssetList(linkInPackage).add(asset); } @override @@ -190,11 +189,11 @@ final class HookOutputImpl implements BuildOutput, LinkOutput { _getAssetList(linkInPackage).addAll(assets.cast()); } - List _getAssetList(String? linkInPackage) => linkInPackage == null + List _getAssetList(String? linkInPackage) => linkInPackage == null ? _assets : (_assetsForLinking[linkInPackage] ??= []); - HookOutputImpl copyWith({Iterable? assets}) => HookOutputImpl( + HookOutputImpl copyWith({Iterable? assets}) => HookOutputImpl( timestamp: timestamp, assets: assets?.toList() ?? _assets, assetsForLinking: assetsForLinking, diff --git a/pkgs/native_assets_cli/lib/src/model/link_config.dart b/pkgs/native_assets_cli/lib/src/model/link_config.dart index bf10c27c6..f3507979a 100644 --- a/pkgs/native_assets_cli/lib/src/model/link_config.dart +++ b/pkgs/native_assets_cli/lib/src/model/link_config.dart @@ -14,7 +14,7 @@ class LinkConfigImpl extends HookConfigImpl implements LinkConfig { static const assetsKey = 'assets'; @override - final Iterable assets; + final Iterable assets; // TODO: Placeholder for the resources.json file URL. We don't want to change // native_assets_builder when implementing the parsing. @@ -43,7 +43,7 @@ class LinkConfigImpl extends HookConfigImpl implements LinkConfig { }) : super( hook: Hook.link, version: version ?? HookConfigImpl.latestVersion, - supportedAssetTypes: supportedAssetTypes ?? [NativeCodeAsset.type], + supportedAssetTypes: supportedAssetTypes ?? [CodeAsset.type], ); LinkConfigImpl.dryRun({ @@ -60,7 +60,7 @@ class LinkConfigImpl extends HookConfigImpl implements LinkConfig { }) : super.dryRun( hook: Hook.link, version: version ?? HookConfigImpl.latestVersion, - supportedAssetTypes: supportedAssetTypes ?? [NativeCodeAsset.type], + supportedAssetTypes: supportedAssetTypes ?? [CodeAsset.type], ); @override @@ -74,7 +74,7 @@ class LinkConfigImpl extends HookConfigImpl implements LinkConfig { ...hookToJson(), if (recordedUsagesFile != null) resourceIdentifierKey: recordedUsagesFile!.toFilePath(), - assetsKey: AssetImpl.listToJson(assets, version), + assetsKey: Asset.listToJson(assets), }.sortOnKey(); static LinkConfig fromArguments(List arguments) { @@ -118,8 +118,8 @@ class LinkConfigImpl extends HookConfigImpl implements LinkConfig { static Uri? parseRecordedUsagesUri(Map config) => config.optionalPath(resourceIdentifierKey); - static List parseAssets(Map config) => - AssetImpl.listFromJson(config.optionalList(assetsKey)); + static List parseAssets(Map config) => + Asset.listFromJson(config.optionalList(assetsKey)); @override bool operator ==(Object other) { diff --git a/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart b/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart deleted file mode 100644 index fcf14eef9..000000000 --- a/pkgs/native_assets_cli/lib/src/model/native_code_asset.dart +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file -// 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. - -part of '../api/asset.dart'; - -final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl { - @override - final Uri? file; - - @override - final LinkMode linkMode; - - @override - final String id; - - @override - final OS os; - - @override - final Architecture? architecture; - - NativeCodeAssetImpl({ - this.file, - required this.id, - required this.linkMode, - required this.os, - this.architecture, - }) { - if (linkMode is DynamicLoading && - linkMode is! DynamicLoadingBundled && - file != null) { - throw ArgumentError.value( - file, - 'file', - 'Must be null if dynamicLoading is not BundledDylib.', - ); - } - } - - factory NativeCodeAssetImpl.fromJson(Map jsonMap) { - final linkModeJson = jsonMap[_linkModeKey]; - final linkMode = LinkMode.fromJson(as>(linkModeJson)); - final fileString = get(jsonMap, _fileKey); - final Uri? file; - if (fileString != null) { - file = Uri(path: fileString); - } else { - file = null; - } - final Architecture? architecture; - final os = OS.fromString(get(jsonMap, _osKey)); - final architectureString = get(jsonMap, _architectureKey); - if (architectureString != null) { - architecture = Architecture.fromString(architectureString); - } else { - architecture = null; - } - - return NativeCodeAssetImpl( - id: get(jsonMap, _idKey), - os: os, - architecture: architecture, - linkMode: linkMode, - file: file, - ); - } - - NativeCodeAssetImpl copyWith({ - LinkMode? linkMode, - String? id, - OS? os, - Architecture? architecture, - Uri? file, - }) => - NativeCodeAssetImpl( - id: id ?? this.id, - linkMode: linkMode ?? this.linkMode, - os: os ?? this.os, - architecture: this.architecture ?? architecture, - file: file ?? this.file, - ); - - @override - bool operator ==(Object other) { - if (other is! NativeCodeAssetImpl) { - return false; - } - return other.id == id && - other.linkMode == linkMode && - other.architecture == architecture && - other.os == os && - other.file == file; - } - - @override - int get hashCode => Object.hash( - id, - linkMode, - architecture, - os, - file, - ); - - @override - Map toJson(Version version) => { - if (architecture != null) _architectureKey: architecture.toString(), - if (file != null) _fileKey: file!.toFilePath(), - _idKey: id, - _linkModeKey: linkMode.toJson(), - _osKey: os.toString(), - typeKey: NativeCodeAsset.type, - }..sortOnKey(); - - static const typeKey = 'type'; - static const _idKey = 'id'; - static const _linkModeKey = 'link_mode'; - static const _fileKey = 'file'; - static const _osKey = 'os'; - static const _architectureKey = 'architecture'; - - @override - String toString() => - 'NativeCodeAsset(${toJson(HookOutputImpl.latestVersion)})'; -} diff --git a/pkgs/native_assets_cli/lib/src/validator/validator.dart b/pkgs/native_assets_cli/lib/src/validator/validator.dart index d8cc1b8bb..1cfc5f2ea 100644 --- a/pkgs/native_assets_cli/lib/src/validator/validator.dart +++ b/pkgs/native_assets_cli/lib/src/validator/validator.dart @@ -4,11 +4,11 @@ import 'dart:io'; -import '../api/asset.dart'; import '../api/build_config.dart'; import '../api/build_output.dart'; import '../api/hook_config.dart'; import '../api/link_config.dart'; +import '../asset.dart'; import '../link_mode.dart'; import '../link_mode_preference.dart'; @@ -26,7 +26,7 @@ Future validateBuild( ...validateAssetsForLinking(config, output), ...validateOutputAssetTypes(config, output), if (!config.dryRun) ...await validateFilesExist(config, output), - ...validateNativeCodeAssets(config, output), + ...validateCodeAssets(config, output), ...validateAssetId(config, output), if (!config.dryRun) ...validateNoDuplicateAssetIds(output), ...validateNoDuplicateDylibs(output.assets), @@ -45,7 +45,7 @@ Future validateLink( final errors = [ ...validateOutputAssetTypes(config, output), if (!config.dryRun) ...await validateFilesExist(config, output), - ...validateNativeCodeAssets(config, output), + ...validateCodeAssets(config, output), if (!config.dryRun) ...validateNoDuplicateAssetIds(output), ...validateNoDuplicateDylibs(output.assets), ]; @@ -117,8 +117,8 @@ Future> validateFilesExist( extension on Asset { String get type { switch (this) { - case NativeCodeAsset _: - return NativeCodeAsset.type; + case CodeAsset _: + return CodeAsset.type; case DataAsset _: return DataAsset.type; } @@ -132,13 +132,13 @@ extension on HookOutputImpl { } /// Native code assets for bundling should have a supported linking type. -List validateNativeCodeAssets( +List validateCodeAssets( HookConfig config, HookOutputImpl output, ) { final errors = []; final linkModePreference = config.linkModePreference; - for (final asset in output.assets.whereType()) { + for (final asset in output.assets.whereType()) { final linkMode = asset.linkMode; if ((linkMode is DynamicLoading && linkModePreference == LinkModePreference.static) || @@ -211,7 +211,7 @@ List validateNoDuplicateDylibs( ) { final errors = []; final fileNameToAssetId = >{}; - for (final asset in assets.whereType()) { + for (final asset in assets.whereType()) { if (asset.linkMode is! DynamicLoadingBundled) { continue; } diff --git a/pkgs/native_assets_cli/test/api/asset_test.dart b/pkgs/native_assets_cli/test/api/asset_test.dart index f256dcfb3..92a6af506 100644 --- a/pkgs/native_assets_cli/test/api/asset_test.dart +++ b/pkgs/native_assets_cli/test/api/asset_test.dart @@ -8,7 +8,7 @@ import 'package:test/test.dart'; void main() { test('Asset constructors', () async { final assets = [ - NativeCodeAsset( + CodeAsset( package: 'my_package', name: 'foo', file: Uri.file('path/to/libfoo.so'), @@ -16,28 +16,28 @@ void main() { os: OS.android, architecture: Architecture.x64, ), - NativeCodeAsset( + CodeAsset( package: 'my_package', name: 'foo3', linkMode: DynamicLoadingSystem(Uri(path: 'libfoo3.so')), os: OS.android, architecture: Architecture.x64, ), - NativeCodeAsset( + CodeAsset( package: 'my_package', name: 'foo4', linkMode: LookupInExecutable(), os: OS.android, architecture: Architecture.x64, ), - NativeCodeAsset( + CodeAsset( package: 'my_package', name: 'foo5', linkMode: LookupInProcess(), os: OS.android, architecture: Architecture.x64, ), - NativeCodeAsset( + CodeAsset( package: 'my_package', name: 'bar', file: Uri(path: 'path/to/libbar.a'), @@ -45,7 +45,7 @@ void main() { architecture: Architecture.arm64, linkMode: StaticLinking(), ), - NativeCodeAsset( + CodeAsset( package: 'my_package', name: 'bla', file: Uri(path: 'path/with spaces/bla.dll'), @@ -72,7 +72,7 @@ void main() { test('Errors', () { expect( - () => NativeCodeAsset( + () => CodeAsset( package: 'my_package', name: 'foo', file: Uri.file('path/to/libfoo.so'), diff --git a/pkgs/native_assets_cli/test/api/build_config_test.dart b/pkgs/native_assets_cli/test/api/build_config_test.dart index 2488ab103..0480536f7 100644 --- a/pkgs/native_assets_cli/test/api/build_config_test.dart +++ b/pkgs/native_assets_cli/test/api/build_config_test.dart @@ -69,7 +69,7 @@ void main() async { ), buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], linkingEnabled: false, ); @@ -83,7 +83,7 @@ void main() async { targetAndroidNdkApi: 30, buildMode: BuildMode.release, linkModePreference: LinkModePreference.preferStatic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], linkingEnabled: false, ); @@ -148,7 +148,7 @@ void main() async { packageRoot: packageRootUri, targetOS: OS.android, linkModePreference: LinkModePreference.preferStatic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], linkingEnabled: true, ); diff --git a/pkgs/native_assets_cli/test/api/build_output_test.dart b/pkgs/native_assets_cli/test/api/build_output_test.dart index a8e15d4cd..797703871 100644 --- a/pkgs/native_assets_cli/test/api/build_output_test.dart +++ b/pkgs/native_assets_cli/test/api/build_output_test.dart @@ -22,7 +22,7 @@ void main() { BuildOutput( timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ - NativeCodeAsset( + CodeAsset( package: 'my_package', name: 'foo', file: Uri(path: 'path/to/libfoo.so'), @@ -30,7 +30,7 @@ void main() { os: OS.android, architecture: Architecture.x64, ), - NativeCodeAsset( + CodeAsset( package: 'my_package', name: 'foo2', linkMode: DynamicLoadingSystem(Uri(path: 'path/to/libfoo2.so')), diff --git a/pkgs/native_assets_cli/test/api/link_config_test.dart b/pkgs/native_assets_cli/test/api/link_config_test.dart index a066acab1..c4cf243bd 100644 --- a/pkgs/native_assets_cli/test/api/link_config_test.dart +++ b/pkgs/native_assets_cli/test/api/link_config_test.dart @@ -63,7 +63,7 @@ void main() async { archiver: fakeAr, ), buildMode: BuildMode.release, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], assets: [], linkModePreference: LinkModePreference.preferStatic, ); @@ -77,7 +77,7 @@ void main() async { targetOS: OS.android, targetAndroidNdkApi: 30, buildMode: BuildMode.release, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], assets: [], linkModePreference: LinkModePreference.preferStatic, ); @@ -140,7 +140,7 @@ void main() async { packageName: packageName, packageRoot: packageRootUri, targetOS: OS.android, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], assets: [], linkModePreference: LinkModePreference.preferStatic, ); diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index 774c7471f..5d3df2fdf 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -14,40 +14,46 @@ void main() { final dataUri = Uri.file('path/to/data.txt'); final data2Uri = Uri.file('path/to/data.json'); final nativeCodeAssets = [ - NativeCodeAssetImpl( - id: 'package:my_package/foo', + CodeAsset( + package: 'my_package', + name: 'foo', file: fooUri, linkMode: DynamicLoadingBundled(), os: OS.android, architecture: Architecture.x64, ), - NativeCodeAssetImpl( - id: 'package:my_package/foo3', + CodeAsset( + package: 'my_package', + name: 'foo3', linkMode: DynamicLoadingSystem(foo3Uri), os: OS.android, architecture: Architecture.x64, ), - NativeCodeAssetImpl( - id: 'package:my_package/foo4', + CodeAsset( + package: 'my_package', + name: 'foo4', linkMode: LookupInExecutable(), os: OS.android, architecture: Architecture.x64, ), - NativeCodeAssetImpl( - id: 'package:my_package/foo5', + CodeAsset( + package: 'my_package', + name: 'foo5', linkMode: LookupInProcess(), os: OS.android, architecture: Architecture.x64, ), - NativeCodeAssetImpl( - id: 'package:my_package/bar', + CodeAsset( + package: 'my_package', + name: 'bar', file: barUri, os: OS.linux, architecture: Architecture.arm64, linkMode: StaticLinking(), ), - NativeCodeAssetImpl( - id: 'package:my_package/bla', + CodeAsset( + package: 'my_package', + name: 'bla', file: blaUri, linkMode: DynamicLoadingBundled(), os: OS.windows, @@ -55,12 +61,12 @@ void main() { ), ]; final dataAssets = [ - DataAssetImpl( + DataAsset( name: 'my_data_asset', package: 'my_package', file: dataUri, ), - DataAssetImpl( + DataAsset( name: 'my_data_asset2', package: 'my_package', file: data2Uri, @@ -135,11 +141,9 @@ void main() { ]; test('asset json', () { - final json = [ - for (final item in assets) item.toJson(HookOutputImpl.latestVersion) - ]; + final json = [for (final item in assets) item.toJson()]; expect(json, assetsJsonEncoding); - final assets2 = AssetImpl.listFromJson(json); + final assets2 = Asset.listFromJson(json); expect(assets, assets2); }); @@ -167,7 +171,7 @@ void main() { test('List hashCode', () async { final assets2 = nativeCodeAssets.take(3).toList(); - const equality = ListEquality(); + const equality = ListEquality(); expect(equality.hash(nativeCodeAssets) != equality.hash(assets2), true); }); diff --git a/pkgs/native_assets_cli/test/model/build_config_test.dart b/pkgs/native_assets_cli/test/model/build_config_test.dart index c41d6cce6..34dcc89ad 100644 --- a/pkgs/native_assets_cli/test/model/build_config_test.dart +++ b/pkgs/native_assets_cli/test/model/build_config_test.dart @@ -278,7 +278,7 @@ void main() async { 'out_dir_shared': outputDirectoryShared.toFilePath(), 'package_name': packageName, 'package_root': tempUri.toFilePath(), - 'supported_asset_types': [NativeCodeAsset.type], + 'supported_asset_types': [CodeAsset.type], 'target_architecture': 'arm64', 'target_ios_sdk': 'iphoneos', 'target_os': 'ios', diff --git a/pkgs/native_assets_cli/test/model/build_output_test.dart b/pkgs/native_assets_cli/test/model/build_output_test.dart index b05c99238..80c47c4ad 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -185,15 +185,17 @@ void main() { final buildOutput = HookOutputImpl( timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ - NativeCodeAssetImpl( - id: 'package:my_package/foo', + CodeAsset( + package: 'my_package', + name: 'foo', file: Uri(path: 'path/to/libfoo.so'), linkMode: DynamicLoadingBundled(), os: OS.android, architecture: Architecture.x64, ), - NativeCodeAssetImpl( - id: 'package:my_package/foo2', + CodeAsset( + package: 'my_package', + name: 'foo2', linkMode: DynamicLoadingSystem(Uri(path: 'path/to/libfoo2.so')), os: OS.android, architecture: Architecture.x64, @@ -213,8 +215,9 @@ void main() { timestamp: DateTime.parse('2022-11-10 13:25:01.000'), ); buildOutput2.addAsset( - NativeCodeAssetImpl( - id: 'package:my_package/foo', + CodeAsset( + package: 'my_package', + name: 'foo', file: Uri(path: 'path/to/libfoo.so'), linkMode: DynamicLoadingBundled(), os: OS.android, @@ -222,8 +225,9 @@ void main() { ), ); buildOutput2.addAssets([ - NativeCodeAssetImpl( - id: 'package:my_package/foo2', + CodeAsset( + package: 'my_package', + name: 'foo2', linkMode: DynamicLoadingSystem(Uri(path: 'path/to/libfoo2.so')), os: OS.android, architecture: Architecture.x64, @@ -250,27 +254,31 @@ void main() { HookOutputImpl getBuildOutput({bool withLinkedAssets = true}) => HookOutputImpl( timestamp: DateTime.parse('2022-11-10 13:25:01.000'), assets: [ - NativeCodeAssetImpl( - id: 'package:my_package/foo', + CodeAsset( + package: 'my_package', + name: 'foo', file: Uri(path: 'path/to/libfoo.so'), linkMode: DynamicLoadingBundled(), os: OS.android, architecture: Architecture.x64, ), - NativeCodeAssetImpl( - id: 'package:my_package/foo2', + CodeAsset( + package: 'my_package', + name: 'foo2', linkMode: DynamicLoadingSystem(Uri(path: 'path/to/libfoo2.so')), os: OS.android, architecture: Architecture.x64, ), - NativeCodeAssetImpl( - id: 'package:my_package/foo3', + CodeAsset( + package: 'my_package', + name: 'foo3', linkMode: LookupInProcess(), os: OS.android, architecture: Architecture.x64, ), - NativeCodeAssetImpl( - id: 'package:my_package/foo4', + CodeAsset( + package: 'my_package', + name: 'foo4', linkMode: LookupInExecutable(), os: OS.android, architecture: Architecture.x64, @@ -279,14 +287,14 @@ HookOutputImpl getBuildOutput({bool withLinkedAssets = true}) => HookOutputImpl( assetsForLinking: withLinkedAssets ? { 'my_package': [ - DataAssetImpl( + DataAsset( file: Uri.file('path/to/data'), name: 'data', package: 'my_package', ) ], 'my_package_2': [ - DataAssetImpl( + DataAsset( file: Uri.file('path/to/data2'), name: 'data', package: 'my_package', diff --git a/pkgs/native_assets_cli/test/model/checksum_test.dart b/pkgs/native_assets_cli/test/model/checksum_test.dart index 140689cf4..f005c213b 100644 --- a/pkgs/native_assets_cli/test/model/checksum_test.dart +++ b/pkgs/native_assets_cli/test/model/checksum_test.dart @@ -24,7 +24,7 @@ void main() { targetOS: OS.linux, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], hook: Hook.build, version: HookConfigImpl.latestVersion, linkingEnabled: true, @@ -95,7 +95,7 @@ void main() { targetOS: OS.linux, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], hook: Hook.build, version: HookConfigImpl.latestVersion, linkingEnabled: false, diff --git a/pkgs/native_assets_cli/test/model/link_config_test.dart b/pkgs/native_assets_cli/test/model/link_config_test.dart index 8339f2ffe..64fa70f5e 100644 --- a/pkgs/native_assets_cli/test/model/link_config_test.dart +++ b/pkgs/native_assets_cli/test/model/link_config_test.dart @@ -27,7 +27,7 @@ void main() async { name: 'name', file: Uri.file('nonexistent'), ), - NativeCodeAsset( + CodeAsset( package: packageName, name: 'name2', linkMode: DynamicLoadingBundled(), @@ -35,7 +35,7 @@ void main() async { file: Uri.file('not there'), architecture: Architecture.riscv64, ) - ].cast(); + ]; setUp(() async { tempUri = (await Directory.systemTemp.createTemp()).uri; @@ -145,7 +145,7 @@ void main() async { 'target_architecture': 'arm64', 'target_os': 'android', 'version': HookOutputImpl.latestVersion.toString(), - 'assets': AssetImpl.listToJson(assets, HookOutputImpl.latestVersion), + 'assets': Asset.listToJson(assets), }; final fromConfig = LinkConfigImpl.fromJson(config); @@ -223,14 +223,14 @@ void main() async { final jsonObject = buildConfig1.toJson(); final expectedJson = { - 'assets': AssetImpl.listToJson(assets, HookConfigImpl.latestVersion), + 'assets': Asset.listToJson(assets), 'build_mode': 'release', 'c_compiler': {'cc': fakeClang.toFilePath(), 'ld': fakeLd.toFilePath()}, 'out_dir': outDir.toFilePath(), 'out_dir_shared': outputDirectoryShared.toFilePath(), 'package_name': packageName, 'package_root': tempUri.toFilePath(), - 'supported_asset_types': [NativeCodeAsset.type], + 'supported_asset_types': [CodeAsset.type], 'target_architecture': 'arm64', 'target_ios_sdk': 'iphoneos', 'target_os': 'ios', diff --git a/pkgs/native_assets_cli/test/validator/validator_test.dart b/pkgs/native_assets_cli/test/validator/validator_test.dart index 5e6102d27..fa831a5bd 100644 --- a/pkgs/native_assets_cli/test/validator/validator_test.dart +++ b/pkgs/native_assets_cli/test/validator/validator_test.dart @@ -41,14 +41,14 @@ void main() { targetIOSSdk: IOSSdk.iPhoneOS, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], linkingEnabled: false, ); final output = BuildOutput(); final assetFile = File.fromUri(outDirUri.resolve('foo.dylib')); await assetFile.writeAsBytes([1, 2, 3]); output.addAsset( - NativeCodeAsset( + CodeAsset( package: config.packageName, name: 'foo.dart', file: assetFile.uri, @@ -77,7 +77,7 @@ void main() { targetIOSSdk: IOSSdk.iPhoneOS, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], linkingEnabled: false, ); final output = BuildOutput(); @@ -136,11 +136,11 @@ void main() { targetIOSSdk: IOSSdk.iPhoneOS, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], linkingEnabled: false, ); final output = BuildOutput(); - output.addAsset(NativeCodeAsset( + output.addAsset(CodeAsset( package: config.packageName, name: 'foo.dylib', architecture: config.targetArchitecture, @@ -170,14 +170,14 @@ void main() { targetIOSSdk: IOSSdk.iPhoneOS, buildMode: BuildMode.release, linkModePreference: linkModePreference, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], linkingEnabled: false, ); final output = BuildOutput(); final assetFile = File.fromUri(outDirUri.resolve('foo.dylib')); await assetFile.writeAsBytes([1, 2, 3]); output.addAsset( - NativeCodeAsset( + CodeAsset( package: config.packageName, name: 'foo.dart', file: assetFile.uri, @@ -208,14 +208,14 @@ void main() { targetIOSSdk: IOSSdk.iPhoneOS, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], linkingEnabled: false, ); final output = BuildOutput(); final assetFile = File.fromUri(outDirUri.resolve('foo.dylib')); await assetFile.writeAsBytes([1, 2, 3]); output.addAsset( - NativeCodeAsset( + CodeAsset( package: config.packageName, name: 'foo.dart', file: assetFile.uri, @@ -245,14 +245,14 @@ void main() { targetIOSSdk: IOSSdk.iPhoneOS, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], linkingEnabled: false, ); final output = BuildOutput(); final assetFile = File.fromUri(outDirUri.resolve('foo.dylib')); await assetFile.writeAsBytes([1, 2, 3]); output.addAsset( - NativeCodeAsset( + CodeAsset( package: config.packageName, name: 'foo.dart', file: assetFile.uri, @@ -281,14 +281,14 @@ void main() { targetIOSSdk: IOSSdk.iPhoneOS, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], linkingEnabled: false, ); final output = BuildOutput(); final assetFile = File.fromUri(outDirUri.resolve('foo.dylib')); await assetFile.writeAsBytes([1, 2, 3]); output.addAsset( - NativeCodeAsset( + CodeAsset( package: config.packageName, name: 'foo.dart', file: assetFile.uri, @@ -385,7 +385,7 @@ void main() { targetIOSSdk: IOSSdk.iPhoneOS, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], assets: [], ); final output = LinkOutput(); @@ -415,7 +415,7 @@ void main() { targetIOSSdk: IOSSdk.iPhoneOS, buildMode: BuildMode.release, linkModePreference: LinkModePreference.dynamic, - supportedAssetTypes: [NativeCodeAsset.type], + supportedAssetTypes: [CodeAsset.type], linkingEnabled: false, ); final output = BuildOutput(); @@ -423,7 +423,7 @@ void main() { final assetFile = File.fromUri(outDirUri.resolve(fileName)); await assetFile.writeAsBytes([1, 2, 3]); output.addAssets([ - NativeCodeAsset( + CodeAsset( package: config.packageName, name: 'src/foo.dart', file: assetFile.uri, @@ -431,7 +431,7 @@ void main() { os: config.targetOS, architecture: config.targetArchitecture, ), - NativeCodeAsset( + CodeAsset( package: config.packageName, name: 'src/bar.dart', file: assetFile.uri, diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index d2202b737..1f38aa885 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -164,7 +164,7 @@ class CBuilder extends CTool implements Builder { if (assetName != null) { output.addAssets( [ - NativeCodeAsset( + CodeAsset( package: config.packageName, name: assetName!, file: libUri, diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/clinker.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/clinker.dart index 28d86d9a5..45790535e 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/clinker.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/clinker.dart @@ -91,7 +91,7 @@ class CLinker extends CTool implements Linker { if (assetName != null) { output.addAssets( [ - NativeCodeAsset( + CodeAsset( package: config.packageName, name: assetName!, file: libUri, diff --git a/pkgs/native_toolchain_c/test/clinker/objects_test.dart b/pkgs/native_toolchain_c/test/clinker/objects_test.dart index 22f30777a..1d9cd47e0 100644 --- a/pkgs/native_toolchain_c/test/clinker/objects_test.dart +++ b/pkgs/native_toolchain_c/test/clinker/objects_test.dart @@ -59,9 +59,9 @@ Future main() async { expect(linkOutput.assets, hasLength(1)); final asset = linkOutput.assets.first; - expect(asset, isA()); + expect(asset, isA()); await expectSymbols( - asset: asset as NativeCodeAsset, + asset: asset as CodeAsset, symbols: [ 'my_func', 'my_other_func', diff --git a/pkgs/native_toolchain_c/test/clinker/treeshake_helper.dart b/pkgs/native_toolchain_c/test/clinker/treeshake_helper.dart index 3abc2339f..56eec3a70 100644 --- a/pkgs/native_toolchain_c/test/clinker/treeshake_helper.dart +++ b/pkgs/native_toolchain_c/test/clinker/treeshake_helper.dart @@ -84,7 +84,7 @@ Future runTests(List architectures) async { logger: logger, ); - final asset = linkOutput.assets.first as NativeCodeAsset; + final asset = linkOutput.assets.first as CodeAsset; final filePath = asset.file!.toFilePath(); final machine = await readelfMachine(filePath); diff --git a/pkgs/native_toolchain_c/test/helpers.dart b/pkgs/native_toolchain_c/test/helpers.dart index 9c4981464..c1575920a 100644 --- a/pkgs/native_toolchain_c/test/helpers.dart +++ b/pkgs/native_toolchain_c/test/helpers.dart @@ -223,7 +223,7 @@ Future readelf(String filePath, String flags) async { return result.stdout; } -Future nmReadSymbols(NativeCodeAsset asset) async { +Future nmReadSymbols(CodeAsset asset) async { final assetUri = asset.file!; final result = await runProcess( executable: Uri(path: 'nm'), @@ -239,7 +239,7 @@ Future nmReadSymbols(NativeCodeAsset asset) async { } Future expectSymbols({ - required NativeCodeAsset asset, + required CodeAsset asset, required List symbols, }) async { if (Platform.isLinux) {