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 /// A named, versioned, unit of code and resource reuse. | 14 /// A named, versioned, unit of code and resource reuse. |
15 class Package { | 15 class Package { |
16 /// Loads the package whose root directory is [packageDir]. [name] is the | |
17 /// expected name of that package (e.g. the name given in the dependency), or | |
18 /// null if the package being loaded is the entrypoint package. | |
19 static Future<Package> load(String name, String packageDir, | |
20 SourceRegistry sources) { | |
21 var pubspecPath = join(packageDir, 'pubspec.yaml'); | |
22 | |
23 return fileExists(pubspecPath).then((exists) { | |
24 if (!exists) throw new PubspecNotFoundException(name); | |
25 return readTextFile(pubspecPath); | |
26 }).then((contents) { | |
27 try { | |
28 var pubspec = new Pubspec.parse(contents, sources); | |
29 | |
30 if (pubspec.name == null) throw new PubspecHasNoNameException(name); | |
31 if (name != null && pubspec.name != name) { | |
32 throw new PubspecNameMismatchException(name, pubspec.name); | |
33 } | |
34 return new Package._(packageDir, pubspec); | |
35 } on FormatException catch (ex) { | |
36 throw 'Could not parse $pubspecPath:\n${ex.message}'; | |
37 } | |
38 }); | |
39 } | |
40 | |
41 /// The path to the directory containing the package. | 16 /// The path to the directory containing the package. |
42 final String dir; | 17 final String dir; |
43 | 18 |
44 /// The name of the package. | 19 /// The name of the package. |
45 String get name { | 20 String get name { |
46 if (pubspec.name != null) return pubspec.name; | 21 if (pubspec.name != null) return pubspec.name; |
47 if (dir != null) return basename(dir); | 22 if (dir != null) return basename(dir); |
48 return null; | 23 return null; |
49 } | 24 } |
50 | 25 |
51 /// The package's version. | 26 /// The package's version. |
52 Version get version => pubspec.version; | 27 Version get version => pubspec.version; |
53 | 28 |
54 /// The parsed pubspec associated with this package. | 29 /// The parsed pubspec associated with this package. |
55 final Pubspec pubspec; | 30 final Pubspec pubspec; |
56 | 31 |
57 /// The ids of the packages that this package depends on. This is what is | 32 /// The ids of the packages that this package depends on. This is what is |
58 /// specified in the pubspec when this package depends on another. | 33 /// specified in the pubspec when this package depends on another. |
59 Collection<PackageRef> get dependencies => pubspec.dependencies; | 34 Collection<PackageRef> get dependencies => pubspec.dependencies; |
60 | 35 |
36 /// Loads the package whose root directory is [packageDir]. [name] is the | |
37 /// expected name of that package (e.g. the name given in the dependency), or | |
38 /// `null` if the package being loaded is the entrypoint package. | |
39 factory Package(String name, String packageDir, SourceRegistry sources) { | |
nweiz
2013/02/01 02:05:55
It may still be correct to call this "Package.load
Bob Nystrom
2013/02/01 23:17:21
I'm personally OK with that being implied by the f
| |
40 var pubspecPath = join(packageDir, 'pubspec.yaml'); | |
41 if (!fileExists(pubspecPath)) throw new PubspecNotFoundException(name); | |
42 | |
43 try { | |
44 var pubspec = new Pubspec.parse(readTextFile(pubspecPath), sources); | |
45 | |
46 if (pubspec.name == null) { | |
47 throw new PubspecHasNoNameException(name); | |
48 } | |
49 | |
50 if (name != null && pubspec.name != name) { | |
51 throw new PubspecNameMismatchException(name, pubspec.name); | |
52 } | |
53 | |
54 return new Package._(packageDir, pubspec); | |
55 } on FormatException catch (ex) { | |
56 throw 'Could not parse $pubspecPath:\n${ex.message}'; | |
57 } | |
58 } | |
59 | |
61 /// Constructs a package with the given pubspec. The package will have no | 60 /// Constructs a package with the given pubspec. The package will have no |
62 /// directory associated with it. | 61 /// directory associated with it. |
63 Package.inMemory(this.pubspec) | 62 Package.inMemory(this.pubspec) |
64 : dir = null; | 63 : dir = null; |
65 | 64 |
66 /// Constructs a package. This should not be called directly. Instead, acquire | 65 /// Constructs a package. This should not be called directly. Instead, acquire |
67 /// packages from [load()]. | 66 /// packages from [load()]. |
68 Package._(this.dir, this.pubspec); | 67 Package._(this.dir, this.pubspec); |
69 | 68 |
70 /// Returns a debug string for the package. | 69 /// Returns a debug string for the package. |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 | 196 |
198 class PubspecNameMismatchException implements Exception { | 197 class PubspecNameMismatchException implements Exception { |
199 final String expectedName; | 198 final String expectedName; |
200 final String actualName; | 199 final String actualName; |
201 | 200 |
202 PubspecNameMismatchException(this.expectedName, this.actualName); | 201 PubspecNameMismatchException(this.expectedName, this.actualName); |
203 | 202 |
204 String toString() => 'The name you specified for your dependency, ' | 203 String toString() => 'The name you specified for your dependency, ' |
205 '"$expectedName", doesn\'t match the name "$actualName" in its pubspec.'; | 204 '"$expectedName", doesn\'t match the name "$actualName" in its pubspec.'; |
206 } | 205 } |
OLD | NEW |