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 /** | |
6 * The local cache of previously installed packages. | |
7 */ | |
8 class PackageCache { | |
9 /** | |
10 * The root directory where this package cache is located. | |
11 */ | |
12 final String rootDir; | |
13 | |
14 // TODO(rnystrom): When packages are versioned, String here and elsewhere will | |
15 // become a name/version/(source?) tuple. | |
16 final Map<String, Package> _loadedPackages; | |
17 | |
18 /** | |
19 * Packages which are currently being asynchronously loaded. | |
20 */ | |
21 final Map<String, Future<Package>> _pendingPackages; | |
22 | |
23 /** | |
24 * Creates a new package cache which is backed by the given directory on the | |
25 * user's file system. | |
26 */ | |
27 PackageCache(this.rootDir) | |
28 : _loadedPackages = <Package>{}, | |
29 _pendingPackages = <Future<Package>>{}; | |
30 | |
31 /** | |
32 * Loads all of the packages in the cache and returns them. | |
33 */ | |
34 Future<List<Package>> listAll() { | |
35 return listDir(rootDir).chain((paths) { | |
36 final packages = paths.map((path) => find(basename(path))); | |
37 return Futures.wait(packages); | |
38 }); | |
39 } | |
40 | |
41 /** | |
42 * Loads the package named [name] from this cache, if present. | |
43 */ | |
44 // TODO(rnystrom): What happens if the package isn't cached? | |
45 Future<Package> find(String name) { | |
46 // Use the previously loaded one. | |
47 final package = _loadedPackages[name]; | |
48 if (package != null) { | |
49 return new Future.immediate(package); | |
50 } | |
51 | |
52 // If we are already in-progress loading it, re-use that one. | |
53 final pending = _pendingPackages[name]; | |
54 if (pending != null) { | |
55 return pending; | |
56 } | |
57 | |
58 return _loadPackage(name); | |
59 } | |
60 | |
61 /** | |
62 * Start loading the package. | |
63 */ | |
64 Future<Package> _loadPackage(String name) { | |
65 final completer = new Completer<Package>(); | |
66 final pubspecPath = join(rootDir, name, 'pubspec'); | |
nweiz
2012/04/17 20:08:46
Unused variable
Bob Nystrom
2012/04/18 18:09:18
Done.
| |
67 | |
68 _pendingPackages[name] = completer.future; | |
69 _parsePubspec(name).then((dependencies) { | |
nweiz
2012/04/17 20:08:46
I think using transform here rather than manually
Bob Nystrom
2012/04/18 18:09:18
Great idea. Still learning my way around transform
| |
70 final package = new Package._(this, name, dependencies); | |
71 | |
72 _pendingPackages.remove(name); | |
73 _loadedPackages[name] = package; | |
74 completer.complete(package); | |
75 }); | |
76 | |
77 return completer.future; | |
78 } | |
79 | |
80 Future<List<String>> _parsePubspec(String name) { | |
81 final completer = new Completer<List<String>>(); | |
82 final pubspecPath = join(rootDir, name, 'pubspec'); | |
83 | |
84 // TODO(rnystrom): Handle the directory not existing. | |
85 // TODO(rnystrom): Error-handling. | |
86 final readFuture = readTextFile(pubspecPath); | |
87 readFuture.handleException((error) { | |
88 // If there is no pubspec, we implicitly treat that as a package with no | |
89 // dependencies. | |
90 // TODO(rnystrom): Distinguish file not found from other real errors. | |
91 completer.complete(<String>[]); | |
92 return true; | |
93 }); | |
94 | |
95 readFuture.then((pubspec) { | |
nweiz
2012/04/17 20:08:46
I feel like it's a flaw in our Future API that you
Bob Nystrom
2012/04/18 18:09:18
Done.
| |
96 // TODO(rnystrom): Use YAML parser when ready. For now, it's just a flat | |
97 // list of newline-separated strings. | |
98 final dependencyNames = pubspec.split('\n') | |
99 .map((name) => name.trim()) | |
nweiz
2012/04/17 20:08:46
I thought the style was putting the . on the previ
Bob Nystrom
2012/04/18 18:09:18
Done.
| |
100 .filter((name) => (name != null) && (name != '')); | |
101 | |
102 completer.complete(dependencyNames); | |
103 }); | |
104 | |
105 return completer.future; | |
106 } | |
107 } | |
OLD | NEW |