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'; |
11 import 'version.dart'; | 11 import 'version.dart'; |
12 import 'yaml/yaml.dart'; | 12 import 'yaml/yaml.dart'; |
13 | 13 |
14 /// The parsed and validated contents of a pubspec file. | 14 /// The parsed and validated contents of a pubspec file. |
15 class Pubspec { | 15 class Pubspec { |
16 /// This package's name. | 16 /// This package's name. |
17 final String name; | 17 final String name; |
18 | 18 |
19 /// This package's version. | 19 /// This package's version. |
20 final Version version; | 20 final Version version; |
21 | 21 |
22 /// The packages this package depends on. | 22 /// The packages this package depends on. |
23 final List<PackageRef> dependencies; | 23 final List<PackageRef> dependencies; |
24 | 24 |
| 25 /// The environment-related metadata. |
| 26 final PubspecEnvironment environment; |
| 27 |
25 /// All pubspec fields. This includes the fields from which other properties | 28 /// All pubspec fields. This includes the fields from which other properties |
26 /// are derived. | 29 /// are derived. |
27 final Map<String, Object> fields; | 30 final Map<String, Object> fields; |
28 | 31 |
29 Pubspec(this.name, this.version, this.dependencies, | 32 Pubspec(this.name, this.version, this.dependencies, this.environment, |
30 [Map<String, Object> fields]) | 33 [Map<String, Object> fields]) |
31 : this.fields = fields == null ? {} : fields; | 34 : this.fields = fields == null ? {} : fields; |
32 | 35 |
33 Pubspec.empty() | 36 Pubspec.empty() |
34 : name = null, | 37 : name = null, |
35 version = Version.none, | 38 version = Version.none, |
36 dependencies = <PackageRef>[], | 39 dependencies = <PackageRef>[], |
| 40 environment = new PubspecEnvironment(), |
37 fields = {}; | 41 fields = {}; |
38 | 42 |
39 /// Whether or not the pubspec has no contents. | 43 /// Whether or not the pubspec has no contents. |
40 bool get isEmpty => | 44 bool get isEmpty => |
41 name == null && version == Version.none && dependencies.isEmpty; | 45 name == null && version == Version.none && dependencies.isEmpty; |
42 | 46 |
43 /// Parses the pubspec whose text is [contents]. If the pubspec doesn't define | 47 /// Parses the pubspec whose text is [contents]. If the pubspec doesn't define |
44 /// version for itself, it defaults to [Version.none]. | 48 /// version for itself, it defaults to [Version.none]. |
45 factory Pubspec.parse(String contents, SourceRegistry sources) { | 49 factory Pubspec.parse(String contents, SourceRegistry sources) { |
46 var name = null; | 50 var name = null; |
47 var version = Version.none; | 51 var version = Version.none; |
48 | 52 |
49 if (contents.trim() == '') return new Pubspec.empty(); | 53 if (contents.trim() == '') return new Pubspec.empty(); |
(...skipping 13 matching lines...) Expand all Loading... |
63 } | 67 } |
64 } | 68 } |
65 | 69 |
66 if (parsedPubspec.containsKey('version')) { | 70 if (parsedPubspec.containsKey('version')) { |
67 version = new Version.parse(parsedPubspec['version']); | 71 version = new Version.parse(parsedPubspec['version']); |
68 } | 72 } |
69 | 73 |
70 var dependencies = _parseDependencies(sources, | 74 var dependencies = _parseDependencies(sources, |
71 parsedPubspec['dependencies']); | 75 parsedPubspec['dependencies']); |
72 | 76 |
| 77 var environmentYaml = parsedPubspec['environment']; |
| 78 var sdkConstraint = VersionConstraint.any; |
| 79 if (environmentYaml != null) { |
| 80 if (environmentYaml is! Map) { |
| 81 throw new FormatException( |
| 82 'The pubspec "environment" field should be a map, but was ' |
| 83 '"$environmentYaml".'); |
| 84 } |
| 85 |
| 86 var sdkYaml = environmentYaml['sdk']; |
| 87 if (sdkYaml is! String) { |
| 88 throw new FormatException( |
| 89 'The "sdk" field of "environment" should be a string, but was ' |
| 90 '"$sdkYaml".'); |
| 91 } |
| 92 |
| 93 sdkConstraint = new VersionConstraint.parse(sdkYaml); |
| 94 } |
| 95 var environment = new PubspecEnvironment(sdkConstraint); |
| 96 |
73 // Even though the pub app itself doesn't use these fields, we validate | 97 // Even though the pub app itself doesn't use these fields, we validate |
74 // them here so that users find errors early before they try to upload to | 98 // them here so that users find errors early before they try to upload to |
75 // the server: | 99 // the server: |
76 // TODO(rnystrom): We should split this validation into separate layers: | 100 // TODO(rnystrom): We should split this validation into separate layers: |
77 // 1. Stuff that is required in any pubspec to perform any command. Things | 101 // 1. Stuff that is required in any pubspec to perform any command. Things |
78 // like "must have a name". That should go here. | 102 // like "must have a name". That should go here. |
79 // 2. Stuff that is required to upload a package. Things like "homepage | 103 // 2. Stuff that is required to upload a package. Things like "homepage |
80 // must use a valid scheme". That should go elsewhere. pub upload should | 104 // must use a valid scheme". That should go elsewhere. pub upload should |
81 // call it, and we should provide a separate command to show the user, | 105 // call it, and we should provide a separate command to show the user, |
82 // and also expose it to the editor in some way. | 106 // and also expose it to the editor in some way. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 throw new FormatException('The pubspec "authors" field should be a ' | 140 throw new FormatException('The pubspec "authors" field should be a ' |
117 'string or a list of strings, but was "$authors".'); | 141 'string or a list of strings, but was "$authors".'); |
118 } | 142 } |
119 | 143 |
120 if (parsedPubspec.containsKey('author')) { | 144 if (parsedPubspec.containsKey('author')) { |
121 throw new FormatException('A pubspec should not have both an "author" ' | 145 throw new FormatException('A pubspec should not have both an "author" ' |
122 'and an "authors" field.'); | 146 'and an "authors" field.'); |
123 } | 147 } |
124 } | 148 } |
125 | 149 |
126 return new Pubspec(name, version, dependencies, parsedPubspec); | 150 return new Pubspec(name, version, dependencies, environment, parsedPubspec); |
127 } | 151 } |
128 } | 152 } |
129 | 153 |
130 List<PackageRef> _parseDependencies(SourceRegistry sources, yaml) { | 154 List<PackageRef> _parseDependencies(SourceRegistry sources, yaml) { |
131 var dependencies = <PackageRef>[]; | 155 var dependencies = <PackageRef>[]; |
132 | 156 |
133 // Allow an empty dependencies key. | 157 // Allow an empty dependencies key. |
134 if (yaml == null) return dependencies; | 158 if (yaml == null) return dependencies; |
135 | 159 |
136 if (yaml is! Map || yaml.keys.any((e) => e is! String)) { | 160 if (yaml is! Map || yaml.keys.any((e) => e is! String)) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 } | 198 } |
175 | 199 |
176 source.validateDescription(description, fromLockFile: false); | 200 source.validateDescription(description, fromLockFile: false); |
177 | 201 |
178 dependencies.add(new PackageRef( | 202 dependencies.add(new PackageRef( |
179 name, source, versionConstraint, description)); | 203 name, source, versionConstraint, description)); |
180 }); | 204 }); |
181 | 205 |
182 return dependencies; | 206 return dependencies; |
183 } | 207 } |
| 208 |
| 209 /// The environment-related metadata in the pubspec. Corresponds to the data |
| 210 /// under the "environment:" key in the pubspec. |
| 211 class PubspecEnvironment { |
| 212 /// The version constraint specifying which SDK versions this package works |
| 213 /// with. |
| 214 final VersionConstraint sdkVersion; |
| 215 |
| 216 PubspecEnvironment([VersionConstraint sdk]) |
| 217 : sdkVersion = sdk != null ? sdk : VersionConstraint.any; |
| 218 } |
OLD | NEW |