Commit d356b2d7 authored by P.Y. Laligand's avatar P.Y. Laligand Committed by GitHub

Generate a depfile for assets of a flutter_app. (#8961)

parent 38e4e48d
...@@ -25,13 +25,17 @@ const String _kOptionOutput = 'output-file'; ...@@ -25,13 +25,17 @@ const String _kOptionOutput = 'output-file';
const String _kOptionHeader = 'header'; const String _kOptionHeader = 'header';
const String _kOptionSnapshot = 'snapshot'; const String _kOptionSnapshot = 'snapshot';
const String _kOptionWorking = 'working-dir'; const String _kOptionWorking = 'working-dir';
const String _kOptionsManifest = 'manifest'; const String _kOptionManifest = 'manifest';
const String _kOptionDepFile = 'depfile';
const String _kOptionBuildRoot = 'build-root';
const List<String> _kRequiredOptions = const <String>[ const List<String> _kRequiredOptions = const <String>[
_kOptionPackages, _kOptionPackages,
_kOptionOutput, _kOptionOutput,
_kOptionHeader, _kOptionHeader,
_kOptionSnapshot, _kOptionSnapshot,
_kOptionWorking, _kOptionWorking,
_kOptionDepFile,
_kOptionBuildRoot,
]; ];
Future<Null> main(List<String> args) async { Future<Null> main(List<String> args) async {
...@@ -59,23 +63,36 @@ Future<Null> run(List<String> args) async { ...@@ -59,23 +63,36 @@ Future<Null> run(List<String> args) async {
..addOption(_kOptionSnapshot, help: 'The generated snapshot file') ..addOption(_kOptionSnapshot, help: 'The generated snapshot file')
..addOption(_kOptionWorking, ..addOption(_kOptionWorking,
help: 'The directory where to put temporary files') 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); 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.'); printError('Missing option! All options must be specified.');
exit(1); exit(1);
} }
Cache.flutterRoot = platform.environment['FLUTTER_ROOT']; Cache.flutterRoot = platform.environment['FLUTTER_ROOT'];
final String outputPath = argResults[_kOptionOutput]; final String outputPath = argResults[_kOptionOutput];
try { try {
await assemble( final List<String> dependencies = await assemble(
outputPath: outputPath, outputPath: outputPath,
snapshotFile: fs.file(argResults[_kOptionSnapshot]), snapshotFile: fs.file(argResults[_kOptionSnapshot]),
workingDirPath: argResults[_kOptionWorking], workingDirPath: argResults[_kOptionWorking],
packagesPath: argResults[_kOptionPackages], packagesPath: argResults[_kOptionPackages],
manifestPath: argResults[_kOptionsManifest] ?? defaultManifestPath, manifestPath: argResults[_kOptionManifest] ?? defaultManifestPath,
includeDefaultFonts: false, 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) { } on ToolExit catch (e) {
printError(e.message); printError(e.message);
exit(e.exitCode); exit(e.exitCode);
...@@ -87,6 +104,17 @@ Future<Null> run(List<String> args) async { ...@@ -87,6 +104,17 @@ Future<Null> run(List<String> args) async {
exit(headerResult); exit(headerResult);
} }
int _createDepfile(
String depFilePath, String target, List<String> 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) { int _addHeader(String outputPath, String header) {
try { try {
final File outputFile = fs.file(outputPath); final File outputFile = fs.file(outputPath);
......
...@@ -42,6 +42,9 @@ abstract class DevFSContent { ...@@ -42,6 +42,9 @@ abstract class DevFSContent {
Stream<List<int>> contentsAsCompressedStream() { Stream<List<int>> contentsAsCompressedStream() {
return contentsAsStream().transform(GZIP.encoder); return contentsAsStream().transform(GZIP.encoder);
} }
/// Return the list of files this content depends on.
List<String> get fileDependencies => <String>[];
} }
// File content to be copied to the device. // File content to be copied to the device.
...@@ -82,6 +85,9 @@ class DevFSFileContent extends DevFSContent { ...@@ -82,6 +85,9 @@ class DevFSFileContent extends DevFSContent {
} }
} }
@override
List<String> get fileDependencies => <String>[_getFile().path];
@override @override
bool get isModified { bool get isModified {
final FileStat _oldFileStat = _fileStat; final FileStat _oldFileStat = _fileStat;
......
...@@ -107,10 +107,10 @@ Future<Null> build({ ...@@ -107,10 +107,10 @@ Future<Null> build({
packagesPath: packagesPath, packagesPath: packagesPath,
includeRobotoFonts: includeRobotoFonts, includeRobotoFonts: includeRobotoFonts,
reportLicensedPackages: reportLicensedPackages reportLicensedPackages: reportLicensedPackages
); ).then((_) => null);
} }
Future<Null> assemble({ Future<List<String>> assemble({
String manifestPath, String manifestPath,
DevFSContent kernelContent, DevFSContent kernelContent,
File snapshotFile, File snapshotFile,
...@@ -145,6 +145,10 @@ Future<Null> assemble({ ...@@ -145,6 +145,10 @@ Future<Null> assemble({
// Add all entries from the asset bundle. // Add all entries from the asset bundle.
zipBuilder.entries.addAll(assetBundle.entries); zipBuilder.entries.addAll(assetBundle.entries);
final List<String> fileDependencies = assetBundle.entries.values
.expand((DevFSContent content) => content.fileDependencies)
.toList();
if (kernelContent != null) if (kernelContent != null)
zipBuilder.entries[_kKernelKey] = kernelContent; zipBuilder.entries[_kKernelKey] = kernelContent;
if (snapshotFile != null) if (snapshotFile != null)
...@@ -156,4 +160,6 @@ Future<Null> assemble({ ...@@ -156,4 +160,6 @@ Future<Null> assemble({
await zipBuilder.createZip(fs.file(outputPath), fs.directory(workingDirPath)); await zipBuilder.createZip(fs.file(outputPath), fs.directory(workingDirPath));
printTrace('Built $outputPath.'); printTrace('Built $outputPath.');
return fileDependencies;
} }
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