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 'io.dart'; | 7 import 'io.dart'; |
8 import 'pubspec.dart'; | 8 import 'pubspec.dart'; |
9 import 'source.dart'; | 9 import 'source.dart'; |
10 import 'source_registry.dart'; | 10 import 'source_registry.dart'; |
11 import 'version.dart'; | 11 import 'version.dart'; |
12 | 12 |
13 /** | 13 /// A named, versioned, unit of code and resource reuse. |
14 * A named, versioned, unit of code and resource reuse. | |
15 */ | |
16 class Package { | 14 class Package { |
17 /** | 15 /// Loads the package whose root directory is [packageDir]. [name] is the |
18 * Loads the package whose root directory is [packageDir]. [name] is the | 16 /// expected name of that package (e.g. the name given in the dependency), or |
19 * expected name of that package (e.g. the name given in the dependency), or | 17 /// null if the package being loaded is the entrypoint package. |
20 * null if the package being loaded is the entrypoint package. | |
21 */ | |
22 static Future<Package> load(String name, String packageDir, | 18 static Future<Package> load(String name, String packageDir, |
23 SourceRegistry sources) { | 19 SourceRegistry sources) { |
24 var pubspecPath = join(packageDir, 'pubspec.yaml'); | 20 var pubspecPath = join(packageDir, 'pubspec.yaml'); |
25 | 21 |
26 return fileExists(pubspecPath).chain((exists) { | 22 return fileExists(pubspecPath).chain((exists) { |
27 if (!exists) throw new PubspecNotFoundException(name); | 23 if (!exists) throw new PubspecNotFoundException(name); |
28 return readTextFile(pubspecPath); | 24 return readTextFile(pubspecPath); |
29 }).transform((contents) { | 25 }).transform((contents) { |
30 try { | 26 try { |
31 var pubspec = new Pubspec.parse(contents, sources); | 27 var pubspec = new Pubspec.parse(contents, sources); |
32 | 28 |
33 if (pubspec.name == null) throw new PubspecHasNoNameException(name); | 29 if (pubspec.name == null) throw new PubspecHasNoNameException(name); |
34 if (name != null && pubspec.name != name) { | 30 if (name != null && pubspec.name != name) { |
35 throw new PubspecNameMismatchException(name, pubspec.name); | 31 throw new PubspecNameMismatchException(name, pubspec.name); |
36 } | 32 } |
37 return new Package._(packageDir, pubspec); | 33 return new Package._(packageDir, pubspec); |
38 } on FormatException catch (ex) { | 34 } on FormatException catch (ex) { |
39 throw 'Could not parse $pubspecPath:\n${ex.message}'; | 35 throw 'Could not parse $pubspecPath:\n${ex.message}'; |
40 } | 36 } |
41 }); | 37 }); |
42 } | 38 } |
43 | 39 |
44 /** | 40 /// The path to the directory containing the package. |
45 * The path to the directory containing the package. | |
46 */ | |
47 final String dir; | 41 final String dir; |
48 | 42 |
49 /** | 43 /// The name of the package. |
50 * The name of the package. | |
51 */ | |
52 String get name { | 44 String get name { |
53 if (pubspec.name != null) return pubspec.name; | 45 if (pubspec.name != null) return pubspec.name; |
54 if (dir != null) return basename(dir); | 46 if (dir != null) return basename(dir); |
55 return null; | 47 return null; |
56 } | 48 } |
57 | 49 |
58 /** | 50 /// The package's version. |
59 * The package's version. | |
60 */ | |
61 Version get version => pubspec.version; | 51 Version get version => pubspec.version; |
62 | 52 |
63 /** | 53 /// The parsed pubspec associated with this package. |
64 * The parsed pubspec associated with this package. | |
65 */ | |
66 final Pubspec pubspec; | 54 final Pubspec pubspec; |
67 | 55 |
68 /** | 56 /// The ids of the packages that this package depends on. This is what is |
69 * The ids of the packages that this package depends on. This is what is | 57 /// specified in the pubspec when this package depends on another. |
70 * specified in the pubspec when this package depends on another. | |
71 */ | |
72 Collection<PackageRef> get dependencies => pubspec.dependencies; | 58 Collection<PackageRef> get dependencies => pubspec.dependencies; |
73 | 59 |
74 /** | 60 /// Constructs a package with the given pubspec. The package will have no |
75 * Constructs a package with the given pubspec. The package will have no | 61 /// directory associated with it. |
76 * directory associated with it. | |
77 */ | |
78 Package.inMemory(this.pubspec) | 62 Package.inMemory(this.pubspec) |
79 : dir = null; | 63 : dir = null; |
80 | 64 |
81 /** | 65 /// Constructs a package. This should not be called directly. Instead, acquire |
82 * Constructs a package. This should not be called directly. Instead, acquire | 66 /// packages from [load()]. |
83 * packages from [load()]. | |
84 */ | |
85 Package._(this.dir, this.pubspec); | 67 Package._(this.dir, this.pubspec); |
86 | 68 |
87 /** | 69 /// Returns a debug string for the package. |
88 * Returns a debug string for the package. | |
89 */ | |
90 String toString() => '$name $version ($dir)'; | 70 String toString() => '$name $version ($dir)'; |
91 } | 71 } |
92 | 72 |
93 /** | 73 /// An unambiguous resolved reference to a package. A package ID contains enough |
94 * An unambiguous resolved reference to a package. A package ID contains enough | 74 /// information to correctly install the package. |
95 * information to correctly install the package. | 75 /// |
96 * | 76 /// Note that it's possible for multiple distinct package IDs to point to |
97 * Note that it's possible for multiple distinct package IDs to point to | 77 /// different directories that happen to contain identical packages. For |
98 * different directories that happen to contain identical packages. For example, | 78 /// example, the same package may be available from multiple sources. As far as |
99 * the same package may be available from multiple sources. As far as Pub is | 79 /// Pub is concerned, those packages are different. |
100 * concerned, those packages are different. | |
101 */ | |
102 class PackageId implements Comparable { | 80 class PackageId implements Comparable { |
103 /// The name of the package being identified. | 81 /// The name of the package being identified. |
104 final String name; | 82 final String name; |
105 | 83 |
106 /** | 84 /// The [Source] used to look up this package given its [description]. |
107 * The [Source] used to look up this package given its [description]. | |
108 */ | |
109 final Source source; | 85 final Source source; |
110 | 86 |
111 /** | 87 /// The package's version. |
112 * The package's version. | |
113 */ | |
114 final Version version; | 88 final Version version; |
115 | 89 |
116 /** | 90 /// The metadata used by the package's [source] to identify and locate it. It |
117 * The metadata used by the package's [source] to identify and locate it. It | 91 /// contains whatever [Source]-specific data it needs to be able to install |
118 * contains whatever [Source]-specific data it needs to be able to install | 92 /// the package. For example, the description of a git sourced package might |
119 * the package. For example, the description of a git sourced package might | 93 /// by the URL "git://github.com/dart/uilib.git". |
120 * by the URL "git://github.com/dart/uilib.git". | |
121 */ | |
122 final description; | 94 final description; |
123 | 95 |
124 PackageId(this.name, this.source, this.version, this.description); | 96 PackageId(this.name, this.source, this.version, this.description); |
125 | 97 |
126 int get hashCode => name.hashCode ^ | 98 int get hashCode => name.hashCode ^ |
127 source.name.hashCode ^ | 99 source.name.hashCode ^ |
128 version.hashCode; | 100 version.hashCode; |
129 | 101 |
130 bool operator ==(other) { | 102 bool operator ==(other) { |
131 if (other is! PackageId) return false; | 103 if (other is! PackageId) return false; |
(...skipping 15 matching lines...) Expand all Loading... |
147 | 119 |
148 var sourceComp = source.name.compareTo(other.source.name); | 120 var sourceComp = source.name.compareTo(other.source.name); |
149 if (sourceComp != 0) return sourceComp; | 121 if (sourceComp != 0) return sourceComp; |
150 | 122 |
151 var nameComp = name.compareTo(other.name); | 123 var nameComp = name.compareTo(other.name); |
152 if (nameComp != 0) return nameComp; | 124 if (nameComp != 0) return nameComp; |
153 | 125 |
154 return version.compareTo(other.version); | 126 return version.compareTo(other.version); |
155 } | 127 } |
156 | 128 |
157 /** | 129 /// Returns the pubspec for this package. |
158 * Returns the pubspec for this package. | |
159 */ | |
160 Future<Pubspec> describe() => source.describe(this); | 130 Future<Pubspec> describe() => source.describe(this); |
161 | 131 |
162 /** | 132 /// Returns a future that completes to the resovled [PackageId] for this id. |
163 * Returns a future that completes to the resovled [PackageId] for this id. | |
164 */ | |
165 Future<PackageId> get resolved => source.resolveId(this); | 133 Future<PackageId> get resolved => source.resolveId(this); |
166 } | 134 } |
167 | 135 |
168 /** | 136 /// A reference to a package. Unlike a [PackageId], a PackageRef may not |
169 * A reference to a package. Unlike a [PackageId], a PackageRef may not | 137 /// unambiguously refer to a single package. It may describe a range of allowed |
170 * unambiguously refer to a single package. It may describe a range of allowed | 138 /// packages. |
171 * packages. | |
172 */ | |
173 class PackageRef { | 139 class PackageRef { |
174 /// The name of the package being identified. | 140 /// The name of the package being identified. |
175 final String name; | 141 final String name; |
176 | 142 |
177 /** | 143 /// The [Source] used to look up the package. |
178 * The [Source] used to look up the package. | |
179 */ | |
180 final Source source; | 144 final Source source; |
181 | 145 |
182 /** | 146 /// The allowed package versions. |
183 * The allowed package versions. | |
184 */ | |
185 final VersionConstraint constraint; | 147 final VersionConstraint constraint; |
186 | 148 |
187 /** | 149 /// The metadata used to identify the package being referenced. The |
188 * The metadata used to identify the package being referenced. The | 150 /// interpretation of this will vary based on the [source]. |
189 * interpretation of this will vary based on the [source]. | |
190 */ | |
191 final description; | 151 final description; |
192 | 152 |
193 PackageRef(this.name, this.source, this.constraint, this.description); | 153 PackageRef(this.name, this.source, this.constraint, this.description); |
194 | 154 |
195 String toString() => "$name $constraint from $source ($description)"; | 155 String toString() => "$name $constraint from $source ($description)"; |
196 | 156 |
197 /** | 157 /// Returns a [PackageId] generated from this [PackageRef] with the given |
198 * Returns a [PackageId] generated from this [PackageRef] with the given | 158 /// concrete version. |
199 * concrete version. | |
200 */ | |
201 PackageId atVersion(Version version) => | 159 PackageId atVersion(Version version) => |
202 new PackageId(name, source, version, description); | 160 new PackageId(name, source, version, description); |
203 } | 161 } |
204 | 162 |
205 class PubspecNotFoundException implements Exception { | 163 class PubspecNotFoundException implements Exception { |
206 final String name; | 164 final String name; |
207 | 165 |
208 PubspecNotFoundException(this.name); | 166 PubspecNotFoundException(this.name); |
209 | 167 |
210 String toString() => 'Package "$name" doesn\'t have a pubspec.yaml file.'; | 168 String toString() => 'Package "$name" doesn\'t have a pubspec.yaml file.'; |
(...skipping 10 matching lines...) Expand all Loading... |
221 | 179 |
222 class PubspecNameMismatchException implements Exception { | 180 class PubspecNameMismatchException implements Exception { |
223 final String expectedName; | 181 final String expectedName; |
224 final String actualName; | 182 final String actualName; |
225 | 183 |
226 PubspecNameMismatchException(this.expectedName, this.actualName); | 184 PubspecNameMismatchException(this.expectedName, this.actualName); |
227 | 185 |
228 String toString() => 'The name you specified for your dependency, ' | 186 String toString() => 'The name you specified for your dependency, ' |
229 '"$expectedName", doesn\'t match the name "$actualName" in its pubspec.'; | 187 '"$expectedName", doesn\'t match the name "$actualName" in its pubspec.'; |
230 } | 188 } |
OLD | NEW |