Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #library('pub_pubspec'); | |
| 6 | |
| 7 #import('io.dart'); | |
| 8 #import('pub.dart'); | |
| 9 #import('source.dart'); | |
| 10 #import('utils.dart'); | |
| 11 #import('version.dart'); | |
| 12 #import('yaml/yaml.dart'); | |
| 13 | |
| 14 /** | |
| 15 * The parsed and validated contents of a pubspec file. | |
| 16 */ | |
| 17 class Pubspec { | |
| 18 /** | |
| 19 * This package's version. | |
| 20 */ | |
| 21 final Version version; | |
| 22 | |
| 23 /** | |
| 24 * The packages this package depends on. | |
| 25 */ | |
| 26 List<PackageRef> dependencies; | |
| 27 | |
| 28 Pubspec._(this.version, this.dependencies); | |
| 29 | |
| 30 Pubspec.empty() | |
| 31 : version = Version.none, | |
| 32 dependencies = <PackageRef>[]; | |
| 33 | |
| 34 /** | |
| 35 * Parses the pubspec whose text is [contents]. If the pubspec doesn't define | |
| 36 * version for itself, it defaults to [Version.none]. | |
| 37 */ | |
| 38 factory Pubspec.parse(String contents, SourceRegistry sources) { | |
| 39 var version = Version.none; | |
| 40 var dependencies = <PackageRef>[]; | |
| 41 | |
| 42 if (contents.trim() == '') return new Pubspec.empty(); | |
| 43 | |
| 44 var parsedPubspec = loadYaml(contents); | |
| 45 if (parsedPubspec is! Map) { | |
| 46 throw new FormatException('The pubspec must be a YAML mapping.'); | |
| 47 } | |
| 48 | |
| 49 if (parsedPubspec.containsKey('version')) { | |
| 50 version = new Version.parse(parsedPubspec['version']); | |
| 51 } | |
| 52 | |
| 53 if (parsedPubspec.containsKey('dependencies')) { | |
| 54 var dependencyEntries = parsedPubspec['dependencies']; | |
| 55 if (dependencyEntries is! Map || | |
| 56 dependencyEntries.getKeys().some((e) => e is! String)) { | |
| 57 throw new FormatException( | |
| 58 'The pubspec dependencies must be a map of package names.'); | |
| 59 } | |
| 60 | |
| 61 dependencyEntries.forEach((name, spec) { | |
| 62 var description, source; | |
| 63 if (spec == null) { | |
| 64 description = name; | |
| 65 source = sources.defaultSource; | |
| 66 } else if (spec is String) { | |
| 67 description = name; | |
| 68 source = sources.defaultSource; | |
| 69 // TODO(rnystrom): Support parsing version constraints like | |
| 70 // ">= 2.0.0 < 3.1.3". | |
| 71 version = new Version.parse(spec); | |
| 72 } else if (spec is Map) { | |
| 73 if (spec.containsKey('version')) { | |
| 74 // TODO(rnystrom): Support parsing version constraints like | |
| 75 // ">= 2.0.0 < 3.1.3". | |
| 76 version = new Version.parse(spec.remove('version')); | |
| 77 } | |
| 78 | |
| 79 var sourceNames = spec.getKeys(); | |
| 80 if (sourceNames.length > 1) { | |
| 81 throw new FormatException( | |
| 82 'Dependency $name may not have multiple sources: $sourceNames.') ; | |
|
nweiz
2012/05/18 21:16:01
Line length
Bob Nystrom
2012/05/18 22:13:04
Done.
| |
| 83 } | |
| 84 | |
| 85 var sourceName = only(sourceNames); | |
| 86 if (sourceName is! String) { | |
| 87 throw new FormatException( | |
| 88 'Source name $sourceName must be a string.'); | |
| 89 } | |
| 90 | |
| 91 source = sources[sourceName]; | |
| 92 description = spec[sourceName]; | |
| 93 } else { | |
| 94 throw new FormatException( | |
| 95 'Dependency specification $spec must be a string or a mapping.'); | |
| 96 } | |
| 97 | |
| 98 source.validateDescription(description); | |
| 99 | |
| 100 dependencies.add(new PackageRef( | |
| 101 name, source, Version.none, description)); | |
| 102 }); | |
| 103 } | |
| 104 | |
| 105 return new Pubspec._(version, dependencies); | |
| 106 } | |
| 107 } | |
| OLD | NEW |