| 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 /** | 5 /** |
| 6 * A named, versioned, unit of code and resource reuse. | 6 * A named, versioned, unit of code and resource reuse. |
| 7 */ | 7 */ |
| 8 class Package implements Hashable { | 8 class Package implements Hashable { |
| 9 /** | 9 /** |
| 10 * Loads the package whose root directory is [packageDir]. | 10 * Loads the package whose root directory is [packageDir]. |
| 11 */ | 11 */ |
| 12 static Future<Package> load(String packageDir, SourceRegistry sources) { | 12 static Future<Package> load(String packageDir) { |
| 13 final pubspecPath = join(packageDir, 'pubspec'); | 13 final pubspecPath = join(packageDir, 'pubspec'); |
| 14 | 14 |
| 15 return _parsePubspec(pubspecPath, sources).transform((dependencies) { | 15 return _parsePubspec(pubspecPath).transform((dependencies) { |
| 16 return new Package._(packageDir, dependencies); | 16 return new Package._(packageDir, dependencies); |
| 17 }); | 17 }); |
| 18 } | 18 } |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * The path to the directory containing the package. | 21 * The path to the directory containing the package. |
| 22 */ | 22 */ |
| 23 final String dir; | 23 final String dir; |
| 24 | 24 |
| 25 /** | 25 /** |
| (...skipping 24 matching lines...) Expand all Loading... |
| 50 | 50 |
| 51 /** | 51 /** |
| 52 * Returns a debug string for the package. | 52 * Returns a debug string for the package. |
| 53 */ | 53 */ |
| 54 String toString() => '$name ($dir)'; | 54 String toString() => '$name ($dir)'; |
| 55 | 55 |
| 56 /** | 56 /** |
| 57 * Parses the pubspec at the given path and returns the list of package | 57 * Parses the pubspec at the given path and returns the list of package |
| 58 * dependencies it exposes. | 58 * dependencies it exposes. |
| 59 */ | 59 */ |
| 60 static Future<List<PackageId>> _parsePubspec(String path, | 60 static Future<List<PackageId>> _parsePubspec(String path) { |
| 61 SourceRegistry sources) { | |
| 62 final completer = new Completer<List<PackageId>>(); | 61 final completer = new Completer<List<PackageId>>(); |
| 63 | 62 |
| 64 // TODO(rnystrom): Handle the directory not existing. | 63 // TODO(rnystrom): Handle the directory not existing. |
| 65 // TODO(rnystrom): Error-handling. | 64 // TODO(rnystrom): Error-handling. |
| 66 final readFuture = readTextFile(path); | 65 final readFuture = readTextFile(path); |
| 67 readFuture.handleException((error) { | 66 readFuture.handleException((error) { |
| 68 // If there is no pubspec, we implicitly treat that as a package with no | 67 // If there is no pubspec, we implicitly treat that as a package with no |
| 69 // dependencies. | 68 // dependencies. |
| 70 // TODO(rnystrom): Distinguish file not found from other real errors. | 69 // TODO(rnystrom): Distinguish file not found from other real errors. |
| 71 completer.complete(<PackageId>[]); | 70 completer.complete(<PackageId>[]); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 82 if (parsedPubspec is! Map) { | 81 if (parsedPubspec is! Map) { |
| 83 completer.completeException('The pubspec must be a YAML mapping.'); | 82 completer.completeException('The pubspec must be a YAML mapping.'); |
| 84 } | 83 } |
| 85 | 84 |
| 86 if (!parsedPubspec.containsKey('dependencies')) { | 85 if (!parsedPubspec.containsKey('dependencies')) { |
| 87 completer.complete(<String>[]); | 86 completer.complete(<String>[]); |
| 88 return; | 87 return; |
| 89 } | 88 } |
| 90 | 89 |
| 91 var dependencies = parsedPubspec['dependencies']; | 90 var dependencies = parsedPubspec['dependencies']; |
| 92 if (dependencies is! Map || | 91 if (dependencies.some((e) => e is! String)) { |
| 93 dependencies.getKeys().some((e) => e is! String)) { | |
| 94 completer.completeException( | 92 completer.completeException( |
| 95 'The pubspec dependencies must be a map of package names.'); | 93 'The pubspec dependencies must be a list of package names.'); |
| 96 } | 94 } |
| 97 | 95 |
| 98 var dependencyIds = <PackageId>[]; | 96 var dependencyIds = |
| 99 dependencies.forEach((name, spec) { | 97 dependencies.map((name) => new PackageId(name, Source.defaultSource)); |
| 100 var fullName, source; | |
| 101 // TODO(nweiz): parse the version once we have version handling | |
| 102 if (spec == null || spec is String) { | |
| 103 fullName = name; | |
| 104 source = sources.defaultSource; | |
| 105 } else if (spec is Map) { | |
| 106 spec.remove('version'); | |
| 107 | |
| 108 var sourceNames = spec.getKeys(); | |
| 109 if (sourceNames.length > 1) { | |
| 110 completer.completeException( | |
| 111 'Dependency $name may not have multiple sources: ' | |
| 112 '$sourceNames.'); | |
| 113 return; | |
| 114 } | |
| 115 | |
| 116 var sourceName = only(sourceNames); | |
| 117 if (sourceName is! String) { | |
| 118 completer.completeException( | |
| 119 'Source name $sourceName must be a string.'); | |
| 120 return; | |
| 121 } | |
| 122 source = sources[sourceName]; | |
| 123 | |
| 124 // TODO(nweiz): At some point we want fullName to be able to be an | |
| 125 // arbitrary object that's parsed by the source. | |
| 126 fullName = spec[sourceName]; | |
| 127 if (fullName is! String) { | |
| 128 completer.completeException( | |
| 129 'Source identifier $fullName must be a string.'); | |
| 130 return; | |
| 131 } | |
| 132 } else { | |
| 133 completer.completeException( | |
| 134 'Dependency specification $spec must be a string or a mapping.'); | |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 var id = new PackageId(fullName, source); | |
| 139 var nameFromSource = source.packageName(id); | |
| 140 if (nameFromSource != name) { | |
| 141 completer.completeException( | |
| 142 'Dependency name "$name" doesn\'t match name "$nameFromSource" ' | |
| 143 'from source "${source.name}".'); | |
| 144 return; | |
| 145 } | |
| 146 | |
| 147 dependencyIds.add(id); | |
| 148 }); | |
| 149 completer.complete(dependencyIds); | 98 completer.complete(dependencyIds); |
| 150 }); | 99 }); |
| 151 | 100 |
| 152 return completer.future; | 101 return completer.future; |
| 153 } | 102 } |
| 154 } | 103 } |
| 155 | 104 |
| 156 /** | 105 /** |
| 157 * A unique identifier for a package. A given package id specifies a single | 106 * A unique identifier for a package. A given package id specifies a single |
| 158 * chunk of code and resources. | 107 * chunk of code and resources. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 | 146 |
| 198 String toString() => "$fullName from ${source.name}"; | 147 String toString() => "$fullName from ${source.name}"; |
| 199 | 148 |
| 200 int compareTo(Comparable other) { | 149 int compareTo(Comparable other) { |
| 201 if (other is! PackageId) throw new IllegalArgumentException(other); | 150 if (other is! PackageId) throw new IllegalArgumentException(other); |
| 202 var sourceComp = this.source.name.compareTo(other.source.name); | 151 var sourceComp = this.source.name.compareTo(other.source.name); |
| 203 if (sourceComp != 0) return sourceComp; | 152 if (sourceComp != 0) return sourceComp; |
| 204 return this.fullName.compareTo(other.fullName); | 153 return this.fullName.compareTo(other.fullName); |
| 205 } | 154 } |
| 206 } | 155 } |
| OLD | NEW |