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 pubspec; | 5 library pubspec; |
6 | 6 |
7 import '../../pkg/yaml/lib/yaml.dart'; | 7 import '../../pkg/yaml/lib/yaml.dart'; |
8 import '../../pkg/pathos/lib/path.dart' as path; | 8 import '../../pkg/pathos/lib/path.dart' as path; |
9 | 9 |
10 import 'io.dart'; | 10 import 'io.dart'; |
11 import 'package.dart'; | 11 import 'package.dart'; |
12 import 'source.dart'; | 12 import 'source.dart'; |
13 import 'source_registry.dart'; | 13 import 'source_registry.dart'; |
14 import 'utils.dart'; | 14 import 'utils.dart'; |
15 import 'version.dart'; | 15 import 'version.dart'; |
16 | 16 |
17 /// The parsed and validated contents of a pubspec file. | 17 /// The parsed and validated contents of a pubspec file. |
18 class Pubspec { | 18 class Pubspec { |
19 /// This package's name. | 19 /// This package's name. |
20 final String name; | 20 final String name; |
21 | 21 |
22 /// This package's version. | 22 /// This package's version. |
23 final Version version; | 23 final Version version; |
24 | 24 |
25 /// The packages this package depends on. | 25 /// The packages this package depends on. |
26 final List<PackageRef> dependencies; | 26 final List<PackageRef> dependencies; |
27 | 27 |
28 /// The packages this package depends on when it is the root package. | |
29 final List<PackageRef> devDependencies; | |
30 | |
28 /// The environment-related metadata. | 31 /// The environment-related metadata. |
29 final PubspecEnvironment environment; | 32 final PubspecEnvironment environment; |
30 | 33 |
31 /// All pubspec fields. This includes the fields from which other properties | 34 /// All pubspec fields. This includes the fields from which other properties |
32 /// are derived. | 35 /// are derived. |
33 final Map<String, Object> fields; | 36 final Map<String, Object> fields; |
34 | 37 |
35 /// Loads the pubspec for a package [name] located in [packageDir]. | 38 /// Loads the pubspec for a package [name] located in [packageDir]. |
36 factory Pubspec.load(String name, String packageDir, SourceRegistry sources) { | 39 factory Pubspec.load(String name, String packageDir, SourceRegistry sources) { |
37 var pubspecPath = path.join(packageDir, 'pubspec.yaml'); | 40 var pubspecPath = path.join(packageDir, 'pubspec.yaml'); |
(...skipping 10 matching lines...) Expand all Loading... | |
48 if (name != null && pubspec.name != name) { | 51 if (name != null && pubspec.name != name) { |
49 throw new PubspecNameMismatchException(name, pubspec.name); | 52 throw new PubspecNameMismatchException(name, pubspec.name); |
50 } | 53 } |
51 | 54 |
52 return pubspec; | 55 return pubspec; |
53 } on FormatException catch (ex) { | 56 } on FormatException catch (ex) { |
54 throw 'Could not parse $pubspecPath:\n${ex.message}'; | 57 throw 'Could not parse $pubspecPath:\n${ex.message}'; |
55 } | 58 } |
56 } | 59 } |
57 | 60 |
58 Pubspec(this.name, this.version, this.dependencies, this.environment, | 61 Pubspec(this.name, this.version, this.dependencies, this.devDependencies, |
59 [Map<String, Object> fields]) | 62 this.environment, [Map<String, Object> fields]) |
60 : this.fields = fields == null ? {} : fields; | 63 : this.fields = fields == null ? {} : fields; |
61 | 64 |
62 Pubspec.empty() | 65 Pubspec.empty() |
63 : name = null, | 66 : name = null, |
64 version = Version.none, | 67 version = Version.none, |
65 dependencies = <PackageRef>[], | 68 dependencies = <PackageRef>[], |
69 devDependencies = <PackageRef>[], | |
66 environment = new PubspecEnvironment(), | 70 environment = new PubspecEnvironment(), |
67 fields = {}; | 71 fields = {}; |
68 | 72 |
69 /// Whether or not the pubspec has no contents. | 73 /// Whether or not the pubspec has no contents. |
70 bool get isEmpty => | 74 bool get isEmpty => |
71 name == null && version == Version.none && dependencies.isEmpty; | 75 name == null && version == Version.none && dependencies.isEmpty; |
72 | 76 |
73 // TODO(rnystrom): Instead of allowing a null argument here, split this up | 77 // TODO(rnystrom): Instead of allowing a null argument here, split this up |
74 // into load(), parse(), and _parse() like LockFile does. | 78 // into load(), parse(), and _parse() like LockFile does. |
75 /// Parses the pubspec stored at [filePath] whose text is [contents]. If the | 79 /// Parses the pubspec stored at [filePath] whose text is [contents]. If the |
(...skipping 22 matching lines...) Expand all Loading... | |
98 } | 102 } |
99 } | 103 } |
100 | 104 |
101 if (parsedPubspec.containsKey('version')) { | 105 if (parsedPubspec.containsKey('version')) { |
102 version = new Version.parse(parsedPubspec['version']); | 106 version = new Version.parse(parsedPubspec['version']); |
103 } | 107 } |
104 | 108 |
105 var dependencies = _parseDependencies(filePath, sources, | 109 var dependencies = _parseDependencies(filePath, sources, |
106 parsedPubspec['dependencies']); | 110 parsedPubspec['dependencies']); |
107 | 111 |
112 var devDependencies = _parseDependencies(filePath, sources, | |
113 parsedPubspec['dev_dependencies']); | |
114 | |
115 // Make sure the same package doesn't appear as both a regular and dev | |
116 // dependency. | |
117 var dependencyNames = dependencies.map((dep) => dep.name).toSet(); | |
118 var collisions = dependencyNames.intersection( | |
119 devDependencies.map((dep) => dep.name).toSet()); | |
120 | |
121 if (!collisions.isEmpty) { | |
122 throw new FormatException( | |
123 'Package "${collisions.first}" cannot appear in both "dependencies" ' | |
124 'and "dev_dependencies".'); | |
nweiz
2013/03/13 18:46:29
Let's list all the collisions so users don't have
Bob Nystrom
2013/03/13 19:42:44
Done.
| |
125 } | |
126 | |
108 var environmentYaml = parsedPubspec['environment']; | 127 var environmentYaml = parsedPubspec['environment']; |
109 var sdkConstraint = VersionConstraint.any; | 128 var sdkConstraint = VersionConstraint.any; |
110 if (environmentYaml != null) { | 129 if (environmentYaml != null) { |
111 if (environmentYaml is! Map) { | 130 if (environmentYaml is! Map) { |
112 throw new FormatException( | 131 throw new FormatException( |
113 'The pubspec "environment" field should be a map, but was ' | 132 'The pubspec "environment" field should be a map, but was ' |
114 '"$environmentYaml".'); | 133 '"$environmentYaml".'); |
115 } | 134 } |
116 | 135 |
117 var sdkYaml = environmentYaml['sdk']; | 136 var sdkYaml = environmentYaml['sdk']; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 throw new FormatException('The pubspec "authors" field should be a ' | 181 throw new FormatException('The pubspec "authors" field should be a ' |
163 'string or a list of strings, but was "$authors".'); | 182 'string or a list of strings, but was "$authors".'); |
164 } | 183 } |
165 | 184 |
166 if (parsedPubspec.containsKey('author')) { | 185 if (parsedPubspec.containsKey('author')) { |
167 throw new FormatException('A pubspec should not have both an "author" ' | 186 throw new FormatException('A pubspec should not have both an "author" ' |
168 'and an "authors" field.'); | 187 'and an "authors" field.'); |
169 } | 188 } |
170 } | 189 } |
171 | 190 |
172 return new Pubspec(name, version, dependencies, environment, parsedPubspec); | 191 return new Pubspec(name, version, dependencies, devDependencies, |
192 environment, parsedPubspec); | |
173 } | 193 } |
174 } | 194 } |
175 | 195 |
176 /** | 196 /** |
177 * Evaluates whether the given [url] for [field] is valid. | 197 * Evaluates whether the given [url] for [field] is valid. |
178 * | 198 * |
179 * Throws [FormatException] on an invalid url. | 199 * Throws [FormatException] on an invalid url. |
180 */ | 200 */ |
181 void _validateFieldUrl(url, String field) { | 201 void _validateFieldUrl(url, String field) { |
182 if (url is! String) { | 202 if (url is! String) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
252 /// The environment-related metadata in the pubspec. Corresponds to the data | 272 /// The environment-related metadata in the pubspec. Corresponds to the data |
253 /// under the "environment:" key in the pubspec. | 273 /// under the "environment:" key in the pubspec. |
254 class PubspecEnvironment { | 274 class PubspecEnvironment { |
255 /// The version constraint specifying which SDK versions this package works | 275 /// The version constraint specifying which SDK versions this package works |
256 /// with. | 276 /// with. |
257 final VersionConstraint sdkVersion; | 277 final VersionConstraint sdkVersion; |
258 | 278 |
259 PubspecEnvironment([VersionConstraint sdk]) | 279 PubspecEnvironment([VersionConstraint sdk]) |
260 : sdkVersion = sdk != null ? sdk : VersionConstraint.any; | 280 : sdkVersion = sdk != null ? sdk : VersionConstraint.any; |
261 } | 281 } |
OLD | NEW |