OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library pub.command.cache_add; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:pub_semver/pub_semver.dart'; | |
10 | |
11 import '../command.dart'; | |
12 import '../log.dart' as log; | |
13 import '../package.dart'; | |
14 import '../utils.dart'; | |
15 | |
16 /// Handles the `cache add` pub command. | |
17 class CacheAddCommand extends PubCommand { | |
18 String get name => "add"; | |
19 String get description => "Install a package."; | |
20 String get invocation => | |
21 "pub cache add <package> [--version <constraint>] [--all]"; | |
22 String get docUrl => "http://dartlang.org/tools/pub/cmd/pub-cache.html"; | |
23 | |
24 CacheAddCommand() { | |
25 argParser.addFlag( | |
26 "all", | |
27 help: "Install all matching versions.", | |
28 negatable: false); | |
29 | |
30 argParser.addOption("version", abbr: "v", help: "Version constraint."); | |
31 } | |
32 | |
33 Future run() { | |
34 // Make sure there is a package. | |
35 if (argResults.rest.isEmpty) { | |
36 usageException("No package to add given."); | |
37 } | |
38 | |
39 // Don't allow extra arguments. | |
40 if (argResults.rest.length > 1) { | |
41 var unexpected = argResults.rest.skip(1).map((arg) => '"$arg"'); | |
42 var arguments = pluralize("argument", unexpected.length); | |
43 usageException("Unexpected $arguments ${toSentence(unexpected)}."); | |
44 } | |
45 | |
46 var package = argResults.rest.single; | |
47 | |
48 // Parse the version constraint, if there is one. | |
49 var constraint = VersionConstraint.any; | |
50 if (argResults["version"] != null) { | |
51 try { | |
52 constraint = new VersionConstraint.parse(argResults["version"]); | |
53 } on FormatException catch (error) { | |
54 usageException(error.message); | |
55 } | |
56 } | |
57 | |
58 // TODO(rnystrom): Support installing from git too. | |
59 var source = cache.sources["hosted"]; | |
60 | |
61 // TODO(rnystrom): Allow specifying the server. | |
62 return source.getVersions(package, package).then((versions) { | |
63 versions = versions.where(constraint.allows).toList(); | |
64 | |
65 if (versions.isEmpty) { | |
66 // TODO(rnystrom): Show most recent unmatching version? | |
67 fail("Package $package has no versions that match $constraint."); | |
68 } | |
69 | |
70 downloadVersion(Version version) { | |
71 var id = new PackageId(package, source.name, version, package); | |
72 return cache.contains(id).then((contained) { | |
73 if (contained) { | |
74 // TODO(rnystrom): Include source and description if not hosted. | |
75 // See solve_report.dart for code to harvest. | |
76 log.message("Already cached ${id.name} ${id.version}."); | |
77 return null; | |
78 } | |
79 | |
80 // Download it. | |
81 return source.downloadToSystemCache(id); | |
82 }); | |
83 } | |
84 | |
85 if (argResults["all"]) { | |
86 // Install them in ascending order. | |
87 versions.sort(); | |
88 return Future.forEach(versions, downloadVersion); | |
89 } else { | |
90 // Pick the best matching version. | |
91 versions.sort(Version.prioritize); | |
92 return downloadVersion(versions.last); | |
93 } | |
94 }); | |
95 } | |
96 } | |
OLD | NEW |