Skip to content

Commit

Permalink
Rename NativeCodeAsset->CodeAsset and merge {CodeAsset,DataAsset} and…
Browse files Browse the repository at this point in the history
… {CodeAsset,DataAsset}Impl (#1614)

This is the last base refactoring. Building on this the next PRs will
gradually introduce more flexibility into the build system (starting
with adding custom asset types) and gradually separate components more
and more.
  • Loading branch information
mkustermann authored Oct 1, 2024
1 parent a8b20a1 commit d144f81
Show file tree
Hide file tree
Showing 58 changed files with 451 additions and 484 deletions.
2 changes: 1 addition & 1 deletion pkgs/native_assets_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
dart-lang/native repository to make it clear those are not intended to be used
by end-users.
- Remove link-dry-run concept as it's unused by Flutter Tools & Dart SDK
- Use unified classes instead of two `{OS,...}` and `{OS,,...}Impl`
- Bump `native_assets_cli` to `0.9.0`

## 0.8.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,17 +431,17 @@ class NativeAssetsBuildRunner {
packageLayout!,
),
);
buildOutput = _expandArchsNativeCodeAssets(buildOutput);
buildOutput = _expandArchsCodeAssets(buildOutput);
hookResult = hookResult.copyAdd(buildOutput, packageSuccess);
}
return hookResult;
}

