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

Enable asset transformation for flutter build for iOS, Android, Windows, MacOS, Linux, and web (also flutter run without hot reload support) #143815

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
51 changes: 46 additions & 5 deletions packages/flutter_tools/lib/src/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ enum AssetKind {
final class AssetBundleEntry {
const AssetBundleEntry(this.content, {
required this.kind,
required this.transformers,
});

final DevFSContent content;
final AssetKind kind;
final List<AssetTransformerEntry> transformers;

Future<List<int>> contentsAsBytes() => content.contentsAsBytes();
}
Expand Down Expand Up @@ -264,6 +266,7 @@ class ManifestAssetBundle implements AssetBundle {
entries[_kAssetManifestJsonFilename] = AssetBundleEntry(
DevFSStringContent('{}'),
kind: AssetKind.regular,
transformers: const <AssetTransformerEntry>[],
);
final ByteData emptyAssetManifest =
const StandardMessageCodec().encodeMessage(<dynamic, dynamic>{})!;
Expand All @@ -272,12 +275,14 @@ class ManifestAssetBundle implements AssetBundle {
emptyAssetManifest.buffer.asUint8List(0, emptyAssetManifest.lengthInBytes),
),
kind: AssetKind.regular,
transformers: const <AssetTransformerEntry>[],
);
// Create .bin.json on web builds.
if (targetPlatform == TargetPlatform.web_javascript) {
entries[_kAssetManifestBinJsonFilename] = AssetBundleEntry(
DevFSStringContent('""'),
kind: AssetKind.regular,
transformers: const <AssetTransformerEntry>[],
);
}
return 0;
Expand Down Expand Up @@ -423,6 +428,7 @@ class ManifestAssetBundle implements AssetBundle {
entries[variant.entryUri.path] ??= AssetBundleEntry(
DevFSFileContent(variantFile),
kind: variant.kind,
transformers: variant.transformers,
);
}
}
Expand Down Expand Up @@ -456,6 +462,7 @@ class ManifestAssetBundle implements AssetBundle {
deferredComponentsEntries[componentName]![variant.entryUri.path] ??= AssetBundleEntry(
DevFSFileContent(variantFile),
kind: AssetKind.regular,
transformers: variant.transformers,
);
}
}
Expand All @@ -471,7 +478,11 @@ class ManifestAssetBundle implements AssetBundle {
for (final _Asset asset in materialAssets) {
final File assetFile = asset.lookupAssetFile(_fileSystem);
assert(assetFile.existsSync(), 'Missing ${assetFile.path}');
entries[asset.entryUri.path] ??= AssetBundleEntry(DevFSFileContent(assetFile), kind: asset.kind);
entries[asset.entryUri.path] ??= AssetBundleEntry(
DevFSFileContent(assetFile),
kind: asset.kind,
transformers: const <AssetTransformerEntry>[],
);
}

// Update wildcard directories we can detect changes in them.
Expand Down Expand Up @@ -534,6 +545,7 @@ class ManifestAssetBundle implements AssetBundle {
entries[key] = AssetBundleEntry(
content,
kind: assetKind,
transformers: const <AssetTransformerEntry>[],
);
}

Expand Down Expand Up @@ -579,6 +591,7 @@ class ManifestAssetBundle implements AssetBundle {
hintString: 'copyrightsoftwaretothisinandorofthe',
),
kind: AssetKind.regular,
transformers: const<AssetTransformerEntry>[],
);
}
}
Expand Down Expand Up @@ -684,6 +697,8 @@ class ManifestAssetBundle implements AssetBundle {
cache,
componentAssets,
assetsEntry.uri,
flavors: assetsEntry.flavors,
transformers: assetsEntry.transformers,
);
} else {
_parseAssetFromFile(
Expand All @@ -693,6 +708,8 @@ class ManifestAssetBundle implements AssetBundle {
cache,
componentAssets,
assetsEntry.uri,
flavors: assetsEntry.flavors,
transformers: assetsEntry.transformers,
);
}
}
Expand Down Expand Up @@ -863,6 +880,7 @@ class ManifestAssetBundle implements AssetBundle {
packageName: packageName,
attributedPackage: attributedPackage,
flavors: assetsEntry.flavors,
transformers: assetsEntry.transformers,
);
} else {
_parseAssetFromFile(
Expand All @@ -875,6 +893,7 @@ class ManifestAssetBundle implements AssetBundle {
packageName: packageName,
attributedPackage: attributedPackage,
flavors: assetsEntry.flavors,
transformers: assetsEntry.transformers,
);
}
}
Expand All @@ -900,6 +919,8 @@ class ManifestAssetBundle implements AssetBundle {
packageName: packageName,
attributedPackage: attributedPackage,
assetKind: AssetKind.shader,
flavors: <String>{},
transformers: <AssetTransformerEntry>[],
);
}

