Skip to content

Commit

Permalink
[native_*] Hooks with DataAssets read from disk (#1345)
Browse files Browse the repository at this point in the history
While working on #1208, I released that most likely you'd want to report all files from the `assets/` directory as `DataAsset`s in a hook. (This also means that there's implicitly a `dependency` on the directory itself.)

This PR also cleans up the remaining dart files listed in `dependencies`.
  • Loading branch information
dcharkes authored Jul 17, 2024
1 parent d4b7cda commit 6690e26
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 102 deletions.
4 changes: 3 additions & 1 deletion pkgs/native_assets_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 0.8.2-wip

- Nothing yet.
- Fix some more cases of: `BuildConfig.dependencies` and
`LinkConfig.dependencies` no longer have to specify Dart sources.
- `DataAsset` test projects report all assets from `assets/` dir.

## 0.8.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ void main() async {
linkingEnabled: true,
);
expect(buildResult.success, true);
expect(_getNames(buildResult.assets), orderedEquals(builtHelperAssets));
expect(
_getNames(buildResult.assets), unorderedEquals(builtHelperAssets));
expect(
_getNames(buildResult.assetsForLinking['complex_link']!),
orderedEquals(assetsForLinking),
unorderedEquals(assetsForLinking),
);

final linkResult = await linkDryRun(
Expand All @@ -88,7 +89,7 @@ void main() async {
);
expect(linkResult.success, true);

expect(_getNames(linkResult.assets), orderedEquals(linkedAssets));
expect(_getNames(linkResult.assets), unorderedEquals(linkedAssets));
});
},
);
Expand Down
7 changes: 4 additions & 3 deletions pkgs/native_assets_builder/test/build_runner/link_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ void main() async {
linkingEnabled: true,
);
expect(buildResult.success, true);
expect(_getNames(buildResult.assets), orderedEquals(builtHelperAssets));
expect(
_getNames(buildResult.assets), unorderedEquals(builtHelperAssets));
expect(
_getNames(buildResult.assetsForLinking['complex_link']!),
orderedEquals(assetsForLinking),
unorderedEquals(assetsForLinking),
);

final linkResult = await link(
Expand All @@ -96,7 +97,7 @@ void main() async {
);
expect(linkResult.success, true);

expect(_getNames(linkResult.assets), orderedEquals(linkedAssets));
expect(_getNames(linkResult.assets), unorderedEquals(linkedAssets));
});
},
);
Expand Down
40 changes: 27 additions & 13 deletions pkgs/native_assets_builder/test_data/complex_link/hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@
// 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 'dart:io';

import 'package:native_assets_cli/native_assets_cli.dart';

