Skip to content

Add package:assets #868

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Split name and package
  • Loading branch information
mosuem committed Feb 28, 2025
commit b7d615e762e130578b38c65807e87f8e92315643
2 changes: 1 addition & 1 deletion pkgs/assets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ At runtime, you can access the data by:
import 'package:assets/src/assets.dart';

Future<void> main(List<String> arguments) async {
final asset = const StringAsset('package/myPackageName/myData1');
final asset = const StringAsset(package: 'myPackage', name: 'myData1');
final json = await asset.load();
print(json);
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/assets/example/assets_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
import 'package:assets/assets.dart';

Future<void> main() async {
final awesome = const StringAsset('testKey');
final awesome = const StringAsset(name: 'testKey', package: 'myPackage');
print('awesome: ${await awesome.load()}');
}
25 changes: 17 additions & 8 deletions pkgs/assets/lib/src/assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import 'assets_internal.dart';
import 'hook/assets_build.dart' show AssetBuilder;

abstract class Asset<T> {
final String key;
final String package;
final String name;

const Asset(this.key);
const Asset({required this.package, required this.name});

/// Load the asset asynchronously.
Future<T> load();
Expand All @@ -22,20 +23,28 @@ abstract class Asset<T> {
class StringAsset extends Asset<String> {
static const uniquePrefix = 'StringAsset';

const StringAsset(@mustBeConst super.key);
const StringAsset({
@mustBeConst required super.package,
@mustBeConst required super.name,
});

@override
Future<String> load() =>
loadAssetString(AssetBuilder.assetNameMangler<StringAsset>(key));
Future<String> load() => loadAssetString(
'package/$package/${AssetBuilder.assetNameMangler<StringAsset>(name)}',
);
}

@RecordUse()
class ByteAsset extends Asset<Uint8List> {
static const uniquePrefix = 'ByteAsset';

const ByteAsset(@mustBeConst super.key);
const ByteAsset({
@mustBeConst required super.package,
@mustBeConst required super.name,
});

@override
Future<Uint8List> load() =>
loadAssetBytes(AssetBuilder.assetNameMangler<ByteAsset>(key));
Future<Uint8List> load() => loadAssetBytes(
'package/$package/${AssetBuilder.assetNameMangler<ByteAsset>(name)}',
);
}
2 changes: 1 addition & 1 deletion pkgs/assets/lib/src/hook/assets_build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class AssetBuilder {
.entries,
].map(
(file) => DataAsset(
file: file.value,
name: file.key,
package: input.packageName,
file: file.value,
),
),
linkInPackage: linkInPackage,
Expand Down
8 changes: 4 additions & 4 deletions pkgs/assets/lib/src/hook/assets_link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class AssetLinker {
final assets = input.assets.data.where(
(asset) =>
usedStringSymbols.contains(
AssetBuilder.assetNameMangler<StringAsset>(asset.id),
AssetBuilder.assetNameMangler<StringAsset>(asset.name),
) ||
usedByteSymbols.contains(
AssetBuilder.assetNameMangler<ByteAsset>(asset.id),
AssetBuilder.assetNameMangler<ByteAsset>(asset.name),
),
);
output.assets.data.addAll(assets);
Expand All @@ -58,8 +58,8 @@ extension on record_use.RecordedUsages {
(instancesOf(identifier) ?? [])
.map(
(instance) =>
// Get the "key" field value from "RecordSymbol"
(instance.instanceConstant.fields.values.first
// Get the "name" field value from the String/ByteAsset class
(instance.instanceConstant.fields['name']
as record_use.StringConstant)
.value,
)
Expand Down