Skip to content

Commit

Permalink
Make resources nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed Apr 16, 2024
1 parent c24e422 commit 725bdd7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ const packageName = 'treeshaking_native_assets';
void main(List<String> arguments) async {
await link(arguments, (config, output) async {
final usedSymbols =
config.resources.map((resource) => resource.metadata.toString());
config.resources?.map((resource) => resource.metadata.toString());
final dynamicLibrary = config.assets.firstWhere((asset) =>
asset.id == 'package:$packageName/src/${packageName}_bindings.dart');
final staticLibrary = config.assets
.firstWhere((asset) => asset.id == 'package:$packageName/staticlib');

final linkerScript = await _writeLinkerScript(usedSymbols);
await _treeshakeStaticLibrary(
usedSymbols,
linkerScript,
dynamicLibrary,
staticLibrary,
);
output.addAsset(dynamicLibrary);
output.addDependency(config.packageRoot.resolve('hook/link.dart'));
if (usedSymbols != null) {
final linkerScript = await _writeLinkerScript(usedSymbols);
await _treeshakeStaticLibrary(
usedSymbols,
linkerScript,
dynamicLibrary,
staticLibrary,
);
output.addAsset(dynamicLibrary);
output.addDependency(config.packageRoot.resolve('hook/link.dart'));
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import 'package:native_assets_cli/native_assets_cli.dart';
void main(List<String> args) async {
await link(args, (config, output) async {
final assetsWithResource = config.assets.whereType<DataAsset>().where(
(asset) => config.resources
.any((resource) => resource.metadata == asset.name));
(asset) =>
config.resources
?.any((resource) => resource.metadata == asset.name) ??
true);
output.addAssets(assetsWithResource);
});
}
3 changes: 2 additions & 1 deletion pkgs/native_assets_cli/lib/src/api/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import 'link_config.dart';
///
/// As the linking runs after kernel compilation, you can use treeshaking
/// information provided through [LinkConfig.resources] to decide which assets
/// to include.
/// to include. The resources are only collected in AOT mode, therefore the
/// field is null in JIT mode.
///
///
/// ```dart
Expand Down
4 changes: 3 additions & 1 deletion pkgs/native_assets_cli/lib/src/api/link_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ abstract class LinkConfig implements HookConfig {
/// A collection of methods annotated with `@ResourceIdentifier`, which are
/// called in the tree-shaken Dart code. This information can be used to
/// dispose unused [assets].
List<Resource> get resources;
///
/// This is `null` in JIT mode, where no resources are collected.
List<Resource>? get resources;

/// Generate the [LinkConfig] from the input arguments to the linking script.
factory LinkConfig.fromArguments(List<String> arguments) =>
Expand Down
6 changes: 3 additions & 3 deletions pkgs/native_assets_cli/lib/src/model/link_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LinkConfigImpl extends HookConfigImpl implements LinkConfig {
final BuildConfigImpl _buildConfig;

@override
final List<Resource> resources;
final List<Resource>? resources;

final _LinkConfigArgs _args;

Expand Down Expand Up @@ -99,8 +99,8 @@ class LinkConfigImpl extends HookConfigImpl implements LinkConfig {
static Version latestVersion = Version(1, 0, 0);
}

List<Resource> fromIdentifiers(ResourceIdentifiers? resourceIdentifiers) =>
(resourceIdentifiers?.identifiers ?? [])
List<Resource>? fromIdentifiers(ResourceIdentifiers? resourceIdentifiers) =>
resourceIdentifiers?.identifiers
.map((e) => Resource(name: e.name, metadata: e.id))
.toList();

Expand Down

0 comments on commit 725bdd7

Please sign in to comment.