[flutter_tools] remove most globals from build system and tests (#63697)

Remove globals and testbed to simplify test cases, using the existing environment configuration. This is some pre-factoring work to make landing #63610 easier
This commit is contained in:
Jonah Williams
2020-08-13 15:00:27 -07:00
committed by GitHub
parent 32ee00849c
commit 06c3de3285
8 changed files with 408 additions and 311 deletions

View File

@@ -6,7 +6,7 @@ import '../../artifacts.dart';
import '../../base/build.dart';
import '../../base/file_system.dart';
import '../../build_info.dart';
import '../../globals.dart' as globals;
import '../../globals.dart' as globals hide fs, artifacts, logger, processManager;
import '../build_system.dart';
import '../depfile.dart';
import '../exceptions.dart';
@@ -52,13 +52,13 @@ abstract class AndroidAssetBundle extends Target {
// Only copy the prebuilt runtimes and kernel blob in debug mode.
if (buildMode == BuildMode.debug) {
final String vmSnapshotData = globals.artifacts.getArtifactPath(Artifact.vmSnapshotData, mode: BuildMode.debug);
final String isolateSnapshotData = globals.artifacts.getArtifactPath(Artifact.isolateSnapshotData, mode: BuildMode.debug);
final String vmSnapshotData = environment.artifacts.getArtifactPath(Artifact.vmSnapshotData, mode: BuildMode.debug);
final String isolateSnapshotData = environment.artifacts.getArtifactPath(Artifact.isolateSnapshotData, mode: BuildMode.debug);
environment.buildDir.childFile('app.dill')
.copySync(outputDirectory.childFile('kernel_blob.bin').path);
globals.fs.file(vmSnapshotData)
environment.fileSystem.file(vmSnapshotData)
.copySync(outputDirectory.childFile('vm_snapshot_data').path);
globals.fs.file(isolateSnapshotData)
environment.fileSystem.file(isolateSnapshotData)
.copySync(outputDirectory.childFile('isolate_snapshot_data').path);
}
if (_copyAssets) {
@@ -68,8 +68,8 @@ abstract class AndroidAssetBundle extends Target {
targetPlatform: TargetPlatform.android,
);
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
fileSystem: environment.fileSystem,
logger: environment.logger,
);
depfileService.writeToFile(
assetDepfile,
@@ -215,11 +215,11 @@ class AndroidAot extends AotElfBase {
Future<void> build(Environment environment) async {
final AOTSnapshotter snapshotter = AOTSnapshotter(
reportTimings: false,
fileSystem: globals.fs,
logger: globals.logger,
fileSystem: environment.fileSystem,
logger: environment.logger,
xcode: globals.xcode,
processManager: globals.processManager,
artifacts: globals.artifacts,
processManager: environment.processManager,
artifacts: environment.artifacts,
);
final Directory output = environment.buildDir.childDirectory(_androidAbiName);
final String splitDebugInfo = environment.defines[kSplitDebugInfo];

View File

@@ -11,7 +11,6 @@ import '../../base/logger.dart';
import '../../build_info.dart';
import '../../convert.dart';
import '../../devfs.dart';
import '../../globals.dart' as globals;
import '../build_system.dart';
import '../depfile.dart';
import 'common.dart';
@@ -44,10 +43,12 @@ Future<Depfile> copyAssets(Environment environment, Directory outputDirectory, {
);
final File pubspecFile = environment.projectDir.childFile('pubspec.yaml');
final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();
// Only the default asset bundle style is supported in assemble.
final AssetBundle assetBundle = AssetBundleFactory.defaultInstance.createBundle();
final int resultCode = await assetBundle.build(
manifestPath: pubspecFile.path,
packagesPath: environment.projectDir.childFile('.packages').path,
assetDirPath: null,
);
if (resultCode != 0) {
throw Exception('Failed to bundle asset files.');
@@ -63,10 +64,10 @@ Future<Depfile> copyAssets(Environment environment, Directory outputDirectory, {
final IconTreeShaker iconTreeShaker = IconTreeShaker(
environment,
assetBundle.entries[kFontManifestJson] as DevFSStringContent,
processManager: globals.processManager,
logger: globals.logger,
fileSystem: globals.fs,
artifacts: globals.artifacts,
processManager: environment.processManager,
logger: environment.logger,
fileSystem: environment.fileSystem,
artifacts: environment.artifacts,
);
final Map<String, DevFSContent> assetEntries = <String, DevFSContent>{
@@ -85,7 +86,8 @@ Future<Depfile> copyAssets(Environment environment, Directory outputDirectory, {
// to `%23.ext`. However, we have to keep it this way since the
// platform channels in the framework will URI encode these values,
// and the native APIs will look for files this way.
final File file = globals.fs.file(globals.fs.path.join(outputDirectory.path, entry.key));
final File file = environment.fileSystem.file(
environment.fileSystem.path.join(outputDirectory.path, entry.key));
outputs.add(file);
file.parent.createSync(recursive: true);
final DevFSContent content = entry.value;
@@ -222,8 +224,8 @@ class CopyAssets extends Target {
targetPlatform: TargetPlatform.android,
);
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
fileSystem: environment.fileSystem,
logger: environment.logger,
);
depfileService.writeToFile(
depfile,

View File

@@ -10,8 +10,7 @@ import '../../base/file_system.dart';
import '../../build_info.dart';
import '../../compile.dart';
import '../../dart/package_map.dart';
import '../../globals.dart' as globals;
import '../../project.dart';
import '../../globals.dart' as globals hide fs, processManager, artifacts, logger;
import '../build_system.dart';
import '../depfile.dart';
import '../exceptions.dart';
@@ -107,13 +106,13 @@ class CopyFlutterBundle extends Target {
// Only copy the prebuilt runtimes and kernel blob in debug mode.
if (buildMode == BuildMode.debug) {
final String vmSnapshotData = globals.artifacts.getArtifactPath(Artifact.vmSnapshotData, mode: BuildMode.debug);
final String isolateSnapshotData = globals.artifacts.getArtifactPath(Artifact.isolateSnapshotData, mode: BuildMode.debug);
final String vmSnapshotData = environment.artifacts.getArtifactPath(Artifact.vmSnapshotData, mode: BuildMode.debug);
final String isolateSnapshotData = environment.artifacts.getArtifactPath(Artifact.isolateSnapshotData, mode: BuildMode.debug);
environment.buildDir.childFile('app.dill')
.copySync(environment.outputDir.childFile('kernel_blob.bin').path);
globals.fs.file(vmSnapshotData)
environment.fileSystem.file(vmSnapshotData)
.copySync(environment.outputDir.childFile('vm_snapshot_data').path);
globals.fs.file(isolateSnapshotData)
environment.fileSystem.file(isolateSnapshotData)
.copySync(environment.outputDir.childFile('isolate_snapshot_data').path);
}
final Depfile assetDepfile = await copyAssets(
@@ -122,8 +121,8 @@ class CopyFlutterBundle extends Target {
targetPlatform: TargetPlatform.android,
);
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
fileSystem: environment.fileSystem,
logger: environment.logger,
);
depfileService.writeToFile(
assetDepfile,
@@ -191,8 +190,11 @@ class KernelSnapshot extends Target {
@override
Future<void> build(Environment environment) async {
final KernelCompiler compiler = await kernelCompilerFactory.create(
FlutterProject.fromDirectory(environment.projectDir),
final KernelCompiler compiler = KernelCompiler(
fileSystem: environment.fileSystem,
logger: environment.logger,
processManager: environment.processManager,
artifacts: environment.artifacts,
);
if (environment.defines[kBuildMode] == null) {
throw MissingDefineException(kBuildMode, 'kernel_snapshot');
@@ -201,9 +203,9 @@ class KernelSnapshot extends Target {
throw MissingDefineException(kTargetPlatform, 'kernel_snapshot');
}
final BuildMode buildMode = getBuildModeForName(environment.defines[kBuildMode]);
final String targetFile = environment.defines[kTargetFile] ?? globals.fs.path.join('lib', 'main.dart');
final String targetFile = environment.defines[kTargetFile] ?? environment.fileSystem.path.join('lib', 'main.dart');
final File packagesFile = environment.projectDir.childFile('.packages');
final String targetFileAbsolute = globals.fs.file(targetFile).absolute.path;
final String targetFileAbsolute = environment.fileSystem.file(targetFile).absolute.path;
// everything besides 'false' is considered to be enabled.
final bool trackWidgetCreation = environment.defines[kTrackWidgetCreation] != 'false';
final TargetPlatform targetPlatform = getTargetPlatformForName(environment.defines[kTargetPlatform]);
@@ -238,7 +240,7 @@ class KernelSnapshot extends Target {
);
final CompilerOutput output = await compiler.compile(
sdkRoot: globals.artifacts.getArtifactPath(
sdkRoot: environment.artifacts.getArtifactPath(
Artifact.flutterPatchedSdkPath,
platform: targetPlatform,
mode: buildMode,
@@ -275,11 +277,11 @@ abstract class AotElfBase extends Target {
Future<void> build(Environment environment) async {
final AOTSnapshotter snapshotter = AOTSnapshotter(
reportTimings: false,
fileSystem: globals.fs,
logger: globals.logger,
fileSystem: environment.fileSystem,
logger: environment.logger,
xcode: globals.xcode,
processManager: globals.processManager,
artifacts: globals.artifacts,
processManager: environment.processManager,
artifacts: environment.artifacts,
);
final String outputPath = environment.buildDir.path;
if (environment.defines[kBuildMode] == null) {

View File

@@ -9,7 +9,7 @@ import '../../base/file_system.dart';
import '../../base/io.dart';
import '../../base/process.dart';
import '../../build_info.dart';
import '../../globals.dart' as globals;
import '../../globals.dart' as globals hide fs, logger, processManager, artifacts;
import '../../macos/xcode.dart';
import '../../project.dart';
import '../build_system.dart';
@@ -33,11 +33,11 @@ abstract class AotAssemblyBase extends Target {
Future<void> build(Environment environment) async {
final AOTSnapshotter snapshotter = AOTSnapshotter(
reportTimings: false,
fileSystem: globals.fs,
logger: globals.logger,
fileSystem: environment.fileSystem,
logger: environment.logger,
xcode: globals.xcode,
artifacts: globals.artifacts,
processManager: globals.processManager,
artifacts: environment.artifacts,
processManager: environment.processManager,
);
final String buildOutputPath = environment.buildDir.path;
if (environment.defines[kBuildMode] == null) {
@@ -76,7 +76,7 @@ abstract class AotAssemblyBase extends Target {
buildMode: buildMode,
mainPath: environment.buildDir.childFile('app.dill').path,
packagesPath: environment.projectDir.childFile('.packages').path,
outputPath: globals.fs.path.join(buildOutputPath, getNameForDarwinArch(iosArch)),
outputPath: environment.fileSystem.path.join(buildOutputPath, getNameForDarwinArch(iosArch)),
darwinArch: iosArch,
bitcode: bitcode,
quiet: true,
@@ -89,12 +89,12 @@ abstract class AotAssemblyBase extends Target {
if (results.any((int result) => result != 0)) {
throw Exception('AOT snapshotter exited with code ${results.join()}');
}
final String resultPath = globals.fs.path.join(environment.buildDir.path, 'App.framework', 'App');
globals.fs.directory(resultPath).parent.createSync(recursive: true);
final ProcessResult result = await globals.processManager.run(<String>[
final String resultPath = environment.fileSystem.path.join(environment.buildDir.path, 'App.framework', 'App');
environment.fileSystem.directory(resultPath).parent.createSync(recursive: true);
final ProcessResult result = await environment.processManager.run(<String>[
'lipo',
...iosArchs.map((DarwinArch iosArch) =>
globals.fs.path.join(buildOutputPath, getNameForDarwinArch(iosArch), 'App.framework', 'App')),
environment.fileSystem.path.join(buildOutputPath, getNameForDarwinArch(iosArch), 'App.framework', 'App')),
'-create',
'-output',
resultPath,
@@ -294,13 +294,13 @@ abstract class IosAssetBundle extends Target {
.childFile('App')
.copySync(frameworkDirectory.childFile('App').path);
final String vmSnapshotData = globals.artifacts.getArtifactPath(Artifact.vmSnapshotData, mode: BuildMode.debug);
final String isolateSnapshotData = globals.artifacts.getArtifactPath(Artifact.isolateSnapshotData, mode: BuildMode.debug);
final String vmSnapshotData = environment.artifacts.getArtifactPath(Artifact.vmSnapshotData, mode: BuildMode.debug);
final String isolateSnapshotData = environment.artifacts.getArtifactPath(Artifact.isolateSnapshotData, mode: BuildMode.debug);
environment.buildDir.childFile('app.dill')
.copySync(assetDirectory.childFile('kernel_blob.bin').path);
globals.fs.file(vmSnapshotData)
environment.fileSystem.file(vmSnapshotData)
.copySync(assetDirectory.childFile('vm_snapshot_data').path);
globals.fs.file(isolateSnapshotData)
environment.fileSystem.file(isolateSnapshotData)
.copySync(assetDirectory.childFile('isolate_snapshot_data').path);
} else {
environment.buildDir.childDirectory('App.framework').childFile('App')
@@ -314,8 +314,8 @@ abstract class IosAssetBundle extends Target {
targetPlatform: TargetPlatform.ios,
);
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
fileSystem: environment.fileSystem,
logger: environment.logger,
);
depfileService.writeToFile(
assetDepfile,
@@ -406,7 +406,8 @@ Future<RunResult> createStubAppFramework(File outputFile, SdkType sdk, { bool in
throwToolExit('Failed to create App.framework stub at ${outputFile.path}: $e');
}
final Directory tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_stub_source.');
final Directory tempDir = outputFile.fileSystem.systemTempDirectory
.createTempSync('flutter_tools_stub_source.');
try {
final File stubSource = tempDir.childFile('debug_app.cc')
..writeAsStringSync(r'''
@@ -444,7 +445,7 @@ Future<RunResult> createStubAppFramework(File outputFile, SdkType sdk, { bool in
} finally {
try {
tempDir.deleteSync(recursive: true);
} on FileSystemException catch (_) {
} on FileSystemException {
// Best effort. Sometimes we can't delete things from system temp.
} on Exception catch (e) {
throwToolExit('Failed to create App.framework stub at ${outputFile.path}: $e');