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 system_cache; | 5 library system_cache; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'git_source.dart'; | 9 import 'git_source.dart'; |
10 import 'hosted_source.dart'; | 10 import 'hosted_source.dart'; |
11 import 'io.dart'; | 11 import 'io.dart'; |
12 import 'io.dart' as io show createTempDir; | 12 import 'io.dart' as io show createTempDir; |
13 import 'log.dart' as log; | 13 import 'log.dart' as log; |
14 import 'package.dart'; | 14 import 'package.dart'; |
15 import 'sdk_source.dart'; | 15 import 'sdk_source.dart'; |
16 import 'source.dart'; | 16 import 'source.dart'; |
17 import 'source_registry.dart'; | 17 import 'source_registry.dart'; |
18 import 'utils.dart'; | 18 import 'utils.dart'; |
19 import 'version.dart'; | 19 import 'version.dart'; |
20 | 20 |
21 /** | 21 /// The system-wide cache of installed packages. |
22 * The system-wide cache of installed packages. | 22 /// |
23 * | 23 /// This cache contains all packages that are downloaded from the internet. |
24 * This cache contains all packages that are downloaded from the internet. | 24 /// Packages that are available locally (e.g. from the SDK) don't use this |
25 * Packages that are available locally (e.g. from the SDK) don't use this cache. | 25 /// cache. |
26 */ | |
27 class SystemCache { | 26 class SystemCache { |
28 /** | 27 /// The root directory where this package cache is located. |
29 * The root directory where this package cache is located. | |
30 */ | |
31 final String rootDir; | 28 final String rootDir; |
32 | 29 |
33 String get tempDir => join(rootDir, '_temp'); | 30 String get tempDir => join(rootDir, '_temp'); |
34 | 31 |
35 /** | 32 /// Packages which are currently being asynchronously installed to the cache. |
36 * Packages which are currently being asynchronously installed to the cache. | |
37 */ | |
38 final Map<PackageId, Future<Package>> _pendingInstalls; | 33 final Map<PackageId, Future<Package>> _pendingInstalls; |
39 | 34 |
40 /** | 35 /// The sources from which to install packages. |
41 * The sources from which to install packages. | |
42 */ | |
43 final SourceRegistry sources; | 36 final SourceRegistry sources; |
44 | 37 |
45 /** | 38 /// Creates a new package cache which is backed by the given directory on the |
46 * Creates a new package cache which is backed by the given directory on the | 39 /// user's file system. |
47 * user's file system. | |
48 */ | |
49 SystemCache(this.rootDir) | 40 SystemCache(this.rootDir) |
50 : _pendingInstalls = new Map<PackageId, Future<Package>>(), | 41 : _pendingInstalls = new Map<PackageId, Future<Package>>(), |
51 sources = new SourceRegistry(); | 42 sources = new SourceRegistry(); |
52 | 43 |
53 /// Creates a system cache and registers the standard set of sources. | 44 /// Creates a system cache and registers the standard set of sources. |
54 factory SystemCache.withSources(String rootDir, String sdkDir) { | 45 factory SystemCache.withSources(String rootDir, String sdkDir) { |
55 var cache = new SystemCache(rootDir); | 46 var cache = new SystemCache(rootDir); |
56 cache.register(new SdkSource(sdkDir)); | 47 cache.register(new SdkSource(sdkDir)); |
57 cache.register(new GitSource()); | 48 cache.register(new GitSource()); |
58 cache.register(new HostedSource()); | 49 cache.register(new HostedSource()); |
59 cache.sources.setDefault('hosted'); | 50 cache.sources.setDefault('hosted'); |
60 return cache; | 51 return cache; |
61 } | 52 } |
62 | 53 |
63 /** | 54 /// Registers a new source. This source must not have the same name as a |
64 * Registers a new source. This source must not have the same name as a source | 55 /// source that's already been registered. |
65 * that's already been registered. | |
66 */ | |
67 void register(Source source) { | 56 void register(Source source) { |
68 source.bind(this); | 57 source.bind(this); |
69 sources.register(source); | 58 sources.register(source); |
70 } | 59 } |
71 | 60 |
72 /** | 61 /// Ensures that the package identified by [id] is installed to the cache, |
73 * Ensures that the package identified by [id] is installed to the cache, | 62 /// loads it, and returns it. |
74 * loads it, and returns it. | 63 /// |
75 * | 64 /// It is an error to try installing a package from a source with `shouldCache |
76 * It is an error to try installing a package from a source with `shouldCache | 65 /// == false` to the system cache. |
77 * == false` to the system cache. | |
78 */ | |
79 Future<Package> install(PackageId id) { | 66 Future<Package> install(PackageId id) { |
80 if (!id.source.shouldCache) { | 67 if (!id.source.shouldCache) { |
81 throw new ArgumentError("Package $id is not cacheable."); | 68 throw new ArgumentError("Package $id is not cacheable."); |
82 } | 69 } |
83 | 70 |
84 var pending = _pendingInstalls[id]; | 71 var pending = _pendingInstalls[id]; |
85 if (pending != null) return pending; | 72 if (pending != null) return pending; |
86 | 73 |
87 var future = id.source.installToSystemCache(id); | 74 var future = id.source.installToSystemCache(id); |
88 always(future, () => _pendingInstalls.remove(id)); | 75 always(future, () => _pendingInstalls.remove(id)); |
(...skipping 14 matching lines...) Expand all Loading... |
103 | 90 |
104 /// Delete's the system cache's internal temp directory. | 91 /// Delete's the system cache's internal temp directory. |
105 Future deleteTempDir() { | 92 Future deleteTempDir() { |
106 log.fine('Clean up system cache temp directory $tempDir.'); | 93 log.fine('Clean up system cache temp directory $tempDir.'); |
107 return dirExists(tempDir).chain((exists) { | 94 return dirExists(tempDir).chain((exists) { |
108 if (!exists) return new Future.immediate(null); | 95 if (!exists) return new Future.immediate(null); |
109 return deleteDir(tempDir); | 96 return deleteDir(tempDir); |
110 }); | 97 }); |
111 } | 98 } |
112 } | 99 } |
OLD | NEW |