Expand All @@ -914,6 +935,8 @@ class ManifestAssetBundle implements AssetBundle {
packageName: packageName,
attributedPackage: attributedPackage,
assetKind: AssetKind.model,
flavors: <String>{},
transformers: <AssetTransformerEntry>[],
);
}

Expand All @@ -927,6 +950,8 @@ class ManifestAssetBundle implements AssetBundle {
packageName,
attributedPackage,
assetKind: AssetKind.font,
flavors: <String>{},
transformers: <AssetTransformerEntry>[],
);
final File baseAssetFile = baseAsset.lookupAssetFile(_fileSystem);
if (!baseAssetFile.existsSync()) {
Expand All @@ -949,7 +974,8 @@ class ManifestAssetBundle implements AssetBundle {
Uri assetUri, {
String? packageName,
Package? attributedPackage,
Set<String>? flavors,
required Set<String> flavors,
required List<AssetTransformerEntry> transformers,
}) {
final String directoryPath;
try {
Expand Down Expand Up @@ -985,6 +1011,7 @@ class ManifestAssetBundle implements AssetBundle {
attributedPackage: attributedPackage,
originUri: assetUri,
flavors: flavors,
transformers: transformers,
);
}
}
Expand All @@ -1000,7 +1027,8 @@ class ManifestAssetBundle implements AssetBundle {
String? packageName,
Package? attributedPackage,
AssetKind assetKind = AssetKind.regular,
Set<String>? flavors,
required Set<String> flavors,
required List<AssetTransformerEntry> transformers,
}) {
final _Asset asset = _resolveAsset(
packageConfig,
Expand All @@ -1011,6 +1039,7 @@ class ManifestAssetBundle implements AssetBundle {
assetKind: assetKind,
originUri: originUri,
flavors: flavors,
transformers: transformers,
);

_checkForFlavorConflicts(asset, result.keys.toList());
Expand All @@ -1032,6 +1061,8 @@ class ManifestAssetBundle implements AssetBundle {
relativeUri: relativeUri,
package: attributedPackage,
kind: assetKind,
flavors: flavors,
transformers: transformers,
),
);
}
Expand Down Expand Up @@ -1116,7 +1147,8 @@ class ManifestAssetBundle implements AssetBundle {
Package? attributedPackage, {
Uri? originUri,
AssetKind assetKind = AssetKind.regular,
Set<String>? flavors,
required Set<String> flavors,
required List<AssetTransformerEntry> transformers,
}) {
final String assetPath = _fileSystem.path.fromUri(assetUri);
if (assetUri.pathSegments.first == 'packages'
Expand All @@ -1130,6 +1162,7 @@ class ManifestAssetBundle implements AssetBundle {
assetKind: assetKind,
originUri: originUri,
flavors: flavors,
transformers: transformers,
);
if (packageAsset != null) {
return packageAsset;
Expand All @@ -1146,6 +1179,7 @@ class ManifestAssetBundle implements AssetBundle {
originUri: originUri,
kind: assetKind,
flavors: flavors,
transformers: transformers,
);
}

Expand All @@ -1156,6 +1190,7 @@ class ManifestAssetBundle implements AssetBundle {
AssetKind assetKind = AssetKind.regular,
Uri? originUri,
Set<String>? flavors,
List<AssetTransformerEntry>? transformers,
}) {
assert(assetUri.pathSegments.first == 'packages');
if (assetUri.pathSegments.length > 1) {
Expand All @@ -1171,6 +1206,7 @@ class ManifestAssetBundle implements AssetBundle {
kind: assetKind,
originUri: originUri,
flavors: flavors,
transformers: transformers,
);
}
}
Expand All @@ -1193,7 +1229,10 @@ class _Asset {
required this.package,
this.kind = AssetKind.regular,
Set<String>? flavors,
}): originUri = originUri ?? entryUri, flavors = flavors ?? const <String>{};
List<AssetTransformerEntry>? transformers,
}) : originUri = originUri ?? entryUri,
flavors = flavors ?? const <String>{},
transformers = transformers ?? const <AssetTransformerEntry>[];

