Unverified Commit 9431229e authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove globals from depfile usage (#50710)

parent 8769f94c
......@@ -2,22 +2,54 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:meta/meta.dart';
import 'package:platform/platform.dart';
import '../base/file_system.dart';
import '../globals.dart' as globals;
import '../base/logger.dart';
/// A class for representing depfile formats.
class Depfile {
/// Create a [Depfile] from a list of [input] files and [output] files.
const Depfile(this.inputs, this.outputs);
/// A service for creating and parsing [Depfile]s.
class DepfileService {
DepfileService({
@required Logger logger,
@required FileSystem fileSystem,
@required Platform platform,
}) : _logger = logger,
_fileSystem = fileSystem,
_platform = platform;
final Logger _logger;
final FileSystem _fileSystem;
final Platform _platform;
static final RegExp _separatorExpr = RegExp(r'([^\\]) ');
static final RegExp _escapeExpr = RegExp(r'\\(.)');
/// Given an [depfile] File, write the depfile contents.
///
/// If either [inputs] or [outputs] is empty, ensures the file does not
/// exist.
void writeToFile(Depfile depfile, File output) {
if (depfile.inputs.isEmpty || depfile.outputs.isEmpty) {
if (output.existsSync()) {
output.deleteSync();
}
return;
}
final StringBuffer buffer = StringBuffer();
_writeFilesToBuffer(depfile.outputs, buffer);
buffer.write(': ');
_writeFilesToBuffer(depfile.inputs, buffer);
output.writeAsStringSync(buffer.toString());
}
/// Parse the depfile contents from [file].
///
/// If the syntax is invalid, returns an empty [Depfile].
factory Depfile.parse(File file) {
Depfile parse(File file) {
final String contents = file.readAsStringSync();
final List<String> colonSeparated = contents.split(': ');
if (colonSeparated.length != 2) {
globals.printError('Invalid depfile: ${file.path}');
_logger.printError('Invalid depfile: ${file.path}');
return const Depfile(<File>[], <File>[]);
}
final List<File> inputs = _processList(colonSeparated[1].trim());
......@@ -25,11 +57,12 @@ class Depfile {
return Depfile(inputs, outputs);
}
/// Parse the output of dart2js's used dependencies.
///
/// The [file] contains a list of newline separated file URIs. The output
/// file must be manually specified.
factory Depfile.parseDart2js(File file, File output) {
Depfile parseDart2js(File file, File output) {
final List<File> inputs = <File>[];
for (final String rawUri in file.readAsLinesSync()) {
if (rawUri.trim().isEmpty) {
......@@ -42,38 +75,14 @@ class Depfile {
if (fileUri.scheme != 'file') {
continue;
}
inputs.add(globals.fs.file(fileUri));
inputs.add(_fileSystem.file(fileUri));
}
return Depfile(inputs, <File>[output]);
}
/// The input files for this depfile.
final List<File> inputs;
/// The output files for this depfile.
final List<File> outputs;
/// Given an [depfile] File, write the depfile contents.
///
/// If either [inputs] or [outputs] is empty, ensures the file does not
/// exist.
void writeToFile(File depfile) {
if (inputs.isEmpty || outputs.isEmpty) {
if (depfile.existsSync()) {
depfile.deleteSync();
}
return;
}
final StringBuffer buffer = StringBuffer();
_writeFilesToBuffer(outputs, buffer);
buffer.write(': ');
_writeFilesToBuffer(inputs, buffer);
depfile.writeAsStringSync(buffer.toString());
}
void _writeFilesToBuffer(List<File> files, StringBuffer buffer) {
for (final File outputFile in files) {
if (globals.platform.isWindows) {
if (_platform.isWindows) {
// Foward slashes and spaces in a depfile have to be escaped on windows.
final String path = outputFile.path
.replaceAll(r'\', r'\\')
......@@ -87,10 +96,7 @@ class Depfile {
}
}
static final RegExp _separatorExpr = RegExp(r'([^\\]) ');
static final RegExp _escapeExpr = RegExp(r'\\(.)');
static List<File> _processList(String rawText) {
List<File> _processList(String rawText) {
return rawText
// Put every file on right-hand side on the separate line
.replaceAllMapped(_separatorExpr, (Match match) => '${match.group(1)}\n')
......@@ -101,7 +107,19 @@ class Depfile {
// The tool doesn't write duplicates to these lists. This call is an attempt to
// be resillient to the outputs of other tools which write or user edits to depfiles.
.toSet()
.map((String path) => globals.fs.file(path))
.map(_fileSystem.file)
.toList();
}
}
/// A class for representing depfile formats.
class Depfile {
/// Create a [Depfile] from a list of [input] files and [output] files.
const Depfile(this.inputs, this.outputs);
/// The input files for this depfile.
final List<File> inputs;
/// The output files for this depfile.
final List<File> outputs;
}
......@@ -63,7 +63,15 @@ abstract class AndroidAssetBundle extends Target {
}
if (_copyAssets) {
final Depfile assetDepfile = await copyAssets(environment, outputDirectory);
assetDepfile.writeToFile(environment.buildDir.childFile('flutter_assets.d'));
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
depfileService.writeToFile(
assetDepfile,
environment.buildDir.childFile('flutter_assets.d'),
);
}
}
......
......@@ -108,7 +108,15 @@ class CopyAssets extends Target {
.childDirectory('flutter_assets');
output.createSync(recursive: true);
final Depfile depfile = await copyAssets(environment, output);
depfile.writeToFile(environment.buildDir.childFile('flutter_assets.d'));
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
depfileService.writeToFile(
depfile,
environment.buildDir.childFile('flutter_assets.d'),
);
}
}
......
......@@ -114,7 +114,15 @@ class CopyFlutterBundle extends Target {
.copySync(environment.outputDir.childFile('isolate_snapshot_data').path);
}
final Depfile assetDepfile = await copyAssets(environment, environment.outputDir);
assetDepfile.writeToFile(environment.buildDir.childFile('flutter_assets.d'));
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
depfileService.writeToFile(
assetDepfile,
environment.buildDir.childFile('flutter_assets.d'),
);
}
@override
......
......@@ -284,7 +284,15 @@ abstract class IosAssetBundle extends Target {
// Copy the assets.
final Depfile assetDepfile = await copyAssets(environment, assetDirectory);
assetDepfile.writeToFile(environment.buildDir.childFile('flutter_assets.d'));
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
depfileService.writeToFile(
assetDepfile,
environment.buildDir.childFile('flutter_assets.d'),
);
// Copy the plist from either the project or module.
......
......@@ -98,7 +98,15 @@ class UnpackLinuxDebug extends Target {
}
}
final Depfile depfile = Depfile(inputs, outputs);
depfile.writeToFile(environment.buildDir.childFile('linux_engine_sources.d'));
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
depfileService.writeToFile(
depfile,
environment.buildDir.childFile('linux_engine_sources.d'),
);
}
}
......@@ -151,6 +159,14 @@ class DebugBundleLinuxAssets extends Target {
.copySync(outputDirectory.childFile('kernel_blob.bin').path);
}
final Depfile depfile = await copyAssets(environment, outputDirectory);
depfile.writeToFile(environment.buildDir.childFile('flutter_assets.d'));
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
depfileService.writeToFile(
depfile,
environment.buildDir.childFile('flutter_assets.d'),
);
}
}
......@@ -286,7 +286,15 @@ abstract class MacOSBundleFlutterAssets extends Target {
.childDirectory('flutter_assets');
assetDirectory.createSync(recursive: true);
final Depfile depfile = await copyAssets(environment, assetDirectory);
depfile.writeToFile(environment.buildDir.childFile('flutter_assets.d'));
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
depfileService.writeToFile(
depfile,
environment.buildDir.childFile('flutter_assets.d'),
);
// Copy Info.plist template.
assetDirectory.parent.childFile('Info.plist')
......
......@@ -191,11 +191,19 @@ class Dart2JSTarget extends Target {
'${dart2jsDeps.path}');
return;
}
final Depfile depfile = Depfile.parseDart2js(
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
final Depfile depfile = depfileService.parseDart2js(
environment.buildDir.childFile('main.dart.js.deps'),
outputFile,
);
depfile.writeToFile(environment.buildDir.childFile('dart2js.d'));
depfileService.writeToFile(
depfile,
environment.buildDir.childFile('dart2js.d'),
);
}
}
......@@ -247,7 +255,15 @@ class WebReleaseBundle extends Target {
final Directory outputDirectory = environment.outputDir.childDirectory('assets');
outputDirectory.createSync(recursive: true);
final Depfile depfile = await copyAssets(environment, environment.outputDir.childDirectory('assets'));
depfile.writeToFile(environment.buildDir.childFile('flutter_assets.d'));
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
depfileService.writeToFile(
depfile,
environment.buildDir.childFile('flutter_assets.d'),
);
final Directory webResources = environment.projectDir
.childDirectory('web');
......@@ -269,8 +285,10 @@ class WebReleaseBundle extends Target {
outputResourcesFiles.add(outputFile);
}
final Depfile resourceFile = Depfile(inputResourceFiles, outputResourcesFiles);
resourceFile.writeToFile(environment.buildDir.childFile('web_resources.d'));
depfileService.writeToFile(
resourceFile,
environment.buildDir.childFile('web_resources.d'),
);
}
}
......@@ -320,7 +338,15 @@ class WebServiceWorker extends Target {
final String serviceWorker = generateServiceWorker(uriToHash);
serviceWorkerFile
.writeAsStringSync(serviceWorker);
depfile.writeToFile(environment.buildDir.childFile('service_worker.d'));
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
depfileService.writeToFile(
depfile,
environment.buildDir.childFile('service_worker.d'),
);
}
}
......
......@@ -149,7 +149,12 @@ Future<void> buildWithAssemble({
if (!outputDepfile.parent.existsSync()) {
outputDepfile.parent.createSync(recursive: true);
}
depfile.writeToFile(outputDepfile);
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
depfileService.writeToFile(depfile, outputDepfile);
}
}
......
......@@ -202,7 +202,12 @@ class AssembleCommand extends FlutterCommand {
if (argResults.wasParsed('depfile')) {
final File depfileFile = globals.fs.file(stringArg('depfile'));
final Depfile depfile = Depfile(result.inputFiles, result.outputFiles);
depfile.writeToFile(globals.fs.file(depfileFile));
final DepfileService depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
depfileService.writeToFile(depfile, globals.fs.file(depfileFile));
}
return FlutterCommandResult.success();
}
......
......@@ -24,6 +24,7 @@ void main() {
Environment environment;
MockPlatform mockPlatform;
MockPlatform mockWindowsPlatform;
DepfileService depfileService;
setUp(() {
mockPlatform = MockPlatform();
......@@ -52,6 +53,11 @@ void main() {
defines: <String, String>{
kTargetFile: globals.fs.path.join('foo', 'lib', 'main.dart'),
}
);
depfileService = DepfileService(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform,
);
environment.buildDir.createSync(recursive: true);
}, overrides: <Type, Generator>{
......@@ -315,7 +321,7 @@ void main() {
await const Dart2JSTarget().build(environment);
expect(environment.buildDir.childFile('dart2js.d').existsSync(), true);
final Depfile depfile = Depfile.parse(environment.buildDir.childFile('dart2js.d'));
final Depfile depfile = depfileService.parse(environment.buildDir.childFile('dart2js.d'));
expect(depfile.inputs.single.path, globals.fs.path.absolute('a.dart'));
expect(depfile.outputs.single.path,
......
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