| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library pub.command.cache; | 5 library pub.command.cache; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import '../command.dart'; | 10 import '../command.dart'; |
| 11 import '../exit_codes.dart' as exit_codes; | 11 import '../exit_codes.dart' as exit_codes; |
| 12 import '../io.dart'; | 12 import '../io.dart'; |
| 13 import '../log.dart' as log; | 13 import '../log.dart' as log; |
| 14 | 14 |
| 15 /// Handles the `cache` pub command. | 15 /// Handles the `cache` pub command. |
| 16 class CacheCommand extends PubCommand { | 16 class CacheCommand extends PubCommand { |
| 17 String get description => "Inspect the system cache."; | 17 String get description => "Inspect the system cache."; |
| 18 String get usage => 'pub cache list'; | 18 String get usage => 'pub cache list'; |
| 19 bool get hidden => true; | 19 bool get hidden => true; |
| 20 bool get requiresEntrypoint => false; | 20 bool get requiresEntrypoint => false; |
| 21 bool get takesArguments => true; |
| 21 | 22 |
| 22 Future onRun() { | 23 Future onRun() { |
| 24 // TODO(rnystrom): Use subcommand for "list". |
| 23 if (commandOptions.rest.length != 1) { | 25 if (commandOptions.rest.length != 1) { |
| 24 log.error('The cache command expects one argument.'); | 26 log.error('The cache command expects one argument.'); |
| 25 this.printUsage(); | 27 this.printUsage(); |
| 26 return flushThenExit(exit_codes.USAGE); | 28 return flushThenExit(exit_codes.USAGE); |
| 27 } | 29 } |
| 28 | 30 |
| 29 if ((commandOptions.rest[0] != 'list')) { | 31 if ((commandOptions.rest[0] != 'list')) { |
| 30 log.error('Unknown cache command "${commandOptions.rest[0]}".'); | 32 log.error('Unknown cache command "${commandOptions.rest[0]}".'); |
| 31 this.printUsage(); | 33 this.printUsage(); |
| 32 return flushThenExit(exit_codes.USAGE); | 34 return flushThenExit(exit_codes.USAGE); |
| 33 } | 35 } |
| 34 | 36 |
| 35 // TODO(keertip): Add flag to list packages from non default sources | 37 // TODO(keertip): Add flag to list packages from non default sources |
| 36 var packagesObj = <String, Map>{}; | 38 var packagesObj = <String, Map>{}; |
| 37 | 39 |
| 38 for (var package in cache.sources.defaultSource.getCachedPackages()) { | 40 for (var package in cache.sources.defaultSource.getCachedPackages()) { |
| 39 | 41 |
| 40 var packageInfo = packagesObj.putIfAbsent(package.name, () => {}); | 42 var packageInfo = packagesObj.putIfAbsent(package.name, () => {}); |
| 41 packageInfo[package.version.toString()] = {'location': package.dir}; | 43 packageInfo[package.version.toString()] = {'location': package.dir}; |
| 42 } | 44 } |
| 43 | 45 |
| 44 // TODO(keertip): Add support for non-JSON format | 46 // TODO(keertip): Add support for non-JSON format |
| 45 // and check for --format flag | 47 // and check for --format flag |
| 46 log.message(JSON.encode({'packages': packagesObj})); | 48 log.message(JSON.encode({'packages': packagesObj})); |
| 47 } | 49 } |
| 48 } | 50 } |
| 49 | 51 |
| OLD | NEW |