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 library system_cache; | |
6 | |
7 import 'dart:io'; | |
8 import 'dart:async'; | |
9 | |
10 import 'package:pathos/path.dart' as path; | |
11 | |
12 import 'git_source.dart'; | |
13 import 'hosted_source.dart'; | |
14 import 'io.dart'; | |
15 import 'io.dart' as io show createTempDir; | |
16 import 'log.dart' as log; | |
17 import 'package.dart'; | |
18 import 'path_source.dart'; | |
19 import 'pubspec.dart'; | |
20 import 'source.dart'; | |
21 import 'source_registry.dart'; | |
22 import 'utils.dart'; | |
23 import 'version.dart'; | |
24 | |
25 /// The system-wide cache of installed packages. | |
26 /// | |
27 /// This cache contains all packages that are downloaded from the internet. | |
28 /// Packages that are available locally (e.g. path dependencies) don't use this | |
29 /// cache. | |
30 class SystemCache { | |
31 /// The root directory where this package cache is located. | |
32 final String rootDir; | |
33 | |
34 String get tempDir => path.join(rootDir, '_temp'); | |
35 | |
36 /// Packages which are currently being asynchronously installed to the cache. | |
37 final Map<PackageId, Future<Package>> _pendingInstalls; | |
38 | |
39 /// The sources from which to install packages. | |
40 final SourceRegistry sources; | |
41 | |
42 /// Creates a new package cache which is backed by the given directory on the | |
43 /// user's file system. | |
44 SystemCache(this.rootDir) | |
45 : _pendingInstalls = new Map<PackageId, Future<Package>>(), | |
46 sources = new SourceRegistry(); | |
47 | |
48 /// Creates a system cache and registers the standard set of sources. | |
49 factory SystemCache.withSources(String rootDir) { | |
50 var cache = new SystemCache(rootDir); | |
51 cache.register(new GitSource()); | |
52 cache.register(new HostedSource()); | |
53 cache.register(new PathSource()); | |
54 cache.sources.setDefault('hosted'); | |
55 return cache; | |
56 } | |
57 | |
58 /// Registers a new source. This source must not have the same name as a | |
59 /// source that's already been registered. | |
60 void register(Source source) { | |
61 source.bind(this); | |
62 sources.register(source); | |
63 } | |
64 | |
65 /// Gets the package identified by [id]. If the package is already cached, | |
66 /// reads it from the cache. Otherwise, requests it from the source. | |
67 Future<Pubspec> describe(PackageId id) { | |
68 if (id.isRoot) throw new ArgumentError("Cannot describe the root package."); | |
69 | |
70 // Try to get it from the system cache first. | |
71 if (id.source.shouldCache) { | |
72 return id.systemCacheDirectory.then((packageDir) { | |
73 if (!fileExists(path.join(packageDir, "pubspec.yaml"))) { | |
74 return id.source.describe(id); | |
75 } | |
76 | |
77 return new Pubspec.load(id.name, packageDir, sources); | |
78 }); | |
79 } | |
80 | |
81 // Not cached, so get it from the source. | |
82 return id.source.describe(id); | |
83 } | |
84 | |
85 /// Ensures that the package identified by [id] is installed to the cache, | |
86 /// loads it, and returns it. | |
87 /// | |
88 /// It is an error to try installing a package from a source with `shouldCache | |
89 /// == false` to the system cache. | |
90 Future<Package> install(PackageId id) { | |
91 if (!id.source.shouldCache) { | |
92 throw new ArgumentError("Package $id is not cacheable."); | |
93 } | |
94 | |
95 var pending = _pendingInstalls[id]; | |
96 if (pending != null) return pending; | |
97 | |
98 var future = id.source.installToSystemCache(id) | |
99 .whenComplete(() { _pendingInstalls.remove(id); }); | |
100 _pendingInstalls[id] = future; | |
101 return future; | |
102 } | |
103 | |
104 /// Create a new temporary directory within the system cache. The system | |
105 /// cache maintains its own temporary directory that it uses to stage | |
106 /// packages into while installing. It uses this instead of the OS's system | |
107 /// temp directory to ensure that it's on the same volume as the pub system | |
108 /// cache so that it can move the directory from it. | |
109 String createTempDir() { | |
110 var temp = ensureDir(tempDir); | |
111 return io.createTempDir(path.join(temp, 'dir')); | |
112 } | |
113 | |
114 /// Deletes the system cache's internal temp directory. | |
115 void deleteTempDir() { | |
116 log.fine('Clean up system cache temp directory $tempDir.'); | |
117 if (dirExists(tempDir)) deleteEntry(tempDir); | |
118 } | |
119 } | |
OLD | NEW |