Skip to content

Commit

Permalink
[native_assets_cli] Rename Asset name to id (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes authored Aug 7, 2023
1 parent 1b984c7 commit 0187d0e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
4 changes: 3 additions & 1 deletion pkgs/native_assets_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.1.1-wip
## 0.2.0

- *Breaking change* Rename `Asset.name` to `Asset.id`
([#100](https://github.com/dart-lang/native/issues/100)).
- Added topics.
- Fixed metadata example.
- Throws `FormatException`s instead of `TypeError`s when failing to parse Yaml
Expand Down
21 changes: 12 additions & 9 deletions pkgs/native_assets_cli/lib/src/model/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,19 @@ class AssetInExecutable implements AssetPath {

class Asset {
final LinkMode linkMode;
final String name;
final String id;
final Target target;
final AssetPath path;

Asset({
required this.name,
required this.id,
required this.linkMode,
required this.target,
required this.path,
});

factory Asset.fromYaml(YamlMap yamlMap) => Asset(
name: as<String>(yamlMap[_nameKey]),
id: as<String>(yamlMap[_idKey] ?? yamlMap[_nameKey]),
path: AssetPath.fromYaml(as<YamlMap>(yamlMap[_pathKey])),
target: Target.fromString(as<String>(yamlMap[_targetKey])),
linkMode: LinkMode.fromName(as<String>(yamlMap[_linkModeKey])),
Expand All @@ -227,12 +227,12 @@ class Asset {

Asset copyWith({
LinkMode? linkMode,
String? name,
String? id,
Target? target,
AssetPath? path,
}) =>
Asset(
name: name ?? this.name,
id: id ?? this.id,
linkMode: linkMode ?? this.linkMode,
target: target ?? this.target,
path: path ?? this.path,
Expand All @@ -243,28 +243,31 @@ class Asset {
if (other is! Asset) {
return false;
}
return other.name == name &&
return other.id == id &&
other.linkMode == linkMode &&
other.target == target &&
other.path == path;
}

@override
int get hashCode => Object.hash(name, linkMode, target, path);
int get hashCode => Object.hash(id, linkMode, target, path);

Map<String, Object> toYaml() => {
_nameKey: name,
_idKey: id,
_linkModeKey: linkMode.name,
_pathKey: path.toYaml(),
_targetKey: target.toString(),
};

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

String toYamlString() => yamlEncode(toYaml());

static const _idKey = 'id';
// TODO(https://github.com/dart-lang/native/issues/100): Remove name key when
// rolling dependencies in example.
static const _nameKey = 'name';
static const _linkModeKey = 'link_mode';
static const _pathKey = 'path';
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_cli/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: native_assets_cli
description: >-
A library that contains the argument and file formats for implementing a
native assets CLI.
version: 0.1.1-wip
version: 0.2.0
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_cli

topics:
Expand Down
32 changes: 16 additions & 16 deletions pkgs/native_assets_cli/test/model/asset_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,84 +14,84 @@ void main() {
final blaUri = Uri(path: 'path/with spaces/bla.dll');
final assets = [
Asset(
name: 'foo',
id: 'foo',
path: AssetAbsolutePath(fooUri),
target: Target.androidX64,
linkMode: LinkMode.dynamic,
),
Asset(
name: 'foo2',
id: 'foo2',
path: AssetRelativePath(foo2Uri),
target: Target.androidX64,
linkMode: LinkMode.dynamic,
),
Asset(
name: 'foo3',
id: 'foo3',
path: AssetSystemPath(foo3Uri),
target: Target.androidX64,
linkMode: LinkMode.dynamic,
),
Asset(
name: 'foo4',
id: 'foo4',
path: AssetInExecutable(),
target: Target.androidX64,
linkMode: LinkMode.dynamic,
),
Asset(
name: 'foo5',
id: 'foo5',
path: AssetInProcess(),
target: Target.androidX64,
linkMode: LinkMode.dynamic,
),
Asset(
name: 'bar',
id: 'bar',
path: AssetAbsolutePath(barUri),
target: Target.linuxArm64,
linkMode: LinkMode.static,
),
Asset(
name: 'bla',
id: 'bla',
path: AssetAbsolutePath(blaUri),
target: Target.windowsX64,
linkMode: LinkMode.dynamic,
),
];

final assetsYamlEncoding = '''- name: foo
final assetsYamlEncoding = '''- id: foo
link_mode: dynamic
path:
path_type: absolute
uri: ${fooUri.toFilePath()}
target: android_x64
- name: foo2
- id: foo2
link_mode: dynamic
path:
path_type: relative
uri: ${foo2Uri.toFilePath()}
target: android_x64
- name: foo3
- id: foo3
link_mode: dynamic
path:
path_type: system
uri: ${foo3Uri.toFilePath()}
target: android_x64
- name: foo4
- id: foo4
link_mode: dynamic
path:
path_type: executable
target: android_x64
- name: foo5
- id: foo5
link_mode: dynamic
path:
path_type: process
target: android_x64
- name: bar
- id: bar
link_mode: static
path:
path_type: absolute
uri: ${barUri.toFilePath()}
target: linux_arm64
- name: bla
- id: bla
link_mode: dynamic
path:
path_type: absolute
Expand Down Expand Up @@ -149,7 +149,7 @@ native-assets:

test('Asset hashCode copyWith', () async {
final asset = assets.first;
final asset2 = asset.copyWith(name: 'foo321');
final asset2 = asset.copyWith(id: 'foo321');
expect(asset.hashCode != asset2.hashCode, true);

final asset3 = asset.copyWith();
Expand Down Expand Up @@ -179,7 +179,7 @@ native-assets:
expect(
assets.first.toYamlString(),
'''
name: foo
id: foo
link_mode: dynamic
path:
path_type: absolute
Expand Down
8 changes: 4 additions & 4 deletions pkgs/native_assets_cli/test/model/build_output_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ void main() {
timestamp: DateTime.parse('2022-11-10 13:25:01.000'),
assets: [
Asset(
name: 'foo',
id: 'foo',
path: AssetAbsolutePath(Uri(path: 'path/to/libfoo.so')),
target: Target.androidX64,
linkMode: LinkMode.dynamic,
),
Asset(
name: 'foo2',
id: 'foo2',
path: AssetRelativePath(Uri(path: 'path/to/libfoo2.so')),
target: Target.androidX64,
linkMode: LinkMode.dynamic,
Expand All @@ -44,13 +44,13 @@ void main() {

final yamlEncoding = '''timestamp: 2022-11-10 13:25:01.000
assets:
- name: foo
- id: foo
link_mode: dynamic
path:
path_type: absolute
uri: path/to/libfoo.so
target: android_x64
- name: foo2
- id: foo2
link_mode: dynamic
path:
path_type: relative
Expand Down

0 comments on commit 0187d0e

Please sign in to comment.