Skip to content

Commit

Permalink
[native_assets_cli] asset id option 3
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes committed Sep 9, 2024
1 parent 75805b2 commit 714b39b
Show file tree
Hide file tree
Showing 31 changed files with 117 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ void main() async {
}

Iterable<String> _getNames(List<AssetImpl> assets) =>
assets.whereType<cli.DataAsset>().map((asset) => asset.name);
assets.whereType<cli.DataAsset>().map((asset) => asset.id.name);
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,4 @@ void main() async {
}

Iterable<String> _getNames(List<AssetImpl> assets) =>
assets.whereType<cli.DataAsset>().map((asset) => asset.name);
assets.whereType<cli.DataAsset>().map((asset) => asset.id.name);
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void main() async {
);

final addLibraryPath = assets
.firstWhere((asset) => asset.id.endsWith('add.dart'))
.firstWhere((asset) => asset.id.string.endsWith('add.dart'))
.file!
.toFilePath();
final addResult = await runProcess(
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: AssetId('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 @@ -26,8 +26,7 @@ void main(List<String> args) async {

output.addAsset(
DataAsset(
package: packageName,
name: name,
id: AssetId(packageName, name),
file: dataAsset.uri,
),
linkInPackage: config.linkingEnabled ? packageName : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ void main(List<String> args) async {
);
}

Iterable<Asset> treeshake(Iterable<Asset> assets) =>
assets.where((asset) => !asset.id.endsWith('assets/data_helper_2.json'));
Iterable<Asset> treeshake(Iterable<Asset> assets) => assets
.where((asset) => !asset.id.string.endsWith('assets/data_helper_2.json'));
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ void main(List<String> args) async {
final forLinking = name.contains('2') || name.contains('3');
output.addAsset(
DataAsset(
package: packageName,
name: name,
id: AssetId(packageName, name),
file: dataAsset.uri,
),
linkInPackage:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ void main(List<String> arguments) async {
print('''
Received ${config.assets.length} assets: ${config.assets.map((e) => e.id)}.
''');
output.addAssets(config.assets.where((asset) => asset.id.endsWith('add')));
output.addAssets(
config.assets.where((asset) => asset.id.string.endsWith('add')));
print('''
Keeping only ${output.assets.map((e) => e.id)}.
''');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ void main(List<String> arguments) async {
await build(arguments, (config, output) async {
output.addAsset(
DataAsset(
name: 'data',
id: AssetId(config.packageName, 'data'),
file: config.packageRoot.resolve('assets/data.json'),
package: config.packageName,
),
linkInPackage:
config.linkingEnabled ? 'fail_on_os_sdk_version_linker' : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ void main(List<String> args) async {

output.addAsset(
DataAsset(
package: config.packageName,
name: name,
id: AssetId(config.packageName, name),
file: dataAsset.uri,
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ void main(List<String> args) async {

output.addAsset(
DataAsset(
package: packageName,
name: name,
id: AssetId(packageName, name),
file: dataAsset.uri,
),
linkInPackage: config.linkingEnabled ? packageName : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void main(List<String> arguments) async {
(config, output) async {
final linker = CLinker.library(
name: config.packageName,
assetName: config.assets.single.id.split('/').skip(1).join('/'),
assetName: config.assets.single.id.name,
linkerOptions: LinkerOptions.treeshake(symbols: ['add']),
sources: [config.assets.single.file!.toFilePath()],
);
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: AssetId(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: AssetId('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: AssetId(packageName, 'asset.txt'),
file: assetPath,
linkMode: DynamicLoadingBundled(),
os: config.targetOS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ void main(List<String> args) async {

output.addAsset(
DataAsset(
package: packageName,
name: name,
id: AssetId(packageName, name),
file: dataAsset.uri,
),
linkInPackage: config.linkingEnabled ? packageName : null,
Expand Down
1 change: 1 addition & 0 deletions pkgs/native_assets_cli/lib/native_assets_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export 'src/api/architecture.dart' show Architecture;
export 'src/api/asset.dart'
show
Asset,
AssetId,
DataAsset,
DynamicLoadingBundled,
DynamicLoadingSystem,
Expand Down
39 changes: 38 additions & 1 deletion pkgs/native_assets_cli/lib/src/api/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract final class Asset {
/// @Native<Int Function(Int, Int)>(assetId: 'package:foo/src/foo.dart')
/// external int add(int a, int b);
/// ```
String get id;
AssetId get id;

/// The file to be bundled with the Dart or Flutter application.
///
Expand All @@ -64,3 +64,40 @@ abstract final class Asset {
/// or Flutter.
Uri? get file;
}

/// The identifier for an [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);
/// ```
extension type AssetId._(String string) {
AssetId.fromString(this.string);

AssetId(String package, String name) : string = 'package:$package/$name';

String get name => string.split('/').skip(1).join('/');

String get package => string.split('/').first.replaceFirst('package:', '');
}
16 changes: 3 additions & 13 deletions pkgs/native_assets_cli/lib/src/api/data_asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,15 @@ part of 'asset.dart';
/// 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,
required AssetId id,
required Uri file,
}) =>
DataAssetImpl(
name: name,
package: package,
name: id.name,
package: id.package,
file: file,
);

/// The package which contains this asset.
String get package;

/// The name of this asset, which must be unique for the package.
String get name;

static const String type = 'data';
}
8 changes: 2 additions & 6 deletions pkgs/native_assets_cli/lib/src/api/native_code_asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,15 @@ abstract final class NativeCodeAsset implements Asset {
Uri? get file;

/// Constructs a native code asset.
///
/// The [id] of this asset is a uri `package:<package>/<name>` from [package]
/// and [name].
factory NativeCodeAsset({
required String package,
required String name,
required AssetId 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
4 changes: 1 addition & 3 deletions pkgs/native_assets_cli/lib/src/model/data_asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ final class DataAssetImpl implements DataAsset, AssetImpl {
@override
final Uri file;

@override
final String name;

@override
final String package;

@override
String get id => 'package:$package/$name';
AssetId get id => AssetId(package, name);

DataAssetImpl({
required this.file,
Expand Down
10 changes: 5 additions & 5 deletions pkgs/native_assets_cli/lib/src/model/native_code_asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl {
final LinkModeImpl linkMode;

@override
final String id;
final AssetId id;

@override
final OSImpl os;
Expand Down Expand Up @@ -301,7 +301,7 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl {
}

return NativeCodeAssetImpl(
id: get<String>(jsonMap, _idKey),
id: AssetId.fromString(get<String>(jsonMap, _idKey)),
os: os,
architecture: architecture,
linkMode: linkMode,
Expand All @@ -311,7 +311,7 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl {

NativeCodeAssetImpl copyWith({
LinkModeImpl? linkMode,
String? id,
AssetId? id,
OSImpl? os,
ArchitectureImpl? architecture,
Uri? file,
Expand Down Expand Up @@ -349,15 +349,15 @@ final class NativeCodeAssetImpl implements NativeCodeAsset, AssetImpl {
Map<String, Object> toJson(Version version) {
if (version == Version(1, 0, 0)) {
return {
_idKey: id,
_idKey: id.string,
...linkMode.toJsonV1_0_0(file),
_targetKey: Target.fromArchitectureAndOS(architecture!, os).toString(),
}..sortOnKey();
}
return {
if (architecture != null) _architectureKey: architecture.toString(),
if (file != null) _fileKey: file!.toFilePath(),
_idKey: id,
_idKey: id.string,
_linkModeKey: linkMode.toJson(),
_osKey: os.toString(),
typeKey: NativeCodeAsset.type,
Expand Down
6 changes: 3 additions & 3 deletions pkgs/native_assets_cli/lib/src/validator/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ List<String> validateAssetId(
final errors = <String>[];
final packageName = config.packageName;
for (final asset in output.assets) {
if (!asset.id.startsWith('package:$packageName/')) {
if (!asset.id.string.startsWith('package:$packageName/')) {
final error = 'Asset "${asset.id}" does not start with '
'"package:$packageName/".';
errors.add(error);
Expand All @@ -193,7 +193,7 @@ List<String> validateNoDuplicateAssetIds(
BuildOutput output,
) {
final errors = <String>[];
final assetIds = <String>{};
final assetIds = <AssetId>{};
for (final asset in output.assets) {
if (assetIds.contains(asset.id)) {
final error = 'Duplicate asset id: "${asset.id}".';
Expand All @@ -209,7 +209,7 @@ List<String> validateNoDuplicateDylibs(
Iterable<Asset> assets,
) {
final errors = <String>[];
final fileNameToAssetId = <String, Set<String>>{};
final fileNameToAssetId = <String, Set<AssetId>>{};
for (final asset in assets.whereType<NativeCodeAsset>()) {
if (asset.linkMode is! DynamicLoadingBundled) {
continue;
Expand Down
Loading

0 comments on commit 714b39b

Please sign in to comment.