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/path/lib/path.dart' as path; | 8 import '../../pkg/path/lib/path.dart' as path; |
9 | 9 |
10 import 'io.dart'; | 10 import 'io.dart'; |
(...skipping 20 matching lines...) Expand all Loading... |
31 /// All pubspec fields. This includes the fields from which other properties | 31 /// All pubspec fields. This includes the fields from which other properties |
32 /// are derived. | 32 /// are derived. |
33 final Map<String, Object> fields; | 33 final Map<String, Object> fields; |
34 | 34 |
35 /// Loads the pubspec for a package [name] located in [packageDir]. | 35 /// Loads the pubspec for a package [name] located in [packageDir]. |
36 factory Pubspec.load(String name, String packageDir, SourceRegistry sources) { | 36 factory Pubspec.load(String name, String packageDir, SourceRegistry sources) { |
37 var pubspecPath = path.join(packageDir, 'pubspec.yaml'); | 37 var pubspecPath = path.join(packageDir, 'pubspec.yaml'); |
38 if (!fileExists(pubspecPath)) throw new PubspecNotFoundException(name); | 38 if (!fileExists(pubspecPath)) throw new PubspecNotFoundException(name); |
39 | 39 |
40 try { | 40 try { |
41 var pubspec = new Pubspec.parse(readTextFile(pubspecPath), sources); | 41 var pubspec = new Pubspec.parse(pubspecPath, readTextFile(pubspecPath), |
| 42 sources); |
42 | 43 |
43 if (pubspec.name == null) { | 44 if (pubspec.name == null) { |
44 throw new PubspecHasNoNameException(name); | 45 throw new PubspecHasNoNameException(name); |
45 } | 46 } |
46 | 47 |
47 if (name != null && pubspec.name != name) { | 48 if (name != null && pubspec.name != name) { |
48 throw new PubspecNameMismatchException(name, pubspec.name); | 49 throw new PubspecNameMismatchException(name, pubspec.name); |
49 } | 50 } |
50 | 51 |
51 return pubspec; | 52 return pubspec; |
(...skipping 10 matching lines...) Expand all Loading... |
62 : name = null, | 63 : name = null, |
63 version = Version.none, | 64 version = Version.none, |
64 dependencies = <PackageRef>[], | 65 dependencies = <PackageRef>[], |
65 environment = new PubspecEnvironment(), | 66 environment = new PubspecEnvironment(), |
66 fields = {}; | 67 fields = {}; |
67 | 68 |
68 /// Whether or not the pubspec has no contents. | 69 /// Whether or not the pubspec has no contents. |
69 bool get isEmpty => | 70 bool get isEmpty => |
70 name == null && version == Version.none && dependencies.isEmpty; | 71 name == null && version == Version.none && dependencies.isEmpty; |
71 | 72 |
72 // TODO(rnystrom): Make this a static method to match corelib. | 73 /// Parses the pubspec stored at [filePath] whose text is [contents]. If the |
73 /// Parses the pubspec whose text is [contents]. If the pubspec doesn't define | 74 /// pubspec doesn't define version for itself, it defaults to [Version.none]. |
74 /// version for itself, it defaults to [Version.none]. | 75 /// [filePath] may be `null` if the pubspec is not on the user's local |
75 factory Pubspec.parse(String contents, SourceRegistry sources) { | 76 /// file system. |
| 77 factory Pubspec.parse(String filePath, String contents, |
| 78 SourceRegistry sources) { |
76 var name = null; | 79 var name = null; |
77 var version = Version.none; | 80 var version = Version.none; |
78 | 81 |
79 if (contents.trim() == '') return new Pubspec.empty(); | 82 if (contents.trim() == '') return new Pubspec.empty(); |
80 | 83 |
81 var parsedPubspec = loadYaml(contents); | 84 var parsedPubspec = loadYaml(contents); |
82 if (parsedPubspec == null) return new Pubspec.empty(); | 85 if (parsedPubspec == null) return new Pubspec.empty(); |
83 | 86 |
84 if (parsedPubspec is! Map) { | 87 if (parsedPubspec is! Map) { |
85 throw new FormatException('The pubspec must be a YAML mapping.'); | 88 throw new FormatException('The pubspec must be a YAML mapping.'); |
86 } | 89 } |
87 | 90 |
88 if (parsedPubspec.containsKey('name')) { | 91 if (parsedPubspec.containsKey('name')) { |
89 name = parsedPubspec['name']; | 92 name = parsedPubspec['name']; |
90 if (name is! String) { | 93 if (name is! String) { |
91 throw new FormatException( | 94 throw new FormatException( |
92 'The pubspec "name" field should be a string, but was "$name".'); | 95 'The pubspec "name" field should be a string, but was "$name".'); |
93 } | 96 } |
94 } | 97 } |
95 | 98 |
96 if (parsedPubspec.containsKey('version')) { | 99 if (parsedPubspec.containsKey('version')) { |
97 version = new Version.parse(parsedPubspec['version']); | 100 version = new Version.parse(parsedPubspec['version']); |
98 } | 101 } |
99 | 102 |
100 var dependencies = _parseDependencies(sources, | 103 var dependencies = _parseDependencies(filePath, sources, |
101 parsedPubspec['dependencies']); | 104 parsedPubspec['dependencies']); |
102 | 105 |
103 var environmentYaml = parsedPubspec['environment']; | 106 var environmentYaml = parsedPubspec['environment']; |
104 var sdkConstraint = VersionConstraint.any; | 107 var sdkConstraint = VersionConstraint.any; |
105 if (environmentYaml != null) { | 108 if (environmentYaml != null) { |
106 if (environmentYaml is! Map) { | 109 if (environmentYaml is! Map) { |
107 throw new FormatException( | 110 throw new FormatException( |
108 'The pubspec "environment" field should be a map, but was ' | 111 'The pubspec "environment" field should be a map, but was ' |
109 '"$environmentYaml".'); | 112 '"$environmentYaml".'); |
110 } | 113 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 183 } |
181 | 184 |
182 var goodScheme = new RegExp(r'^https?:'); | 185 var goodScheme = new RegExp(r'^https?:'); |
183 if (!goodScheme.hasMatch(url)) { | 186 if (!goodScheme.hasMatch(url)) { |
184 throw new FormatException( | 187 throw new FormatException( |
185 'The "$field" field should be an "http:" or "https:" URL, but ' | 188 'The "$field" field should be an "http:" or "https:" URL, but ' |
186 'was "$url".'); | 189 'was "$url".'); |
187 } | 190 } |
188 } | 191 } |
189 | 192 |
190 List<PackageRef> _parseDependencies(SourceRegistry sources, yaml) { | 193 List<PackageRef> _parseDependencies(String pubspecPath, SourceRegistry sources, |
| 194 yaml) { |
191 var dependencies = <PackageRef>[]; | 195 var dependencies = <PackageRef>[]; |
192 | 196 |
193 // Allow an empty dependencies key. | 197 // Allow an empty dependencies key. |
194 if (yaml == null) return dependencies; | 198 if (yaml == null) return dependencies; |
195 | 199 |
196 if (yaml is! Map || yaml.keys.any((e) => e is! String)) { | 200 if (yaml is! Map || yaml.keys.any((e) => e is! String)) { |
197 throw new FormatException( | 201 throw new FormatException( |
198 'The pubspec dependencies should be a map of package names, but ' | 202 'The pubspec dependencies should be a map of package names, but ' |
199 'was ${yaml}.'); | 203 'was ${yaml}.'); |
200 } | 204 } |
(...skipping 25 matching lines...) Expand all Loading... |
226 'Source name $sourceName should be a string.'); | 230 'Source name $sourceName should be a string.'); |
227 } | 231 } |
228 | 232 |
229 source = sources[sourceName]; | 233 source = sources[sourceName]; |
230 description = spec[sourceName]; | 234 description = spec[sourceName]; |
231 } else { | 235 } else { |
232 throw new FormatException( | 236 throw new FormatException( |
233 'Dependency specification $spec should be a string or a mapping.'); | 237 'Dependency specification $spec should be a string or a mapping.'); |
234 } | 238 } |
235 | 239 |
236 source.validateDescription(description, fromLockFile: false); | 240 description = source.parseDescription(pubspecPath, description, |
| 241 fromLockFile: false); |
237 | 242 |
238 dependencies.add(new PackageRef( | 243 dependencies.add(new PackageRef( |
239 name, source, versionConstraint, description)); | 244 name, source, versionConstraint, description)); |
240 }); | 245 }); |
241 | 246 |
242 return dependencies; | 247 return dependencies; |
243 } | 248 } |
244 | 249 |
245 /// The environment-related metadata in the pubspec. Corresponds to the data | 250 /// The environment-related metadata in the pubspec. Corresponds to the data |
246 /// under the "environment:" key in the pubspec. | 251 /// under the "environment:" key in the pubspec. |
247 class PubspecEnvironment { | 252 class PubspecEnvironment { |
248 /// The version constraint specifying which SDK versions this package works | 253 /// The version constraint specifying which SDK versions this package works |
249 /// with. | 254 /// with. |
250 final VersionConstraint sdkVersion; | 255 final VersionConstraint sdkVersion; |
251 | 256 |
252 PubspecEnvironment([VersionConstraint sdk]) | 257 PubspecEnvironment([VersionConstraint sdk]) |
253 : sdkVersion = sdk != null ? sdk : VersionConstraint.any; | 258 : sdkVersion = sdk != null ? sdk : VersionConstraint.any; |
254 } | 259 } |
OLD | NEW |