From 682ae5d229d0ddec4c91b1d356bbf358b8d3b08d Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 9 Jan 2024 15:40:35 +0100 Subject: [PATCH] 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 c1c8a57d4..d5e52c43b 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 6dc1b4a9f..b16b024e4 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 a2d85c3d1..b62746bb1 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 002f81a9e..06739fcc6 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 e8d43e327..741fd3b6b 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 0389fbaff..f4499a959 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 34da58dc9..c0651e321 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 4d064f051..160c11815 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); + } +}