final String baseDir;

Expand All @@ -1214,6 +1253,8 @@ class _Asset {

final Set<String> flavors;

final List<AssetTransformerEntry> transformers;

File lookupAssetFile(FileSystem fileSystem) {
return fileSystem.file(fileSystem.path.join(baseDir, fileSystem.path.fromUri(relativeUri)));
}
Expand Down
16 changes: 16 additions & 0 deletions packages/flutter_tools/lib/src/base/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,19 @@ bool setEquals<T>(Set<T>? a, Set<T>? b) {
}
return true;
}

/// Tests for shallow equality on two lists.
bool listEquals<T>(List<T> a, List<T> b) {
if (identical(a, b)) {
return true;
}
if (a.length != b.length) {
return false;
}
for (int index = 0; index < a.length; index++) {
if (a[index] != b[index]) {
return false;
}
}
return true;
}
29 changes: 27 additions & 2 deletions packages/flutter_tools/lib/src/build_system/targets/assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

import 'package:pool/pool.dart';

import '../../artifacts.dart';
import '../../asset.dart';
import '../../base/common.dart';
import '../../base/file_system.dart';
import '../../base/logger.dart';
import '../../build_info.dart';
import '../../convert.dart';
import '../../devfs.dart';
import '../../flutter_manifest.dart';
import '../build_system.dart';
import '../depfile.dart';
import '../tools/asset_transformer.dart';
import '../tools/scene_importer.dart';
import '../tools/shader_compiler.dart';
import 'common.dart';
Expand Down Expand Up @@ -93,19 +97,29 @@ Future<Depfile> copyAssets(
fileSystem: environment.fileSystem,
artifacts: environment.artifacts,
);
final AssetTransformer assetTransformer = AssetTransformer(
processManager: environment.processManager,
fileSystem: environment.fileSystem,
dartBinaryPath: environment.artifacts.getArtifactPath(Artifact.engineDartBinary),
);

final Map<String, AssetBundleEntry> assetEntries = <String, AssetBundleEntry>{
...assetBundle.entries,
...additionalContent.map((String key, DevFSContent value) {
return MapEntry<String, AssetBundleEntry>(
key,
AssetBundleEntry(value, kind: AssetKind.regular),
AssetBundleEntry(
value,
kind: AssetKind.regular,
transformers: const <AssetTransformerEntry>[],
),
);
}),
if (skslBundle != null)
kSkSLShaderBundlePath: AssetBundleEntry(
skslBundle,
kind: AssetKind.regular,
transformers: const <AssetTransformerEntry>[],
),
};

Expand All @@ -128,7 +142,18 @@ Future<Depfile> copyAssets(
bool doCopy = true;
switch (entry.value.kind) {
case AssetKind.regular:
break;
if (entry.value.transformers.isNotEmpty) {
final AssetTransformationFailure? failure = await assetTransformer.transformAsset(
asset: content.file as File,
outputPath: file.path,
workingDirectory: environment.projectDir.path,
transformerEntries: entry.value.transformers,
);
doCopy = false;
if (failure != null) {
throwToolExit(failure.message);
}
}
case AssetKind.font:
doCopy = !await iconTreeShaker.subsetFont(
input: content.file as File,
Expand Down
Loading