From f735604acab4fb6ac42de8f54193582628e6ba45 Mon Sep 17 00:00:00 2001 From: James Robinson Date: Tue, 22 Sep 2015 15:19:25 -0700 Subject: [PATCH] Add cache command to sky_tools with subcommands for populating/clearing This adds the following commands to sky_tools: sky_tools cache clear: Nukes all local artifacts in the cache sky_tools cache populate: Populates the cache with all known artifacts This is useful both to fix busted caches and to make sure that the cache is fully populated so that subsequent operations can proceed without needing network access. --- packages/flutter_tools/bin/sky_tools.dart | 2 + packages/flutter_tools/lib/src/artifacts.dart | 46 +++++++++---- packages/flutter_tools/lib/src/cache.dart | 68 +++++++++++++++++++ 3 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 packages/flutter_tools/lib/src/cache.dart diff --git a/packages/flutter_tools/bin/sky_tools.dart b/packages/flutter_tools/bin/sky_tools.dart index 6e0576c0ae..71c0fda31f 100644 --- a/packages/flutter_tools/bin/sky_tools.dart +++ b/packages/flutter_tools/bin/sky_tools.dart @@ -7,6 +7,7 @@ import 'dart:io'; import 'package:args/args.dart'; import 'package:logging/logging.dart'; import 'package:sky_tools/src/build.dart'; +import 'package:sky_tools/src/cache.dart'; import 'package:sky_tools/src/common.dart'; import 'package:sky_tools/src/init.dart'; import 'package:sky_tools/src/install.dart'; @@ -92,6 +93,7 @@ void main(List args) { for (CommandHandler handler in [ new BuildCommandHandler(), + new CacheCommandHandler(), new InitCommandHandler(), new InstallCommandHandler(), new RunMojoCommandHandler(), diff --git a/packages/flutter_tools/lib/src/artifacts.dart b/packages/flutter_tools/lib/src/artifacts.dart index 1ac743f825..717b363fba 100644 --- a/packages/flutter_tools/lib/src/artifacts.dart +++ b/packages/flutter_tools/lib/src/artifacts.dart @@ -43,33 +43,42 @@ class ArtifactStore { } Future _cacheDir() async { - String cacheDirPath = path.join(packageRoot, 'sky_tools', 'cache', 'sky_engine', engineRevision); - Directory cacheDir = new Directory(cacheDirPath); + Directory cacheDir = new Directory(path.join(packageRoot, 'sky_tools', 'cache')); if (!await cacheDir.exists()) { await cacheDir.create(recursive: true); } return cacheDir; } + Future _engineSpecificCacheDir() async { + Directory cacheDir = await _cacheDir(); + Directory engineSpecificDir = new Directory(path.join(cacheDir.path, 'sky_engine', engineRevision)); + + if (!await engineSpecificDir.exists()) { + await engineSpecificDir.create(recursive: true); + } + return engineSpecificDir; + } + // Whether the artifact needs to be marked as executable on disk. bool _needsToBeExecutable(Artifact artifact) { return artifact == Artifact.FlutterCompiler; } Future getPath(Artifact artifact) async { - Directory cacheDir = await _cacheDir(); + Directory cacheDir = await _engineSpecificCacheDir(); String category, name; - if (artifact == Artifact.FlutterCompiler) { - category = 'shell'; - name = 'sky_snapshot'; - } else if (artifact == Artifact.SkyViewerMojo) { - category = 'viewer'; - name = 'sky_viewer.mojo'; - } else { - // Unknown artifact. - return ''; + switch (artifact) { + case Artifact.FlutterCompiler: + category = 'shell'; + name = 'sky_snapshot'; + break; + case Artifact.SkyViewerMojo: + category = 'viewer'; + name = 'sky_viewer.mojo'; + break; } File cachedFile = new File(path.join(cacheDir.path, name)); @@ -84,4 +93,17 @@ class ArtifactStore { } return cachedFile.path; } + + Future clear() async { + Directory cacheDir = await _cacheDir(); + _logging.fine('Clearing cache directory ${cacheDir.path}'); + await cacheDir.delete(recursive: true); + } + + Future populate() async { + for (Artifact artifact in Artifact.values) { + _logging.fine('Populating cache with $artifact'); + await getPath(artifact); + } + } } diff --git a/packages/flutter_tools/lib/src/cache.dart b/packages/flutter_tools/lib/src/cache.dart new file mode 100644 index 0000000000..d8e90453d8 --- /dev/null +++ b/packages/flutter_tools/lib/src/cache.dart @@ -0,0 +1,68 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +library sky_tools.cache; + +import 'dart:async'; + +import 'package:args/args.dart'; +import 'package:logging/logging.dart'; + +import 'artifacts.dart'; +import 'common.dart'; + +final Logger _logging = new Logger('sky_tools.cache'); + +class CacheCommandHandler extends CommandHandler { + CacheCommandHandler() : super('cache', 'Manages sky_tools\' cache of binary artifacts.'); + + ArgParser get parser { + ArgParser parser = new ArgParser(); + parser.addFlag('help', abbr: 'h', negatable: false); + parser.addOption('package-root', defaultsTo: 'packages'); + ArgParser clearParser = parser.addCommand('clear'); + clearParser.addFlag('help', abbr: 'h', negatable: false); + ArgParser populateParser = parser.addCommand('populate'); + populateParser.addFlag('help', abbr: 'h', negatable: false); + return parser; + } + + Future _clear(String packageRoot, ArgResults results) async { + if (results['help']) { + print('Clears all artifacts from the cache.'); + print(parser.usage); + return 0; + } + ArtifactStore artifacts = new ArtifactStore(packageRoot); + await artifacts.clear(); + return 0; + } + + Future _populate(String packageRoot, ArgResults results) async { + if (results['help']) { + print('Populates the cache with all known artifacts.'); + print(parser.usage); + return 0; + } + ArtifactStore artifacts = new ArtifactStore(packageRoot); + await artifacts.populate(); + return 0; + } + + @override + Future processArgResults(ArgResults results) async { + if (results['help'] || results.command == null) { + print(parser.usage); + return 0; + } + if (results.command.name == 'clear') { + return _clear(results['package-root'], results.command); + } else if (results.command.name == 'populate') { + return _populate(results['package-root'], results.command); + } else { + _logging.severe('Unknown cache command \"${results.command.name}\"'); + return 2; + } + } +}