OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 package; | 5 library package; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:pathos/path.dart' as path; | 9 import 'package:pathos/path.dart' as path; |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... | |
29 } | 29 } |
30 | 30 |
31 /// The package's version. | 31 /// The package's version. |
32 Version get version => pubspec.version; | 32 Version get version => pubspec.version; |
33 | 33 |
34 /// The parsed pubspec associated with this package. | 34 /// The parsed pubspec associated with this package. |
35 final Pubspec pubspec; | 35 final Pubspec pubspec; |
36 | 36 |
37 /// The ids of the packages that this package depends on. This is what is | 37 /// The ids of the packages that this package depends on. This is what is |
38 /// specified in the pubspec when this package depends on another. | 38 /// specified in the pubspec when this package depends on another. |
39 List<PackageRef> get dependencies => pubspec.dependencies; | 39 List<PackageDep> get dependencies => pubspec.dependencies; |
40 | 40 |
41 /// Returns the path to the README file at the root of the entrypoint, or null | 41 /// Returns the path to the README file at the root of the entrypoint, or null |
42 /// if no README file is found. If multiple READMEs are found, this uses the | 42 /// if no README file is found. If multiple READMEs are found, this uses the |
43 /// same conventions as pub.dartlang.org for choosing the primary one: the | 43 /// same conventions as pub.dartlang.org for choosing the primary one: the |
44 /// README with the fewest extensions that is lexically ordered first is | 44 /// README with the fewest extensions that is lexically ordered first is |
45 /// chosen. | 45 /// chosen. |
46 String get readmePath { | 46 String get readmePath { |
47 var readmes = listDir(dir).map(path.basename). | 47 var readmes = listDir(dir).map(path.basename). |
48 where((entry) => entry.contains(_README_REGEXP)); | 48 where((entry) => entry.contains(_README_REGEXP)); |
49 if (readmes.isEmpty) return null; | 49 if (readmes.isEmpty) return null; |
(...skipping 20 matching lines...) Expand all Loading... | |
70 : dir = null; | 70 : dir = null; |
71 | 71 |
72 /// Constructs a package. This should not be called directly. Instead, acquire | 72 /// Constructs a package. This should not be called directly. Instead, acquire |
73 /// packages from [load()]. | 73 /// packages from [load()]. |
74 Package._(this.dir, this.pubspec); | 74 Package._(this.dir, this.pubspec); |
75 | 75 |
76 /// Returns a debug string for the package. | 76 /// Returns a debug string for the package. |
77 String toString() => '$name $version ($dir)'; | 77 String toString() => '$name $version ($dir)'; |
78 } | 78 } |
79 | 79 |
80 /// An unambiguous resolved reference to a package. A package ID contains enough | 80 /// This is the private base class of [PackageRef], [PackageID], and |
81 /// information to correctly install the package. | 81 /// [PackageDep]. It contains functionality and state that those classes share |
82 /// | 82 /// but is private so that from outside of this library, there is no type |
83 /// Note that it's possible for multiple distinct package IDs to point to | 83 /// relationship between those three types. |
84 /// different directories that happen to contain identical packages. For | 84 class _PackageName { |
85 /// example, the same package may be available from multiple sources. As far as | 85 _PackageName(this.name, this.source, this.description); |
86 /// Pub is concerned, those packages are different. | 86 |
87 class PackageId implements Comparable<PackageId> { | |
88 /// The name of the package being identified. | 87 /// The name of the package being identified. |
89 final String name; | 88 final String name; |
90 | 89 |
91 /// The [Source] used to look up this package given its [description]. If | 90 /// The [Source] used to look up this package given its [description]. If |
92 /// this is a root package ID, this will be `null`. | 91 /// this is a root package, this will be `null`. |
93 final Source source; | 92 final Source source; |
94 | 93 |
95 /// The package's version. | |
96 final Version version; | |
97 | |
98 /// The metadata used by the package's [source] to identify and locate it. It | 94 /// The metadata used by the package's [source] to identify and locate it. It |
99 /// contains whatever [Source]-specific data it needs to be able to install | 95 /// contains whatever [Source]-specific data it needs to be able to install |
100 /// the package. For example, the description of a git sourced package might | 96 /// the package. For example, the description of a git sourced package might |
101 /// by the URL "git://github.com/dart/uilib.git". | 97 /// by the URL "git://github.com/dart/uilib.git". |
102 final description; | 98 final description; |
103 | 99 |
104 PackageId(this.name, this.source, this.version, this.description); | 100 /// Whether this package is the root package. |
105 | |
106 /// Creates an ID for the given root package. | |
107 PackageId.root(Package package) | |
108 : name = package.name, | |
109 source = null, | |
110 version = package.version, | |
111 description = package.name; | |
112 | |
113 /// Whether this ID identifies the root package. | |
114 bool get isRoot => source == null; | 101 bool get isRoot => source == null; |
115 | 102 |
116 int get hashCode => name.hashCode ^ source.hashCode ^ version.hashCode; | |
117 | |
118 /// Gets the directory where this package is or would be found in the | 103 /// Gets the directory where this package is or would be found in the |
119 /// [SystemCache]. | 104 /// [SystemCache]. |
120 Future<String> get systemCacheDirectory => source.systemCacheDirectory(this); | 105 Future<String> get systemCacheDirectory => source.systemCacheDirectory(this); |
121 | 106 |
107 String toString() { | |
108 if (isRoot) return "$name (root)"; | |
109 if (source.isDefault) return name; | |
110 return "$name from $source"; | |
111 } | |
112 | |
113 /// Returns a [PackageRef] with this one's [name], [source], and | |
114 /// [description]. | |
115 PackageRef toRef() => new PackageRef(name, source, description); | |
116 | |
117 /// Returns a [PackageId] for this package with the given concrete version. | |
118 PackageId atVersion(Version version) => | |
119 new PackageId(name, source, version, description); | |
120 | |
121 /// Returns `true` if this package's description matches [other]'s. | |
122 bool descriptionEquals(PackageDep other) { | |
123 return source.descriptionsEqual(description, other.description); | |
124 } | |
125 } | |
126 | |
127 /// A reference to a [Package], but not any particular version(s) of it. | |
128 class PackageRef extends _PackageName { | |
129 PackageRef(String name, Source source, description) | |
130 : super(name, source, description); | |
131 | |
132 int get hashCode => name.hashCode ^ source.hashCode; | |
133 | |
134 bool operator ==(other) { | |
135 if (other is! PackageRef) return false; | |
nweiz
2013/04/27 00:12:18
You can write this as "return other is PackageRef
Bob Nystrom
2013/04/30 17:44:25
Done.
| |
136 // TODO(rnystrom): We're assuming here that we don't need to delve into the | |
137 // description. | |
138 return other.name == name && other.source == source; | |
139 } | |
140 | |
141 /// Gets the list of ids of all versions of the package that are described by | |
142 /// this reference. | |
143 Future<List<PackageId>> getVersions() { | |
144 if (isRoot) { | |
145 throw new StateError("Cannot get versions for the root package."); | |
146 } | |
147 | |
148 return source.getVersions(name, description).then((versions) { | |
149 return versions.map((version) => atVersion(version)).toList(); | |
150 }); | |
151 } | |
152 } | |
153 | |
154 /// A reference to a specific version of a package. A package ID contains | |
155 /// enough information to correctly install the package. | |
156 /// | |
157 /// Note that it's possible for multiple distinct package IDs to point to | |
158 /// different packages that have identical contents. For example, the same | |
159 /// package may be available from multiple sources. As far as Pub is concerned, | |
160 /// those packages are different. | |
161 class PackageId extends _PackageName { | |
162 /// The package's version. | |
163 final Version version; | |
164 | |
165 PackageId(String name, Source source, this.version, description) | |
166 : super(name, source, description); | |
167 | |
168 /// Creates an ID for the given root package. | |
169 PackageId.root(Package package) | |
170 : version = package.version, | |
171 super(package.name, null, package.name); | |
172 | |
173 int get hashCode => name.hashCode ^ source.hashCode ^ version.hashCode; | |
174 | |
122 bool operator ==(other) { | 175 bool operator ==(other) { |
123 if (other is! PackageId) return false; | 176 if (other is! PackageId) return false; |
124 // TODO(rnystrom): We're assuming here the name/version/source tuple is | 177 // TODO(rnystrom): We're assuming here that we don't need to delve into the |
125 // enough to uniquely identify the package and that we don't need to delve | 178 // description. |
126 // into the description. | |
127 return other.name == name && | 179 return other.name == name && |
128 other.source == source && | 180 other.source == source && |
129 other.version == version; | 181 other.version == version; |
130 } | 182 } |
131 | 183 |
132 String toString() { | 184 String toString() { |
133 if (isRoot) return "$name $version (root)"; | 185 if (isRoot) return "$name $version (root)"; |
134 if (source.isDefault) return "$name $version"; | 186 if (source.isDefault) return "$name $version"; |
135 return "$name $version from $source"; | 187 return "$name $version from $source"; |
136 } | 188 } |
137 | 189 |
138 int compareTo(PackageId other) { | |
139 var sourceComp = source.name.compareTo(other.source.name); | |
140 if (sourceComp != 0) return sourceComp; | |
141 | |
142 var nameComp = name.compareTo(other.name); | |
143 if (nameComp != 0) return nameComp; | |
144 | |
145 return version.compareTo(other.version); | |
146 } | |
147 | |
148 /// Returns the pubspec for this package. | 190 /// Returns the pubspec for this package. |
149 Future<Pubspec> describe() => source.systemCache.describe(this); | 191 Future<Pubspec> describe() => source.systemCache.describe(this); |
150 | 192 |
151 /// Returns a future that completes to the resovled [PackageId] for this id. | 193 /// Returns a future that completes to the resolved [PackageId] for this id. |
152 Future<PackageId> get resolved => source.resolveId(this); | 194 Future<PackageId> get resolved => source.resolveId(this); |
153 | |
154 /// Returns a [PackageRef] that references this package and constrains its | |
155 /// version to exactly match [version]. | |
156 PackageRef toRef() { | |
157 return new PackageRef(name, source, version, description); | |
158 } | |
159 | |
160 /// Returns `true` if this id's description matches [other]'s. | |
161 bool descriptionEquals(PackageRef other) { | |
162 return source.descriptionsEqual(description, other.description); | |
163 } | |
164 } | 195 } |
165 | 196 |
166 /// A reference to a package. Unlike a [PackageId], a PackageRef may not | 197 /// A reference to a constrained range of versions of one package. |
167 /// unambiguously refer to a single package. It may describe a range of allowed | 198 class PackageDep extends _PackageName { |
168 /// packages. | |
169 class PackageRef { | |
170 /// The name of the package being identified. | |
171 final String name; | |
172 | |
173 /// The [Source] used to look up the package. If this refers to a root | |
174 /// package, this will be `null`. | |
175 final Source source; | |
176 | |
177 /// The allowed package versions. | 199 /// The allowed package versions. |
178 final VersionConstraint constraint; | 200 final VersionConstraint constraint; |
179 | 201 |
180 /// The metadata used to identify the package being referenced. The | 202 PackageDep(String name, Source source, this.constraint, description) |
181 /// interpretation of this will vary based on the [source]. | 203 : super(name, source, description); |
182 final description; | |
183 | |
184 PackageRef(this.name, this.source, this.constraint, this.description); | |
185 | |
186 // TODO(rnystrom): Remove this if the old version solver is removed. | |
187 /// Creates a reference to the given root package. | |
188 PackageRef.root(Package package) | |
189 : name = package.name, | |
190 source = null, | |
191 constraint = package.version, | |
192 description = package.name; | |
193 | |
194 /// Whether this refers to the root package. | |
195 bool get isRoot => source == null; | |
196 | 204 |
197 String toString() { | 205 String toString() { |
198 if (isRoot) return "$name $constraint (root)"; | 206 if (isRoot) return "$name $constraint (root)"; |
199 return "$name $constraint from $source ($description)"; | 207 return "$name $constraint from $source ($description)"; |
200 } | 208 } |
201 | 209 |
202 /// Returns a [PackageId] generated from this [PackageRef] with the given | 210 int get hashCode => name.hashCode ^ source.hashCode; |
203 /// concrete version. | |
204 PackageId atVersion(Version version) => | |
205 new PackageId(name, source, version, description); | |
206 | 211 |
207 /// Returns `true` if this reference's description matches [other]'s. | 212 bool operator ==(other) { |
208 bool descriptionEquals(PackageRef other) { | 213 if (other is! PackageDep) return false; |
209 return source.descriptionsEqual(description, other.description); | 214 // TODO(rnystrom): We're assuming here that we don't need to delve into the |
215 // description. | |
216 return other.name == name && | |
217 other.source == source && | |
218 other.constraint == constraint; | |
210 } | 219 } |
211 } | 220 } |
212 | 221 |
213 class PubspecNotFoundException implements Exception { | 222 class PubspecNotFoundException implements Exception { |
214 final String name; | 223 final String name; |
215 | 224 |
216 PubspecNotFoundException(this.name); | 225 PubspecNotFoundException(this.name); |
217 | 226 |
218 String toString() => 'Package "$name" doesn\'t have a pubspec.yaml file.'; | 227 String toString() => 'Package "$name" doesn\'t have a pubspec.yaml file.'; |
219 } | 228 } |
220 | 229 |
221 class PubspecHasNoNameException implements Exception { | 230 class PubspecHasNoNameException implements Exception { |
222 final String name; | 231 final String name; |
223 | 232 |
224 PubspecHasNoNameException(this.name); | 233 PubspecHasNoNameException(this.name); |
225 | 234 |
226 String toString() => 'Package "$name"\'s pubspec.yaml file is missing the ' | 235 String toString() => 'Package "$name"\'s pubspec.yaml file is missing the ' |
227 'required "name" field (e.g. "name: $name").'; | 236 'required "name" field (e.g. "name: $name").'; |
228 } | 237 } |
229 | 238 |
230 class PubspecNameMismatchException implements Exception { | 239 class PubspecNameMismatchException implements Exception { |
231 final String expectedName; | 240 final String expectedName; |
232 final String actualName; | 241 final String actualName; |
233 | 242 |
234 PubspecNameMismatchException(this.expectedName, this.actualName); | 243 PubspecNameMismatchException(this.expectedName, this.actualName); |
235 | 244 |
236 String toString() => 'The name you specified for your dependency, ' | 245 String toString() => 'The name you specified for your dependency, ' |
237 '"$expectedName", doesn\'t match the name "$actualName" in its pubspec.'; | 246 '"$expectedName", doesn\'t match the name "$actualName" in its pubspec.'; |
238 } | 247 } |
OLD | NEW |