Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native_assets_cli] asset ids option 1 #1536

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,8 @@ ${compileResult.stdout}
if (!packagesWithLink.contains(targetPackage)) {
for (final asset in output.assetsForLinking[targetPackage]!) {
success &= false;
// Note that DataAssets don't have an ID in their API, so error
// messages might be confusing.
errors.add(
'Asset "${asset.id}" is sent to package "$targetPackage" for'
' linking, but that package does not have a link hook.',
Expand Down
2 changes: 2 additions & 0 deletions pkgs/native_assets_builder/lib/src/model/hook_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ final class HookResult
final twoInOne = assets1.where((asset) => assets2.contains(asset));
final oneInTwo = assets2.where((asset) => assets1.contains(asset));
if (twoInOne.isNotEmpty || oneInTwo.isNotEmpty) {
// Note that DataAssets don't have an ID in their API, so error
// messages might be confusing.
throw ArgumentError(
'Found duplicate IDs, ${oneInTwo.map((e) => e.id).toList()}');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ void main(List<String> arguments) async {
output
..addAsset(
NativeCodeAsset(
package: 'add_asset_link',
name: 'dylib_add_link',
id: 'package:add_asset_link/dylib_add_link',
linkMode: builtDylib.linkMode,
os: builtDylib.os,
architecture: builtDylib.architecture,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import 'package:native_assets_cli/native_assets_cli.dart';
void main(List<String> args) async {
await link(
args,
(config, output) async => output.addAssets(treeshake(config.assets)),
(config, output) async =>
output.addAssets(treeshake(config.assets.whereType<DataAsset>())),
);
}

Iterable<Asset> treeshake(Iterable<Asset> assets) =>
assets.where((asset) => !asset.id.endsWith('assets/data_helper_2.json'));
Iterable<Asset> treeshake(Iterable<DataAsset> assets) =>
assets.where((asset) => !asset.name.endsWith('assets/data_helper_2.json'));
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import 'package:native_assets_cli/native_assets_cli.dart';

void main(List<String> arguments) async {
await link(arguments, (config, output) async {
final dataAssets = config.assets.whereType<DataAsset>();
print('''
Received ${config.assets.length} assets: ${config.assets.map((e) => e.id)}.
Received ${dataAssets.length} assets: ${dataAssets.map((e) => '${e.package} ${e.name}')}.
''');
output.addAssets(config.assets.where((asset) => asset.id.endsWith('add')));
output.addAssets(dataAssets.where((asset) => asset.name.endsWith('add')));
print('''
Keeping only ${output.assets.map((e) => e.id)}.
Keeping only ${dataAssets.map((e) => '${e.package} ${e.name}')}.
''');
output.addDependency(config.packageRoot.resolve('hook/link.dart'));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ void main(List<String> arguments) async {
await link(
arguments,
(config, output) async {
final asset = config.assets.single as NativeCodeAsset;
final linker = CLinker.library(
name: config.packageName,
assetName: config.assets.single.id.split('/').skip(1).join('/'),
assetName: asset.id.split('/').skip(1).join('/'),
linkerOptions: LinkerOptions.treeshake(symbols: ['add']),
sources: [config.assets.single.file!.toFilePath()],
sources: [asset.file!.toFilePath()],
);
await linker.run(
config: config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ void main(List<String> arguments) async {

output.addAsset(
NativeCodeAsset(
package: config.packageName,
name: 'foo',
id: 'package:${config.packageName}/foo',
file: assetUri,
linkMode: DynamicLoadingBundled(),
os: OS.current,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ void main(List<String> arguments) async {

output.addAsset(
NativeCodeAsset(
package: 'other_package',
name: 'foo',
id: 'package:other_package/foo',
file: assetUri,
linkMode: DynamicLoadingBundled(),
os: OS.current,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ Future<void> main(List<String> args) async {
output.addAsset(
// TODO: Change to DataAsset once the Dart/Flutter SDK can consume it.
NativeCodeAsset(
package: packageName,
name: 'asset.txt',
id: 'package:$packageName/asset.txt',
file: assetPath,
linkMode: DynamicLoadingBundled(),
os: config.targetOS,
Expand Down
31 changes: 1 addition & 30 deletions pkgs/native_assets_cli/lib/src/api/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,8 @@ part 'native_code_asset.dart';
/// Data or code bundled with a Dart or Flutter application.
///
/// An asset is data or code which is accessible from a Dart or Flutter
/// application. To access an asset at runtime, the asset [id] is used.
/// application.
abstract final class Asset {
/// The identifier for this asset.
///
/// An [Asset] has a string identifier called "asset id". Dart code that uses
/// an asset references the asset using this asset id.
///
/// An asset identifier consists of two elements, the `package` and `name`,
/// which together make a library uri `package:<package>/<name>`. The package
/// being part of the identifer prevents name collisions between assets of
/// different packages.
///
/// The default asset id for an asset reference from `lib/src/foo.dart` is
/// `'package:foo/src/foo.dart'`. For example a [NativeCodeAsset] can be accessed
/// via `@Native` with the `assetId` argument omitted:
///
/// ```dart
/// // file package:foo/src/foo.dart
/// @Native<Int Function(Int, Int)>()
/// external int add(int a, int b);
/// ```
///
/// This will be then automatically expanded to
///
/// ```dart
/// // file package:foo/src/foo.dart
/// @Native<Int Function(Int, Int)>(assetId: 'package:foo/src/foo.dart')
/// external int add(int a, int b);
/// ```
String get id;

/// The file to be bundled with the Dart or Flutter application.
///
/// How this file is bundled depends on the kind of asset, represented by a
Expand Down
7 changes: 1 addition & 6 deletions pkgs/native_assets_cli/lib/src/api/data_asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ part of 'asset.dart';

/// Data bundled with a Dart or Flutter application.
///
/// A data asset is accessible in a Dart or Flutter application. To retrieve an
/// asset at runtime, the [id] is used. This enables access to the asset
/// irrespective of how and where the application is run.
/// A data asset is accessible in a Dart or Flutter application.
///
/// An data asset must provide a [Asset.file]. The Dart and Flutter SDK will
/// bundle this code in the final application.
abstract final class DataAsset implements Asset {
/// Constructs a data asset.
///
/// The unique [id] of this asset is a uri `package:<package>/<name>` from
/// [package] and [name].
factory DataAsset({
required String package,
required String name,
Expand Down
37 changes: 32 additions & 5 deletions pkgs/native_assets_cli/lib/src/api/native_code_asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ part of 'asset.dart';
/// [file] from its specified location on the current system into the
/// application bundle.
abstract final class NativeCodeAsset implements Asset {
/// The identifier for this asset.
///
/// An [Asset] has a string identifier called "asset id". Dart code that uses
/// an asset references the asset using this asset id.
///
/// An asset identifier consists of two elements, the `package` and `name`,
/// which together make a library uri `package:<package>/<name>`. The package
/// being part of the identifer prevents name collisions between assets of
/// different packages.
///
/// The default asset id for an asset reference from `lib/src/foo.dart` is
/// `'package:foo/src/foo.dart'`. For example a [NativeCodeAsset] can be accessed
/// via `@Native` with the `assetId` argument omitted:
///
/// ```dart
/// // file package:foo/src/foo.dart
/// @Native<Int Function(Int, Int)>()
/// external int add(int a, int b);
/// ```
///
/// This will be then automatically expanded to
///
/// ```dart
/// // file package:foo/src/foo.dart
/// @Native<Int Function(Int, Int)>(assetId: 'package:foo/src/foo.dart')
/// external int add(int a, int b);
/// ```
String get id;

/// The operating system this asset can run on.
OS get os;

Expand Down Expand Up @@ -77,18 +106,16 @@ abstract final class NativeCodeAsset implements Asset {

/// Constructs a native code asset.
///
/// The [id] of this asset is a uri `package:<package>/<name>` from [package]
/// and [name].
/// The [id] of this asset is a uri `package:<package>/<name>`.
factory NativeCodeAsset({
required String package,
required String name,
required String id,
required LinkMode linkMode,
required OS os,
Uri? file,
Architecture? architecture,
}) =>
NativeCodeAssetImpl(
id: 'package:$package/$name',
id: id,
linkMode: linkMode as LinkModeImpl,
os: os as OSImpl,
architecture: architecture as ArchitectureImpl?,
Expand Down
2 changes: 2 additions & 0 deletions pkgs/native_assets_cli/lib/src/model/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ part of '../api/asset.dart';
abstract final class AssetImpl implements Asset {
Map<String, Object> toJson(Version version);

String get id;

static List<AssetImpl> listFromJson(List<Object?>? list) {
final assets = <AssetImpl>[];
if (list == null) return assets;
Expand Down
11 changes: 11 additions & 0 deletions pkgs/native_assets_cli/lib/src/validator/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ List<String> validateOutputAssetTypes(
final supportedAssetTypes = config.supportedAssetTypes;
for (final asset in output.assets) {
if (!supportedAssetTypes.contains(asset.type)) {
// Note that DataAssets don't have an ID in their API, so error messages
// might be confusing.
final error =
'Asset "${asset.id}" has asset type "${asset.type}", which is '
'not in supportedAssetTypes';
Expand All @@ -98,6 +100,9 @@ Future<List<String>> validateFilesExist(

await Future.wait(output.allAssets.map((asset) async {
final file = asset.file;
asset as AssetImpl;
// Note that DataAssets don't have an ID in their API, so error messages
// might be confusing.
if (file == null && !config.dryRun) {
final error = 'Asset "${asset.id}" has no file.';
errors.add(error);
Expand Down Expand Up @@ -180,6 +185,9 @@ List<String> validateAssetId(
final errors = <String>[];
final packageName = config.packageName;
for (final asset in output.assets) {
asset as AssetImpl;
// Note that DataAssets don't have an ID in their API, so error messages
// might be confusing.
if (!asset.id.startsWith('package:$packageName/')) {
final error = 'Asset "${asset.id}" does not start with '
'"package:$packageName/".';
Expand All @@ -195,6 +203,9 @@ List<String> validateNoDuplicateAssetIds(
final errors = <String>[];
final assetIds = <String>{};
for (final asset in output.assets) {
asset as AssetImpl;
// Note that DataAssets don't have an ID in their API, so error messages
// might be confusing.
if (assetIds.contains(asset.id)) {
final error = 'Duplicate asset id: "${asset.id}".';
errors.add(error);
Expand Down
21 changes: 7 additions & 14 deletions pkgs/native_assets_cli/test/api/asset_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,39 @@ void main() {
test('Asset constructors', () async {
final assets = [
NativeCodeAsset(
package: 'my_package',
name: 'foo',
id: 'package:my_package/foo',
file: Uri.file('path/to/libfoo.so'),
linkMode: DynamicLoadingBundled(),
os: OS.android,
architecture: Architecture.x64,
),
NativeCodeAsset(
package: 'my_package',
name: 'foo3',
id: 'package:my_package/foo3',
linkMode: DynamicLoadingSystem(Uri(path: 'libfoo3.so')),
os: OS.android,
architecture: Architecture.x64,
),
NativeCodeAsset(
package: 'my_package',
name: 'foo4',
id: 'package:my_package/foo4',
linkMode: LookupInExecutable(),
os: OS.android,
architecture: Architecture.x64,
),
NativeCodeAsset(
package: 'my_package',
name: 'foo5',
id: 'package:my_package/foo5',
linkMode: LookupInProcess(),
os: OS.android,
architecture: Architecture.x64,
),
NativeCodeAsset(
package: 'my_package',
name: 'bar',
id: 'package:my_package/bar',
file: Uri(path: 'path/to/libbar.a'),
os: OS.linux,
architecture: Architecture.arm64,
linkMode: StaticLinking(),
),
NativeCodeAsset(
package: 'my_package',
name: 'bla',
id: 'package:my_package/bla',
file: Uri(path: 'path/with spaces/bla.dll'),
linkMode: DynamicLoadingBundled(),
os: OS.windows,
Expand All @@ -73,8 +67,7 @@ void main() {
test('Errors', () {
expect(
() => NativeCodeAsset(
package: 'my_package',
name: 'foo',
id: 'package:my_package/foo',
file: Uri.file('path/to/libfoo.so'),
linkMode: LookupInExecutable(),
os: OS.android,
Expand Down
6 changes: 2 additions & 4 deletions pkgs/native_assets_cli/test/api/build_output_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ void main() {
timestamp: DateTime.parse('2022-11-10 13:25:01.000'),
assets: [
NativeCodeAsset(
package: 'my_package',
name: 'foo',
id: 'package:my_package/foo',
file: Uri(path: 'path/to/libfoo.so'),
linkMode: DynamicLoadingBundled(),
os: OS.android,
architecture: Architecture.x64,
),
NativeCodeAsset(
package: 'my_package',
name: 'foo2',
id: 'package:my_package/foo2',
linkMode: DynamicLoadingSystem(Uri(path: 'path/to/libfoo2.so')),
os: OS.android,
architecture: Architecture.x64,
Expand Down
3 changes: 1 addition & 2 deletions pkgs/native_assets_cli/test/model/link_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ void main() async {
file: Uri.file('nonexistent'),
),
NativeCodeAsset(
package: packageName,
name: 'name2',
id: 'package:$packageName/name2',
linkMode: DynamicLoadingBundled(),
os: OS.android,
file: Uri.file('not there'),
Expand Down
Loading
Loading