Skip to content

Commit

Permalink
Remove resources from protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed May 3, 2024
1 parent 49914d7 commit 3f5bd87
Show file tree
Hide file tree
Showing 11 changed files with 12 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const packageName = 'treeshaking_native_assets';

void main(List<String> arguments) async {
await link(arguments, (config, output) async {
final usedSymbols = config.treeshakingInformation
?.map((resource) => resource.metadata.toString());
final List<String>? usedSymbols = [];
final dynamicLibrary = config.assets.firstWhere((asset) =>
asset.id == 'package:$packageName/src/${packageName}_bindings.dart');
final staticLibrary = config.assets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ 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.treeshakingInformation
?.any((resource) => resource.metadata == asset.name) ??
true);
output.addAssets(assetsWithResource);
final dataAssets = config.assets.whereType<DataAsset>();
output.addAssets(dataAssets);
});
}
1 change: 0 additions & 1 deletion pkgs/native_assets_cli/lib/native_assets_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ export 'src/api/link.dart';
export 'src/api/link_config.dart' show LinkConfig;
export 'src/api/link_mode_preference.dart' show LinkModePreference;
export 'src/api/os.dart' show OS;
export 'src/api/resource.dart';
1 change: 0 additions & 1 deletion pkgs/native_assets_cli/lib/native_assets_cli_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export 'src/api/ios_sdk.dart' show IOSSdkImpl;
export 'src/api/link_config.dart' show LinkConfigImpl;
export 'src/api/link_mode_preference.dart' show LinkModePreferenceImpl;
export 'src/api/os.dart' show OSImpl;
export 'src/api/resource.dart';
export 'src/model/dependencies.dart';
export 'src/model/hook.dart';
export 'src/model/metadata.dart';
Expand Down
13 changes: 3 additions & 10 deletions pkgs/native_assets_cli/lib/src/api/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,15 @@ import 'link_config.dart';
/// through [LinkConfig.assets]. They will only be bundled with the final
/// application if included in the [LinkOutput].
///
/// As the linking runs after kernel compilation, you can use treeshaking
/// information provided through [LinkConfig.treeshakingInformation] to decide
/// which assets to include. The resources are only collected in AOT mode,
/// therefore the field is null in JIT mode.
///
///
/// ```dart
/// 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));
/// output.addAssets(assetsWithResource);
/// final dataAssets = config.assets
/// .whereType<DataAsset>();
/// output.addAssets(dataAssets);
/// });
/// }
/// ```
Expand Down
17 changes: 1 addition & 16 deletions pkgs/native_assets_cli/lib/src/api/link_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:collection/collection.dart';
import 'package:pub_semver/pub_semver.dart';

import '../model/hook.dart';
import '../model/resource_identifiers.dart';
import '../utils/map.dart';
import 'architecture.dart';
import 'asset.dart';
Expand All @@ -20,28 +19,18 @@ import 'hook_config.dart';
import 'ios_sdk.dart';
import 'link_mode_preference.dart';
import 'os.dart';
import 'resource.dart';

part '../model/link_config.dart';

