From d356b2d77a860135ea539af5bda125e2ab0af391 Mon Sep 17 00:00:00 2001 From: "P.Y. Laligand" Date: Thu, 23 Mar 2017 09:42:58 -0700 Subject: [PATCH] Generate a depfile for assets of a flutter_app. (#8961) --- .../flutter_tools/bin/fuchsia_builder.dart | 38 ++++++++++++++++--- packages/flutter_tools/lib/src/devfs.dart | 6 +++ packages/flutter_tools/lib/src/flx.dart | 10 ++++- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/packages/flutter_tools/bin/fuchsia_builder.dart b/packages/flutter_tools/bin/fuchsia_builder.dart index ffeae8556f..6d9f2374b2 100644 --- a/packages/flutter_tools/bin/fuchsia_builder.dart +++ b/packages/flutter_tools/bin/fuchsia_builder.dart @@ -25,13 +25,17 @@ const String _kOptionOutput = 'output-file'; const String _kOptionHeader = 'header'; const String _kOptionSnapshot = 'snapshot'; const String _kOptionWorking = 'working-dir'; -const String _kOptionsManifest = 'manifest'; +const String _kOptionManifest = 'manifest'; +const String _kOptionDepFile = 'depfile'; +const String _kOptionBuildRoot = 'build-root'; const List _kRequiredOptions = const [ _kOptionPackages, _kOptionOutput, _kOptionHeader, _kOptionSnapshot, _kOptionWorking, + _kOptionDepFile, + _kOptionBuildRoot, ]; Future main(List args) async { @@ -59,23 +63,36 @@ Future run(List args) async { ..addOption(_kOptionSnapshot, help: 'The generated snapshot file') ..addOption(_kOptionWorking, help: 'The directory where to put temporary files') - ..addOption(_kOptionsManifest, help: 'The manifest file'); + ..addOption(_kOptionManifest, help: 'The manifest file') + ..addOption(_kOptionDepFile, help: 'The generated depfile') + ..addOption(_kOptionBuildRoot, help: 'The build\'s root directory'); final ArgResults argResults = parser.parse(args); - if (_kRequiredOptions.any((String option) => !argResults.options.contains(option))) { + if (_kRequiredOptions + .any((String option) => !argResults.options.contains(option))) { printError('Missing option! All options must be specified.'); exit(1); } Cache.flutterRoot = platform.environment['FLUTTER_ROOT']; final String outputPath = argResults[_kOptionOutput]; try { - await assemble( + final List dependencies = await assemble( outputPath: outputPath, snapshotFile: fs.file(argResults[_kOptionSnapshot]), workingDirPath: argResults[_kOptionWorking], packagesPath: argResults[_kOptionPackages], - manifestPath: argResults[_kOptionsManifest] ?? defaultManifestPath, + manifestPath: argResults[_kOptionManifest] ?? defaultManifestPath, includeDefaultFonts: false, ); + final String depFilePath = argResults[_kOptionDepFile]; + final int depFileResult = _createDepfile( + depFilePath, + fs.path.relative(argResults[_kOptionOutput], + from: argResults[_kOptionBuildRoot]), + dependencies); + if (depFileResult != 0) { + printError('Error creating depfile $depFilePath: $depFileResult.'); + exit(depFileResult); + } } on ToolExit catch (e) { printError(e.message); exit(e.exitCode); @@ -87,6 +104,17 @@ Future run(List args) async { exit(headerResult); } +int _createDepfile( + String depFilePath, String target, List dependencies) { + try { + final File depFile = fs.file(depFilePath); + depFile.writeAsStringSync('$target: ${dependencies.join(' ')}\n'); + return 0; + } catch (_) { + return 1; + } +} + int _addHeader(String outputPath, String header) { try { final File outputFile = fs.file(outputPath); diff --git a/packages/flutter_tools/lib/src/devfs.dart b/packages/flutter_tools/lib/src/devfs.dart index ef53273b3b..8d88e51479 100644 --- a/packages/flutter_tools/lib/src/devfs.dart +++ b/packages/flutter_tools/lib/src/devfs.dart @@ -42,6 +42,9 @@ abstract class DevFSContent { Stream> contentsAsCompressedStream() { return contentsAsStream().transform(GZIP.encoder); } + + /// Return the list of files this content depends on. + List get fileDependencies => []; } // File content to be copied to the device. @@ -82,6 +85,9 @@ class DevFSFileContent extends DevFSContent { } } + @override + List get fileDependencies => [_getFile().path]; + @override bool get isModified { final FileStat _oldFileStat = _fileStat; diff --git a/packages/flutter_tools/lib/src/flx.dart b/packages/flutter_tools/lib/src/flx.dart index 9a1b809b20..63e1826fdc 100644 --- a/packages/flutter_tools/lib/src/flx.dart +++ b/packages/flutter_tools/lib/src/flx.dart @@ -107,10 +107,10 @@ Future build({ packagesPath: packagesPath, includeRobotoFonts: includeRobotoFonts, reportLicensedPackages: reportLicensedPackages - ); + ).then((_) => null); } -Future assemble({ +Future> assemble({ String manifestPath, DevFSContent kernelContent, File snapshotFile, @@ -145,6 +145,10 @@ Future assemble({ // Add all entries from the asset bundle. zipBuilder.entries.addAll(assetBundle.entries); + final List fileDependencies = assetBundle.entries.values + .expand((DevFSContent content) => content.fileDependencies) + .toList(); + if (kernelContent != null) zipBuilder.entries[_kKernelKey] = kernelContent; if (snapshotFile != null) @@ -156,4 +160,6 @@ Future assemble({ await zipBuilder.createZip(fs.file(outputPath), fs.directory(workingDirPath)); printTrace('Built $outputPath.'); + + return fileDependencies; }