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

Rename NativeCodeAsset->CodeAsset and merge {CodeAsset,DataAsset} and {CodeAsset,DataAsset}Impl #1614

Merged
merged 3 commits into from
Oct 1, 2024
Merged
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
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
4 changes: 2 additions & 2 deletions pkgs/native_assets_builder/test/build_runner/link_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,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) =>
Iterable<String> _getNames(List<Asset> assets) =>
assets.whereType<cli.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
3 changes: 1 addition & 2 deletions pkgs/native_assets_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
- 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** Use unified classes instead of an Api and a Model class.
mkustermann marked this conversation as resolved.
Show resolved Hide resolved

## 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,7 @@
/// build hook (`hook/build.dart`).
library native_assets_cli;

export 'src/api/asset.dart'
show Asset, DataAsset, NativeCodeAsset, OSLibraryNaming;
export 'src/api/asset.dart' show Asset, CodeAsset, DataAsset, 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 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,7 +17,7 @@
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/asset.dart' show Asset, CodeAsset, DataAsset;
export 'src/api/build_config.dart' show BuildConfigImpl;
export 'src/api/build_output.dart' show HookOutputImpl;
export 'src/api/hook_config.dart' show HookConfigImpl;
Expand Down
45 changes: 37 additions & 8 deletions pkgs/native_assets_cli/lib/src/api/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@
// 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:pub_semver/pub_semver.dart';

import '../architecture.dart';
import '../json_utils.dart';
import '../link_mode.dart';
import '../os.dart';
import '../utils/json.dart';
import '../utils/map.dart';
import 'build_config.dart';
import 'build_output.dart';

part '../model/asset.dart';
part '../model/data_asset.dart';
part '../model/native_code_asset.dart';
part 'code_asset.dart';
part 'data_asset.dart';
part 'native_code_asset.dart';

/// Data or code bundled with a Dart or Flutter application.
///
Expand All @@ -34,7 +29,7 @@ abstract final class Asset {
/// different packages.
///
/// The default asset id for an asset reference from `lib/src/foo.dart` is
/// `'package:foo/src/foo.dart'`. For example a [NativeCodeAsset] can be accessed
/// `'package:foo/src/foo.dart'`. For example a [CodeAsset] can be accessed
/// via `@Native` with the `assetId` argument omitted:
///
/// ```dart
Expand Down Expand Up @@ -63,4 +58,38 @@ abstract final class Asset {
/// already present on the target system or an asset already present in Dart
/// or Flutter.
Uri? get file;

/// A json representation of this [Asset].
Map<String, Object> toJson();

static List<Asset> listFromJson(List<Object?>? list) {
final assets = <Asset>[];
if (list == null) return assets;
for (var i = 0; i < list.length; ++i) {
final jsonMap = list.getObject(i);
final type = jsonMap[_typeKey];
switch (type) {
case CodeAsset.type:
assets.add(CodeAsset.fromJson(jsonMap));
case DataAsset.type:
assets.add(DataAsset.fromJson(jsonMap));
default:
// Do nothing, some other launcher might define it's own asset types.
}
}
return assets;
}

static List<Map<String, Object>> listToJson(Iterable<Asset> assets) => [
for (final asset in assets) asset.toJson(),
];
}

const _architectureKey = 'architecture';
const _fileKey = 'file';
const _idKey = 'id';
const _linkModeKey = 'link_mode';
const _nameKey = 'name';
const _osKey = 'os';
const _packageKey = 'package';
const _typeKey = 'type';
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
Loading