HookOutputImpl _expandArchsNativeCodeAssets(HookOutputImpl buildOutput) {
final assets = <AssetImpl>[];
HookOutputImpl _expandArchsCodeAssets(HookOutputImpl buildOutput) {
final assets = <Asset>[];
for (final asset in buildOutput.assets) {
switch (asset) {
case NativeCodeAssetImpl _:
case CodeAsset _:
if (asset.architecture != null) {
// Backwards compatibility, if an architecture is provided use it.
assets.add(asset);
Expand All @@ -453,7 +453,7 @@ class NativeAssetsBuildRunner {
assets.add(asset.copyWith(architecture: architecture));
}
}
case DataAssetImpl _:
case DataAsset _:
assets.add(asset);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import '../../native_assets_builder.dart';
/// the dependency tree of the entry point application.
abstract interface class BuildDryRunResult {
/// The native assets produced by the hooks, which should be bundled.
List<AssetImpl> get assets;
List<Asset> get assets;

/// 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<AssetImpl>> get assetsForLinking;
Map<String, List<Asset>> get assetsForLinking;
}
4 changes: 2 additions & 2 deletions pkgs/native_assets_builder/lib/src/model/build_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ abstract class BuildResult {
bool get success;

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

/// The native assets produced by the hooks, which should be linked.
Map<String, List<AssetImpl>> get assetsForLinking;
Map<String, List<Asset>> get assetsForLinking;
}
8 changes: 4 additions & 4 deletions pkgs/native_assets_builder/lib/src/model/hook_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import '../../native_assets_builder.dart';
final class HookResult implements BuildResult, BuildDryRunResult, LinkResult {
/// The native assets produced by the hooks, which should be bundled.
@override
final List<AssetImpl> assets;
final List<Asset> assets;

/// The assets produced by the hooks, which should be linked.
@override
final Map<String, List<AssetImpl>> assetsForLinking;
final Map<String, List<Asset>> assetsForLinking;

/// The files used by the hooks.
@override
Expand All @@ -36,8 +36,8 @@ final class HookResult implements BuildResult, BuildDryRunResult, LinkResult {
});

factory HookResult({
List<AssetImpl>? assets,
Map<String, List<AssetImpl>>? assetsForLinking,
List<Asset>? assets,
Map<String, List<Asset>>? assetsForLinking,
List<Uri>? dependencies,
bool success = true,
}) =>
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_builder/lib/src/model/link_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import '../build_runner/build_runner.dart';
/// the dependency tree of the entry point application.
abstract interface class LinkResult {
/// The native assets produced by the hooks, which should be bundled.
List<AssetImpl> get assets;
List<Asset> get assets;

/// The files used by the hooks.
List<Uri> get dependencies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ void main() async {
final dryRunAsset = dryRunAssets[i];
final buildAsset = result.assets[0];
expect(dryRunAsset.id, buildAsset.id);
// The build runner expands NativeCodeAssets to all architectures.
// The build runner expands CodeAssets to all architectures.
expect(buildAsset.file, isNotNull);
if (dryRunAsset is NativeCodeAssetImpl &&
buildAsset is NativeCodeAssetImpl) {
if (dryRunAsset is CodeAsset && buildAsset is CodeAsset) {
expect(dryRunAsset.architecture, isNotNull);
expect(buildAsset.architecture, isNotNull);
expect(dryRunAsset.os, buildAsset.os);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ void main() async {
{
final result = await build(packageUri, logger, dartExecutable);
await expectSymbols(
asset: result.assets.single as NativeCodeAssetImpl,
symbols: ['add']);
asset: result.assets.single as CodeAsset, symbols: ['add']);
}

await copyTestProjects(
Expand All @@ -100,7 +99,7 @@ void main() async {
{
final result = await build(packageUri, logger, dartExecutable);
await expectSymbols(
asset: result.assets.single as NativeCodeAssetImpl,
asset: result.assets.single as CodeAsset,
symbols: ['add', 'subtract'],
);
}
Expand Down Expand Up @@ -136,7 +135,7 @@ void main() async {
}
logMessages.clear();
await expectSymbols(
asset: result.assets.single as NativeCodeAssetImpl,
asset: result.assets.single as CodeAsset,
symbols: ['add'],
);

Expand All @@ -156,7 +155,7 @@ void main() async {
}
logMessages.clear();
await expectSymbols(
asset: result.assets.single as NativeCodeAssetImpl,
asset: result.assets.single as CodeAsset,
symbols: ['add', 'multiply'],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ void main() async {
final result = await build(packageUri, logger, dartExecutable);
expect(result.assets.length, 1);
await expectSymbols(
asset: result.assets.single as NativeCodeAssetImpl,
symbols: ['add']);
asset: result.assets.single as CodeAsset, symbols: ['add']);
expect(
result.dependencies,
[
Expand Down Expand Up @@ -72,8 +71,7 @@ void main() async {
final result = await build(packageUri, logger, dartExecutable);
expect(result.assets.length, 1);
await expectSymbols(
asset: result.assets.single as NativeCodeAssetImpl,
symbols: ['add']);
asset: result.assets.single as CodeAsset, symbols: ['add']);
expect(
result.dependencies,
[
Expand Down
4 changes: 2 additions & 2 deletions pkgs/native_assets_builder/test/build_runner/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,14 @@ Future<BuildDryRunResult> buildDryRun(
return result;
});

Future<void> expectAssetsExist(List<AssetImpl> assets) async {
Future<void> expectAssetsExist(List<Asset> assets) async {
for (final asset in assets) {
expect(File.fromUri(asset.file!), exists);
}
}

Future<void> expectSymbols({
required NativeCodeAssetImpl asset,
required CodeAsset asset,
required List<String> symbols,
}) async {
if (Platform.isLinux) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'dart:io';

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

import '../helpers.dart';
Expand Down
9 changes: 4 additions & 5 deletions pkgs/native_assets_builder/test/build_runner/link_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

import 'dart:io';

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

import '../helpers.dart';
Expand Down Expand Up @@ -198,11 +197,11 @@ void main() async {
capturedLogs: logMessages,
);
expect(linkResult.assets.length, 1);
expect(linkResult.assets.first, isA<NativeCodeAsset>());
expect(linkResult.assets.first, isA<CodeAsset>());
});
},
);
}

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

// This package honors preferences.
expect(
(resultDynamic.assets.single as NativeCodeAssetImpl).linkMode,
(resultDynamic.assets.single as CodeAsset).linkMode,
DynamicLoadingBundled(),
);
expect(
(resultPreferDynamic.assets.single as NativeCodeAssetImpl).linkMode,
(resultPreferDynamic.assets.single as CodeAsset).linkMode,
DynamicLoadingBundled(),
);
expect(
(resultStatic.assets.single as NativeCodeAssetImpl).linkMode,
(resultStatic.assets.single as CodeAsset).linkMode,
StaticLinking(),
);
expect(
(resultPreferStatic.assets.single as NativeCodeAssetImpl).linkMode,
(resultPreferStatic.assets.single as CodeAsset).linkMode,
StaticLinking(),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import 'package:native_assets_cli/native_assets_cli.dart';

void main(List<String> arguments) async {
await link(arguments, (config, output) async {
final builtDylib = config.assets.first as NativeCodeAsset;
final builtDylib = config.assets.first as CodeAsset;
output
..addAsset(
NativeCodeAsset(
CodeAsset(
package: 'add_asset_link',
name: 'dylib_add_link',
linkMode: builtDylib.linkMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main(List<String> arguments) async {
}

output.addAsset(
NativeCodeAsset(
CodeAsset(
package: config.packageName,
name: 'foo',
file: assetUri,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main(List<String> arguments) async {
}

output.addAsset(
NativeCodeAsset(
CodeAsset(
package: 'other_package',
name: 'foo',
file: assetUri,
Expand Down
5 changes: 3 additions & 2 deletions pkgs/native_assets_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
- No longer try to resolve uris encoded in `config.json` against any base uri.
The `hook/{build,link}.dart` invoker has to ensure the uris it encodes can be
opened as-is (i.e. without resolving against any base uri)
- **Breaking change** Use unified classes instead of two `{OS,...}` and
move methods (e.g. from `OS`) to extensions (e.g. `OSLibraryNaming`)
- **Breaking change** Moved some methods to be extension methods.
- Some classes in the `BuildConfig` and `BuildOutput` now expose `fromJson` and
`toJson`.

## 0.8.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Future<void> main(List<String> args) async {

output.addAsset(
// TODO: Change to DataAsset once the Dart/Flutter SDK can consume it.
NativeCodeAsset(
CodeAsset(
package: packageName,
name: 'asset.txt',
file: assetPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ void main() {
mainMethod: build.main,
check: (_, output) {
expect(output.assets, isNotEmpty);
expect(output.assets.first, isA<NativeCodeAsset>());
expect(output.assets.first, isA<CodeAsset>());
expect(
(output.assets.first as NativeCodeAsset).id,
(output.assets.first as CodeAsset).id,
'package:local_asset/asset.txt',
);
},
Expand Down
3 changes: 1 addition & 2 deletions pkgs/native_assets_cli/lib/native_assets_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
/// build hook (`hook/build.dart`).
library native_assets_cli;

export 'src/api/asset.dart'
show Asset, DataAsset, NativeCodeAsset, OSLibraryNaming;
export 'src/api/build.dart' show build;
export 'src/api/build_config.dart' show BuildConfig;
export 'src/api/build_output.dart' show BuildOutput, LinkOutput;
Expand All @@ -17,6 +15,7 @@ export 'src/api/link.dart' show link;
export 'src/api/link_config.dart' show LinkConfig;
export 'src/api/linker.dart' show Linker;
export 'src/architecture.dart' show Architecture;
export 'src/asset.dart' show Asset, CodeAsset, DataAsset, OSLibraryNaming;
export 'src/build_mode.dart' show BuildMode;
export 'src/c_compiler_config.dart' show CCompilerConfig;
export 'src/ios_sdk.dart' show IOSSdk;
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_cli/lib/native_assets_cli_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
library native_assets_cli_internal;

export 'native_assets_cli.dart' hide build, link;
export 'src/api/asset.dart' show AssetImpl, DataAssetImpl, NativeCodeAssetImpl;
export 'src/api/build_config.dart' show BuildConfigImpl;
export 'src/api/build_output.dart' show HookOutputImpl;
export 'src/api/hook_config.dart' show HookConfigImpl;
export 'src/api/link_config.dart' show LinkConfigImpl;
export 'src/asset.dart' show Asset, CodeAsset, DataAsset;
export 'src/model/dependencies.dart';
export 'src/model/hook.dart';
export 'src/model/metadata.dart';
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_cli/lib/src/api/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import 'build_output.dart';
///
/// output.addAsset(
/// // TODO: Change to DataAsset once the Dart/Flutter SDK can consume it.
/// NativeCodeAsset(
/// CodeAsset(
/// package: packageName,
/// name: 'asset.txt',
/// file: assetPath,
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_assets_cli/lib/src/api/build_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:pub_semver/pub_semver.dart';

import '../architecture.dart';
import '../args_parser.dart';
import '../asset.dart';
import '../build_mode.dart';
import '../c_compiler_config.dart';
import '../ios_sdk.dart';
Expand All @@ -20,7 +21,6 @@ import '../model/metadata.dart';
import '../os.dart';
import '../utils/json.dart';
import '../utils/map.dart';
import 'asset.dart';
import 'build.dart';
import 'build_output.dart';
import 'deprecation_messages.dart';
Expand Down
6 changes: 3 additions & 3 deletions pkgs/native_assets_cli/lib/src/api/build_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import 'package:collection/collection.dart';
import 'package:pub_semver/pub_semver.dart';

import '../architecture.dart';
import '../asset.dart';
import '../model/dependencies.dart';
import '../model/metadata.dart';
import '../os.dart';
import '../utils/datetime.dart';
import '../utils/file.dart';
import '../utils/json.dart';
import '../utils/map.dart';
import 'asset.dart';
import 'build.dart';
import 'build_config.dart';
import 'builder.dart';
Expand Down Expand Up @@ -85,7 +85,7 @@ abstract final class BuildOutput {
///
/// The [Asset]s produced by this build or dry-run can be provided to the
/// constructor as [assets], or can be added later using [addAsset] and
/// [addAssets]. In dry runs, the [Architecture] for [NativeCodeAsset]s can be
/// [addAssets]. In dry runs, the [Architecture] for [CodeAsset]s can be
/// omitted.
///
/// The files used by this build must be provided to the constructor as
Expand All @@ -106,7 +106,7 @@ abstract final class BuildOutput {
}) =>
HookOutputImpl(
timestamp: timestamp,
assets: assets?.cast<AssetImpl>().toList(),
assets: assets?.cast<Asset>().toList(),
dependencies: Dependencies([...?dependencies]),
metadata: Metadata({...?metadata}),
);
Expand Down
Loading

0 comments on commit d144f81

Please sign in to comment.