Skip to content

Commit 4c5fca9

Browse files
authored
[native-*] DataAsset names default to the full path (#1352)
Does not yet create a helper function as suggested in #1346, but does standardize the asset names to be the file path. @mosuem I don't believe this should break https://dart-review.googlesource.com/c/sdk/+/351140, as the asset id is part of the `.dart.debug` files and I updated it there.
1 parent 6690e26 commit 4c5fca9

File tree

16 files changed

+69
-28
lines changed

16 files changed

+69
-28
lines changed

pkgs/native_assets_builder/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
- Fix some more cases of: `BuildConfig.dependencies` and
44
`LinkConfig.dependencies` no longer have to specify Dart sources.
5-
- `DataAsset` test projects report all assets from `assets/` dir.
5+
- `DataAsset` test projects report all assets from `assets/` dir and default the
6+
asset names to the path inside the package.
67

78
## 0.8.1
89

pkgs/native_assets_builder/test/build_runner/link_dry_run_test.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,20 @@ void main() async {
5050
timeout: longTimeout,
5151
() async {
5252
await inTempDir((tempUri) async {
53-
// From package:complex_link_helper
54-
const builtHelperAssets = ['data_helper_0', 'data_helper_1'];
55-
const helperAssetsForLinking = ['data_helper_2', 'data_helper_3'];
56-
// From package:complex_link
57-
const mainAssetsForLinking = ['data_0', 'data_1'];
53+
// From package:complex_link_helper.
54+
const builtHelperAssets = [
55+
'assets/data_helper_0.json',
56+
'assets/data_helper_1.json',
57+
];
58+
const helperAssetsForLinking = [
59+
'assets/data_helper_2.json',
60+
'assets/data_helper_3.json',
61+
];
62+
// From package:complex_link.
63+
const mainAssetsForLinking = [
64+
'assets/data_0.json',
65+
'assets/data_1.json',
66+
];
5867
final assetsForLinking = [
5968
...helperAssetsForLinking,
6069
...mainAssetsForLinking,

pkgs/native_assets_builder/test/build_runner/link_test.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,20 @@ void main() async {
5858
timeout: longTimeout,
5959
() async {
6060
await inTempDir((tempUri) async {
61-
// From package:complex_link_helper
62-
const builtHelperAssets = ['data_helper_0', 'data_helper_1'];
63-
const helperAssetsForLinking = ['data_helper_2', 'data_helper_3'];
64-
// From package:complex_link
65-
const mainAssetsForLinking = ['data_0', 'data_1'];
61+
// From package:complex_link_helper.
62+
const builtHelperAssets = [
63+
'assets/data_helper_0.json',
64+
'assets/data_helper_1.json',
65+
];
66+
const helperAssetsForLinking = [
67+
'assets/data_helper_2.json',
68+
'assets/data_helper_3.json',
69+
];
70+
// From package:complex_link.
71+
const mainAssetsForLinking = [
72+
'assets/data_0.json',
73+
'assets/data_1.json',
74+
];
6675
final assetsForLinking = [
6776
...helperAssetsForLinking,
6877
...mainAssetsForLinking,

pkgs/native_assets_builder/test_data/complex_link/hook/build.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ void main(List<String> args) async {
1818
if (dataAsset is! File) {
1919
continue;
2020
}
21-
final fileName = dataAsset.uri.pathSegments.last;
22-
final name = fileName.split('.').first;
21+
22+
// The file path relative to the package root, with forward slashes.
23+
final name = dataAsset.uri
24+
.toFilePath(windows: false)
25+
.substring(config.packageRoot.toFilePath(windows: false).length);
26+
2327
output.addAsset(
2428
DataAsset(
2529
package: packageName,

pkgs/native_assets_builder/test_data/complex_link/hook/link.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ void main(List<String> args) async {
1212
}
1313

1414
Iterable<Asset> treeshake(Iterable<Asset> assets) =>
15-
assets.where((asset) => !asset.id.endsWith('data_helper_2'));
15+
assets.where((asset) => !asset.id.endsWith('assets/data_helper_2.json'));

pkgs/native_assets_builder/test_data/complex_link_helper/hook/build.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ void main(List<String> args) async {
1818
if (dataAsset is! File) {
1919
continue;
2020
}
21-
final fileName = dataAsset.uri.pathSegments.last;
22-
final name = fileName.split('.').first;
21+
22+
// The file path relative to the package root, with forward slashes.
23+
final name = dataAsset.uri
24+
.toFilePath(windows: false)
25+
.substring(config.packageRoot.toFilePath(windows: false).length);
26+
2327
final forLinking = name.contains('2') || name.contains('3');
2428
output.addAsset(
2529
DataAsset(

pkgs/native_assets_builder/test_data/manifest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
- simple_link/hook/link.dart
8686
- simple_link/pubspec.yaml
8787
- simple_data_asset/pubspec.yaml
88-
- simple_data_asset/asset/test_asset.txt
88+
- simple_data_asset/assets/test_asset.txt
8989
- simple_data_asset/README.md
9090
- simple_data_asset/hook/build.dart
9191
- simple_data_asset/bin/simple_data_asset.dart.debug

pkgs/native_assets_builder/test_data/simple_data_asset/bin/deep_modify_data_asset.dart.debug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Future<void> main(List<String> args) async {
1010

1111
// Expect this to throw
1212
Future<String> getWorld() async {
13-
const asset = ByteAsset('package:simple_data_asset/test_asset.txt');
13+
const asset = ByteAsset('package:simple_data_asset/assets/test_asset.txt');
1414
final byteBuffer = await asset.load();
1515
byteBuffer.buffer.asFloat32List()[0] = 1.0;
1616
return String.fromCharCodes(byteBuffer);

pkgs/native_assets_builder/test_data/simple_data_asset/bin/modify_data_asset.dart.debug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Future<void> main(List<String> args) async {
1010

1111
// Expect this to throw
1212
Future<String> getWorld() async {
13-
const asset = ByteAsset('package:simple_data_asset/test_asset.txt');
13+
const asset = ByteAsset('package:simple_data_asset/assets/test_asset.txt');
1414
final byteBuffer = await asset.load();
1515
byteBuffer[0] = 42;
1616
return String.fromCharCodes(byteBuffer);

pkgs/native_assets_builder/test_data/simple_data_asset/bin/simple_data_asset.dart.debug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Future<void> main(List<String> args) async {
88
}
99

1010
Future<String> getWorld() async {
11-
const asset = ByteAsset('package:simple_data_asset/test_asset.txt');
11+
const asset = ByteAsset('package:simple_data_asset/assets/test_asset.txt');
1212
final byteBuffer = await asset.load();
1313
return String.fromCharCodes(byteBuffer);
1414
}

pkgs/native_assets_builder/test_data/simple_data_asset/hook/build.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ void main(List<String> args) async {
1818
if (dataAsset is! File) {
1919
continue;
2020
}
21-
final name = dataAsset.uri.pathSegments.last;
21+
22+
// The file path relative to the package root, with forward slashes.
23+
final name = dataAsset.uri
24+
.toFilePath(windows: false)
25+
.substring(config.packageRoot.toFilePath(windows: false).length);
26+
2227
output.addAsset(
2328
DataAsset(
2429
package: config.packageName,

pkgs/native_assets_builder/test_data/simple_link/hook/build.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ void main(List<String> args) async {
1818
if (dataAsset is! File) {
1919
continue;
2020
}
21-
final fileName = dataAsset.uri.pathSegments.last;
22-
final name = fileName.split('.').first;
21+
22+
// The file path relative to the package root, with forward slashes.
23+
final name = dataAsset.uri
24+
.toFilePath(windows: false)
25+
.substring(config.packageRoot.toFilePath(windows: false).length);
26+
2327
output.addAsset(
2428
DataAsset(
2529
package: packageName,

pkgs/native_assets_cli/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
- Fix some more cases of: `BuildConfig.dependencies` and
44
`LinkConfig.dependencies` no longer have to specify Dart sources.
5-
- `DataAsset` examples report all assets from `assets/` dir.
5+
- `DataAsset` examples report all assets from `assets/` dir and default the
6+
asset names to the path inside the package.
67

78
## 0.7.2
89

pkgs/native_assets_cli/example/link/package_with_assets/hook/build.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ void main(List<String> args) async {
1818
if (dataAsset is! File) {
1919
continue;
2020
}
21-
final fileName = dataAsset.uri.pathSegments.last;
22-
final name = fileName.split('.').first;
21+
22+
// The file path relative to the package root, with forward slashes.
23+
final name = dataAsset.uri
24+
.toFilePath(windows: false)
25+
.substring(config.packageRoot.toFilePath(windows: false).length);
26+
2327
output.addAsset(
2428
DataAsset(
2529
package: packageName,

pkgs/native_assets_cli/example/link/package_with_assets/lib/package_with_assets.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import 'package:meta/meta.dart';
66

77
//TODO: Actually use the assets, needs the AssetBundle interface for Dart. See
88
//also https://github.com/dart-lang/sdk/issues/54003.
9-
@ResourceIdentifier('used_asset')
9+
@ResourceIdentifier('assets/used_asset.json')
1010
String someMethod() => 'Using used_asset';
1111

12-
@ResourceIdentifier('unused_asset')
12+
@ResourceIdentifier('assets/unused_asset.json')
1313
String someOtherMethod() => 'Using unused_asset';

0 commit comments

Comments
 (0)