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