Unverified Commit 907bbe1b authored by Andrew Kolos's avatar Andrew Kolos Committed by GitHub

refactor asset bundle code to not depend on the global Cache.flutterRoot (#142277)

Fixes https://github.com/flutter/flutter/issues/142285.

Part of work on https://github.com/flutter/flutter/pull/141194.

This is a refactor. There should be no changes in tool behavior.
parent 97fef98b
......@@ -64,7 +64,12 @@ abstract class AssetBundleFactory {
required FileSystem fileSystem,
required Platform platform,
bool splitDeferredAssets = false,
}) => _ManifestAssetBundleFactory(logger: logger, fileSystem: fileSystem, platform: platform, splitDeferredAssets: splitDeferredAssets);
}) => _ManifestAssetBundleFactory(
logger: logger,
fileSystem: fileSystem,
platform: platform,
splitDeferredAssets: splitDeferredAssets,
);
/// Creates a new [AssetBundle].
AssetBundle createBundle();
......@@ -138,7 +143,13 @@ class _ManifestAssetBundleFactory implements AssetBundleFactory {
final bool _splitDeferredAssets;
@override
AssetBundle createBundle() => ManifestAssetBundle(logger: _logger, fileSystem: _fileSystem, platform: _platform, splitDeferredAssets: _splitDeferredAssets);
AssetBundle createBundle() => ManifestAssetBundle(
logger: _logger,
fileSystem: _fileSystem,
platform: _platform,
flutterRoot: Cache.flutterRoot!,
splitDeferredAssets: _splitDeferredAssets,
);
}
/// An asset bundle based on a pubspec.yaml file.
......@@ -149,10 +160,12 @@ class ManifestAssetBundle implements AssetBundle {
required Logger logger,
required FileSystem fileSystem,
required Platform platform,
required String flutterRoot,
bool splitDeferredAssets = false,
}) : _logger = logger,
_fileSystem = fileSystem,
_platform = platform,
_flutterRoot = flutterRoot,
_splitDeferredAssets = splitDeferredAssets,
_licenseCollector = LicenseCollector(fileSystem: fileSystem);
......@@ -160,6 +173,7 @@ class ManifestAssetBundle implements AssetBundle {
final FileSystem _fileSystem;
final LicenseCollector _licenseCollector;
final Platform _platform;
final String _flutterRoot;
final bool _splitDeferredAssets;
@override
......@@ -584,7 +598,7 @@ class ManifestAssetBundle implements AssetBundle {
final Uri entryUri = _fileSystem.path.toUri(asset);
result.add(_Asset(
baseDir: _fileSystem.path.join(
Cache.flutterRoot!,
_flutterRoot,
'bin', 'cache', 'artifacts', 'material_fonts',
),
relativeUri: Uri(path: entryUri.pathSegments.last),
......@@ -600,8 +614,7 @@ class ManifestAssetBundle implements AssetBundle {
List<_Asset> _getMaterialShaders() {
final String shaderPath = _fileSystem.path.join(
Cache.flutterRoot!,
'packages', 'flutter', 'lib', 'src', 'material', 'shaders',
_flutterRoot, 'packages', 'flutter', 'lib', 'src', 'material', 'shaders',
);
// This file will exist in a real invocation unless the git checkout is
// corrupted somehow, but unit tests generally don't create this file
......
......@@ -368,6 +368,11 @@ flutter:
logger: logger,
fileSystem: testFileSystem,
platform: platform,
flutterRoot: Cache.defaultFlutterRoot(
platform: platform,
fileSystem: testFileSystem,
userMessages: UserMessages(),
),
splitDeferredAssets: true,
);
......@@ -379,15 +384,6 @@ flutter:
return bundle;
}
late String? previousCacheFlutterRootValue;
setUp(() {
previousCacheFlutterRootValue = Cache.flutterRoot;
Cache.flutterRoot = Cache.defaultFlutterRoot(platform: platform, fileSystem: testFileSystem, userMessages: UserMessages());
});
tearDown(() => Cache.flutterRoot = previousCacheFlutterRootValue);
testWithoutContext('correctly bundles assets given a simple asset manifest with flavors', () async {
testFileSystem.file('.packages').createSync();
testFileSystem.file(testFileSystem.path.join('assets', 'common', 'image.png')).createSync(recursive: true);
......
......@@ -42,14 +42,15 @@ void main() {
group('AssetBundle asset variants (with Unix-style paths)', () {
late Platform platform;
late FileSystem fs;
late String flutterRoot;
setUp(() {
platform = FakePlatform();
fs = MemoryFileSystem.test();
Cache.flutterRoot = Cache.defaultFlutterRoot(
flutterRoot = Cache.defaultFlutterRoot(
platform: platform,
fileSystem: fs,
userMessages: UserMessages()
userMessages: UserMessages(),
);
fs.file('.packages').createSync();
......@@ -94,6 +95,7 @@ ${assets.map((String entry) => ' - $entry').join('\n')}
logger: BufferLogger.test(),
fileSystem: fs,
platform: platform,
flutterRoot: flutterRoot,
);
await bundle.build(
......@@ -147,6 +149,7 @@ ${assets.map((String entry) => ' - $entry').join('\n')}
final ManifestAssetBundle bundle = ManifestAssetBundle(
logger: BufferLogger.test(),
fileSystem: fs,
flutterRoot: flutterRoot,
platform: platform,
);
......@@ -203,6 +206,7 @@ ${assets.map((String entry) => ' - $entry').join('\n')}
logger: BufferLogger.test(),
fileSystem: fs,
platform: platform,
flutterRoot: flutterRoot,
);
await bundle.build(
......@@ -248,6 +252,7 @@ ${assets.map((String entry) => ' - $entry').join('\n')}
logger: BufferLogger.test(),
fileSystem: fs,
platform: platform,
flutterRoot: flutterRoot,
);
await bundle.build(
......@@ -274,11 +279,12 @@ ${assets.map((String entry) => ' - $entry').join('\n')}
group('AssetBundle asset variants (with Windows-style filepaths)', () {
late final Platform platform;
late final FileSystem fs;
late final String flutterRoot;
setUp(() {
platform = FakePlatform(operatingSystem: 'windows');
fs = MemoryFileSystem.test(style: FileSystemStyle.windows);
Cache.flutterRoot = Cache.defaultFlutterRoot(
flutterRoot = Cache.defaultFlutterRoot(
platform: platform,
fileSystem: fs,
userMessages: UserMessages()
......@@ -318,6 +324,7 @@ flutter:
logger: BufferLogger.test(),
fileSystem: fs,
platform: platform,
flutterRoot: flutterRoot,
);
await bundle.build(
......
......@@ -26,16 +26,8 @@ void main() {
group('Assets (${style.name} file system)', () {
late FileSystem fileSystem;
late BufferLogger logger;
late String? previousCacheFlutterRootValue;
late Platform platform;
setUpAll(() {
previousCacheFlutterRootValue = Cache.flutterRoot;
});
tearDownAll(() {
Cache.flutterRoot = previousCacheFlutterRootValue;
});
late String flutterRoot;
setUp(() {
fileSystem = MemoryFileSystem(
......@@ -44,7 +36,7 @@ void main() {
logger = BufferLogger.test();
platform = FakePlatform(
operatingSystem: style == Style.posix ? 'linux' : 'windows');
Cache.flutterRoot = Cache.defaultFlutterRoot(
flutterRoot = Cache.defaultFlutterRoot(
platform: platform,
fileSystem: fileSystem,
userMessages: UserMessages(),
......@@ -60,6 +52,7 @@ void main() {
fileSystem: fileSystem,
platform: platform,
splitDeferredAssets: true,
flutterRoot: flutterRoot,
);
fileSystem.file(fileSystem.path.join('font', 'pubspec.yaml'))
......@@ -157,6 +150,7 @@ dependencies:
fileSystem: fileSystem,
platform: platform,
splitDeferredAssets: true,
flutterRoot: flutterRoot,
);
await assetBundle.build(
......@@ -175,7 +169,7 @@ dependencies:
testWithoutContext('bundles material shaders on non-web platforms',
() async {
final String shaderPath = fileSystem.path.join(
Cache.flutterRoot!,
flutterRoot,
'packages',
'flutter',
'lib',
......@@ -205,6 +199,7 @@ dependencies:
logger: logger,
fileSystem: fileSystem,
platform: platform,
flutterRoot: flutterRoot,
);
await assetBundle.build(
......@@ -219,7 +214,7 @@ dependencies:
testWithoutContext('bundles material shaders on web platforms',
() async {
final String shaderPath = fileSystem.path.join(
Cache.flutterRoot!,
flutterRoot,
'packages',
'flutter',
'lib',
......@@ -249,6 +244,7 @@ dependencies:
logger: logger,
fileSystem: fileSystem,
platform: platform,
flutterRoot: flutterRoot,
);
await assetBundle.build(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment