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('utils.dart'); | |
| 10 #import('version.dart'); | |
| 11 #import('yaml/yaml.dart'); | |
| 12 | |
| 13 /** | |
| 14 * The parsed and validated contents of a pubspec file. | |
| 15 */ | |
| 16 class Pubspec { | |
| 17 /** | |
| 18 * Parses the pubspec at the given path. | |
| 19 */ | |
| 20 static Future<Pubspec> parse(String path, SourceRegistry sources) { | |
| 21 var version = Version.none; | |
| 22 var dependencies = <PackageRef>[]; | |
| 23 var completer = new Completer<Pubspec>(); | |
| 24 complete() => completer.complete(new Pubspec(version, dependencies)); | |
| 25 | |
| 26 // TODO(rnystrom): Handle the directory not existing. | |
| 27 // TODO(rnystrom): Error-handling. | |
| 28 var readFuture = readTextFile(path); | |
| 29 readFuture.handleException((error) { | |
| 30 // If there is no pubspec, we implicitly treat that as a package with no | |
| 31 // dependencies. | |
| 32 // TODO(rnystrom): Distinguish file not found from other real errors. | |
| 33 complete(); | |
| 34 return true; | |
| 35 }); | |
| 36 | |
| 37 readFuture.then((pubspec) { | |
| 38 if (pubspec.trim() == '') { | |
| 39 complete(); | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 var parsedPubspec = loadYaml(pubspec); | |
| 44 if (parsedPubspec is! Map) { | |
| 45 completer.completeException('The pubspec must be a YAML mapping.'); | |
| 46 } | |
| 47 | |
| 48 if (parsedPubspec.containsKey('version')) { | |
| 49 version = new Version.parse(parsedPubspec['version']); | |
| 50 } | |
| 51 | |
| 52 if (!parsedPubspec.containsKey('dependencies')) { | |
| 53 complete(); | |
| 54 return; | |
| 55 } | |
| 56 | |
| 57 var dependencyEntries = parsedPubspec['dependencies']; | |
| 58 if (dependencyEntries is! Map || | |
| 59 dependencyEntries.getKeys().some((e) => e is! String)) { | |
| 60 completer.completeException( | |
| 61 'The pubspec dependencies must be a map of package names.'); | |
| 62 } | |
| 63 | |
| 64 dependencyEntries.forEach((name, spec) { | |
| 65 var description, source; | |
| 66 // TODO(nweiz): parse the version once we have version handling | |
|
nweiz
2012/05/18 00:08:24
You can remove this now
Bob Nystrom
2012/05/18 20:02:38
Done.
| |
| 67 if (spec == null || spec is String) { | |
|
nweiz
2012/05/18 00:08:24
If spec is a string, you should parse it as a vers
Bob Nystrom
2012/05/18 20:02:38
Done.
| |
| 68 description = name; | |
| 69 source = sources.defaultSource; | |
| 70 } else if (spec is Map) { | |
| 71 if (spec.containsKey('version')) { | |
| 72 // TODO(rnystrom): Support parsing version constraints like | |
| 73 // ">= 2.0.0 < 3.1.3". | |
| 74 version = new Version.parse(spec['version']); | |
|
nweiz
2012/05/18 00:08:24
Replace spec['version'] here with spec.remove('ver
Bob Nystrom
2012/05/18 20:02:38
Done.
| |
| 75 spec.remove('version'); | |
| 76 } | |
| 77 | |
| 78 var sourceNames = spec.getKeys(); | |
| 79 if (sourceNames.length > 1) { | |
| 80 completer.completeException( | |
| 81 'Dependency $name may not have multiple sources: ' | |
| 82 '$sourceNames.'); | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 var sourceName = only(sourceNames); | |
| 87 if (sourceName is! String) { | |
| 88 completer.completeException( | |
| 89 'Source name $sourceName must be a string.'); | |
| 90 return; | |
| 91 } | |
| 92 source = sources[sourceName]; | |
| 93 description = spec[sourceName]; | |
| 94 } else { | |
| 95 completer.completeException( | |
| 96 'Dependency specification $spec must be a string or a mapping.'); | |
| 97 return; | |
| 98 } | |
| 99 | |
| 100 dependencies.add(new PackageRef( | |
| 101 name, source, Version.none, description)); | |
| 102 }); | |
| 103 | |
| 104 complete(); | |
| 105 }); | |
| 106 | |
| 107 return completer.future; | |
| 108 } | |
| 109 | |
| 110 /** | |
| 111 * This package's version. Defaults to [Version.none] if not present in | |
| 112 * the pubspec. | |
|
nweiz
2012/05/18 00:08:24
I wouldn't document the default here when it only
Bob Nystrom
2012/05/18 20:02:38
Done.
| |
| 113 */ | |
| 114 final Version version; | |
| 115 | |
| 116 /** | |
| 117 * The packages this package depends on. | |
| 118 */ | |
| 119 List<PackageRef> dependencies; | |
| 120 | |
| 121 Pubspec(this.version, this.dependencies); | |
| 122 } | |
| OLD | NEW |