| 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 package; | 5 library package; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'io.dart'; | 8 import 'io.dart'; |
| 9 import 'pubspec.dart'; | 9 import 'pubspec.dart'; |
| 10 import 'source.dart'; | 10 import 'source.dart'; |
| 11 import 'source_registry.dart'; | 11 import 'source_registry.dart'; |
| 12 import 'version.dart'; | 12 import 'version.dart'; |
| 13 | 13 |
| 14 final _README_REGEXP = new RegExp(r"^README($|\.)", caseSensitive: false); | 14 final _README_REGEXP = new RegExp(r"^README($|\.)", caseSensitive: false); |
| 15 | 15 |
| 16 /// A named, versioned, unit of code and resource reuse. | 16 /// A named, versioned, unit of code and resource reuse. |
| 17 class Package { | 17 class Package { |
| 18 /// Loads the package whose root directory is [packageDir]. [name] is the | |
| 19 /// expected name of that package (e.g. the name given in the dependency), or | |
| 20 /// null if the package being loaded is the entrypoint package. | |
| 21 static Future<Package> load(String name, String packageDir, | |
| 22 SourceRegistry sources) { | |
| 23 var pubspecPath = join(packageDir, 'pubspec.yaml'); | |
| 24 | |
| 25 return fileExists(pubspecPath).then((exists) { | |
| 26 if (!exists) throw new PubspecNotFoundException(name); | |
| 27 return readTextFile(pubspecPath); | |
| 28 }).then((contents) { | |
| 29 try { | |
| 30 var pubspec = new Pubspec.parse(contents, sources); | |
| 31 | |
| 32 if (pubspec.name == null) throw new PubspecHasNoNameException(name); | |
| 33 if (name != null && pubspec.name != name) { | |
| 34 throw new PubspecNameMismatchException(name, pubspec.name); | |
| 35 } | |
| 36 return new Package._(packageDir, pubspec); | |
| 37 } on FormatException catch (ex) { | |
| 38 throw 'Could not parse $pubspecPath:\n${ex.message}'; | |
| 39 } | |
| 40 }); | |
| 41 } | |
| 42 | |
| 43 /// The path to the directory containing the package. | 18 /// The path to the directory containing the package. |
| 44 final String dir; | 19 final String dir; |
| 45 | 20 |
| 46 /// The name of the package. | 21 /// The name of the package. |
| 47 String get name { | 22 String get name { |
| 48 if (pubspec.name != null) return pubspec.name; | 23 if (pubspec.name != null) return pubspec.name; |
| 49 if (dir != null) return basename(dir); | 24 if (dir != null) return basename(dir); |
| 50 return null; | 25 return null; |
| 51 } | 26 } |
| 52 | 27 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 73 return readmes.min((readme1, readme2) { | 48 return readmes.min((readme1, readme2) { |
| 74 var extensions1 = ".".allMatches(readme1).length; | 49 var extensions1 = ".".allMatches(readme1).length; |
| 75 var extensions2 = ".".allMatches(readme2).length; | 50 var extensions2 = ".".allMatches(readme2).length; |
| 76 var comparison = extensions1.compareTo(extensions2); | 51 var comparison = extensions1.compareTo(extensions2); |
| 77 if (comparison != 0) return comparison; | 52 if (comparison != 0) return comparison; |
| 78 return readme1.compareTo(readme2); | 53 return readme1.compareTo(readme2); |
| 79 }); | 54 }); |
| 80 }); | 55 }); |
| 81 } | 56 } |
| 82 | 57 |
| 58 /// Loads the package whose root directory is [packageDir]. [name] is the |
| 59 /// expected name of that package (e.g. the name given in the dependency), or |
| 60 /// `null` if the package being loaded is the entrypoint package. |
| 61 factory Package(String name, String packageDir, SourceRegistry sources) { |
| 62 var pubspecPath = join(packageDir, 'pubspec.yaml'); |
| 63 if (!fileExists(pubspecPath)) throw new PubspecNotFoundException(name); |
| 64 |
| 65 try { |
| 66 var pubspec = new Pubspec.parse(readTextFile(pubspecPath), sources); |
| 67 |
| 68 if (pubspec.name == null) { |
| 69 throw new PubspecHasNoNameException(name); |
| 70 } |
| 71 |
| 72 if (name != null && pubspec.name != name) { |
| 73 throw new PubspecNameMismatchException(name, pubspec.name); |
| 74 } |
| 75 |
| 76 return new Package._(packageDir, pubspec); |
| 77 } on FormatException catch (ex) { |
| 78 throw 'Could not parse $pubspecPath:\n${ex.message}'; |
| 79 } |
| 80 } |
| 81 |
| 83 /// Constructs a package with the given pubspec. The package will have no | 82 /// Constructs a package with the given pubspec. The package will have no |
| 84 /// directory associated with it. | 83 /// directory associated with it. |
| 85 Package.inMemory(this.pubspec) | 84 Package.inMemory(this.pubspec) |
| 86 : dir = null; | 85 : dir = null; |
| 87 | 86 |
| 88 /// Constructs a package. This should not be called directly. Instead, acquire | 87 /// Constructs a package. This should not be called directly. Instead, acquire |
| 89 /// packages from [load()]. | 88 /// packages from [load()]. |
| 90 Package._(this.dir, this.pubspec); | 89 Package._(this.dir, this.pubspec); |
| 91 | 90 |
| 92 /// Returns a debug string for the package. | 91 /// Returns a debug string for the package. |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 | 222 |
| 224 class PubspecNameMismatchException implements Exception { | 223 class PubspecNameMismatchException implements Exception { |
| 225 final String expectedName; | 224 final String expectedName; |
| 226 final String actualName; | 225 final String actualName; |
| 227 | 226 |
| 228 PubspecNameMismatchException(this.expectedName, this.actualName); | 227 PubspecNameMismatchException(this.expectedName, this.actualName); |
| 229 | 228 |
| 230 String toString() => 'The name you specified for your dependency, ' | 229 String toString() => 'The name you specified for your dependency, ' |
| 231 '"$expectedName", doesn\'t match the name "$actualName" in its pubspec.'; | 230 '"$expectedName", doesn\'t match the name "$actualName" in its pubspec.'; |
| 232 } | 231 } |
| OLD | NEW |