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 var packageNames; |
| 123 if (collisions.length == 1) { |
| 124 packageNames = 'Package "${collisions.first}"'; |
| 125 } else { |
| 126 var names = collisions.toList(); |
| 127 names.sort(); |
| 128 var buffer = new StringBuffer(); |
| 129 buffer.write("Packages "); |
| 130 for (var i = 0; i < names.length; i++) { |
| 131 buffer.write('"'); |
| 132 buffer.write(names[i]); |
| 133 buffer.write('"'); |
| 134 if (i == names.length - 2) { |
| 135 buffer.write(", "); |
| 136 } else if (i == names.length - 1) { |
| 137 buffer.write(", and "); |
| 138 } |
| 139 } |
| 140 |
| 141 packageNames = buffer.toString(); |
| 142 } |
| 143 throw new FormatException( |
| 144 '$packageNames cannot appear in both "dependencies" and ' |
| 145 '"dev_dependencies".'); |
| 146 } |
| 147 |
108 var environmentYaml = parsedPubspec['environment']; | 148 var environmentYaml = parsedPubspec['environment']; |
109 var sdkConstraint = VersionConstraint.any; | 149 var sdkConstraint = VersionConstraint.any; |
110 if (environmentYaml != null) { | 150 if (environmentYaml != null) { |
111 if (environmentYaml is! Map) { | 151 if (environmentYaml is! Map) { |
112 throw new FormatException( | 152 throw new FormatException( |
113 'The pubspec "environment" field should be a map, but was ' | 153 'The pubspec "environment" field should be a map, but was ' |
114 '"$environmentYaml".'); | 154 '"$environmentYaml".'); |
115 } | 155 } |
116 | 156 |
117 var sdkYaml = environmentYaml['sdk']; | 157 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 ' | 202 throw new FormatException('The pubspec "authors" field should be a ' |
163 'string or a list of strings, but was "$authors".'); | 203 'string or a list of strings, but was "$authors".'); |
164 } | 204 } |
165 | 205 |
166 if (parsedPubspec.containsKey('author')) { | 206 if (parsedPubspec.containsKey('author')) { |
167 throw new FormatException('A pubspec should not have both an "author" ' | 207 throw new FormatException('A pubspec should not have both an "author" ' |
168 'and an "authors" field.'); | 208 'and an "authors" field.'); |
169 } | 209 } |
170 } | 210 } |
171 | 211 |
172 return new Pubspec(name, version, dependencies, environment, parsedPubspec); | 212 return new Pubspec(name, version, dependencies, devDependencies, |
| 213 environment, parsedPubspec); |
173 } | 214 } |
174 } | 215 } |
175 | 216 |
176 /** | 217 /** |
177 * Evaluates whether the given [url] for [field] is valid. | 218 * Evaluates whether the given [url] for [field] is valid. |
178 * | 219 * |
179 * Throws [FormatException] on an invalid url. | 220 * Throws [FormatException] on an invalid url. |
180 */ | 221 */ |
181 void _validateFieldUrl(url, String field) { | 222 void _validateFieldUrl(url, String field) { |
182 if (url is! String) { | 223 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 | 293 /// The environment-related metadata in the pubspec. Corresponds to the data |
253 /// under the "environment:" key in the pubspec. | 294 /// under the "environment:" key in the pubspec. |
254 class PubspecEnvironment { | 295 class PubspecEnvironment { |
255 /// The version constraint specifying which SDK versions this package works | 296 /// The version constraint specifying which SDK versions this package works |
256 /// with. | 297 /// with. |
257 final VersionConstraint sdkVersion; | 298 final VersionConstraint sdkVersion; |
258 | 299 |
259 PubspecEnvironment([VersionConstraint sdk]) | 300 PubspecEnvironment([VersionConstraint sdk]) |
260 : sdkVersion = sdk != null ? sdk : VersionConstraint.any; | 301 : sdkVersion = sdk != null ? sdk : VersionConstraint.any; |
261 } | 302 } |
OLD | NEW |