const packageName = 'complex_link';
void main(List<String> args) async {
await build(args, (config, output) async {
final packageName = config.packageName;
final assetDirectory =
Directory.fromUri(config.packageRoot.resolve('assets/'));
// If assets are added, rerun hook.
output.addDependency(assetDirectory.uri);

void main(List<String> args) async => build(args, (config, output) async {
output.addAssets(
List.generate(
2,
(index) => DataAsset(
name: 'data_$index',
// TODO(mosuem): Simplify specifying files/file paths
file: config.packageRoot.resolve('assets/data_$index.json'),
package: packageName,
),
await for (final dataAsset in assetDirectory.list()) {
if (dataAsset is! File) {
continue;
}
final fileName = dataAsset.uri.pathSegments.last;
final name = fileName.split('.').first;
output.addAsset(
DataAsset(
package: packageName,
name: name,
file: dataAsset.uri,
),
linkInPackage: config.linkingEnabled ? 'complex_link' : null,
linkInPackage: config.linkingEnabled ? packageName : null,
);
});
// TODO(https://github.com/dart-lang/native/issues/1208): Report
// dependency on asset.
output.addDependency(dataAsset.uri);
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@
// 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 'dart:io';

import 'package:native_assets_cli/native_assets_cli.dart';

const packageName = 'complex_link_helper';
void main(List<String> args) async {
await build(args, (config, output) async {
final packageName = config.packageName;
final assetDirectory =
Directory.fromUri(config.packageRoot.resolve('assets/'));
// If assets are added, rerun hook.
output.addDependency(assetDirectory.uri);

void main(List<String> args) async => build(args, (config, output) async {
output.addAssets(
List.generate(
2,
(index) => DataAsset(
name: 'data_helper_$index',
// TODO(mosuem): Simplify specifying files/file paths
file: config.packageRoot.resolve('assets/data_helper_$index.json'),
package: packageName,
),
),
);
output.addAssets(
List.generate(
2,
(index) => DataAsset(
name: 'data_helper_${index + 2}',
// TODO(mosuem): Simplify specifying files/file paths
file: config.packageRoot
.resolve('assets/data_helper_${index + 2}.json'),
package: packageName,
),
await for (final dataAsset in assetDirectory.list()) {
if (dataAsset is! File) {
continue;
}
final fileName = dataAsset.uri.pathSegments.last;
final name = fileName.split('.').first;
final forLinking = name.contains('2') || name.contains('3');
output.addAsset(
DataAsset(
package: packageName,
name: name,
file: dataAsset.uri,
),
linkInPackage: config.linkingEnabled ? 'complex_link' : null,
linkInPackage:
forLinking && config.linkingEnabled ? 'complex_link' : null,
);
});
// TODO(https://github.com/dart-lang/native/issues/1208): Report
// dependency on asset.
output.addDependency(dataAsset.uri);
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import 'package:logging/logging.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';

const packageName = 'drop_dylib_link';

void main(List<String> arguments) async {
await build(arguments, (config, output) async {
final logger = Logger('')
..level = Level.ALL
..onRecord.listen((record) {
print('${record.level.name}: ${record.time}: ${record.message}');
});
final linkInPackage = config.linkingEnabled ? packageName : null;
final linkInPackage = config.linkingEnabled ? config.packageName : null;
await CBuilder.library(
name: 'add',
assetName: 'dylib_add',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import 'package:logging/logging.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';

const packageName = 'native_add';

void main(List<String> arguments) async {
await build(arguments, (config, output) async {
final packageName = config.packageName;
final cbuilder = CBuilder.library(
name: packageName,
assetName: 'src/${packageName}_bindings_generated.dart',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import 'package:logging/logging.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';

const packageName = 'native_add';

void main(List<String> arguments) async {
await build(arguments, (config, output) async {
final packageName = config.packageName;
final cbuilder = CBuilder.library(
name: packageName,
assetName: '${packageName}_bindings_generated.dart',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import 'package:logging/logging.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';

const packageName = 'native_subtract';

void main(List<String> arguments) async {
await build(arguments, (config, output) async {
final packageName = config.packageName;
final cbuilder = CBuilder.library(
name: packageName,
assetName: 'src/${packageName}_bindings_generated.dart',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Future<void> main(List<String> args) async {

// Expect this to throw
Future<String> getWorld() async {
const asset = ByteAsset('package:simple_data_asset/assetId');
const asset = ByteAsset('package:simple_data_asset/test_asset.txt');
final byteBuffer = await asset.load();
byteBuffer.buffer.asFloat32List()[0] = 1.0;
return String.fromCharCodes(byteBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Future<void> main(List<String> args) async {

// Expect this to throw
Future<String> getWorld() async {
const asset = ByteAsset('package:simple_data_asset/assetId');
const asset = ByteAsset('package:simple_data_asset/test_asset.txt');
final byteBuffer = await asset.load();
byteBuffer[0] = 42;
return String.fromCharCodes(byteBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Future<void> main(List<String> args) async {
}

Future<String> getWorld() async {
const asset = ByteAsset('package:simple_data_asset/assetId');
const asset = ByteAsset('package:simple_data_asset/test_asset.txt');
final byteBuffer = await asset.load();
return String.fromCharCodes(byteBuffer);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
// 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.
import 'dart:io';

import 'package:native_assets_cli/native_assets_cli.dart';

void main(List<String> args) => build(
args,
(config, output) async {
void main(List<String> args) async {
await build(
args,
(config, output) async {
final assetDirectory =
Directory.fromUri(config.packageRoot.resolve('assets/'));
// If assets are added, rerun hook.
output.addDependency(assetDirectory.uri);

await for (final dataAsset in assetDirectory.list()) {
if (dataAsset is! File) {
continue;
}
final name = dataAsset.uri.pathSegments.last;
output.addAsset(
DataAsset(
package: config.packageName,
name: 'assetId',
file: config.packageRoot.resolve('asset/test_asset.txt'),
name: name,
file: dataAsset.uri,
),
);
output.addDependency(config.packageRoot.resolve('hook/build.dart'));
},
);
// TODO(https://github.com/dart-lang/native/issues/1208): Report
// dependency on asset.
output.addDependency(dataAsset.uri);
}
},
);
}
40 changes: 27 additions & 13 deletions pkgs/native_assets_builder/test_data/simple_link/hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@
// 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 'dart:io';

import 'package:native_assets_cli/native_assets_cli.dart';

const packageName = 'simple_link';
void main(List<String> args) async {
await build(args, (config, output) async {
final packageName = config.packageName;
final assetDirectory =
Directory.fromUri(config.packageRoot.resolve('assets/'));
// If assets are added, rerun hook.
output.addDependency(assetDirectory.uri);

void main(List<String> args) async => build(args, (config, output) async {
output.addAssets(
List.generate(
4,
(index) => DataAsset(
name: 'data_$index',
// TODO(mosuem): Simplify specifying files/file paths
file: config.packageRoot.resolve('assets/data_$index.json'),
package: packageName,
),
await for (final dataAsset in assetDirectory.list()) {
if (dataAsset is! File) {
continue;
}
final fileName = dataAsset.uri.pathSegments.last;
final name = fileName.split('.').first;
output.addAsset(
DataAsset(
package: packageName,
name: name,
file: dataAsset.uri,
),
linkInPackage: config.linkingEnabled ? 'simple_link' : null,
linkInPackage: config.linkingEnabled ? packageName : null,
);
});
// TODO(https://github.com/dart-lang/native/issues/1208): Report
// dependency on asset.
output.addDependency(dataAsset.uri);
}
});
}
6 changes: 6 additions & 0 deletions pkgs/native_assets_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.7.3-wip

- Fix some more cases of: `BuildConfig.dependencies` and
`LinkConfig.dependencies` no longer have to specify Dart sources.
- `DataAsset` examples report all assets from `assets/` dir.

## 0.7.2

- Deprecate metadata concept.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ void main(List<String> args) async {

output.addDependencies([
assetSourcePath,
config.packageRoot.resolve('hook/build.dart'),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import 'package:logging/logging.dart';
import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';

const packageName = 'use_dart_api';

void main(List<String> arguments) async {
await build(arguments, (config, output) async {
final packageName = config.packageName;
final cbuilder = CBuilder.library(
name: packageName,
assetName: 'src/${packageName}_bindings_generated.dart',
Expand Down
Loading

0 comments on commit 6690e26

Please sign in to comment.