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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 | 52 |
53 if (contents.trim() == '') return new Pubspec.empty(); | 53 if (contents.trim() == '') return new Pubspec.empty(); |
54 | 54 |
55 var parsedPubspec = loadYaml(contents); | 55 var parsedPubspec = loadYaml(contents); |
56 if (parsedPubspec == null) return new Pubspec.empty(); | 56 if (parsedPubspec == null) return new Pubspec.empty(); |
57 | 57 |
58 if (parsedPubspec is! Map) { | 58 if (parsedPubspec is! Map) { |
59 throw new FormatException('The pubspec must be a YAML mapping.'); | 59 throw new FormatException('The pubspec must be a YAML mapping.'); |
60 } | 60 } |
61 | 61 |
62 if (parsedPubspec.containsKey('name')) name = parsedPubspec['name']; | 62 if (parsedPubspec.containsKey('name')) { |
| 63 name = parsedPubspec['name']; |
| 64 if (name is! String) { |
| 65 throw new FormatException( |
| 66 'The pubspec "name" field should be a string, but was "$name".'); |
| 67 } |
| 68 } |
63 | 69 |
64 if (parsedPubspec.containsKey('version')) { | 70 if (parsedPubspec.containsKey('version')) { |
65 version = new Version.parse(parsedPubspec['version']); | 71 version = new Version.parse(parsedPubspec['version']); |
66 } | 72 } |
67 | 73 |
68 if (parsedPubspec.containsKey('dependencies')) { | 74 if (parsedPubspec.containsKey('dependencies')) { |
69 var dependencyEntries = parsedPubspec['dependencies']; | 75 var dependencyEntries = parsedPubspec['dependencies']; |
70 if (dependencyEntries is! Map || | 76 if (dependencyEntries is! Map || |
71 dependencyEntries.getKeys().some((e) => e is! String)) { | 77 dependencyEntries.getKeys().some((e) => e is! String)) { |
72 throw new FormatException( | 78 throw new FormatException( |
73 'The pubspec dependencies must be a map of package names.'); | 79 'The pubspec dependencies should be a map of package names, but ' |
| 80 'was ${dependencyEntries}.'); |
74 } | 81 } |
75 | 82 |
76 dependencyEntries.forEach((name, spec) { | 83 dependencyEntries.forEach((name, spec) { |
77 var description, source; | 84 var description, source; |
78 var versionConstraint = new VersionRange(); | 85 var versionConstraint = new VersionRange(); |
79 if (spec == null) { | 86 if (spec == null) { |
80 description = name; | 87 description = name; |
81 source = sources.defaultSource; | 88 source = sources.defaultSource; |
82 } else if (spec is String) { | 89 } else if (spec is String) { |
83 description = name; | 90 description = name; |
84 source = sources.defaultSource; | 91 source = sources.defaultSource; |
85 versionConstraint = new VersionConstraint.parse(spec); | 92 versionConstraint = new VersionConstraint.parse(spec); |
86 } else if (spec is Map) { | 93 } else if (spec is Map) { |
87 if (spec.containsKey('version')) { | 94 if (spec.containsKey('version')) { |
88 versionConstraint = new VersionConstraint.parse( | 95 versionConstraint = new VersionConstraint.parse( |
89 spec.remove('version')); | 96 spec.remove('version')); |
90 } | 97 } |
91 | 98 |
92 var sourceNames = spec.getKeys(); | 99 var sourceNames = spec.getKeys(); |
93 if (sourceNames.length > 1) { | 100 if (sourceNames.length > 1) { |
94 throw new FormatException( | 101 throw new FormatException( |
95 'Dependency $name may only have one source: $sourceNames.'); | 102 'Dependency $name may only have one source: $sourceNames.'); |
96 } | 103 } |
97 | 104 |
98 var sourceName = only(sourceNames); | 105 var sourceName = only(sourceNames); |
99 if (sourceName is! String) { | 106 if (sourceName is! String) { |
100 throw new FormatException( | 107 throw new FormatException( |
101 'Source name $sourceName must be a string.'); | 108 'Source name $sourceName should be a string.'); |
102 } | 109 } |
103 | 110 |
104 source = sources[sourceName]; | 111 source = sources[sourceName]; |
105 description = spec[sourceName]; | 112 description = spec[sourceName]; |
106 } else { | 113 } else { |
107 throw new FormatException( | 114 throw new FormatException( |
108 'Dependency specification $spec must be a string or a mapping.'); | 115 'Dependency specification $spec should be a string or a ' |
| 116 'mapping.'); |
109 } | 117 } |
110 | 118 |
111 source.validateDescription(description, fromLockFile: false); | 119 source.validateDescription(description, fromLockFile: false); |
112 | 120 |
113 dependencies.add(new PackageRef( | 121 dependencies.add(new PackageRef( |
114 name, source, versionConstraint, description)); | 122 name, source, versionConstraint, description)); |
115 }); | 123 }); |
116 } | 124 } |
117 | 125 |
| 126 // 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 |
| 128 // the server: |
| 129 |
| 130 if (parsedPubspec.containsKey('homepage') && |
| 131 parsedPubspec['homepage'] is! String) { |
| 132 throw new FormatException( |
| 133 'The "homepage" field should be a string, but was ' |
| 134 '${parsedPubspec["homepage"]}.'); |
| 135 } |
| 136 |
| 137 if (parsedPubspec.containsKey('author') && |
| 138 parsedPubspec['author'] is! String) { |
| 139 throw new FormatException( |
| 140 'The "author" field should be a string, but was ' |
| 141 '${parsedPubspec["author"]}.'); |
| 142 } |
| 143 |
| 144 if (parsedPubspec.containsKey('authors')) { |
| 145 var authors = parsedPubspec['authors']; |
| 146 if (authors is List) { |
| 147 // All of the elements must be strings. |
| 148 if (!authors.every((author) => author is String)) { |
| 149 throw new FormatException('The "authors" field should be a string ' |
| 150 'or a list of strings, but was "$authors".'); |
| 151 } |
| 152 } else if (authors is! String) { |
| 153 throw new FormatException('The pubspec "authors" field should be a ' |
| 154 'string or a list of strings, but was "$authors".'); |
| 155 } |
| 156 |
| 157 if (parsedPubspec.containsKey('author')) { |
| 158 throw new FormatException('A pubspec should not have both an "author" ' |
| 159 'and an "authors" field.'); |
| 160 } |
| 161 } |
| 162 |
118 return new Pubspec(name, version, dependencies); | 163 return new Pubspec(name, version, dependencies); |
119 } | 164 } |
120 } | 165 } |
OLD | NEW |