From eaccd1c83a0da34109c5b5c3a701cd99e3806c11 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 9 Jan 2024 12:42:12 +0100 Subject: [PATCH 1/6] [native_assets_cli] Move non-`build.dart` code to `package:native_assets_builder` --- .../lib/src/model/asset.dart | 74 ++++++++++++++ .../lib/src/utils/yaml.dart | 27 ++++++ pkgs/native_assets_builder/pubspec.yaml | 3 +- .../test/model/asset_test.dart | 97 +++++++++++++++++++ pkgs/native_assets_cli/CHANGELOG.md | 7 +- .../lib/src/model/asset.dart | 79 --------------- pkgs/native_assets_cli/pubspec.yaml | 4 +- pkgs/native_assets_cli/test/helpers.dart | 9 ++ .../test/model/asset_test.dart | 69 ------------- .../test/model/build_output_test.dart | 4 +- 10 files changed, 219 insertions(+), 154 deletions(-) create mode 100644 pkgs/native_assets_builder/lib/src/model/asset.dart create mode 100644 pkgs/native_assets_builder/lib/src/utils/yaml.dart create mode 100644 pkgs/native_assets_builder/test/model/asset_test.dart diff --git a/pkgs/native_assets_builder/lib/src/model/asset.dart b/pkgs/native_assets_builder/lib/src/model/asset.dart new file mode 100644 index 0000000000..6dc1b4a9f9 --- /dev/null +++ b/pkgs/native_assets_builder/lib/src/model/asset.dart @@ -0,0 +1,74 @@ +// 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. + +import 'package:native_assets_cli/native_assets_cli.dart'; + +import '../utils/yaml.dart'; + +/// Asset is avaliable on a relative path. +/// +/// If [LinkMode] of an [Asset] is [LinkMode.dynamic], +/// `Platform.script.resolve(uri)` will be used to load the asset at runtime. +class AssetRelativePath implements AssetPath { + final Uri uri; + + AssetRelativePath(this.uri); + + static const _pathTypeValue = 'relative'; + + @override + Map toYaml() => throw UnimplementedError(); + @override + List toDartConst() => [_pathTypeValue, uri.toFilePath()]; + + @override + int get hashCode => Object.hash(uri, 133717); + + @override + bool operator ==(Object other) { + if (other is! AssetRelativePath) { + return false; + } + return uri == other.uri; + } + + @override + Future exists() => throw UnimplementedError(); +} + +extension AssetIterable on Iterable { + Iterable whereLinkMode(LinkMode linkMode) => + where((e) => e.linkMode == linkMode); + + Map> get assetsPerTarget { + final result = >{}; + for (final asset in this) { + final assets = result[asset.target] ?? []; + assets.add(asset); + result[asset.target] = assets; + } + return result; + } + + Map>> toDartConst() => { + for (final entry in assetsPerTarget.entries) + entry.key.toString(): + _combineMaps(entry.value.map((e) => e.toDartConst()).toList()) + }; + + Map toNativeAssetsFileEncoding() => { + 'format-version': [1, 0, 0], + 'native-assets': toDartConst(), + }; + + String toNativeAssetsFile() => yamlEncode(toNativeAssetsFileEncoding()); +} + +Map _combineMaps(Iterable> maps) { + final result = {}; + for (final map in maps) { + result.addAll(map); + } + return result; +} diff --git a/pkgs/native_assets_builder/lib/src/utils/yaml.dart b/pkgs/native_assets_builder/lib/src/utils/yaml.dart new file mode 100644 index 0000000000..a2d85c3d19 --- /dev/null +++ b/pkgs/native_assets_builder/lib/src/utils/yaml.dart @@ -0,0 +1,27 @@ +// 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. + +import 'package:yaml/yaml.dart'; +import 'package:yaml_edit/yaml_edit.dart'; + +String yamlEncode(Object yamlEncoding) { + final editor = YamlEditor(''); + editor.update( + [], + wrapAsYamlNode( + yamlEncoding, + collectionStyle: CollectionStyle.BLOCK, + ), + ); + return editor.toString(); +} + +T as(Object? object) { + if (object is T) { + return object; + } + throw FormatException( + "Unexpected value '$object' in YAML. Expected a $T.", + ); +} diff --git a/pkgs/native_assets_builder/pubspec.yaml b/pkgs/native_assets_builder/pubspec.yaml index ba23b57a4d..5d68b6045f 100644 --- a/pkgs/native_assets_builder/pubspec.yaml +++ b/pkgs/native_assets_builder/pubspec.yaml @@ -12,9 +12,10 @@ dependencies: logging: ^1.2.0 native_assets_cli: ^0.3.2 package_config: ^2.1.0 + yaml: ^3.1.2 + yaml_edit: ^2.1.0 dev_dependencies: dart_flutter_team_lints: ^2.1.1 file_testing: ^3.0.0 test: ^1.24.3 - yaml: ^3.1.2 diff --git a/pkgs/native_assets_builder/test/model/asset_test.dart b/pkgs/native_assets_builder/test/model/asset_test.dart new file mode 100644 index 0000000000..37345fc380 --- /dev/null +++ b/pkgs/native_assets_builder/test/model/asset_test.dart @@ -0,0 +1,97 @@ +// 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. + +import 'package:native_assets_builder/src/model/asset.dart'; +import 'package:native_assets_cli/native_assets_cli.dart'; +import 'package:test/test.dart'; + +void main() { + final fooUri = Uri.file('path/to/libfoo.so'); + final foo2Uri = Uri.file('path/to/libfoo2.so'); + final foo3Uri = Uri(path: 'libfoo3.so'); + final barUri = Uri(path: 'path/to/libbar.a'); + final blaUri = Uri(path: 'path/with spaces/bla.dll'); + final assets = [ + Asset( + id: 'foo', + path: AssetAbsolutePath(fooUri), + target: Target.androidX64, + linkMode: LinkMode.dynamic, + ), + Asset( + id: 'foo2', + path: AssetRelativePath(foo2Uri), + target: Target.androidX64, + linkMode: LinkMode.dynamic, + ), + Asset( + id: 'foo3', + path: AssetSystemPath(foo3Uri), + target: Target.androidX64, + linkMode: LinkMode.dynamic, + ), + Asset( + id: 'foo4', + path: AssetInExecutable(), + target: Target.androidX64, + linkMode: LinkMode.dynamic, + ), + Asset( + id: 'foo5', + path: AssetInProcess(), + target: Target.androidX64, + linkMode: LinkMode.dynamic, + ), + Asset( + id: 'bar', + path: AssetAbsolutePath(barUri), + target: Target.linuxArm64, + linkMode: LinkMode.static, + ), + Asset( + id: 'bla', + path: AssetAbsolutePath(blaUri), + target: Target.windowsX64, + linkMode: LinkMode.dynamic, + ), + ]; + + final assetsDartEncoding = '''format-version: + - 1 + - 0 + - 0 +native-assets: + android_x64: + foo: + - absolute + - ${fooUri.toFilePath()} + foo2: + - relative + - ${foo2Uri.toFilePath()} + foo3: + - system + - ${foo3Uri.toFilePath()} + foo4: + - executable + foo5: + - process + linux_arm64: + bar: + - absolute + - ${barUri.toFilePath()} + windows_x64: + bla: + - absolute + - ${blaUri.toFilePath()}'''; + + test('asset yaml', () async { + final fileContents = assets.toNativeAssetsFile(); + expect(fileContents, assetsDartEncoding); + }); + + test('List whereLinkMode', () async { + final assets2 = assets.whereLinkMode(LinkMode.dynamic); + expect(assets2.length, 6); + }); +} diff --git a/pkgs/native_assets_cli/CHANGELOG.md b/pkgs/native_assets_cli/CHANGELOG.md index e7133fcd2a..e8d43e327b 100644 --- a/pkgs/native_assets_cli/CHANGELOG.md +++ b/pkgs/native_assets_cli/CHANGELOG.md @@ -1,7 +1,12 @@ -## 0.3.3-wip +## 0.4.0-wip - Added [example/use_dart_api/](example/use_dart_api/) detailing how to use `dart_api_dl.h` from the Dart SDK in native code. +- **Breaking change** Moved code not used in `build.dart` to + `package:native_assets_builder`. + `AssetRelativePath` is only defined inside `toNativeAssetsFile()` which is + passed to the VM, not inside `build.dart` output. + ## 0.3.2 diff --git a/pkgs/native_assets_cli/lib/src/model/asset.dart b/pkgs/native_assets_cli/lib/src/model/asset.dart index 616688f0b9..0389fbaff1 100644 --- a/pkgs/native_assets_cli/lib/src/model/asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/asset.dart @@ -14,8 +14,6 @@ abstract class AssetPath { switch (pathType) { case AssetAbsolutePath._pathTypeValue: return AssetAbsolutePath(uri!); - case AssetRelativePath._pathTypeValue: - return AssetRelativePath(uri!); case AssetSystemPath._pathTypeValue: return AssetSystemPath(uri!); case AssetInExecutable._pathTypeValue: @@ -74,41 +72,6 @@ class AssetAbsolutePath implements AssetPath { Future exists() => uri.fileSystemEntity.exists(); } -/// Asset is avaliable on a relative path. -/// -/// If [LinkMode] of an [Asset] is [LinkMode.dynamic], -/// `Platform.script.resolve(uri)` will be used to load the asset at runtime. -class AssetRelativePath implements AssetPath { - final Uri uri; - - AssetRelativePath(this.uri); - - static const _pathTypeValue = 'relative'; - - @override - Map toYaml() => { - AssetPath._pathTypeKey: _pathTypeValue, - AssetPath._uriKey: uri.toFilePath(), - }; - - @override - List toDartConst() => [_pathTypeValue, uri.toFilePath()]; - - @override - int get hashCode => Object.hash(uri, 133717); - - @override - bool operator ==(Object other) { - if (other is! AssetRelativePath) { - return false; - } - return uri == other.uri; - } - - @override - Future exists() => uri.fileSystemEntity.exists(); -} - /// Asset is avaliable on the system `PATH`. /// /// [uri] only contains a file name. @@ -263,8 +226,6 @@ class Asset { id: path.toDartConst(), }; - String toYamlString() => yamlEncode(toYaml()); - static const _idKey = 'id'; static const _linkModeKey = 'link_mode'; static const _pathKey = 'path'; @@ -280,44 +241,4 @@ extension AssetIterable on Iterable { List toYaml() => [for (final item in this) item.toYaml()]; String toYamlString() => yamlEncode(toYaml()); - - Iterable whereLinkMode(LinkMode linkMode) => - where((e) => e.linkMode == linkMode); - - Map> get assetsPerTarget { - final result = >{}; - for (final asset in this) { - final assets = result[asset.target] ?? []; - assets.add(asset); - result[asset.target] = assets; - } - return result; - } - - Map>> toDartConst() => { - for (final entry in assetsPerTarget.entries) - entry.key.toString(): - _combineMaps(entry.value.map((e) => e.toDartConst()).toList()) - }; - - Map toNativeAssetsFileEncoding() => { - 'format-version': [1, 0, 0], - 'native-assets': toDartConst(), - }; - - String toNativeAssetsFile() => yamlEncode(toNativeAssetsFileEncoding()); - - Future allExist() async { - final allResults = await Future.wait(map((e) => e.exists())); - final missing = allResults.contains(false); - return !missing; - } -} - -Map _combineMaps(Iterable> maps) { - final result = {}; - for (final map in maps) { - result.addAll(map); - } - return result; } diff --git a/pkgs/native_assets_cli/pubspec.yaml b/pkgs/native_assets_cli/pubspec.yaml index a79e72cb95..02ec12373e 100644 --- a/pkgs/native_assets_cli/pubspec.yaml +++ b/pkgs/native_assets_cli/pubspec.yaml @@ -4,7 +4,7 @@ description: >- native assets CLI. # Note: Bump BuildConfig.version and BuildOutput.version on breaking changes! -version: 0.3.3-wip +version: 0.4.0-wip repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli topics: @@ -13,7 +13,7 @@ topics: - native-assets environment: - sdk: ">=3.0.0 <4.0.0" + sdk: '>=3.0.0 <4.0.0' dependencies: cli_config: ^0.1.1 diff --git a/pkgs/native_assets_cli/test/helpers.dart b/pkgs/native_assets_cli/test/helpers.dart index 2bd274227a..4d064f0511 100644 --- a/pkgs/native_assets_cli/test/helpers.dart +++ b/pkgs/native_assets_cli/test/helpers.dart @@ -4,6 +4,7 @@ import 'dart:io'; +import 'package:native_assets_cli/src/model/asset.dart'; import 'package:native_assets_cli/src/model/build_config.dart'; const keepTempKey = 'KEEP_TEMPORARY_DIRECTORIES'; @@ -103,3 +104,11 @@ final List? envScriptArgs = Platform extension on String { Uri asFileUri() => Uri.file(this); } + +extension AssetIterable on Iterable { + Future allExist() async { + final allResults = await Future.wait(map((e) => e.exists())); + final missing = allResults.contains(false); + return !missing; + } +} diff --git a/pkgs/native_assets_cli/test/model/asset_test.dart b/pkgs/native_assets_cli/test/model/asset_test.dart index c9f2e9f801..c897dab8d1 100644 --- a/pkgs/native_assets_cli/test/model/asset_test.dart +++ b/pkgs/native_assets_cli/test/model/asset_test.dart @@ -8,7 +8,6 @@ import 'package:test/test.dart'; void main() { final fooUri = Uri.file('path/to/libfoo.so'); - final foo2Uri = Uri.file('path/to/libfoo2.so'); final foo3Uri = Uri(path: 'libfoo3.so'); final barUri = Uri(path: 'path/to/libbar.a'); final blaUri = Uri(path: 'path/with spaces/bla.dll'); @@ -19,12 +18,6 @@ void main() { target: Target.androidX64, linkMode: LinkMode.dynamic, ), - Asset( - id: 'foo2', - path: AssetRelativePath(foo2Uri), - target: Target.androidX64, - linkMode: LinkMode.dynamic, - ), Asset( id: 'foo3', path: AssetSystemPath(foo3Uri), @@ -63,12 +56,6 @@ void main() { path_type: absolute uri: ${fooUri.toFilePath()} target: android_x64 -- id: foo2 - link_mode: dynamic - path: - path_type: relative - uri: ${foo2Uri.toFilePath()} - target: android_x64 - id: foo3 link_mode: dynamic path: @@ -98,34 +85,6 @@ void main() { uri: ${blaUri.toFilePath()} target: windows_x64'''; - final assetsDartEncoding = '''format-version: - - 1 - - 0 - - 0 -native-assets: - android_x64: - foo: - - absolute - - ${fooUri.toFilePath()} - foo2: - - relative - - ${foo2Uri.toFilePath()} - foo3: - - system - - ${foo3Uri.toFilePath()} - foo4: - - executable - foo5: - - process - linux_arm64: - bar: - - absolute - - ${barUri.toFilePath()} - windows_x64: - bla: - - absolute - - ${blaUri.toFilePath()}'''; - test('asset yaml', () { final yaml = assets.toYamlString(); expect(yaml, assetsYamlEncoding); @@ -133,11 +92,6 @@ native-assets: expect(assets, assets2); }); - test('asset yaml', () async { - final fileContents = assets.toNativeAssetsFile(); - expect(fileContents, assetsDartEncoding); - }); - test('AssetPath factory', () async { expect( () => AssetPath('wrong', null), @@ -162,33 +116,10 @@ native-assets: expect(equality.hash(assets) != equality.hash(assets2), true); }); - test('List whereLinkMode', () async { - final assets2 = assets.whereLinkMode(LinkMode.dynamic); - expect(assets2.length, 6); - }); - test('Asset toString', () async { assets.toString(); }); - test('Asset toString', () async { - expect(await assets.allExist(), false); - }); - - test('Asset toYaml', () async { - expect( - assets.first.toYamlString(), - ''' -id: foo -link_mode: dynamic -path: - path_type: absolute - uri: ${fooUri.toFilePath()} -target: android_x64 -''' - .trim()); - }); - test('Asset listFromYamlString', () async { final assets = Asset.listFromYamlString(''); expect(assets, []); 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 0dc0bad272..805cddfc3b 100644 --- a/pkgs/native_assets_cli/test/model/build_output_test.dart +++ b/pkgs/native_assets_cli/test/model/build_output_test.dart @@ -29,7 +29,7 @@ void main() { ), Asset( id: 'foo2', - path: AssetRelativePath(Uri(path: 'path/to/libfoo2.so')), + path: AssetSystemPath(Uri(path: 'path/to/libfoo2.so')), target: Target.androidX64, linkMode: LinkMode.dynamic, ), @@ -53,7 +53,7 @@ assets: - id: foo2 link_mode: dynamic path: - path_type: relative + path_type: system uri: path/to/libfoo2.so target: android_x64 dependencies: From 24393296cc31bef8ac8a825c83654cf96b956af2 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 9 Jan 2024 12:50:23 +0100 Subject: [PATCH 2/6] Fix analysis against published dependency --- pkgs/native_assets_builder/test/model/asset_test.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkgs/native_assets_builder/test/model/asset_test.dart b/pkgs/native_assets_builder/test/model/asset_test.dart index 37345fc380..002f81a9e9 100644 --- a/pkgs/native_assets_builder/test/model/asset_test.dart +++ b/pkgs/native_assets_builder/test/model/asset_test.dart @@ -2,8 +2,11 @@ // 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. +// ignore_for_file: undefined_hidden_name + import 'package:native_assets_builder/src/model/asset.dart'; -import 'package:native_assets_cli/native_assets_cli.dart'; +import 'package:native_assets_cli/native_assets_cli.dart' + hide AssetIterable, AssetRelativePath; import 'package:test/test.dart'; void main() { From 682ae5d229d0ddec4c91b1d356bbf358b8d3b08d Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 9 Jan 2024 15:40:35 +0100 Subject: [PATCH 3/6] Move more --- pkgs/native_assets_builder/dart_test.yaml | 1 + .../lib/src/model/asset.dart | 44 ++++++++----------- .../lib/src/utils/yaml.dart | 9 ---- .../test/model/asset_test.dart | 14 +----- pkgs/native_assets_cli/CHANGELOG.md | 2 - .../lib/src/model/asset.dart | 34 +------------- .../test/example/native_add_library_test.dart | 5 ++- pkgs/native_assets_cli/test/helpers.dart | 22 +++++++++- 8 files changed, 46 insertions(+), 85 deletions(-) diff --git a/pkgs/native_assets_builder/dart_test.yaml b/pkgs/native_assets_builder/dart_test.yaml index c1c8a57d43..d5e52c43b8 100644 --- a/pkgs/native_assets_builder/dart_test.yaml +++ b/pkgs/native_assets_builder/dart_test.yaml @@ -1,2 +1,3 @@ paths: - test/build_runner/ + - test/model/ diff --git a/pkgs/native_assets_builder/lib/src/model/asset.dart b/pkgs/native_assets_builder/lib/src/model/asset.dart index 6dc1b4a9f9..b16b024e44 100644 --- a/pkgs/native_assets_builder/lib/src/model/asset.dart +++ b/pkgs/native_assets_builder/lib/src/model/asset.dart @@ -6,35 +6,27 @@ import 'package:native_assets_cli/native_assets_cli.dart'; import '../utils/yaml.dart'; -/// Asset is avaliable on a relative path. -/// -/// If [LinkMode] of an [Asset] is [LinkMode.dynamic], -/// `Platform.script.resolve(uri)` will be used to load the asset at runtime. -class AssetRelativePath implements AssetPath { - final Uri uri; - - AssetRelativePath(this.uri); - - static const _pathTypeValue = 'relative'; - - @override - Map toYaml() => throw UnimplementedError(); - @override - List toDartConst() => [_pathTypeValue, uri.toFilePath()]; - - @override - int get hashCode => Object.hash(uri, 133717); +extension on Asset { + Map> toDartConst() => { + id: path.toDartConst(), + }; +} - @override - bool operator ==(Object other) { - if (other is! AssetRelativePath) { - return false; +extension on AssetPath { + List toDartConst() { + final this_ = this; + switch (this_) { + case AssetAbsolutePath _: + return ['absolute', this_.uri.toFilePath()]; + case AssetSystemPath _: + return ['system', this_.uri.toFilePath()]; + case AssetInProcess _: + return ['process']; + default: + assert(this_ is AssetInExecutable); + return ['executable']; } - return uri == other.uri; } - - @override - Future exists() => throw UnimplementedError(); } extension AssetIterable on Iterable { diff --git a/pkgs/native_assets_builder/lib/src/utils/yaml.dart b/pkgs/native_assets_builder/lib/src/utils/yaml.dart index a2d85c3d19..b62746bb12 100644 --- a/pkgs/native_assets_builder/lib/src/utils/yaml.dart +++ b/pkgs/native_assets_builder/lib/src/utils/yaml.dart @@ -16,12 +16,3 @@ String yamlEncode(Object yamlEncoding) { ); return editor.toString(); } - -T as(Object? object) { - if (object is T) { - return object; - } - throw FormatException( - "Unexpected value '$object' in YAML. Expected a $T.", - ); -} diff --git a/pkgs/native_assets_builder/test/model/asset_test.dart b/pkgs/native_assets_builder/test/model/asset_test.dart index 002f81a9e9..06739fcc64 100644 --- a/pkgs/native_assets_builder/test/model/asset_test.dart +++ b/pkgs/native_assets_builder/test/model/asset_test.dart @@ -1,4 +1,4 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// 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. @@ -11,7 +11,6 @@ import 'package:test/test.dart'; void main() { final fooUri = Uri.file('path/to/libfoo.so'); - final foo2Uri = Uri.file('path/to/libfoo2.so'); final foo3Uri = Uri(path: 'libfoo3.so'); final barUri = Uri(path: 'path/to/libbar.a'); final blaUri = Uri(path: 'path/with spaces/bla.dll'); @@ -22,12 +21,6 @@ void main() { target: Target.androidX64, linkMode: LinkMode.dynamic, ), - Asset( - id: 'foo2', - path: AssetRelativePath(foo2Uri), - target: Target.androidX64, - linkMode: LinkMode.dynamic, - ), Asset( id: 'foo3', path: AssetSystemPath(foo3Uri), @@ -69,9 +62,6 @@ native-assets: foo: - absolute - ${fooUri.toFilePath()} - foo2: - - relative - - ${foo2Uri.toFilePath()} foo3: - system - ${foo3Uri.toFilePath()} @@ -95,6 +85,6 @@ native-assets: test('List whereLinkMode', () async { final assets2 = assets.whereLinkMode(LinkMode.dynamic); - expect(assets2.length, 6); + expect(assets2.length, 5); }); } diff --git a/pkgs/native_assets_cli/CHANGELOG.md b/pkgs/native_assets_cli/CHANGELOG.md index e8d43e327b..741fd3b6b4 100644 --- a/pkgs/native_assets_cli/CHANGELOG.md +++ b/pkgs/native_assets_cli/CHANGELOG.md @@ -4,8 +4,6 @@ `dart_api_dl.h` from the Dart SDK in native code. - **Breaking change** Moved code not used in `build.dart` to `package:native_assets_builder`. - `AssetRelativePath` is only defined inside `toNativeAssetsFile()` which is - passed to the VM, not inside `build.dart` output. ## 0.3.2 diff --git a/pkgs/native_assets_cli/lib/src/model/asset.dart b/pkgs/native_assets_cli/lib/src/model/asset.dart index 0389fbaff1..f4499a9594 100644 --- a/pkgs/native_assets_cli/lib/src/model/asset.dart +++ b/pkgs/native_assets_cli/lib/src/model/asset.dart @@ -4,7 +4,6 @@ import 'package:yaml/yaml.dart'; -import '../utils/uri.dart'; import '../utils/yaml.dart'; import 'link_mode.dart'; import 'target.dart'; @@ -32,12 +31,9 @@ abstract class AssetPath { } Map toYaml(); - List toDartConst(); static const _pathTypeKey = 'path_type'; static const _uriKey = 'uri'; - - Future exists(); } /// Asset at absolute path [uri]. @@ -54,9 +50,6 @@ class AssetAbsolutePath implements AssetPath { AssetPath._uriKey: uri.toFilePath(), }; - @override - List toDartConst() => [_pathTypeValue, uri.toFilePath()]; - @override int get hashCode => Object.hash(uri, 133711); @@ -67,9 +60,6 @@ class AssetAbsolutePath implements AssetPath { } return uri == other.uri; } - - @override - Future exists() => uri.fileSystemEntity.exists(); } /// Asset is avaliable on the system `PATH`. @@ -88,9 +78,6 @@ class AssetSystemPath implements AssetPath { AssetPath._uriKey: uri.toFilePath(), }; - @override - List toDartConst() => [_pathTypeValue, uri.toFilePath()]; - @override int get hashCode => Object.hash(uri, 133723); @@ -101,9 +88,6 @@ class AssetSystemPath implements AssetPath { } return uri == other.uri; } - - @override - Future exists() => Future.value(true); } /// Asset is loaded in the process and symbols are available through @@ -121,12 +105,6 @@ class AssetInProcess implements AssetPath { Map toYaml() => { AssetPath._pathTypeKey: _pathTypeValue, }; - - @override - List toDartConst() => [_pathTypeValue]; - - @override - Future exists() => Future.value(true); } /// Asset is embedded in executable and symbols are available through @@ -144,12 +122,6 @@ class AssetInExecutable implements AssetPath { Map toYaml() => { AssetPath._pathTypeKey: _pathTypeValue, }; - - @override - List toDartConst() => [_pathTypeValue]; - - @override - Future exists() => Future.value(true); } class Asset { @@ -222,16 +194,12 @@ class Asset { _targetKey: target.toString(), }; - Map> toDartConst() => { - id: path.toDartConst(), - }; - static const _idKey = 'id'; static const _linkModeKey = 'link_mode'; static const _pathKey = 'path'; static const _targetKey = 'target'; - Future exists() => path.exists(); + // Future exists() => path.exists(); @override String toString() => 'Asset(${toYaml()})'; diff --git a/pkgs/native_assets_cli/test/example/native_add_library_test.dart b/pkgs/native_assets_cli/test/example/native_add_library_test.dart index 34da58dc95..c0651e321c 100644 --- a/pkgs/native_assets_cli/test/example/native_add_library_test.dart +++ b/pkgs/native_assets_cli/test/example/native_add_library_test.dart @@ -74,7 +74,10 @@ void main() async { final dependencies = buildOutput.dependencies; if (dryRun) { expect(assets.length, greaterThanOrEqualTo(1)); - expect(await assets.first.exists(), false); + expect( + await File.fromUri((assets.first.path as AssetAbsolutePath).uri) + .exists(), + false); expect(dependencies.dependencies, []); } else { expect(assets.length, 1); diff --git a/pkgs/native_assets_cli/test/helpers.dart b/pkgs/native_assets_cli/test/helpers.dart index 4d064f0511..160c11815c 100644 --- a/pkgs/native_assets_cli/test/helpers.dart +++ b/pkgs/native_assets_cli/test/helpers.dart @@ -4,8 +4,7 @@ import 'dart:io'; -import 'package:native_assets_cli/src/model/asset.dart'; -import 'package:native_assets_cli/src/model/build_config.dart'; +import 'package:native_assets_cli/native_assets_cli.dart'; const keepTempKey = 'KEEP_TEMPORARY_DIRECTORIES'; @@ -112,3 +111,22 @@ extension AssetIterable on Iterable { return !missing; } } + +extension on Asset { + Future exists() async { + final path_ = path; + return switch (path_) { + AssetAbsolutePath _ => await path_.uri.fileSystemEntity.exists(), + _ => true, + }; + } +} + +extension UriExtension on Uri { + FileSystemEntity get fileSystemEntity { + if (path.endsWith(Platform.pathSeparator) || path.endsWith('/')) { + return Directory.fromUri(this); + } + return File.fromUri(this); + } +} From f1cbbf24a41001dde4194b2f1fb73b89e7c9cd83 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 9 Jan 2024 15:48:03 +0100 Subject: [PATCH 4/6] Prevent shadowing between extension methods and methods --- .../lib/native_assets_builder.dart | 1 + .../lib/src/model/asset.dart | 44 +++++++++---------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/pkgs/native_assets_builder/lib/native_assets_builder.dart b/pkgs/native_assets_builder/lib/native_assets_builder.dart index 3ff29ab0c3..aa8c1971dc 100644 --- a/pkgs/native_assets_builder/lib/native_assets_builder.dart +++ b/pkgs/native_assets_builder/lib/native_assets_builder.dart @@ -3,4 +3,5 @@ // BSD-style license that can be found in the LICENSE file. export 'package:native_assets_builder/src/build_runner/build_runner.dart'; +export 'package:native_assets_builder/src/model/asset.dart'; export 'package:native_assets_builder/src/package_layout/package_layout.dart'; diff --git a/pkgs/native_assets_builder/lib/src/model/asset.dart b/pkgs/native_assets_builder/lib/src/model/asset.dart index b16b024e44..d9d8c4ae5f 100644 --- a/pkgs/native_assets_builder/lib/src/model/asset.dart +++ b/pkgs/native_assets_builder/lib/src/model/asset.dart @@ -6,28 +6,7 @@ import 'package:native_assets_cli/native_assets_cli.dart'; import '../utils/yaml.dart'; -extension on Asset { - Map> toDartConst() => { - id: path.toDartConst(), - }; -} - -extension on AssetPath { - List toDartConst() { - final this_ = this; - switch (this_) { - case AssetAbsolutePath _: - return ['absolute', this_.uri.toFilePath()]; - case AssetSystemPath _: - return ['system', this_.uri.toFilePath()]; - case AssetInProcess _: - return ['process']; - default: - assert(this_ is AssetInExecutable); - return ['executable']; - } - } -} +extension on AssetPath {} extension AssetIterable on Iterable { Iterable whereLinkMode(LinkMode linkMode) => @@ -45,8 +24,11 @@ extension AssetIterable on Iterable { Map>> toDartConst() => { for (final entry in assetsPerTarget.entries) - entry.key.toString(): - _combineMaps(entry.value.map((e) => e.toDartConst()).toList()) + entry.key.toString(): _combineMaps(entry.value + .map((e) => { + e.id: _toDartConst(e.path), + }) + .toList()) }; Map toNativeAssetsFileEncoding() => { @@ -57,6 +39,20 @@ extension AssetIterable on Iterable { String toNativeAssetsFile() => yamlEncode(toNativeAssetsFileEncoding()); } +List _toDartConst(AssetPath path) { + switch (path) { + case AssetAbsolutePath _: + return ['absolute', path.uri.toFilePath()]; + case AssetSystemPath _: + return ['system', path.uri.toFilePath()]; + case AssetInProcess _: + return ['process']; + default: + assert(path is AssetInExecutable); + return ['executable']; + } +} + Map _combineMaps(Iterable> maps) { final result = {}; for (final map in maps) { From 3a9cfce444677a51e49194663ae059515a653b72 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 9 Jan 2024 16:15:54 +0100 Subject: [PATCH 5/6] 2024 --- pkgs/native_assets_builder/lib/src/model/asset.dart | 2 +- pkgs/native_assets_builder/lib/src/utils/yaml.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/native_assets_builder/lib/src/model/asset.dart b/pkgs/native_assets_builder/lib/src/model/asset.dart index d9d8c4ae5f..5d93751c89 100644 --- a/pkgs/native_assets_builder/lib/src/model/asset.dart +++ b/pkgs/native_assets_builder/lib/src/model/asset.dart @@ -1,4 +1,4 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// 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. diff --git a/pkgs/native_assets_builder/lib/src/utils/yaml.dart b/pkgs/native_assets_builder/lib/src/utils/yaml.dart index b62746bb12..d626f3633f 100644 --- a/pkgs/native_assets_builder/lib/src/utils/yaml.dart +++ b/pkgs/native_assets_builder/lib/src/utils/yaml.dart @@ -1,4 +1,4 @@ -// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// 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. From 83f197ed98fb54d3de1fc15cd6d55f58142c94f3 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Wed, 10 Jan 2024 12:26:38 +0100 Subject: [PATCH 6/6] Add coverage back in --- .github/workflows/health.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/health.yaml b/.github/workflows/health.yaml index 72e04d29d9..293985a52e 100644 --- a/.github/workflows/health.yaml +++ b/.github/workflows/health.yaml @@ -8,7 +8,7 @@ jobs: uses: dart-lang/ecosystem/.github/workflows/health.yaml@main with: coverage_web: false - checks: "version,changelog,license,do-not-submit,breaking" + checks: "version,changelog,license,do-not-submit,breaking,coverage" use-flutter: true sdk: master permissions: