Skip to content

Commit

Permalink
Move more
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes committed Jan 9, 2024
1 parent 2439329 commit 682ae5d
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 85 deletions.
1 change: 1 addition & 0 deletions pkgs/native_assets_builder/dart_test.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
paths:
- test/build_runner/
- test/model/
44 changes: 18 additions & 26 deletions pkgs/native_assets_builder/lib/src/model/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> toYaml() => throw UnimplementedError();
@override
List<String> toDartConst() => [_pathTypeValue, uri.toFilePath()];

@override
int get hashCode => Object.hash(uri, 133717);
extension on Asset {
Map<String, List<String>> toDartConst() => {
id: path.toDartConst(),
};
}

@override
bool operator ==(Object other) {
if (other is! AssetRelativePath) {
return false;
extension on AssetPath {
List<String> 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<bool> exists() => throw UnimplementedError();
}

extension AssetIterable on Iterable<Asset> {
Expand Down
9 changes: 0 additions & 9 deletions pkgs/native_assets_builder/lib/src/utils/yaml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,3 @@ String yamlEncode(Object yamlEncoding) {
);
return editor.toString();
}

T as<T>(Object? object) {
if (object is T) {
return object;
}
throw FormatException(
"Unexpected value '$object' in YAML. Expected a $T.",
);
}
14 changes: 2 additions & 12 deletions pkgs/native_assets_builder/test/model/asset_test.dart
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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');
Expand All @@ -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),
Expand Down Expand Up @@ -69,9 +62,6 @@ native-assets:
foo:
- absolute
- ${fooUri.toFilePath()}
foo2:
- relative
- ${foo2Uri.toFilePath()}
foo3:
- system
- ${foo3Uri.toFilePath()}
Expand All @@ -95,6 +85,6 @@ native-assets:

test('List<Asset> whereLinkMode', () async {
final assets2 = assets.whereLinkMode(LinkMode.dynamic);
expect(assets2.length, 6);
expect(assets2.length, 5);
});
}
2 changes: 0 additions & 2 deletions pkgs/native_assets_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 1 addition & 33 deletions pkgs/native_assets_cli/lib/src/model/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import 'package:yaml/yaml.dart';

import '../utils/uri.dart';
import '../utils/yaml.dart';
import 'link_mode.dart';
import 'target.dart';
Expand Down Expand Up @@ -32,12 +31,9 @@ abstract class AssetPath {
}

Map<String, Object> toYaml();
List<String> toDartConst();

static const _pathTypeKey = 'path_type';
static const _uriKey = 'uri';

Future<bool> exists();
}

/// Asset at absolute path [uri].
Expand All @@ -54,9 +50,6 @@ class AssetAbsolutePath implements AssetPath {
AssetPath._uriKey: uri.toFilePath(),
};

@override
List<String> toDartConst() => [_pathTypeValue, uri.toFilePath()];

@override
int get hashCode => Object.hash(uri, 133711);

Expand All @@ -67,9 +60,6 @@ class AssetAbsolutePath implements AssetPath {
}
return uri == other.uri;
}

@override
Future<bool> exists() => uri.fileSystemEntity.exists();
}

/// Asset is avaliable on the system `PATH`.
Expand All @@ -88,9 +78,6 @@ class AssetSystemPath implements AssetPath {
AssetPath._uriKey: uri.toFilePath(),
};

@override
List<String> toDartConst() => [_pathTypeValue, uri.toFilePath()];

@override
int get hashCode => Object.hash(uri, 133723);

Expand All @@ -101,9 +88,6 @@ class AssetSystemPath implements AssetPath {
}
return uri == other.uri;
}

@override
Future<bool> exists() => Future.value(true);
}

/// Asset is loaded in the process and symbols are available through
Expand All @@ -121,12 +105,6 @@ class AssetInProcess implements AssetPath {
Map<String, Object> toYaml() => {
AssetPath._pathTypeKey: _pathTypeValue,
};

@override
List<String> toDartConst() => [_pathTypeValue];

@override
Future<bool> exists() => Future.value(true);
}

/// Asset is embedded in executable and symbols are available through
Expand All @@ -144,12 +122,6 @@ class AssetInExecutable implements AssetPath {
Map<String, Object> toYaml() => {
AssetPath._pathTypeKey: _pathTypeValue,
};

@override
List<String> toDartConst() => [_pathTypeValue];

@override
Future<bool> exists() => Future.value(true);
}

class Asset {
Expand Down Expand Up @@ -222,16 +194,12 @@ class Asset {
_targetKey: target.toString(),
};

Map<String, List<String>> toDartConst() => {
id: path.toDartConst(),
};

static const _idKey = 'id';
static const _linkModeKey = 'link_mode';
static const _pathKey = 'path';
static const _targetKey = 'target';

Future<bool> exists() => path.exists();
// Future<bool> exists() => path.exists();

@override
String toString() => 'Asset(${toYaml()})';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, <Uri>[]);
} else {
expect(assets.length, 1);
Expand Down
22 changes: 20 additions & 2 deletions pkgs/native_assets_cli/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -112,3 +111,22 @@ extension AssetIterable on Iterable<Asset> {
return !missing;
}
}

extension on Asset {
Future<bool> 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);
}
}

0 comments on commit 682ae5d

Please sign in to comment.