| 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 'package.dart'; | 7 import 'package.dart'; |
| 8 import 'source.dart'; | 8 import 'source.dart'; |
| 9 import 'source_registry.dart'; | 9 import 'source_registry.dart'; |
| 10 import 'utils.dart'; | 10 import 'utils.dart'; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 bool get isEmpty => | 41 bool get isEmpty => |
| 42 name == null && version == Version.none && dependencies.isEmpty; | 42 name == null && version == Version.none && dependencies.isEmpty; |
| 43 | 43 |
| 44 /** | 44 /** |
| 45 * Parses the pubspec whose text is [contents]. If the pubspec doesn't define | 45 * Parses the pubspec whose text is [contents]. If the pubspec doesn't define |
| 46 * version for itself, it defaults to [Version.none]. | 46 * version for itself, it defaults to [Version.none]. |
| 47 */ | 47 */ |
| 48 factory Pubspec.parse(String contents, SourceRegistry sources) { | 48 factory Pubspec.parse(String contents, SourceRegistry sources) { |
| 49 var name = null; | 49 var name = null; |
| 50 var version = Version.none; | 50 var version = Version.none; |
| 51 var dependencies = <PackageRef>[]; | |
| 52 | 51 |
| 53 if (contents.trim() == '') return new Pubspec.empty(); | 52 if (contents.trim() == '') return new Pubspec.empty(); |
| 54 | 53 |
| 55 var parsedPubspec = loadYaml(contents); | 54 var parsedPubspec = loadYaml(contents); |
| 56 if (parsedPubspec == null) return new Pubspec.empty(); | 55 if (parsedPubspec == null) return new Pubspec.empty(); |
| 57 | 56 |
| 58 if (parsedPubspec is! Map) { | 57 if (parsedPubspec is! Map) { |
| 59 throw new FormatException('The pubspec must be a YAML mapping.'); | 58 throw new FormatException('The pubspec must be a YAML mapping.'); |
| 60 } | 59 } |
| 61 | 60 |
| 62 if (parsedPubspec.containsKey('name')) { | 61 if (parsedPubspec.containsKey('name')) { |
| 63 name = parsedPubspec['name']; | 62 name = parsedPubspec['name']; |
| 64 if (name is! String) { | 63 if (name is! String) { |
| 65 throw new FormatException( | 64 throw new FormatException( |
| 66 'The pubspec "name" field should be a string, but was "$name".'); | 65 'The pubspec "name" field should be a string, but was "$name".'); |
| 67 } | 66 } |
| 68 } | 67 } |
| 69 | 68 |
| 70 if (parsedPubspec.containsKey('version')) { | 69 if (parsedPubspec.containsKey('version')) { |
| 71 version = new Version.parse(parsedPubspec['version']); | 70 version = new Version.parse(parsedPubspec['version']); |
| 72 } | 71 } |
| 73 | 72 |
| 74 if (parsedPubspec.containsKey('dependencies')) { | 73 var dependencies = _parseDependencies(sources, |
| 75 var dependencyEntries = parsedPubspec['dependencies']; | 74 parsedPubspec['dependencies']); |
| 76 if (dependencyEntries is! Map || | |
| 77 dependencyEntries.keys.some((e) => e is! String)) { | |
| 78 throw new FormatException( | |
| 79 'The pubspec dependencies should be a map of package names, but ' | |
| 80 'was ${dependencyEntries}.'); | |
| 81 } | |
| 82 | |
| 83 dependencyEntries.forEach((name, spec) { | |
| 84 var description, source; | |
| 85 var versionConstraint = new VersionRange(); | |
| 86 if (spec == null) { | |
| 87 description = name; | |
| 88 source = sources.defaultSource; | |
| 89 } else if (spec is String) { | |
| 90 description = name; | |
| 91 source = sources.defaultSource; | |
| 92 versionConstraint = new VersionConstraint.parse(spec); | |
| 93 } else if (spec is Map) { | |
| 94 if (spec.containsKey('version')) { | |
| 95 versionConstraint = new VersionConstraint.parse( | |
| 96 spec.remove('version')); | |
| 97 } | |
| 98 | |
| 99 var sourceNames = spec.keys; | |
| 100 if (sourceNames.length > 1) { | |
| 101 throw new FormatException( | |
| 102 'Dependency $name may only have one source: $sourceNames.'); | |
| 103 } | |
| 104 | |
| 105 var sourceName = only(sourceNames); | |
| 106 if (sourceName is! String) { | |
| 107 throw new FormatException( | |
| 108 'Source name $sourceName should be a string.'); | |
| 109 } | |
| 110 | |
| 111 source = sources[sourceName]; | |
| 112 description = spec[sourceName]; | |
| 113 } else { | |
| 114 throw new FormatException( | |
| 115 'Dependency specification $spec should be a string or a ' | |
| 116 'mapping.'); | |
| 117 } | |
| 118 | |
| 119 source.validateDescription(description, fromLockFile: false); | |
| 120 | |
| 121 dependencies.add(new PackageRef( | |
| 122 name, source, versionConstraint, description)); | |
| 123 }); | |
| 124 } | |
| 125 | 75 |
| 126 // Even though the pub app itself doesn't use these fields, we validate | 76 // Even though the pub app itself doesn't use these fields, we validate |
| 127 // them here so that users find errors early before they try to upload to | 77 // them here so that users find errors early before they try to upload to |
| 128 // the server: | 78 // the server: |
| 129 | 79 |
| 130 if (parsedPubspec.containsKey('homepage') && | 80 if (parsedPubspec.containsKey('homepage') && |
| 131 parsedPubspec['homepage'] is! String) { | 81 parsedPubspec['homepage'] is! String) { |
| 132 throw new FormatException( | 82 throw new FormatException( |
| 133 'The "homepage" field should be a string, but was ' | 83 'The "homepage" field should be a string, but was ' |
| 134 '${parsedPubspec["homepage"]}.'); | 84 '${parsedPubspec["homepage"]}.'); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 156 | 106 |
| 157 if (parsedPubspec.containsKey('author')) { | 107 if (parsedPubspec.containsKey('author')) { |
| 158 throw new FormatException('A pubspec should not have both an "author" ' | 108 throw new FormatException('A pubspec should not have both an "author" ' |
| 159 'and an "authors" field.'); | 109 'and an "authors" field.'); |
| 160 } | 110 } |
| 161 } | 111 } |
| 162 | 112 |
| 163 return new Pubspec(name, version, dependencies); | 113 return new Pubspec(name, version, dependencies); |
| 164 } | 114 } |
| 165 } | 115 } |
| 116 |
| 117 List<PackageRef> _parseDependencies(SourceRegistry sources, yaml) { |
| 118 var dependencies = <PackageRef>[]; |
| 119 |
| 120 // Allow an empty dependencies key. |
| 121 if (yaml == null) return dependencies; |
| 122 |
| 123 if (yaml is! Map || yaml.keys.some((e) => e is! String)) { |
| 124 throw new FormatException( |
| 125 'The pubspec dependencies should be a map of package names, but ' |
| 126 'was ${yaml}.'); |
| 127 } |
| 128 |
| 129 yaml.forEach((name, spec) { |
| 130 var description, source; |
| 131 var versionConstraint = new VersionRange(); |
| 132 if (spec == null) { |
| 133 description = name; |
| 134 source = sources.defaultSource; |
| 135 } else if (spec is String) { |
| 136 description = name; |
| 137 source = sources.defaultSource; |
| 138 versionConstraint = new VersionConstraint.parse(spec); |
| 139 } else if (spec is Map) { |
| 140 if (spec.containsKey('version')) { |
| 141 versionConstraint = new VersionConstraint.parse(spec.remove('version')); |
| 142 } |
| 143 |
| 144 var sourceNames = spec.keys; |
| 145 if (sourceNames.length > 1) { |
| 146 throw new FormatException( |
| 147 'Dependency $name may only have one source: $sourceNames.'); |
| 148 } |
| 149 |
| 150 var sourceName = only(sourceNames); |
| 151 if (sourceName is! String) { |
| 152 throw new FormatException( |
| 153 'Source name $sourceName should be a string.'); |
| 154 } |
| 155 |
| 156 source = sources[sourceName]; |
| 157 description = spec[sourceName]; |
| 158 } else { |
| 159 throw new FormatException( |
| 160 'Dependency specification $spec should be a string or a mapping.'); |
| 161 } |
| 162 |
| 163 source.validateDescription(description, fromLockFile: false); |
| 164 |
| 165 dependencies.add(new PackageRef( |
| 166 name, source, versionConstraint, description)); |
| 167 }); |
| 168 |
| 169 return dependencies; |
| 170 } |
| OLD | NEW |