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