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

Allow custom build/link configuration, decouple core CLI infrastructure from code assets, data assets, ... #1643

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
615 changes: 273 additions & 342 deletions pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@

import 'package:native_assets_cli/native_assets_cli_internal.dart';

import '../../native_assets_builder.dart';

/// The result of executing the build hooks in dry run mode from all packages in
/// the dependency tree of the entry point application.
abstract interface class BuildDryRunResult {
/// The native assets produced by the hooks, which should be bundled.
List<EncodedAsset> get encodedAssets;

/// Whether all hooks completed without errors.
///
/// All error messages are streamed to [NativeAssetsBuildRunner.logger].
bool get success;

/// The native assets produced by the hooks, which should be linked.
Map<String, List<EncodedAsset>> get encodedAssetsForLinking;
}
7 changes: 0 additions & 7 deletions pkgs/native_assets_builder/lib/src/model/build_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@

import 'package:native_assets_cli/native_assets_cli_internal.dart';

import '../build_runner/build_runner.dart';

/// The result of executing build hooks from all packages in the dependency tree
/// of the entry point application.
abstract class BuildResult {
/// The files used by the hooks.
List<Uri> get dependencies;

/// Whether all hooks completed without errors.
///
/// All error messages are streamed to [NativeAssetsBuildRunner.logger].
bool get success;

/// The native assets produced by the hooks, which should be bundled.
List<EncodedAsset> get encodedAssets;

Expand Down
41 changes: 14 additions & 27 deletions pkgs/native_assets_builder/lib/src/model/hook_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,48 @@ final class HookResult implements BuildResult, BuildDryRunResult, LinkResult {
@override
final List<Uri> dependencies;

/// Whether all hooks completed without errors.
///
/// All error messages are streamed to [NativeAssetsBuildRunner.logger].
@override
final bool success;

HookResult._({
required this.encodedAssets,
required this.encodedAssetsForLinking,
required this.dependencies,
required this.success,
});

factory HookResult({
List<EncodedAsset>? encodedAssets,
Map<String, List<EncodedAsset>>? encodedAssetsForLinking,
List<Uri>? dependencies,
bool success = true,
}) =>
HookResult._(
encodedAssets: encodedAssets ?? [],
encodedAssetsForLinking: encodedAssetsForLinking ?? {},
dependencies: dependencies ?? [],
success: success,
);

factory HookResult.failure() => HookResult(success: false);

HookResult copyAdd(HookOutputImpl hookOutput, bool hookSuccess) {
final mergedMaps =
mergeMaps(encodedAssetsForLinking, hookOutput.encodedAssetsForLinking,
value: (encodedAssets1, encodedAssets2) => [
...encodedAssets1,
...encodedAssets2,
]);
HookResult copyAdd(HookOutput hookOutput) {
final mergedMaps = mergeMaps(
encodedAssetsForLinking,
hookOutput is BuildOutput
? hookOutput.encodedAssetsForLinking
: <String, List<EncodedAsset>>{},
value: (encodedAssets1, encodedAssets2) => [
...encodedAssets1,
...encodedAssets2,
]);
final hookOutputAssets = (hookOutput is BuildOutput)
? hookOutput.encodedAssets
: (hookOutput as LinkOutput).encodedAssets;
return HookResult(
encodedAssets: [
...encodedAssets,
...hookOutput.encodedAssets,
...hookOutputAssets,
],
encodedAssetsForLinking: mergedMaps,
dependencies: [
...dependencies,
...hookOutput.dependencies,
]..sort(_uriCompare),
success: success && hookSuccess,
);
}

HookResult withSuccess(bool success) => HookResult(
encodedAssets: encodedAssets,
encodedAssetsForLinking: encodedAssetsForLinking,
dependencies: dependencies,
success: success,
);
}

int _uriCompare(Uri u1, Uri u2) => u1.toString().compareTo(u2.toString());
7 changes: 0 additions & 7 deletions pkgs/native_assets_builder/lib/src/model/link_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import 'package:native_assets_cli/native_assets_cli_internal.dart';

import '../build_runner/build_runner.dart';

/// The result of executing the link hooks in dry run mode from all packages in
/// the dependency tree of the entry point application.
abstract interface class LinkResult {
Expand All @@ -15,9 +13,4 @@ abstract interface class LinkResult {

/// The files used by the hooks.
List<Uri> get dependencies;

/// Whether all hooks completed without errors.
///
/// All error messages are streamed to [NativeAssetsBuildRunner.logger].
bool get success;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ void main() async {
// Trigger a build, should invoke build for libraries with native assets.
{
final logMessages = <String>[];
final result = await build(
final result = (await build(
packageUri,
logger,
dartExecutable,
capturedLogs: logMessages,
supportedAssetTypes: [CodeAsset.type],
buildValidator: validateCodeAssetBuildOutput,
applicationAssetValidator: validateCodeAssetsInApplication,
);
))!;
expect(
logMessages.join('\n'),
stringContainsInOrder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void main() async {
applicationAssetValidator: validateCodeAssetsInApplication,
);
final fullLog = logMessages.join('\n');
expect(result.success, false);
expect(result, isNull);
expect(
fullLog,
contains('does not start with "package:wrong_namespace_asset/"'),
Expand Down Expand Up @@ -66,7 +66,7 @@ void main() async {
buildValidator: validateCodeAssetBuildOutput,
applicationAssetValidator: validateCodeAssetsInApplication,
);
expect(result.success, true);
expect(result, isNotNull);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ void main() async {
logger: logger,
);

final dryRunResult = await buildDryRun(
final dryRunResult = (await buildDryRun(
packageUri,
logger,
dartExecutable,
linkingEnabled: false,
supportedAssetTypes: [CodeAsset.type],
buildValidator: validateCodeAssetBuildOutput,
);
final buildResult = await build(
))!;
final buildResult = (await build(
packageUri,
logger,
dartExecutable,
supportedAssetTypes: [CodeAsset.type],
buildValidator: validateCodeAssetBuildOutput,
applicationAssetValidator: validateCodeAssetsInApplication,
);
))!;

expect(dryRunResult.encodedAssets.length, 1);
expect(buildResult.encodedAssets.length, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void main() async {
applicationAssetValidator: validateCodeAssetsInApplication,
);
final fullLog = logMessages.join('\n');
expect(result.success, false);
expect(result, isNull);
if (package == 'wrong_build_output_3') {
// Should re-execute the process on second run.
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ void main() async {

{
final logMessages = <String>[];
final result = await build(
final result = (await build(
packageUri,
logger,
dartExecutable,
capturedLogs: logMessages,
supportedAssetTypes: [CodeAsset.type],
buildValidator: validateCodeAssetBuildOutput,
applicationAssetValidator: validateCodeAssetsInApplication,
);
))!;
expect(
logMessages.join('\n'),
contains(
Expand All @@ -54,15 +54,15 @@ void main() async {

{
final logMessages = <String>[];
final result = await build(
final result = (await build(
packageUri,
logger,
dartExecutable,
capturedLogs: logMessages,
supportedAssetTypes: [CodeAsset.type],
buildValidator: validateCodeAssetBuildOutput,
applicationAssetValidator: validateCodeAssetsInApplication,
);
))!;
expect(
logMessages.join('\n'),
contains('Skipping build for native_add'),
Expand Down Expand Up @@ -99,14 +99,14 @@ void main() async {
await Future<void>.delayed(const Duration(seconds: 1));

{
final result = await build(
final result = (await build(
packageUri,
logger,
dartExecutable,
supportedAssetTypes: [CodeAsset.type],
buildValidator: validateCodeAssetBuildOutput,
applicationAssetValidator: validateCodeAssetsInApplication,
);
))!;
await expectSymbols(
asset: CodeAsset.fromEncoded(result.encodedAssets.single),
symbols: ['add']);
Expand All @@ -118,14 +118,14 @@ void main() async {
);

{
final result = await build(
final result = (await build(
packageUri,
logger,
dartExecutable,
supportedAssetTypes: [CodeAsset.type],
buildValidator: validateCodeAssetBuildOutput,
applicationAssetValidator: validateCodeAssetsInApplication,
);
))!;
await expectSymbols(
asset: CodeAsset.fromEncoded(result.encodedAssets.single),
symbols: ['add', 'subtract'],
Expand All @@ -152,14 +152,14 @@ void main() async {
// cached.
await Future<void>.delayed(const Duration(seconds: 1));

final result = await build(
final result = (await build(
packageUri,
logger,
dartExecutable,
supportedAssetTypes: [CodeAsset.type],
buildValidator: validateCodeAssetBuildOutput,
applicationAssetValidator: validateCodeAssetsInApplication,
);
))!;
{
final compiledHook = logMessages
.where((m) =>
Expand All @@ -179,14 +179,14 @@ void main() async {
targetUri: packageUri);

{
final result = await build(
final result = (await build(
packageUri,
logger,
dartExecutable,
supportedAssetTypes: [CodeAsset.type],
buildValidator: validateCodeAssetBuildOutput,
applicationAssetValidator: validateCodeAssetsInApplication,
);
))!;
{
final compiledHook = logMessages
.where((m) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() async {
buildValidator: (config, output) async => [],
);
final fullLog = logMessages.join('\n');
expect(result.success, false);
expect(result, isNull);
expect(
fullLog,
contains(
Expand All @@ -52,7 +52,7 @@ void main() async {
applicationAssetValidator: (_) async => [],
);
final fullLog = logMessages.join('\n');
expect(result.success, false);
expect(result, isNull);
expect(
fullLog,
contains(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ void main() async {
);

{
final result = await build(
final result = (await build(
packageUri,
logger,
dartExecutable,
supportedAssetTypes: [CodeAsset.type],
buildValidator: validateCodeAssetBuildOutput,
applicationAssetValidator: validateCodeAssetsInApplication,
);
))!;
expect(result.encodedAssets.length, 1);
await expectSymbols(
asset: CodeAsset.fromEncoded(result.encodedAssets.single),
Expand Down Expand Up @@ -60,7 +60,7 @@ void main() async {
applicationAssetValidator: validateCodeAssetsInApplication,
);
final fullLog = logMessages.join('\n');
expect(result.success, false);
expect(result, isNull);
expect(fullLog, contains('To reproduce run:'));
final reproCommand = fullLog
.split('\n')
Expand All @@ -78,14 +78,14 @@ void main() async {
);

{
final result = await build(
final result = (await build(
packageUri,
logger,
dartExecutable,
supportedAssetTypes: [CodeAsset.type],
buildValidator: validateCodeAssetBuildOutput,
applicationAssetValidator: validateCodeAssetsInApplication,
);
))!;
expect(result.encodedAssets.length, 1);
await expectSymbols(
asset: CodeAsset.fromEncoded(result.encodedAssets.single),
Expand Down
Loading
Loading