/// The configuration for a link hook (`hook/link.dart`) invocation.
///
/// It consists of a subset of the fields from the [BuildConfig] already passed
/// to the build hook, the [assets] from the build step, and the
/// [treeshakingInformation] generated during the kernel compilation.
/// to the build hook and the [assets] from the build step.
abstract class LinkConfig implements HookConfig {
/// The list of assets to be linked. These are the assets generated by a
/// `build.dart` script destined for this packages `link.dart`.
Iterable<Asset> get assets;

/// 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].
///
/// This is `null` in JIT mode, where no resources are collected, or in a dry
/// run.
Iterable<Resource>? get treeshakingInformation;

/// Generate the [LinkConfig] from the input arguments to the linking script.
factory LinkConfig.fromArguments(List<String> arguments) =>
LinkConfigImpl.fromArguments(arguments);
Expand All @@ -58,14 +47,12 @@ abstract class LinkConfig implements HookConfig {
List<String>? supportedAssetTypes,
int? targetAndroidNdkApi,
required Iterable<Asset> assets,
Uri? resourceIdentifierUri,
required LinkModePreference linkModePreference,
bool? dryRun,
Version? version,
}) =>
LinkConfigImpl(
assets: assets.cast(),
resourceIdentifierUri: resourceIdentifierUri,
outputDirectory: outputDirectory,
packageName: packageName,
packageRoot: packageRoot,
Expand All @@ -88,13 +75,11 @@ abstract class LinkConfig implements HookConfig {
required OS targetOS,
List<String>? supportedAssetTypes,
required Iterable<Asset> assets,
Uri? resourceIdentifierUri,
required LinkModePreference linkModePreference,
Version? version,
}) =>
LinkConfigImpl.dryRun(
assets: assets.cast(),
resourceIdentifierUri: resourceIdentifierUri,
outputDirectory: outputDirectory,
packageName: packageName,
packageRoot: packageRoot,
Expand Down
45 changes: 0 additions & 45 deletions pkgs/native_assets_cli/lib/src/api/resource.dart

This file was deleted.

27 changes: 4 additions & 23 deletions pkgs/native_assets_cli/lib/src/model/link_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@

part of '../api/link_config.dart';

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

/// The input to the linking script.
///
/// It consists of the fields inherited from the [HookConfig], the [assets] from
/// the build step, and the [treeshakingInformation] generated during the kernel
/// compilation.
/// It consists of the fields inherited from the [HookConfig] and the [assets]
/// from the build step.
class LinkConfigImpl extends HookConfigImpl implements LinkConfig {
static const resourceIdentifierKey = 'resource_identifiers';

Expand All @@ -22,17 +16,8 @@ class LinkConfigImpl extends HookConfigImpl implements LinkConfig {
@override
final Iterable<AssetImpl> assets;

Iterable<Resource>? _treeshakingInformation;

@override
Iterable<Resource>? get treeshakingInformation {
if (_resourceIdentifierUri != null && _treeshakingInformation == null) {
_treeshakingInformation = fromIdentifiers(
ResourceIdentifiers.fromFile(_resourceIdentifierUri.toFilePath()));
}
return _treeshakingInformation;
}

// TODO: Placeholder for the resources.json file URL. We don't want to change
// native_assets_builder when implementing the parsing.
final Uri? _resourceIdentifierUri;

LinkConfigImpl({
Expand Down Expand Up @@ -69,10 +54,6 @@ class LinkConfigImpl extends HookConfigImpl implements LinkConfig {
required super.linkModePreference,
required super.targetOS,
}) : _resourceIdentifierUri = resourceIdentifierUri,
_treeshakingInformation = resourceIdentifierUri != null
? fromIdentifiers(ResourceIdentifiers.fromFile(
resourceIdentifierUri.toFilePath()))
: null,
super.dryRun(
hook: Hook.link,
version: version ?? HookConfigImpl.latestVersion,
Expand Down
6 changes: 0 additions & 6 deletions pkgs/native_assets_cli/test/api/resource_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@
// 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 'package:native_assets_cli/src/api/resource.dart';
import 'package:native_assets_cli/src/model/resource_identifiers.dart';

final resourceList = [
Resource(name: 'methodName1', metadata: 'someMetadata'),
Resource(name: 'methodName2', metadata: 'someOtherMetadata'),
];

const resourceFile = '''{
"_comment": "Resources referenced by annotated resource identifiers",
"AppTag": "TBD",
Expand Down
14 changes: 0 additions & 14 deletions pkgs/native_assets_cli/test/api/resource_test.dart

This file was deleted.

27 changes: 1 addition & 26 deletions pkgs/native_assets_cli/test/model/link_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
// 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:convert';
import 'dart:io';

import 'package:native_assets_cli/native_assets_cli.dart';
import 'package:native_assets_cli/native_assets_cli_internal.dart';
import 'package:native_assets_cli/src/api/asset.dart';
import 'package:test/test.dart';

import '../api/resource_data.dart';

void main() async {
late Uri tempUri;
late Uri outDirUri;
Expand Down Expand Up @@ -59,8 +56,7 @@ void main() async {
fakeVcVars = tempUri.resolve('vcvarsall.bat');
await File.fromUri(fakeVcVars).create();
resources = tempUri.resolve('resources.json');
final file = File.fromUri(resources)..createSync();
file.writeAsStringSync(jsonEncode(resourceIdentifiers));
File.fromUri(resources).createSync();
});

tearDown(() async {
Expand Down Expand Up @@ -115,8 +111,6 @@ void main() async {
true);
expect(config1.cCompiler != config2.cCompiler, true);
expect(config1.assets != config2.assets, true);
expect(
config1.treeshakingInformation != config2.treeshakingInformation, true);
});

test('LinkConfig fromConfig', () {
Expand Down Expand Up @@ -196,25 +190,6 @@ void main() async {
final fromConfig = LinkConfigImpl.fromJson(configFile);
expect(fromConfig, equals(buildConfig1));
});
test('LinkConfig fetch treeshaking information', () {
final buildConfig1 = LinkConfigImpl(
outputDirectory: outDirUri,
packageName: packageName,
packageRoot: packageRootUri,
targetArchitecture: ArchitectureImpl.arm64,
targetOS: OSImpl.iOS,
targetIOSSdk: IOSSdkImpl.iPhoneOS,
cCompiler: CCompilerConfigImpl(
compiler: fakeClang,
linker: fakeLd,
),
buildMode: BuildModeImpl.release,
assets: assets,
resourceIdentifierUri: resources,
linkModePreference: LinkModePreferenceImpl.preferStatic,
);
expect(buildConfig1.treeshakingInformation, resourceList);
});

test('LinkConfig toJson fromJson', () {
final outDir = outDirUri;
Expand Down

0 comments on commit 3f5bd87

Please sign in to comment.