![auto-submit[bot]](/assets/img/avatar_default.png)
Reverts flutter/flutter#132985 Initiated by: christopherfujino This change reverts the following previous change: Original Description: Provides support for conditional bundling of assets through the existing `--flavor` option for `flutter build` and `flutter run`. Closes https://github.com/flutter/flutter/issues/21682. Resolves https://github.com/flutter/flutter/issues/136092 ## Change Within the `assets` section pubspec.yaml, the user can now specify one or more `flavors` that an asset belongs to. Consider this example: ```yaml # pubspec.yaml flutter: assets: - assets/normal-asset.png - path: assets/vanilla/ice-cream.png flavors: - vanilla - path: assets/strawberry/ice-cream.png flavors: - strawberry ``` With this pubspec, * `flutter run --flavor vanilla` will not include `assets/strawberry/ice-cream.png` in the build output. * `flutter run --flavor strawberry` will not include `assets/vanilla/ice-cream.png`. * `flutter run` will only include `assets/normal-asset.png`. ## Open questions * Should this be supported for all platforms, or should this change be limited to ones with documented `--flavor` support (Android, iOS, and (implicitly) MacOS)? This PR currently only enables this feature for officially supported platforms. ## Design thoughts, what this PR does not do, etc. ### This does not provide an automatic mapping/resolution of asset keys/paths to others based on flavor at runtime. The implementation in this PR represents a simplest approach. Notably, it does not give Flutter the ability to dynamically choose an asset based on flavor using a single asset key. For example, one can't use `Image.asset('config.json')` to dynamically choose between different "flavors" of `config.json` (such as `dev-flavor/config.json` or `prod-flavor/config.json`). However, a user could always implement such a mechanism in their project or in a library by examining the flavor at runtime. ### When multiple entries affect the same file and 1) at least one of these entries have a `flavors` list provided and 2) these lists are not equivalent, we always consider the manifest to be ambiguous and will throw a `ToolExit`. <details> For example, these manifests would all be considered ambiguous: ```yaml assets: - assets/ - path: assets/vanilla.png flavors: - vanilla assets: - path: assets/vanilla/ flavors: - vanilla - path: assets/vanilla/cherry.png flavor: - cherry # Thinking towards the future where we might add glob/regex support and more conditions other than flavor: assets: - path: assets/vanilla/** flavors: - vanilla - path: assets/**/ios/** platforms: - ios # Ambiguous in the case of assets like "assets/vanilla/ios/icon.svg" since we # don't know if flavor `vanilla` and platform `ios` should be combined using or-logic or and-logic. ``` See [this review comment thread](https://github.com/flutter/flutter/pull/132985#discussion_r1381909942) for the full story on how I arrived at this decision. </details> ### This does not support Android's multidimensional flavors feature (in an intuitive way) <details> Conder this excerpt from a Flutter project's android/app/build.gradle file: ```groovy android { // ... flavorDimensions "mode", "api" productFlavors { free { dimension "mode" applicationIdSuffix ".free" } premium { dimension "mode" applicationIdSuffix ".premium" } minApi23 { dimension "api" versionNameSuffix "-minApi23" } minApi21 { dimension "api" versionNameSuffix "-minApi21" } } } ``` In this setup, the following values are valid `--flavor` are valid `freeMinApi21`, `freeMinApi23`, `premiumMinApi21`, and `premiumMinApi23`. We call these values "flavor combinations". Consider the following from the Android documentation[^1]: > In addition to the source set directories you can create for each individual product flavor and build variant, you can also create source set directories for each combination of product flavors. For example, you can create and add Java sources to the src/demoMinApi24/java/ directory, and Gradle uses those sources only when building a variant that combines those two product flavors. > > Source sets you create for product flavor combinations have a higher priority than source sets that belong to each individual product flavor. To learn more about source sets and how Gradle merges resources, read the section about how to [create source sets](https://developer.android.com/build/build-variants#sourcesets). This feature will not behave in this way. If a user utilizes this feature and also Android's multidimensional flavors feature, they will have to list out all flavor combinations that contain the flavor they want to limit an asset to: ```yaml assets: - assets/free/ flavors: - freeMinApi21 - freeMinApi23 ``` This is mostly due to a technical limitation in the hot-reload feature of `flutter run`. During a hot reload, the tool will try to update the asset bundle on the device, but the tool does not know the flavors contained within the flavor combination (that the user passes to `--flavor`). Gradle is the source of truth of what flavors were involved in the build, and `flutter run` currently does not access to that information since it's an implementation detail of the build process. We could bubble up this information, but it would require a nontrivial amount of engineering work, and it's unclear how desired this functionality is. It might not be worth implementing. </details> See https://flutter.dev/go/flavor-specific-assets for the (outdated) design document. <summary>Pre-launch Checklist</summary> </details> [^1]: https://developer.android.com/build/build-variants#flavor-dimensions
832 lines
30 KiB
Dart
832 lines
30 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io' as io show Process, ProcessSignal;
|
|
|
|
import 'package:file/file.dart';
|
|
import 'package:file/memory.dart';
|
|
import 'package:file_testing/file_testing.dart';
|
|
import 'package:flutter_tools/src/artifacts.dart';
|
|
import 'package:flutter_tools/src/asset.dart';
|
|
import 'package:flutter_tools/src/base/file_system.dart';
|
|
import 'package:flutter_tools/src/base/io.dart';
|
|
import 'package:flutter_tools/src/base/logger.dart';
|
|
import 'package:flutter_tools/src/base/os.dart';
|
|
import 'package:flutter_tools/src/base/platform.dart';
|
|
import 'package:flutter_tools/src/build_info.dart';
|
|
import 'package:flutter_tools/src/build_system/targets/shader_compiler.dart';
|
|
import 'package:flutter_tools/src/compile.dart';
|
|
import 'package:flutter_tools/src/devfs.dart';
|
|
import 'package:flutter_tools/src/device.dart';
|
|
import 'package:flutter_tools/src/vmservice.dart';
|
|
import 'package:package_config/package_config.dart';
|
|
import 'package:test/fake.dart';
|
|
|
|
import '../src/common.dart';
|
|
import '../src/context.dart';
|
|
import '../src/fake_http_client.dart';
|
|
import '../src/fake_vm_services.dart';
|
|
import '../src/fakes.dart';
|
|
import '../src/logging_logger.dart';
|
|
|
|
final FakeVmServiceRequest createDevFSRequest = FakeVmServiceRequest(
|
|
method: '_createDevFS',
|
|
args: <String, Object>{
|
|
'fsName': 'test',
|
|
},
|
|
jsonResponse: <String, Object>{
|
|
'uri': Uri.parse('test').toString(),
|
|
}
|
|
);
|
|
|
|
const FakeVmServiceRequest failingCreateDevFSRequest = FakeVmServiceRequest(
|
|
method: '_createDevFS',
|
|
args: <String, Object>{
|
|
'fsName': 'test',
|
|
},
|
|
errorCode: RPCErrorCodes.kServiceDisappeared,
|
|
);
|
|
|
|
const FakeVmServiceRequest failingDeleteDevFSRequest = FakeVmServiceRequest(
|
|
method: '_deleteDevFS',
|
|
args: <String, dynamic>{'fsName': 'test'},
|
|
errorCode: RPCErrorCodes.kServiceDisappeared,
|
|
);
|
|
|
|
void main() {
|
|
testWithoutContext('DevFSByteContent', () {
|
|
final DevFSByteContent content = DevFSByteContent(<int>[4, 5, 6]);
|
|
|
|
expect(content.bytes, orderedEquals(<int>[4, 5, 6]));
|
|
expect(content.isModified, isTrue);
|
|
expect(content.isModified, isFalse);
|
|
content.bytes = <int>[7, 8, 9, 2];
|
|
expect(content.bytes, orderedEquals(<int>[7, 8, 9, 2]));
|
|
expect(content.isModified, isTrue);
|
|
expect(content.isModified, isFalse);
|
|
});
|
|
|
|
testWithoutContext('DevFSStringContent', () {
|
|
final DevFSStringContent content = DevFSStringContent('some string');
|
|
|
|
expect(content.string, 'some string');
|
|
expect(content.bytes, orderedEquals(utf8.encode('some string')));
|
|
expect(content.isModified, isTrue);
|
|
expect(content.isModified, isFalse);
|
|
content.string = 'another string';
|
|
expect(content.string, 'another string');
|
|
expect(content.bytes, orderedEquals(utf8.encode('another string')));
|
|
expect(content.isModified, isTrue);
|
|
expect(content.isModified, isFalse);
|
|
content.bytes = utf8.encode('foo bar');
|
|
expect(content.string, 'foo bar');
|
|
expect(content.bytes, orderedEquals(utf8.encode('foo bar')));
|
|
expect(content.isModified, isTrue);
|
|
expect(content.isModified, isFalse);
|
|
});
|
|
|
|
testWithoutContext('DevFSFileContent', () async {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final File file = fileSystem.file('foo.txt');
|
|
final DevFSFileContent content = DevFSFileContent(file);
|
|
expect(content.isModified, isFalse);
|
|
expect(content.isModified, isFalse);
|
|
|
|
file.parent.createSync(recursive: true);
|
|
file.writeAsBytesSync(<int>[1, 2, 3], flush: true);
|
|
|
|
final DateTime fiveSecondsAgo = file.statSync().modified.subtract(const Duration(seconds: 5));
|
|
expect(content.isModifiedAfter(fiveSecondsAgo), isTrue);
|
|
expect(content.isModifiedAfter(fiveSecondsAgo), isTrue);
|
|
|
|
file.writeAsBytesSync(<int>[2, 3, 4], flush: true);
|
|
|
|
expect(content.isModified, isTrue);
|
|
expect(content.isModified, isFalse);
|
|
expect(await content.contentsAsBytes(), <int>[2, 3, 4]);
|
|
|
|
expect(content.isModified, isFalse);
|
|
expect(content.isModified, isFalse);
|
|
|
|
file.deleteSync();
|
|
expect(content.isModified, isTrue);
|
|
expect(content.isModified, isFalse);
|
|
expect(content.isModified, isFalse);
|
|
});
|
|
|
|
testWithoutContext('DevFSStringCompressingBytesContent', () {
|
|
final DevFSStringCompressingBytesContent content =
|
|
DevFSStringCompressingBytesContent('uncompressed string');
|
|
|
|
expect(content.equals('uncompressed string'), isTrue);
|
|
expect(content.bytes, isNotNull);
|
|
expect(content.isModified, isTrue);
|
|
expect(content.isModified, isFalse);
|
|
});
|
|
|
|
testWithoutContext('DevFS create throws a DevFSException when vmservice disconnects unexpectedly', () async {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final OperatingSystemUtils osUtils = FakeOperatingSystemUtils();
|
|
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
|
|
requests: <VmServiceExpectation>[failingCreateDevFSRequest],
|
|
httpAddress: Uri.parse('http://localhost'),
|
|
);
|
|
|
|
final DevFS devFS = DevFS(
|
|
fakeVmServiceHost.vmService,
|
|
'test',
|
|
fileSystem.currentDirectory,
|
|
osUtils: osUtils,
|
|
fileSystem: fileSystem,
|
|
logger: BufferLogger.test(),
|
|
httpClient: FakeHttpClient.any(),
|
|
);
|
|
expect(() async => devFS.create(), throwsA(isA<DevFSException>()));
|
|
});
|
|
|
|
testWithoutContext('DevFS destroy is resilient to vmservice disconnection', () async {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final OperatingSystemUtils osUtils = FakeOperatingSystemUtils();
|
|
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
|
|
requests: <VmServiceExpectation>[
|
|
createDevFSRequest,
|
|
failingDeleteDevFSRequest,
|
|
],
|
|
httpAddress: Uri.parse('http://localhost'),
|
|
);
|
|
|
|
final DevFS devFS = DevFS(
|
|
fakeVmServiceHost.vmService,
|
|
'test',
|
|
fileSystem.currentDirectory,
|
|
osUtils: osUtils,
|
|
fileSystem: fileSystem,
|
|
logger: BufferLogger.test(),
|
|
httpClient: FakeHttpClient.any(),
|
|
);
|
|
|
|
expect(await devFS.create(), isNotNull);
|
|
await devFS.destroy(); // Testing that this does not throw.
|
|
});
|
|
|
|
testWithoutContext('DevFS retries uploads when connection reset by peer', () async {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final OperatingSystemUtils osUtils = OperatingSystemUtils(
|
|
fileSystem: fileSystem,
|
|
platform: FakePlatform(),
|
|
logger: BufferLogger.test(),
|
|
processManager: FakeProcessManager.any(),
|
|
);
|
|
final FakeResidentCompiler residentCompiler = FakeResidentCompiler();
|
|
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
|
|
requests: <VmServiceExpectation>[createDevFSRequest],
|
|
httpAddress: Uri.parse('http://localhost'),
|
|
);
|
|
residentCompiler.onRecompile = (Uri mainUri, List<Uri>? invalidatedFiles) async {
|
|
fileSystem.file('lib/foo.dill')
|
|
..createSync(recursive: true)
|
|
..writeAsBytesSync(<int>[1, 2, 3, 4, 5]);
|
|
return const CompilerOutput('lib/foo.dill', 0, <Uri>[]);
|
|
};
|
|
|
|
/// This output can change based on the host platform.
|
|
final List<List<int>> expectedEncoded = await osUtils.gzipLevel1Stream(
|
|
Stream<List<int>>.value(<int>[1, 2, 3, 4, 5]),
|
|
).toList();
|
|
|
|
final DevFS devFS = DevFS(
|
|
fakeVmServiceHost.vmService,
|
|
'test',
|
|
fileSystem.currentDirectory,
|
|
osUtils: osUtils,
|
|
fileSystem: fileSystem,
|
|
logger: BufferLogger.test(),
|
|
httpClient: FakeHttpClient.list(<FakeRequest>[
|
|
FakeRequest(Uri.parse('http://localhost'), method: HttpMethod.put, responseError: const OSError('Connection Reset by peer')),
|
|
FakeRequest(Uri.parse('http://localhost'), method: HttpMethod.put, responseError: const OSError('Connection Reset by peer')),
|
|
FakeRequest(Uri.parse('http://localhost'), method: HttpMethod.put, responseError: const OSError('Connection Reset by peer')),
|
|
FakeRequest(Uri.parse('http://localhost'), method: HttpMethod.put, responseError: const OSError('Connection Reset by peer')),
|
|
FakeRequest(Uri.parse('http://localhost'), method: HttpMethod.put, responseError: const OSError('Connection Reset by peer')),
|
|
// This is the value of `<int>[1, 2, 3, 4, 5]` run through `osUtils.gzipLevel1Stream`.
|
|
FakeRequest(Uri.parse('http://localhost'), method: HttpMethod.put, body: <int>[for (final List<int> chunk in expectedEncoded) ...chunk]),
|
|
]),
|
|
uploadRetryThrottle: Duration.zero,
|
|
);
|
|
await devFS.create();
|
|
|
|
final UpdateFSReport report = await devFS.update(
|
|
mainUri: Uri.parse('lib/foo.txt'),
|
|
dillOutputPath: 'lib/foo.dill',
|
|
generator: residentCompiler,
|
|
pathToReload: 'lib/foo.txt.dill',
|
|
trackWidgetCreation: false,
|
|
invalidatedFiles: <Uri>[],
|
|
packageConfig: PackageConfig.empty,
|
|
shaderCompiler: const FakeShaderCompiler(),
|
|
);
|
|
|
|
expect(report.syncedBytes, 5);
|
|
expect(report.success, isTrue);
|
|
});
|
|
|
|
testWithoutContext('DevFS reports unsuccessful compile when errors are returned', () async {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
|
|
requests: <VmServiceExpectation>[createDevFSRequest],
|
|
httpAddress: Uri.parse('http://localhost'),
|
|
);
|
|
|
|
final DevFS devFS = DevFS(
|
|
fakeVmServiceHost.vmService,
|
|
'test',
|
|
fileSystem.currentDirectory,
|
|
fileSystem: fileSystem,
|
|
logger: BufferLogger.test(),
|
|
osUtils: FakeOperatingSystemUtils(),
|
|
httpClient: FakeHttpClient.any(),
|
|
);
|
|
|
|
await devFS.create();
|
|
final DateTime? previousCompile = devFS.lastCompiled;
|
|
|
|
final FakeResidentCompiler residentCompiler = FakeResidentCompiler();
|
|
residentCompiler.onRecompile = (Uri mainUri, List<Uri>? invalidatedFiles) async {
|
|
return const CompilerOutput('lib/foo.dill', 2, <Uri>[]);
|
|
};
|
|
|
|
final UpdateFSReport report = await devFS.update(
|
|
mainUri: Uri.parse('lib/foo.txt'),
|
|
generator: residentCompiler,
|
|
dillOutputPath: 'lib/foo.dill',
|
|
pathToReload: 'lib/foo.txt.dill',
|
|
trackWidgetCreation: false,
|
|
invalidatedFiles: <Uri>[],
|
|
packageConfig: PackageConfig.empty,
|
|
shaderCompiler: const FakeShaderCompiler(),
|
|
);
|
|
|
|
expect(report.success, false);
|
|
expect(devFS.lastCompiled, previousCompile);
|
|
});
|
|
|
|
testWithoutContext('DevFS correctly updates last compiled time when compilation does not fail', () async {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
|
|
requests: <VmServiceExpectation>[createDevFSRequest],
|
|
httpAddress: Uri.parse('http://localhost'),
|
|
);
|
|
|
|
final DevFS devFS = DevFS(
|
|
fakeVmServiceHost.vmService,
|
|
'test',
|
|
fileSystem.currentDirectory,
|
|
fileSystem: fileSystem,
|
|
logger: BufferLogger.test(),
|
|
osUtils: FakeOperatingSystemUtils(),
|
|
httpClient: FakeHttpClient.any(),
|
|
);
|
|
|
|
await devFS.create();
|
|
final DateTime? previousCompile = devFS.lastCompiled;
|
|
|
|
final FakeResidentCompiler residentCompiler = FakeResidentCompiler();
|
|
residentCompiler.onRecompile = (Uri mainUri, List<Uri>? invalidatedFiles) async {
|
|
fileSystem.file('lib/foo.txt.dill').createSync(recursive: true);
|
|
return const CompilerOutput('lib/foo.txt.dill', 0, <Uri>[]);
|
|
};
|
|
|
|
final UpdateFSReport report = await devFS.update(
|
|
mainUri: Uri.parse('lib/main.dart'),
|
|
generator: residentCompiler,
|
|
dillOutputPath: 'lib/foo.dill',
|
|
pathToReload: 'lib/foo.txt.dill',
|
|
trackWidgetCreation: false,
|
|
invalidatedFiles: <Uri>[],
|
|
packageConfig: PackageConfig.empty,
|
|
shaderCompiler: const FakeShaderCompiler(),
|
|
);
|
|
|
|
expect(report.success, true);
|
|
expect(devFS.lastCompiled, isNot(previousCompile));
|
|
});
|
|
|
|
testWithoutContext('DevFS can reset compilation time', () async {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
|
|
requests: <VmServiceExpectation>[createDevFSRequest],
|
|
);
|
|
final LocalDevFSWriter localDevFSWriter = LocalDevFSWriter(fileSystem: fileSystem);
|
|
fileSystem.directory('test').createSync();
|
|
|
|
final DevFS devFS = DevFS(
|
|
fakeVmServiceHost.vmService,
|
|
'test',
|
|
fileSystem.currentDirectory,
|
|
fileSystem: fileSystem,
|
|
logger: BufferLogger.test(),
|
|
osUtils: FakeOperatingSystemUtils(),
|
|
httpClient: HttpClient(),
|
|
);
|
|
|
|
await devFS.create();
|
|
final DateTime? previousCompile = devFS.lastCompiled;
|
|
|
|
final FakeResidentCompiler residentCompiler = FakeResidentCompiler();
|
|
residentCompiler.onRecompile = (Uri mainUri, List<Uri>? invalidatedFiles) async {
|
|
fileSystem.file('lib/foo.txt.dill').createSync(recursive: true);
|
|
return const CompilerOutput('lib/foo.txt.dill', 0, <Uri>[]);
|
|
};
|
|
|
|
final UpdateFSReport report = await devFS.update(
|
|
mainUri: Uri.parse('lib/main.dart'),
|
|
generator: residentCompiler,
|
|
dillOutputPath: 'lib/foo.dill',
|
|
pathToReload: 'lib/foo.txt.dill',
|
|
trackWidgetCreation: false,
|
|
invalidatedFiles: <Uri>[],
|
|
packageConfig: PackageConfig.empty,
|
|
devFSWriter: localDevFSWriter,
|
|
shaderCompiler: const FakeShaderCompiler(),
|
|
);
|
|
|
|
expect(report.success, true);
|
|
expect(devFS.lastCompiled, isNot(previousCompile));
|
|
|
|
devFS.resetLastCompiled();
|
|
expect(devFS.lastCompiled, previousCompile);
|
|
|
|
// Does not reset to report compile time.
|
|
devFS.resetLastCompiled();
|
|
expect(devFS.lastCompiled, previousCompile);
|
|
});
|
|
|
|
testWithoutContext('DevFS uses provided DevFSWriter instead of default HTTP writer', () async {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final FakeDevFSWriter writer = FakeDevFSWriter();
|
|
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
|
|
requests: <VmServiceExpectation>[createDevFSRequest],
|
|
);
|
|
|
|
final DevFS devFS = DevFS(
|
|
fakeVmServiceHost.vmService,
|
|
'test',
|
|
fileSystem.currentDirectory,
|
|
fileSystem: fileSystem,
|
|
logger: BufferLogger.test(),
|
|
osUtils: FakeOperatingSystemUtils(),
|
|
httpClient: FakeHttpClient.any(),
|
|
);
|
|
|
|
await devFS.create();
|
|
|
|
final FakeResidentCompiler residentCompiler = FakeResidentCompiler();
|
|
residentCompiler.onRecompile = (Uri mainUri, List<Uri>? invalidatedFiles) async {
|
|
fileSystem.file('example').createSync();
|
|
return const CompilerOutput('lib/foo.txt.dill', 0, <Uri>[]);
|
|
};
|
|
|
|
expect(writer.written, false);
|
|
|
|
final UpdateFSReport report = await devFS.update(
|
|
mainUri: Uri.parse('lib/main.dart'),
|
|
generator: residentCompiler,
|
|
dillOutputPath: 'lib/foo.dill',
|
|
pathToReload: 'lib/foo.txt.dill',
|
|
trackWidgetCreation: false,
|
|
invalidatedFiles: <Uri>[],
|
|
packageConfig: PackageConfig.empty,
|
|
devFSWriter: writer,
|
|
shaderCompiler: const FakeShaderCompiler(),
|
|
);
|
|
|
|
expect(report.success, true);
|
|
expect(writer.written, true);
|
|
});
|
|
|
|
testWithoutContext('Local DevFSWriter can copy and write files', () async {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final File file = fileSystem.file('foo_bar')
|
|
..writeAsStringSync('goodbye');
|
|
final LocalDevFSWriter writer = LocalDevFSWriter(fileSystem: fileSystem);
|
|
|
|
await writer.write(<Uri, DevFSContent>{
|
|
Uri.parse('hello'): DevFSStringContent('hello'),
|
|
Uri.parse('goodbye'): DevFSFileContent(file),
|
|
}, Uri.parse('/foo/bar/devfs/'));
|
|
|
|
expect(fileSystem.file('/foo/bar/devfs/hello'), exists);
|
|
expect(fileSystem.file('/foo/bar/devfs/hello').readAsStringSync(), 'hello');
|
|
expect(fileSystem.file('/foo/bar/devfs/goodbye'), exists);
|
|
expect(fileSystem.file('/foo/bar/devfs/goodbye').readAsStringSync(), 'goodbye');
|
|
});
|
|
|
|
testWithoutContext('Local DevFSWriter turns FileSystemException into DevFSException', () async {
|
|
final FileExceptionHandler handler = FileExceptionHandler();
|
|
final FileSystem fileSystem = MemoryFileSystem.test(opHandle: handler.opHandle);
|
|
final LocalDevFSWriter writer = LocalDevFSWriter(fileSystem: fileSystem);
|
|
final File file = fileSystem.file('foo');
|
|
handler.addError(file, FileSystemOp.read, const FileSystemException('foo'));
|
|
|
|
await expectLater(() async => writer.write(<Uri, DevFSContent>{
|
|
Uri.parse('goodbye'): DevFSFileContent(file),
|
|
}, Uri.parse('/foo/bar/devfs/')), throwsA(isA<DevFSException>()));
|
|
});
|
|
|
|
testWithoutContext('DevFS correctly records the elapsed time', () async {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
// final FakeDevFSWriter writer = FakeDevFSWriter();
|
|
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
|
|
requests: <VmServiceExpectation>[createDevFSRequest],
|
|
httpAddress: Uri.parse('http://localhost'),
|
|
);
|
|
|
|
final DevFS devFS = DevFS(
|
|
fakeVmServiceHost.vmService,
|
|
'test',
|
|
fileSystem.currentDirectory,
|
|
fileSystem: fileSystem,
|
|
logger: BufferLogger.test(),
|
|
osUtils: FakeOperatingSystemUtils(),
|
|
httpClient: FakeHttpClient.any(),
|
|
stopwatchFactory: FakeStopwatchFactory(stopwatches: <String, Stopwatch>{
|
|
'compile': FakeStopwatch()..elapsed = const Duration(seconds: 3),
|
|
'transfer': FakeStopwatch()..elapsed = const Duration(seconds: 5),
|
|
}),
|
|
);
|
|
|
|
await devFS.create();
|
|
|
|
final FakeResidentCompiler residentCompiler = FakeResidentCompiler();
|
|
residentCompiler.onRecompile = (Uri mainUri, List<Uri>? invalidatedFiles) async {
|
|
fileSystem.file('lib/foo.txt.dill').createSync(recursive: true);
|
|
return const CompilerOutput('lib/foo.txt.dill', 0, <Uri>[]);
|
|
};
|
|
|
|
final UpdateFSReport report = await devFS.update(
|
|
mainUri: Uri.parse('lib/main.dart'),
|
|
generator: residentCompiler,
|
|
dillOutputPath: 'lib/foo.dill',
|
|
pathToReload: 'lib/foo.txt.dill',
|
|
trackWidgetCreation: false,
|
|
invalidatedFiles: <Uri>[],
|
|
packageConfig: PackageConfig.empty,
|
|
shaderCompiler: const FakeShaderCompiler(),
|
|
);
|
|
|
|
expect(report.success, true);
|
|
expect(report.compileDuration, const Duration(seconds: 3));
|
|
expect(report.transferDuration, const Duration(seconds: 5));
|
|
});
|
|
|
|
|
|
testUsingContext('DevFS actually starts compile before processing bundle', () async {
|
|
final FileSystem fileSystem = MemoryFileSystem.test();
|
|
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
|
|
requests: <VmServiceExpectation>[createDevFSRequest],
|
|
httpAddress: Uri.parse('http://localhost'),
|
|
);
|
|
|
|
final LoggingLogger logger = LoggingLogger();
|
|
|
|
final DevFS devFS = DevFS(
|
|
fakeVmServiceHost.vmService,
|
|
'test',
|
|
fileSystem.currentDirectory,
|
|
fileSystem: fileSystem,
|
|
logger: logger,
|
|
osUtils: FakeOperatingSystemUtils(),
|
|
httpClient: FakeHttpClient.any(),
|
|
);
|
|
|
|
await devFS.create();
|
|
|
|
final MemoryIOSink frontendServerStdIn = MemoryIOSink();
|
|
Stream<List<int>> frontendServerStdOut() async* {
|
|
int processed = 0;
|
|
while (true) {
|
|
while (frontendServerStdIn.writes.length == processed) {
|
|
await Future<dynamic>.delayed(const Duration(milliseconds: 5));
|
|
}
|
|
|
|
String? boundaryKey;
|
|
while (processed < frontendServerStdIn.writes.length) {
|
|
final List<int> data = frontendServerStdIn.writes[processed];
|
|
final String stringData = utf8.decode(data);
|
|
if (stringData.startsWith('compile ')) {
|
|
yield utf8.encode('result abc1\nline1\nline2\nabc1\nabc1 lib/foo.txt.dill 0\n');
|
|
} else if (stringData.startsWith('recompile ')) {
|
|
final String line = stringData.split('\n').first;
|
|
final int spaceDelim = line.lastIndexOf(' ');
|
|
boundaryKey = line.substring(spaceDelim + 1);
|
|
} else if (boundaryKey != null && stringData.startsWith(boundaryKey)) {
|
|
yield utf8.encode('result abc2\nline1\nline2\nabc2\nabc2 lib/foo.txt.dill 0\n');
|
|
} else {
|
|
throw Exception('Saw $data ($stringData)');
|
|
}
|
|
processed++;
|
|
}
|
|
}
|
|
}
|
|
Stream<List<int>> frontendServerStdErr() async* {
|
|
// Output nothing on stderr.
|
|
}
|
|
|
|
final AnsweringFakeProcessManager fakeProcessManager = AnsweringFakeProcessManager(frontendServerStdOut(), frontendServerStdErr(), frontendServerStdIn);
|
|
final StdoutHandler generatorStdoutHandler = StdoutHandler(logger: testLogger, fileSystem: fileSystem);
|
|
|
|
final DefaultResidentCompiler residentCompiler = DefaultResidentCompiler(
|
|
'sdkroot',
|
|
buildMode: BuildMode.debug,
|
|
logger: logger,
|
|
processManager: fakeProcessManager,
|
|
artifacts: Artifacts.test(),
|
|
platform: FakePlatform(),
|
|
fileSystem: fileSystem,
|
|
stdoutHandler: generatorStdoutHandler,
|
|
);
|
|
|
|
fileSystem.file('lib/foo.txt.dill').createSync(recursive: true);
|
|
|
|
final UpdateFSReport report1 = await devFS.update(
|
|
mainUri: Uri.parse('lib/main.dart'),
|
|
generator: residentCompiler,
|
|
dillOutputPath: 'lib/foo.dill',
|
|
pathToReload: 'lib/foo.txt.dill',
|
|
trackWidgetCreation: false,
|
|
invalidatedFiles: <Uri>[],
|
|
packageConfig: PackageConfig.empty,
|
|
bundle: FakeBundle(),
|
|
shaderCompiler: const FakeShaderCompiler(),
|
|
);
|
|
expect(report1.success, true);
|
|
logger.messages.clear();
|
|
|
|
final UpdateFSReport report2 = await devFS.update(
|
|
mainUri: Uri.parse('lib/main.dart'),
|
|
generator: residentCompiler,
|
|
dillOutputPath: 'lib/foo.dill',
|
|
pathToReload: 'lib/foo.txt.dill',
|
|
trackWidgetCreation: false,
|
|
invalidatedFiles: <Uri>[],
|
|
packageConfig: PackageConfig.empty,
|
|
bundle: FakeBundle(),
|
|
shaderCompiler: const FakeShaderCompiler(),
|
|
);
|
|
expect(report2.success, true);
|
|
|
|
final int processingBundleIndex = logger.messages.indexOf('Processing bundle.');
|
|
final int bundleProcessingDoneIndex = logger.messages.indexOf('Bundle processing done.');
|
|
final int compileLibMainIndex = logger.messages.indexWhere((String element) => element.startsWith('<- recompile lib/main.dart '));
|
|
expect(processingBundleIndex, greaterThanOrEqualTo(0));
|
|
expect(bundleProcessingDoneIndex, greaterThanOrEqualTo(0));
|
|
expect(compileLibMainIndex, greaterThanOrEqualTo(0));
|
|
expect(bundleProcessingDoneIndex, greaterThan(compileLibMainIndex));
|
|
});
|
|
|
|
group('Shader compilation', () {
|
|
late FileSystem fileSystem;
|
|
late ProcessManager processManager;
|
|
|
|
setUp(() {
|
|
fileSystem = MemoryFileSystem.test();
|
|
processManager = FakeProcessManager.any();
|
|
});
|
|
|
|
testUsingContext('DevFS recompiles shaders', () async {
|
|
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
|
|
requests: <VmServiceExpectation>[createDevFSRequest],
|
|
httpAddress: Uri.parse('http://localhost'),
|
|
);
|
|
final BufferLogger logger = BufferLogger.test();
|
|
final DevFS devFS = DevFS(
|
|
fakeVmServiceHost.vmService,
|
|
'test',
|
|
fileSystem.currentDirectory,
|
|
fileSystem: fileSystem,
|
|
logger: logger,
|
|
osUtils: FakeOperatingSystemUtils(),
|
|
httpClient: FakeHttpClient.any(),
|
|
);
|
|
|
|
await devFS.create();
|
|
|
|
final FakeResidentCompiler residentCompiler = FakeResidentCompiler()
|
|
..onRecompile = (Uri mainUri, List<Uri>? invalidatedFiles) async {
|
|
fileSystem.file('lib/foo.dill')
|
|
..createSync(recursive: true)
|
|
..writeAsBytesSync(<int>[1, 2, 3, 4, 5]);
|
|
return const CompilerOutput('lib/foo.dill', 0, <Uri>[]);
|
|
};
|
|
final FakeBundle bundle = FakeBundle()
|
|
..entries['foo.frag'] = DevFSByteContent(<int>[1, 2, 3, 4])
|
|
..entries['not.frag'] = DevFSByteContent(<int>[1, 2, 3, 4])
|
|
..entryKinds['foo.frag'] = AssetKind.shader;
|
|
|
|
final UpdateFSReport report = await devFS.update(
|
|
mainUri: Uri.parse('lib/main.dart'),
|
|
generator: residentCompiler,
|
|
dillOutputPath: 'lib/foo.dill',
|
|
pathToReload: 'lib/foo.txt.dill',
|
|
trackWidgetCreation: false,
|
|
invalidatedFiles: <Uri>[],
|
|
packageConfig: PackageConfig.empty,
|
|
shaderCompiler: const FakeShaderCompiler(),
|
|
bundle: bundle,
|
|
);
|
|
|
|
expect(report.success, true);
|
|
expect(devFS.shaderPathsToEvict, <String>{'foo.frag'});
|
|
expect(devFS.assetPathsToEvict, <String>{'not.frag'});
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => fileSystem,
|
|
ProcessManager: () => processManager,
|
|
});
|
|
|
|
testUsingContext('DevFS tracks when FontManifest is updated', () async {
|
|
final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(
|
|
requests: <VmServiceExpectation>[createDevFSRequest],
|
|
httpAddress: Uri.parse('http://localhost'),
|
|
);
|
|
final BufferLogger logger = BufferLogger.test();
|
|
final DevFS devFS = DevFS(
|
|
fakeVmServiceHost.vmService,
|
|
'test',
|
|
fileSystem.currentDirectory,
|
|
fileSystem: fileSystem,
|
|
logger: logger,
|
|
osUtils: FakeOperatingSystemUtils(),
|
|
httpClient: FakeHttpClient.any(),
|
|
);
|
|
|
|
await devFS.create();
|
|
|
|
expect(devFS.didUpdateFontManifest, false);
|
|
|
|
final FakeResidentCompiler residentCompiler = FakeResidentCompiler()
|
|
..onRecompile = (Uri mainUri, List<Uri>? invalidatedFiles) async {
|
|
fileSystem.file('lib/foo.dill')
|
|
..createSync(recursive: true)
|
|
..writeAsBytesSync(<int>[1, 2, 3, 4, 5]);
|
|
return const CompilerOutput('lib/foo.dill', 0, <Uri>[]);
|
|
};
|
|
final FakeBundle bundle = FakeBundle()
|
|
..entries['FontManifest.json'] = DevFSByteContent(<int>[1, 2, 3, 4]);
|
|
|
|
final UpdateFSReport report = await devFS.update(
|
|
mainUri: Uri.parse('lib/main.dart'),
|
|
generator: residentCompiler,
|
|
dillOutputPath: 'lib/foo.dill',
|
|
pathToReload: 'lib/foo.txt.dill',
|
|
trackWidgetCreation: false,
|
|
invalidatedFiles: <Uri>[],
|
|
packageConfig: PackageConfig.empty,
|
|
shaderCompiler: const FakeShaderCompiler(),
|
|
bundle: bundle,
|
|
);
|
|
|
|
expect(report.success, true);
|
|
expect(devFS.shaderPathsToEvict, <String>{});
|
|
expect(devFS.assetPathsToEvict, <String>{'FontManifest.json'});
|
|
expect(devFS.didUpdateFontManifest, true);
|
|
}, overrides: <Type, Generator>{
|
|
FileSystem: () => fileSystem,
|
|
ProcessManager: () => processManager,
|
|
});
|
|
});
|
|
}
|
|
|
|
class FakeResidentCompiler extends Fake implements ResidentCompiler {
|
|
Future<CompilerOutput> Function(Uri mainUri, List<Uri>? invalidatedFiles)? onRecompile;
|
|
|
|
@override
|
|
Future<CompilerOutput> recompile(
|
|
Uri mainUri,
|
|
List<Uri>? invalidatedFiles, {
|
|
String? outputPath,
|
|
PackageConfig? packageConfig,
|
|
String? projectRootPath,
|
|
FileSystem? fs,
|
|
bool suppressErrors = false,
|
|
bool checkDartPluginRegistry = false,
|
|
File? dartPluginRegistrant,
|
|
Uri? nativeAssetsYaml,
|
|
}) {
|
|
return onRecompile?.call(mainUri, invalidatedFiles)
|
|
?? Future<CompilerOutput>.value(const CompilerOutput('', 1, <Uri>[]));
|
|
}
|
|
}
|
|
|
|
class FakeDevFSWriter implements DevFSWriter {
|
|
bool written = false;
|
|
|
|
@override
|
|
Future<void> write(Map<Uri, DevFSContent> entries, Uri baseUri, DevFSWriter parent) async {
|
|
written = true;
|
|
}
|
|
}
|
|
|
|
class FakeBundle extends AssetBundle {
|
|
@override
|
|
List<File> get additionalDependencies => <File>[];
|
|
|
|
@override
|
|
Future<int> build({String manifestPath = defaultManifestPath, String? assetDirPath, String? packagesPath, bool deferredComponentsEnabled = false, TargetPlatform? targetPlatform}) async {
|
|
return 0;
|
|
}
|
|
|
|
@override
|
|
Map<String, Map<String, DevFSContent>> get deferredComponentsEntries => <String, Map<String, DevFSContent>>{};
|
|
|
|
@override
|
|
final Map<String, DevFSContent> entries = <String, DevFSContent>{};
|
|
|
|
@override
|
|
final Map<String, AssetKind> entryKinds = <String, AssetKind>{};
|
|
|
|
@override
|
|
List<File> get inputFiles => <File>[];
|
|
|
|
@override
|
|
bool needsBuild({String manifestPath = defaultManifestPath}) {
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
bool wasBuiltOnce() {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
class AnsweringFakeProcessManager implements ProcessManager {
|
|
AnsweringFakeProcessManager(this.stdout, this.stderr, this.stdin);
|
|
|
|
final Stream<List<int>> stdout;
|
|
final Stream<List<int>> stderr;
|
|
final IOSink stdin;
|
|
|
|
@override
|
|
bool canRun(dynamic executable, {String? workingDirectory}) {
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
bool killPid(int pid, [io.ProcessSignal signal = io.ProcessSignal.sigterm]) {
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
Future<ProcessResult> run(List<Object> command, {String? workingDirectory, Map<String, String>? environment, bool includeParentEnvironment = true, bool runInShell = false, Encoding? stdoutEncoding = systemEncoding, Encoding? stderrEncoding = systemEncoding}) async {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
ProcessResult runSync(List<Object> command, {String? workingDirectory, Map<String, String>? environment, bool includeParentEnvironment = true, bool runInShell = false, Encoding? stdoutEncoding = systemEncoding, Encoding? stderrEncoding = systemEncoding}) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<Process> start(List<Object> command, {String? workingDirectory, Map<String, String>? environment, bool includeParentEnvironment = true, bool runInShell = false, ProcessStartMode mode = ProcessStartMode.normal}) async {
|
|
return AnsweringFakeProcess(stdout, stderr, stdin);
|
|
}
|
|
}
|
|
|
|
class AnsweringFakeProcess implements io.Process {
|
|
AnsweringFakeProcess(this.stdout,this.stderr, this.stdin);
|
|
|
|
@override
|
|
final Stream<List<int>> stdout;
|
|
@override
|
|
final Stream<List<int>> stderr;
|
|
@override
|
|
final IOSink stdin;
|
|
|
|
@override
|
|
Future<int> get exitCode async => 0;
|
|
|
|
@override
|
|
bool kill([io.ProcessSignal signal = io.ProcessSignal.sigterm]) {
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
int get pid => 42;
|
|
}
|
|
|
|
class FakeShaderCompiler implements DevelopmentShaderCompiler {
|
|
const FakeShaderCompiler();
|
|
|
|
@override
|
|
void configureCompiler(
|
|
TargetPlatform? platform, {
|
|
required ImpellerStatus impellerStatus,
|
|
}) { }
|
|
|
|
@override
|
|
Future<DevFSContent> recompileShader(DevFSContent inputShader) async {
|
|
return DevFSByteContent(await inputShader.contentsAsBytes());
|
|
}
|
|
}
|