| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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_repair; | 5 library pub.command.cache_repair; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import '../command.dart'; | 9 import '../command.dart'; |
| 10 import '../exit_codes.dart' as exit_codes; | 10 import '../exit_codes.dart' as exit_codes; |
| 11 import '../io.dart'; | 11 import '../io.dart'; |
| 12 import '../log.dart' as log; | 12 import '../log.dart' as log; |
| 13 import '../source/cached.dart'; | 13 import '../source/cached.dart'; |
| 14 import '../utils.dart'; | 14 import '../utils.dart'; |
| 15 | 15 |
| 16 /// Handles the `cache repair` pub command. | 16 /// Handles the `cache repair` pub command. |
| 17 class CacheRepairCommand extends PubCommand { | 17 class CacheRepairCommand extends PubCommand { |
| 18 String get description => "Reinstall cached packages."; | 18 String get description => "Reinstall cached packages."; |
| 19 String get usage => "pub cache repair"; | 19 String get usage => "pub cache repair"; |
| 20 String get docUrl => "http://dartlang.org/tools/pub/cmd/pub-cache.html"; | 20 String get docUrl => "http://dartlang.org/tools/pub/cmd/pub-cache.html"; |
| 21 | 21 |
| 22 Future onRun() { | 22 Future onRun() async { |
| 23 var successes = 0; | 23 var successes = 0; |
| 24 var failures = 0; | 24 var failures = 0; |
| 25 | 25 |
| 26 // Repair every cached source. | 26 // Repair every cached source. |
| 27 return Future.forEach(cache.sources.where( | 27 for (var source in cache.sources) { |
| 28 (source) => source is CachedSource), (source) { | 28 if (source is! CachedSource) continue; |
| 29 return source.repairCachedPackages().then((results) { | |
| 30 successes += results.first; | |
| 31 failures += results.last; | |
| 32 }); | |
| 33 }).then((_) { | |
| 34 if (successes > 0) { | |
| 35 var packages = pluralize("package", successes); | |
| 36 log.message("Reinstalled ${log.green(successes)} $packages."); | |
| 37 } | |
| 38 | 29 |
| 39 if (failures > 0) { | 30 var results = await source.repairCachedPackages(); |
| 40 var packages = pluralize("package", failures); | 31 successes += results.first; |
| 41 log.message("Failed to reinstall ${log.red(failures)} $packages."); | 32 failures += results.last; |
| 42 } | 33 } |
| 43 | 34 |
| 44 if (successes == 0 && failures == 0) { | 35 if (successes > 0) { |
| 45 log.message("No packages in cache, so nothing to repair."); | 36 var packages = pluralize("package", successes); |
| 46 } | 37 log.message("Reinstalled ${log.green(successes)} $packages."); |
| 38 } |
| 47 | 39 |
| 48 if (failures > 0) return flushThenExit(exit_codes.UNAVAILABLE); | 40 if (failures > 0) { |
| 49 }); | 41 var packages = pluralize("package", failures); |
| 42 log.message("Failed to reinstall ${log.red(failures)} $packages."); |
| 43 } |
| 44 |
| 45 if (successes == 0 && failures == 0) { |
| 46 log.message("No packages in cache, so nothing to repair."); |
| 47 } |
| 48 |
| 49 if (failures > 0) return flushThenExit(exit_codes.UNAVAILABLE); |
| 50 } | 50 } |
| 51 } | 51 } |
| OLD | NEW |