Roll native_assets_builder to 0.5.0 (#143472)

Roll of https://github.com/dart-lang/native/pull/964, which separates the `KernelAsset`s (the asset information embedded in the Dart kernel snapshot) from `Asset`s (the assets in the `build.dart` protocol). See the linked issue for why they ought to be different instead of shared.

This PR does not change any functionality in Flutter.

(Now that https://github.com/flutter/flutter/pull/143055 has landed, we can land breaking changes.)

For reference, the same roll in the Dart SDK: https://dart-review.googlesource.com/c/sdk/+/352642
This commit is contained in:
Daco Harkes 2024-02-14 21:23:23 +01:00 committed by GitHub
parent 5b005e4791
commit db83bc6e59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 184 additions and 107 deletions

View file

@ -3,7 +3,7 @@
// found in the LICENSE file.
import 'package:meta/meta.dart';
import 'package:native_assets_cli/native_assets_cli_internal.dart' show Asset;
import 'package:native_assets_builder/native_assets_builder.dart' hide NativeAssetsBuildRunner;
import 'package:package_config/package_config_types.dart';
import '../../android/gradle_utils.dart';
@ -56,7 +56,7 @@ class NativeAssets extends Target {
final File nativeAssetsFile = environment.buildDir.childFile('native_assets.yaml');
if (nativeAssetsEnvironment == 'false') {
dependencies = <Uri>[];
await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem);
await writeNativeAssetsYaml(KernelAssets(), environment.buildDir.uri, fileSystem);
} else {
final String? targetPlatformEnvironment = environment.defines[kTargetPlatform];
if (targetPlatformEnvironment == null) {
@ -145,7 +145,7 @@ class NativeAssets extends Target {
} else {
// TODO(dacoharkes): Implement other OSes. https://github.com/flutter/flutter/issues/129757
// Write the file we claim to have in the [outputs].
await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem);
await writeNativeAssetsYaml(KernelAssets(), environment.buildDir.uri, fileSystem);
dependencies = <Uri>[];
}
case TargetPlatform.android_arm:
@ -165,7 +165,7 @@ class NativeAssets extends Target {
case TargetPlatform.web_javascript:
// TODO(dacoharkes): Implement other OSes. https://github.com/flutter/flutter/issues/129757
// Write the file we claim to have in the [outputs].
await writeNativeAssetsYaml(<Asset>[], environment.buildDir.uri, fileSystem);
await writeNativeAssetsYaml(KernelAssets(), environment.buildDir.uri, fileSystem);
dependencies = <Uri>[];
}
}

View file

@ -3,7 +3,7 @@
// found in the LICENSE file.
import 'package:native_assets_builder/native_assets_builder.dart'
show BuildResult, DryRunResult;
hide NativeAssetsBuildRunner;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
as native_assets_cli;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
@ -31,21 +31,21 @@ Future<Uri?> dryRunNativeAssetsAndroid({
}
final Uri buildUri_ = nativeAssetsBuildUri(projectUri, OS.android);
final Iterable<Asset> nativeAssetPaths =
final Iterable<KernelAsset> nativeAssetPaths =
await dryRunNativeAssetsAndroidInternal(
fileSystem,
projectUri,
buildRunner,
);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
nativeAssetPaths,
KernelAssets(nativeAssetPaths),
buildUri_,
fileSystem,
);
return nativeAssetsUri;
}
Future<Iterable<Asset>> dryRunNativeAssetsAndroidInternal(
Future<Iterable<KernelAsset>> dryRunNativeAssetsAndroidInternal(
FileSystem fileSystem,
Uri projectUri,
NativeAssetsBuildRunner buildRunner,
@ -63,10 +63,9 @@ Future<Iterable<Asset>> dryRunNativeAssetsAndroidInternal(
final List<Asset> nativeAssets = dryRunResult.assets;
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Dry running native assets for $targetOS done.');
final Map<Asset, Asset> assetTargetLocations =
final Map<Asset, KernelAsset> assetTargetLocations =
_assetTargetLocations(nativeAssets);
final Iterable<Asset> nativeAssetPaths = assetTargetLocations.values;
return nativeAssetPaths;
return assetTargetLocations.values;
}
/// Builds native assets.
@ -85,7 +84,7 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)>
final Uri buildUri_ = nativeAssetsBuildUri(projectUri, targetOS);
if (!await nativeBuildRequired(buildRunner)) {
final Uri nativeAssetsYaml = await writeNativeAssetsYaml(
<Asset>[],
KernelAssets(),
yamlParentDirectory ?? buildUri_,
fileSystem,
);
@ -116,11 +115,11 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)>
}
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Building native assets for $targets done.');
final Map<Asset, Asset> assetTargetLocations =
final Map<Asset, KernelAsset> assetTargetLocations =
_assetTargetLocations(nativeAssets);
await _copyNativeAssetsAndroid(buildUri_, assetTargetLocations, fileSystem);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
assetTargetLocations.values,
KernelAssets(assetTargetLocations.values),
yamlParentDirectory ?? buildUri_,
fileSystem);
return (nativeAssetsUri, dependencies.toList());
@ -128,7 +127,7 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)>
Future<void> _copyNativeAssetsAndroid(
Uri buildUri,
Map<Asset, Asset> assetTargetLocations,
Map<Asset, KernelAsset> assetTargetLocations,
FileSystem fileSystem,
) async {
if (assetTargetLocations.isNotEmpty) {
@ -142,10 +141,10 @@ Future<void> _copyNativeAssetsAndroid(
final Uri archUri = buildUri.resolve('jniLibs/lib/$jniArchDir/');
await fileSystem.directory(archUri).create(recursive: true);
}
for (final MapEntry<Asset, Asset> assetMapping
for (final MapEntry<Asset, KernelAsset> assetMapping
in assetTargetLocations.entries) {
final Uri source = (assetMapping.key.path as AssetAbsolutePath).uri;
final Uri target = (assetMapping.value.path as AssetAbsolutePath).uri;
final Uri target = (assetMapping.value.path as KernelAssetAbsolutePath).uri;
final AndroidArch androidArch =
_getAndroidArch(assetMapping.value.target);
final String jniArchDir = androidArch.archName;
@ -190,8 +189,8 @@ AndroidArch _getAndroidArch(Target target) {
}
}
Map<Asset, Asset> _assetTargetLocations(List<Asset> nativeAssets) {
return <Asset, Asset>{
Map<Asset, KernelAsset> _assetTargetLocations(List<Asset> nativeAssets) {
return <Asset, KernelAsset>{
for (final Asset asset in nativeAssets)
asset: _targetLocationAndroid(asset),
};
@ -199,19 +198,28 @@ Map<Asset, Asset> _assetTargetLocations(List<Asset> nativeAssets) {
/// Converts the `path` of [asset] as output from a `build.dart` invocation to
/// the path used inside the Flutter app bundle.
Asset _targetLocationAndroid(Asset asset) {
KernelAsset _targetLocationAndroid(Asset asset) {
final AssetPath path = asset.path;
final KernelAssetPath kernelAssetPath;
switch (path) {
case AssetSystemPath _:
kernelAssetPath = KernelAssetSystemPath(path.uri);
case AssetInExecutable _:
kernelAssetPath = KernelAssetInExecutable();
case AssetInProcess _:
return asset;
kernelAssetPath = KernelAssetInProcess();
case AssetAbsolutePath _:
final String fileName = path.uri.pathSegments.last;
return asset.copyWith(path: AssetAbsolutePath(Uri(path: fileName)));
kernelAssetPath = KernelAssetAbsolutePath(Uri(path: fileName));
default:
throw Exception(
'Unsupported asset path type ${path.runtimeType} in asset $asset',
);
}
throw Exception(
'Unsupported asset path type ${path.runtimeType} in asset $asset',
return KernelAsset(
id: asset.id,
target: asset.target,
path: kernelAssetPath,
);
}

View file

@ -3,7 +3,7 @@
// found in the LICENSE file.
import 'package:native_assets_builder/native_assets_builder.dart'
show BuildResult, DryRunResult;
hide NativeAssetsBuildRunner;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
hide BuildMode;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
@ -31,20 +31,20 @@ Future<Uri?> dryRunNativeAssetsIOS({
}
final Uri buildUri = nativeAssetsBuildUri(projectUri, OS.iOS);
final Iterable<Asset> assetTargetLocations = await dryRunNativeAssetsIOSInternal(
final Iterable<KernelAsset> assetTargetLocations = await dryRunNativeAssetsIOSInternal(
fileSystem,
projectUri,
buildRunner,
);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
assetTargetLocations,
KernelAssets(assetTargetLocations),
buildUri,
fileSystem,
);
return nativeAssetsUri;
}
Future<Iterable<Asset>> dryRunNativeAssetsIOSInternal(
Future<Iterable<KernelAsset>> dryRunNativeAssetsIOSInternal(
FileSystem fileSystem,
Uri projectUri,
NativeAssetsBuildRunner buildRunner,
@ -61,8 +61,7 @@ Future<Iterable<Asset>> dryRunNativeAssetsIOSInternal(
final List<Asset> nativeAssets = dryRunResult.assets;
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Dry running native assets for $targetOS done.');
final Iterable<Asset> assetTargetLocations = _assetTargetLocations(nativeAssets).values;
return assetTargetLocations;
return _assetTargetLocations(nativeAssets).values;
}
/// Builds native assets.
@ -77,7 +76,7 @@ Future<List<Uri>> buildNativeAssetsIOS({
required FileSystem fileSystem,
}) async {
if (!await nativeBuildRequired(buildRunner)) {
await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory, fileSystem);
await writeNativeAssetsYaml(KernelAssets(), yamlParentDirectory, fileSystem);
return <Uri>[];
}
@ -107,7 +106,7 @@ Future<List<Uri>> buildNativeAssetsIOS({
}
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Building native assets for $targets done.');
final Map<AssetPath, List<Asset>> fatAssetTargetLocations = _fatAssetTargetLocations(nativeAssets);
final Map<KernelAssetPath, List<Asset>> fatAssetTargetLocations = _fatAssetTargetLocations(nativeAssets);
await _copyNativeAssetsIOS(
buildUri,
fatAssetTargetLocations,
@ -116,9 +115,9 @@ Future<List<Uri>> buildNativeAssetsIOS({
fileSystem,
);
final Map<Asset, Asset> assetTargetLocations = _assetTargetLocations(nativeAssets);
final Map<Asset, KernelAsset> assetTargetLocations = _assetTargetLocations(nativeAssets);
await writeNativeAssetsYaml(
assetTargetLocations.values,
KernelAssets(assetTargetLocations.values),
yamlParentDirectory,
fileSystem,
);
@ -146,40 +145,51 @@ Target _getNativeTarget(DarwinArch darwinArch) {
}
}
Map<AssetPath, List<Asset>> _fatAssetTargetLocations(List<Asset> nativeAssets) {
Map<KernelAssetPath, List<Asset>> _fatAssetTargetLocations(List<Asset> nativeAssets) {
final Set<String> alreadyTakenNames = <String>{};
final Map<AssetPath, List<Asset>> result = <AssetPath, List<Asset>>{};
final Map<KernelAssetPath, List<Asset>> result = <KernelAssetPath, List<Asset>>{};
for (final Asset asset in nativeAssets) {
final AssetPath path = _targetLocationIOS(asset, alreadyTakenNames).path;
final KernelAssetPath path = _targetLocationIOS(asset, alreadyTakenNames).path;
result[path] ??= <Asset>[];
result[path]!.add(asset);
}
return result;
}
Map<Asset, Asset> _assetTargetLocations(List<Asset> nativeAssets) {
Map<Asset, KernelAsset> _assetTargetLocations(List<Asset> nativeAssets) {
final Set<String> alreadyTakenNames = <String>{};
return <Asset, Asset>{
return <Asset, KernelAsset>{
for (final Asset asset in nativeAssets)
asset: _targetLocationIOS(asset, alreadyTakenNames),
};
}
Asset _targetLocationIOS(Asset asset, Set<String> alreadyTakenNames) {
KernelAsset _targetLocationIOS(Asset asset, Set<String> alreadyTakenNames) {
final AssetPath path = asset.path;
final KernelAssetPath kernelAssetPath;
switch (path) {
case AssetSystemPath _:
kernelAssetPath = KernelAssetSystemPath(path.uri);
case AssetInExecutable _:
kernelAssetPath = KernelAssetInExecutable();
case AssetInProcess _:
return asset;
kernelAssetPath = KernelAssetInProcess();
case AssetAbsolutePath _:
final String fileName = path.uri.pathSegments.last;
return asset.copyWith(
path: AssetAbsolutePath(frameworkUri(fileName, alreadyTakenNames)),
kernelAssetPath = KernelAssetAbsolutePath(frameworkUri(
fileName,
alreadyTakenNames,
));
default:
throw Exception(
'Unsupported asset path type ${path.runtimeType} in asset $asset',
);
}
throw Exception(
'Unsupported asset path type ${path.runtimeType} in asset $asset');
return KernelAsset(
id: asset.id,
target: asset.target,
path: kernelAssetPath,
);
}
/// Copies native assets into a framework per dynamic library.
@ -194,7 +204,7 @@ Asset _targetLocationIOS(Asset asset, Set<String> alreadyTakenNames) {
/// in xcode_backend.dart.
Future<void> _copyNativeAssetsIOS(
Uri buildUri,
Map<AssetPath, List<Asset>> assetTargetLocations,
Map<KernelAssetPath, List<Asset>> assetTargetLocations,
String? codesignIdentity,
BuildMode buildMode,
FileSystem fileSystem,
@ -202,9 +212,9 @@ Future<void> _copyNativeAssetsIOS(
if (assetTargetLocations.isNotEmpty) {
globals.logger
.printTrace('Copying native assets to ${buildUri.toFilePath()}.');
for (final MapEntry<AssetPath, List<Asset>> assetMapping
for (final MapEntry<KernelAssetPath, List<Asset>> assetMapping
in assetTargetLocations.entries) {
final Uri target = (assetMapping.key as AssetAbsolutePath).uri;
final Uri target = (assetMapping.key as KernelAssetAbsolutePath).uri;
final List<Uri> sources = <Uri>[
for (final Asset source in assetMapping.value)
(source.path as AssetAbsolutePath).uri

View file

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:native_assets_builder/native_assets_builder.dart'
hide NativeAssetsBuildRunner;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
hide BuildMode;
@ -31,7 +33,7 @@ Future<Uri?> dryRunNativeAssetsLinux({
);
}
Future<Iterable<Asset>> dryRunNativeAssetsLinuxInternal(
Future<Iterable<KernelAsset>> dryRunNativeAssetsLinuxInternal(
FileSystem fileSystem,
Uri projectUri,
bool flutterTester,

View file

@ -3,7 +3,7 @@
// found in the LICENSE file.
import 'package:native_assets_builder/native_assets_builder.dart'
show BuildResult, DryRunResult;
hide NativeAssetsBuildRunner;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
hide BuildMode;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
@ -31,12 +31,21 @@ Future<Uri?> dryRunNativeAssetsMacOS({
}
final Uri buildUri = nativeAssetsBuildUri(projectUri, OS.macOS);
final Iterable<Asset> nativeAssetPaths = await dryRunNativeAssetsMacOSInternal(fileSystem, projectUri, flutterTester, buildRunner);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(nativeAssetPaths, buildUri, fileSystem);
final Iterable<KernelAsset> nativeAssetPaths = await dryRunNativeAssetsMacOSInternal(
fileSystem,
projectUri,
flutterTester,
buildRunner,
);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
KernelAssets(nativeAssetPaths),
buildUri,
fileSystem,
);
return nativeAssetsUri;
}
Future<Iterable<Asset>> dryRunNativeAssetsMacOSInternal(
Future<Iterable<KernelAsset>> dryRunNativeAssetsMacOSInternal(
FileSystem fileSystem,
Uri projectUri,
bool flutterTester,
@ -57,9 +66,11 @@ Future<Iterable<Asset>> dryRunNativeAssetsMacOSInternal(
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Dry running native assets for $targetOS done.');
final Uri? absolutePath = flutterTester ? buildUri : null;
final Map<Asset, Asset> assetTargetLocations = _assetTargetLocations(nativeAssets, absolutePath);
final Iterable<Asset> nativeAssetPaths = assetTargetLocations.values;
return nativeAssetPaths;
final Map<Asset, KernelAsset> assetTargetLocations = _assetTargetLocations(
nativeAssets,
absolutePath,
);
return assetTargetLocations.values;
}
/// Builds native assets.
@ -82,14 +93,22 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsMacOS({
const OS targetOS = OS.macOS;
final Uri buildUri = nativeAssetsBuildUri(projectUri, targetOS);
if (!await nativeBuildRequired(buildRunner)) {
final Uri nativeAssetsYaml = await writeNativeAssetsYaml(<Asset>[], yamlParentDirectory ?? buildUri, fileSystem);
final Uri nativeAssetsYaml = await writeNativeAssetsYaml(
KernelAssets(),
yamlParentDirectory ?? buildUri,
fileSystem,
);
return (nativeAssetsYaml, <Uri>[]);
}
final List<Target> targets = darwinArchs != null ? darwinArchs.map(_getNativeTarget).toList() : <Target>[Target.current];
final native_assets_cli.BuildMode buildModeCli = nativeAssetsBuildMode(buildMode);
final List<Target> targets = darwinArchs != null
? darwinArchs.map(_getNativeTarget).toList()
: <Target>[Target.current];
final native_assets_cli.BuildMode buildModeCli =
nativeAssetsBuildMode(buildMode);
globals.logger.printTrace('Building native assets for $targets $buildModeCli.');
globals.logger
.printTrace('Building native assets for $targets $buildModeCli.');
final List<Asset> nativeAssets = <Asset>[];
final Set<Uri> dependencies = <Uri>{};
for (final Target target in targets) {
@ -108,8 +127,10 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsMacOS({
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Building native assets for $targets done.');
final Uri? absolutePath = flutterTester ? buildUri : null;
final Map<Asset, Asset> assetTargetLocations = _assetTargetLocations(nativeAssets, absolutePath);
final Map<AssetPath, List<Asset>> fatAssetTargetLocations = _fatAssetTargetLocations(nativeAssets, absolutePath);
final Map<Asset, KernelAsset> assetTargetLocations =
_assetTargetLocations(nativeAssets, absolutePath);
final Map<KernelAssetPath, List<Asset>> fatAssetTargetLocations =
_fatAssetTargetLocations(nativeAssets, absolutePath);
if (flutterTester) {
await _copyNativeAssetsMacOSFlutterTester(
buildUri,
@ -127,7 +148,11 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsMacOS({
fileSystem,
);
}
final Uri nativeAssetsUri = await writeNativeAssetsYaml(assetTargetLocations.values, yamlParentDirectory ?? buildUri, fileSystem);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
KernelAssets(assetTargetLocations.values),
yamlParentDirectory ?? buildUri,
fileSystem,
);
return (nativeAssetsUri, dependencies.toList());
}
@ -143,14 +168,15 @@ Target _getNativeTarget(DarwinArch darwinArch) {
}
}
Map<AssetPath, List<Asset>> _fatAssetTargetLocations(
Map<KernelAssetPath, List<Asset>> _fatAssetTargetLocations(
List<Asset> nativeAssets,
Uri? absolutePath,
) {
final Set<String> alreadyTakenNames = <String>{};
final Map<AssetPath, List<Asset>> result = <AssetPath, List<Asset>>{};
final Map<KernelAssetPath, List<Asset>> result =
<KernelAssetPath, List<Asset>>{};
for (final Asset asset in nativeAssets) {
final AssetPath path = _targetLocationMacOS(
final KernelAssetPath path = _targetLocationMacOS(
asset,
absolutePath,
alreadyTakenNames,
@ -161,28 +187,31 @@ Map<AssetPath, List<Asset>> _fatAssetTargetLocations(
return result;
}
Map<Asset, Asset> _assetTargetLocations(
Map<Asset, KernelAsset> _assetTargetLocations(
List<Asset> nativeAssets,
Uri? absolutePath,
) {
final Set<String> alreadyTakenNames = <String>{};
return <Asset, Asset>{
return <Asset, KernelAsset>{
for (final Asset asset in nativeAssets)
asset: _targetLocationMacOS(asset, absolutePath, alreadyTakenNames),
};
}
Asset _targetLocationMacOS(
KernelAsset _targetLocationMacOS(
Asset asset,
Uri? absolutePath,
Set<String> alreadyTakenNames,
) {
final AssetPath path = asset.path;
final KernelAssetPath kernelAssetPath;
switch (path) {
case AssetSystemPath _:
kernelAssetPath = KernelAssetSystemPath(path.uri);
case AssetInExecutable _:
kernelAssetPath = KernelAssetInExecutable();
case AssetInProcess _:
return asset;
kernelAssetPath = KernelAssetInProcess();
case AssetAbsolutePath _:
final String fileName = path.uri.pathSegments.last;
Uri uri;
@ -194,11 +223,18 @@ Asset _targetLocationMacOS(
// "relative" in the context of native assets would be relative to the
// kernel or aot snapshot.
uri = frameworkUri(fileName, alreadyTakenNames);
}
return asset.copyWith(path: AssetAbsolutePath(uri));
kernelAssetPath = KernelAssetAbsolutePath(uri);
default:
throw Exception(
'Unsupported asset path type ${path.runtimeType} in asset $asset',
);
}
throw Exception('Unsupported asset path type ${path.runtimeType} in asset $asset');
return KernelAsset(
id: asset.id,
target: asset.target,
path: kernelAssetPath,
);
}
/// Copies native assets into a framework per dynamic library.
@ -216,15 +252,18 @@ Asset _targetLocationMacOS(
/// in macos_assemble.sh.
Future<void> _copyNativeAssetsMacOS(
Uri buildUri,
Map<AssetPath, List<Asset>> assetTargetLocations,
Map<KernelAssetPath, List<Asset>> assetTargetLocations,
String? codesignIdentity,
BuildMode buildMode,
FileSystem fileSystem,
) async {
if (assetTargetLocations.isNotEmpty) {
globals.logger.printTrace('Copying native assets to ${buildUri.toFilePath()}.');
for (final MapEntry<AssetPath, List<Asset>> assetMapping in assetTargetLocations.entries) {
final Uri target = (assetMapping.key as AssetAbsolutePath).uri;
globals.logger.printTrace(
'Copying native assets to ${buildUri.toFilePath()}.',
);
for (final MapEntry<KernelAssetPath, List<Asset>> assetMapping
in assetTargetLocations.entries) {
final Uri target = (assetMapping.key as KernelAssetAbsolutePath).uri;
final List<Uri> sources = <Uri>[
for (final Asset source in assetMapping.value)
(source.path as AssetAbsolutePath).uri,
@ -273,7 +312,6 @@ Future<void> _copyNativeAssetsMacOS(
}
}
/// Copies native assets for flutter tester.
///
/// For `flutter run -release` a multi-architecture solution is needed. So,
@ -284,15 +322,18 @@ Future<void> _copyNativeAssetsMacOS(
/// Code signing is also done here.
Future<void> _copyNativeAssetsMacOSFlutterTester(
Uri buildUri,
Map<AssetPath, List<Asset>> assetTargetLocations,
Map<KernelAssetPath, List<Asset>> assetTargetLocations,
String? codesignIdentity,
BuildMode buildMode,
FileSystem fileSystem,
) async {
if (assetTargetLocations.isNotEmpty) {
globals.logger.printTrace('Copying native assets to ${buildUri.toFilePath()}.');
for (final MapEntry<AssetPath, List<Asset>> assetMapping in assetTargetLocations.entries) {
final Uri target = (assetMapping.key as AssetAbsolutePath).uri;
globals.logger.printTrace(
'Copying native assets to ${buildUri.toFilePath()}.',
);
for (final MapEntry<KernelAssetPath, List<Asset>> assetMapping
in assetTargetLocations.entries) {
final Uri target = (assetMapping.key as KernelAssetAbsolutePath).uri;
final List<Uri> sources = <Uri>[
for (final Asset source in assetMapping.value)
(source.path as AssetAbsolutePath).uri,

View file

@ -195,7 +195,7 @@ class NativeAssetsBuildRunnerImpl implements NativeAssetsBuildRunner {
/// Write [assets] to `native_assets.yaml` in [yamlParentDirectory].
Future<Uri> writeNativeAssetsYaml(
Iterable<Asset> assets,
KernelAssets assets,
Uri yamlParentDirectory,
FileSystem fileSystem,
) async {
@ -295,7 +295,7 @@ Future<void> ensureNoNativeAssetsOrOsIsSupported(
///
/// Therefore, ensure all `build.dart` scripts return only dynamic libraries.
void ensureNoLinkModeStatic(List<Asset> nativeAssets) {
final Iterable<Asset> staticAssets = nativeAssets.whereLinkMode(LinkMode.static);
final Iterable<Asset> staticAssets = nativeAssets.where((Asset e) => e.linkMode == LinkMode.static);
if (staticAssets.isNotEmpty) {
final String assetIds = staticAssets.map((Asset a) => a.id).toSet().join(', ');
throwToolExit(
@ -453,7 +453,7 @@ Future<Uri?> dryRunNativeAssetsMultipleOSes({
}
final Uri buildUri = buildUriMultiple(projectUri);
final Iterable<Asset> nativeAssetPaths = <Asset>[
final Iterable<KernelAsset> nativeAssetPaths = <KernelAsset>[
if (targetPlatforms.contains(build_info.TargetPlatform.darwin) ||
(targetPlatforms.contains(build_info.TargetPlatform.tester) && OS.current == OS.macOS))
...await dryRunNativeAssetsMacOSInternal(
@ -497,7 +497,11 @@ Future<Uri?> dryRunNativeAssetsMultipleOSes({
buildRunner,
),
];
final Uri nativeAssetsUri = await writeNativeAssetsYaml(nativeAssetPaths, buildUri, fileSystem);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
KernelAssets(nativeAssetPaths),
buildUri,
fileSystem,
);
return nativeAssetsUri;
}
@ -524,7 +528,7 @@ Future<Uri?> dryRunNativeAssetsSingleArchitecture({
}
final Uri buildUri = nativeAssetsBuildUri(projectUri, os);
final Iterable<Asset> nativeAssetPaths = await dryRunNativeAssetsSingleArchitectureInternal(
final Iterable<KernelAsset> nativeAssetPaths = await dryRunNativeAssetsSingleArchitectureInternal(
fileSystem,
projectUri,
flutterTester,
@ -532,14 +536,14 @@ Future<Uri?> dryRunNativeAssetsSingleArchitecture({
os,
);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
nativeAssetPaths,
KernelAssets(nativeAssetPaths.toList()),
buildUri,
fileSystem,
);
return nativeAssetsUri;
}
Future<Iterable<Asset>> dryRunNativeAssetsSingleArchitectureInternal(
Future<Iterable<KernelAsset>> dryRunNativeAssetsSingleArchitectureInternal(
FileSystem fileSystem,
Uri projectUri,
bool flutterTester,
@ -561,12 +565,11 @@ Future<Iterable<Asset>> dryRunNativeAssetsSingleArchitectureInternal(
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Dry running native assets for $targetOS done.');
final Uri? absolutePath = flutterTester ? buildUri : null;
final Map<Asset, Asset> assetTargetLocations = _assetTargetLocationsSingleArchitecture(
final Map<Asset, KernelAsset> assetTargetLocations = _assetTargetLocationsSingleArchitecture(
nativeAssets,
absolutePath,
);
final Iterable<Asset> nativeAssetPaths = assetTargetLocations.values;
return nativeAssetPaths;
return assetTargetLocations.values;
}
/// Builds native assets.
@ -595,7 +598,7 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsSingleA
}
if (!await nativeBuildRequired(buildRunner)) {
final Uri nativeAssetsYaml = await writeNativeAssetsYaml(
<Asset>[],
KernelAssets(),
yamlParentDirectory ?? buildUri,
fileSystem,
);
@ -619,7 +622,7 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsSingleA
ensureNoLinkModeStatic(nativeAssets);
globals.logger.printTrace('Building native assets for $target done.');
final Uri? absolutePath = flutterTester ? buildUri : null;
final Map<Asset, Asset> assetTargetLocations = _assetTargetLocationsSingleArchitecture(nativeAssets, absolutePath);
final Map<Asset, KernelAsset> assetTargetLocations = _assetTargetLocationsSingleArchitecture(nativeAssets, absolutePath);
await _copyNativeAssetsSingleArchitecture(
buildUri,
assetTargetLocations,
@ -627,18 +630,18 @@ Future<(Uri? nativeAssetsYaml, List<Uri> dependencies)> buildNativeAssetsSingleA
fileSystem,
);
final Uri nativeAssetsUri = await writeNativeAssetsYaml(
assetTargetLocations.values,
KernelAssets(assetTargetLocations.values.toList()),
yamlParentDirectory ?? buildUri,
fileSystem,
);
return (nativeAssetsUri, dependencies.toList());
}
Map<Asset, Asset> _assetTargetLocationsSingleArchitecture(
Map<Asset, KernelAsset> _assetTargetLocationsSingleArchitecture(
List<Asset> nativeAssets,
Uri? absolutePath,
) {
return <Asset, Asset>{
return <Asset, KernelAsset>{
for (final Asset asset in nativeAssets)
asset: _targetLocationSingleArchitecture(
asset,
@ -647,13 +650,16 @@ Map<Asset, Asset> _assetTargetLocationsSingleArchitecture(
};
}
Asset _targetLocationSingleArchitecture(Asset asset, Uri? absolutePath) {
KernelAsset _targetLocationSingleArchitecture(Asset asset, Uri? absolutePath) {
final AssetPath path = asset.path;
final KernelAssetPath kernelAssetPath;
switch (path) {
case AssetSystemPath _:
kernelAssetPath = KernelAssetSystemPath(path.uri);
case AssetInExecutable _:
kernelAssetPath = KernelAssetInExecutable();
case AssetInProcess _:
return asset;
kernelAssetPath = KernelAssetInProcess();
case AssetAbsolutePath _:
final String fileName = path.uri.pathSegments.last;
Uri uri;
@ -666,9 +672,17 @@ Asset _targetLocationSingleArchitecture(Asset asset, Uri? absolutePath) {
// kernel or aot snapshot.
uri = Uri(path: fileName);
}
return asset.copyWith(path: AssetAbsolutePath(uri));
kernelAssetPath = KernelAssetAbsolutePath(uri);
default:
throw Exception(
'Unsupported asset path type ${path.runtimeType} in asset $asset',
);
}
throw Exception('Unsupported asset path type ${path.runtimeType} in asset $asset');
return KernelAsset(
id: asset.id,
target: asset.target,
path: kernelAssetPath,
);
}
/// Extract the [Target] from a [TargetPlatform].
@ -702,7 +716,7 @@ Target _getNativeTarget(build_info.TargetPlatform targetPlatform) {
Future<void> _copyNativeAssetsSingleArchitecture(
Uri buildUri,
Map<Asset, Asset> assetTargetLocations,
Map<Asset, KernelAsset> assetTargetLocations,
build_info.BuildMode buildMode,
FileSystem fileSystem,
) async {
@ -712,9 +726,9 @@ Future<void> _copyNativeAssetsSingleArchitecture(
if (!buildDir.existsSync()) {
buildDir.createSync(recursive: true);
}
for (final MapEntry<Asset, Asset> assetMapping in assetTargetLocations.entries) {
for (final MapEntry<Asset, KernelAsset> assetMapping in assetTargetLocations.entries) {
final Uri source = (assetMapping.key.path as AssetAbsolutePath).uri;
final Uri target = (assetMapping.value.path as AssetAbsolutePath).uri;
final Uri target = (assetMapping.value.path as KernelAssetAbsolutePath).uri;
final Uri targetUri = buildUri.resolveUri(target);
final String targetFullPath = targetUri.toFilePath();
await fileSystem.file(source).copy(targetFullPath);

View file

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:native_assets_builder/native_assets_builder.dart'
hide NativeAssetsBuildRunner;
import 'package:native_assets_cli/native_assets_cli_internal.dart'
hide BuildMode;
@ -30,7 +32,7 @@ Future<Uri?> dryRunNativeAssetsWindows({
);
}
Future<Iterable<Asset>> dryRunNativeAssetsWindowsInternal(
Future<Iterable<KernelAsset>> dryRunNativeAssetsWindowsInternal(
FileSystem fileSystem,
Uri projectUri,
bool flutterTester,

View file

@ -55,7 +55,7 @@ dependencies:
cli_config: 0.1.2
graphs: 2.3.1
native_assets_builder: 0.3.2
native_assets_builder: 0.5.0
native_assets_cli: 0.4.2
# We depend on very specific internal implementation details of the
@ -118,4 +118,4 @@ dartdoc:
# Exclude this package from the hosted API docs.
nodoc: true
# PUBSPEC CHECKSUM: 980d
# PUBSPEC CHECKSUM: a00d