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 import 'dart:io'; | 5 import 'dart:io'; |
6 | 6 |
7 import 'package:path/path.dart' as path; | 7 import 'package:path/path.dart' as p; |
8 | 8 |
9 import 'io.dart'; | 9 import 'io.dart'; |
10 import 'io.dart' as io show createTempDir; | 10 import 'io.dart' as io show createTempDir; |
11 import 'log.dart' as log; | 11 import 'log.dart' as log; |
12 import 'package.dart'; | 12 import 'package.dart'; |
13 import 'source/cached.dart'; | 13 import 'source/cached.dart'; |
14 import 'source/git.dart'; | 14 import 'source/git.dart'; |
15 import 'source/hosted.dart'; | 15 import 'source/hosted.dart'; |
16 import 'source/path.dart'; | 16 import 'source/path.dart'; |
| 17 import 'source/unknown.dart'; |
17 import 'source.dart'; | 18 import 'source.dart'; |
18 import 'source_registry.dart'; | 19 import 'source_registry.dart'; |
19 | 20 |
20 /// The system-wide cache of downloaded packages. | 21 /// The system-wide cache of downloaded packages. |
21 /// | 22 /// |
22 /// This cache contains all packages that are downloaded from the internet. | 23 /// This cache contains all packages that are downloaded from the internet. |
23 /// Packages that are available locally (e.g. path dependencies) don't use this | 24 /// Packages that are available locally (e.g. path dependencies) don't use this |
24 /// cache. | 25 /// cache. |
25 class SystemCache { | 26 class SystemCache { |
26 /// The root directory where this package cache is located. | 27 /// The root directory where this package cache is located. |
27 final String rootDir; | 28 final String rootDir; |
28 | 29 |
29 String get tempDir => path.join(rootDir, '_temp'); | 30 String get tempDir => p.join(rootDir, '_temp'); |
30 | |
31 /// The sources from which to get packages. | |
32 final sources = new SourceRegistry(); | |
33 | 31 |
34 static String defaultDir = (() { | 32 static String defaultDir = (() { |
35 if (Platform.environment.containsKey('PUB_CACHE')) { | 33 if (Platform.environment.containsKey('PUB_CACHE')) { |
36 return Platform.environment['PUB_CACHE']; | 34 return Platform.environment['PUB_CACHE']; |
37 } else if (Platform.operatingSystem == 'windows') { | 35 } else if (Platform.operatingSystem == 'windows') { |
38 var appData = Platform.environment['APPDATA']; | 36 var appData = Platform.environment['APPDATA']; |
39 return path.join(appData, 'Pub', 'Cache'); | 37 return p.join(appData, 'Pub', 'Cache'); |
40 } else { | 38 } else { |
41 return '${Platform.environment['HOME']}/.pub-cache'; | 39 return '${Platform.environment['HOME']}/.pub-cache'; |
42 } | 40 } |
43 })(); | 41 })(); |
44 | 42 |
45 /// Creates a new package cache which is backed by the given directory on the | 43 /// The registry for sources used by this system cache. |
46 /// user's file system. | 44 /// |
47 SystemCache([String rootDir]) | 45 /// New sources registered here will be available through [liveSources] and |
48 : rootDir = rootDir == null ? SystemCache.defaultDir : rootDir; | 46 /// [liveSource]. |
| 47 final sources = new SourceRegistry(); |
49 | 48 |
50 /// Creates a system cache and registers the standard set of sources. | 49 /// The live sources bound to this cache. |
| 50 final _liveSources = <String, LiveSource>{}; |
| 51 |
| 52 /// The live sources bound to this cache, in name order. |
| 53 List<LiveSource> get liveSources { |
| 54 return sources.sources.map((source) { |
| 55 return _liveSources.putIfAbsent(source.name, () => source.bind(this)); |
| 56 }).toList(); |
| 57 } |
| 58 |
| 59 /// The built-in live Git source bound to this cache. |
| 60 LiveGitSource get git => _liveSources["git"] as LiveGitSource; |
| 61 |
| 62 /// The built-in live hosted source bound to this cache. |
| 63 LiveHostedSource get hosted => _liveSources["hosted"] as LiveHostedSource; |
| 64 |
| 65 /// The built-in live path source bound to this cache. |
| 66 LivePathSource get path => _liveSources["path"] as LivePathSource; |
| 67 |
| 68 /// The default source bound to this cache. |
| 69 LiveSource get defaultSource => liveSource(null); |
| 70 |
| 71 /// Creates a system cache and registers all sources in [sources]. |
51 /// | 72 /// |
52 /// If [isOffline] is `true`, then the offline hosted source will be used. | 73 /// If [isOffline] is `true`, then the offline hosted source will be used. |
53 /// Defaults to `false`. | 74 /// Defaults to `false`. |
54 factory SystemCache.withSources({String rootDir, bool isOffline: false}) { | 75 SystemCache({String rootDir, bool isOffline: false}) |
55 var cache = new SystemCache(rootDir); | 76 : rootDir = rootDir == null ? SystemCache.defaultDir : rootDir { |
56 cache.register(new GitSource()); | 77 for (var source in sources.sources) { |
| 78 if (source is HostedSource) { |
| 79 _liveSources[source.name] = source.bind(this, isOffline: isOffline); |
| 80 } else { |
| 81 _liveSources[source.name] = source.bind(this); |
| 82 } |
| 83 } |
| 84 } |
57 | 85 |
58 if (isOffline) { | 86 /// Returns the live source bound to this cache named [name]. |
59 cache.register(new OfflineHostedSource()); | 87 /// |
60 } else { | 88 /// Returns a live [UnknownSource] if no source with that name has been |
61 cache.register(new HostedSource()); | 89 /// registered. If [name] is null, returns the default source. |
| 90 LiveSource liveSource(String name) => |
| 91 _liveSources.putIfAbsent(name, () => sources[name].bind(this)); |
| 92 |
| 93 /// Loads the package identified by [id]. |
| 94 /// |
| 95 /// Throws an [ArgumentError] if [id] has an invalid source. |
| 96 Package load(PackageId id) { |
| 97 var source = liveSource(id.source); |
| 98 if (source.source is UnknownSource) { |
| 99 throw new ArgumentError("Unknown source ${id.source}."); |
62 } | 100 } |
63 | 101 |
64 cache.register(new PathSource()); | 102 var dir = source.getDirectory(id); |
65 cache.sources.setDefault('hosted'); | 103 return new Package.load(id.name, dir, sources); |
66 return cache; | |
67 } | |
68 | |
69 /// Registers a new source. | |
70 /// | |
71 /// This source must not have the same name as a source that's already been | |
72 /// registered. | |
73 void register(Source source) { | |
74 source.bind(this); | |
75 sources.register(source); | |
76 } | 104 } |
77 | 105 |
78 /// Determines if the system cache contains the package identified by [id]. | 106 /// Determines if the system cache contains the package identified by [id]. |
79 bool contains(PackageId id) { | 107 bool contains(PackageId id) { |
80 var source = sources[id.source]; | 108 var source = liveSource(id.source); |
81 | 109 |
82 if (source is! CachedSource) { | 110 if (source is! CachedSource) { |
83 throw new ArgumentError("Package $id is not cacheable."); | 111 throw new ArgumentError("Package $id is not cacheable."); |
84 } | 112 } |
85 | 113 |
86 return source.isInSystemCache(id); | 114 return source.isInSystemCache(id); |
87 } | 115 } |
88 | 116 |
89 /// Create a new temporary directory within the system cache. | 117 /// Create a new temporary directory within the system cache. |
90 /// | 118 /// |
91 /// The system cache maintains its own temporary directory that it uses to | 119 /// The system cache maintains its own temporary directory that it uses to |
92 /// stage packages into while downloading. It uses this instead of the OS's | 120 /// stage packages into while downloading. It uses this instead of the OS's |
93 /// system temp directory to ensure that it's on the same volume as the pub | 121 /// system temp directory to ensure that it's on the same volume as the pub |
94 /// system cache so that it can move the directory from it. | 122 /// system cache so that it can move the directory from it. |
95 String createTempDir() { | 123 String createTempDir() { |
96 var temp = ensureDir(tempDir); | 124 var temp = ensureDir(tempDir); |
97 return io.createTempDir(temp, 'dir'); | 125 return io.createTempDir(temp, 'dir'); |
98 } | 126 } |
99 | 127 |
100 /// Deletes the system cache's internal temp directory. | 128 /// Deletes the system cache's internal temp directory. |
101 void deleteTempDir() { | 129 void deleteTempDir() { |
102 log.fine('Clean up system cache temp directory $tempDir.'); | 130 log.fine('Clean up system cache temp directory $tempDir.'); |
103 if (dirExists(tempDir)) deleteEntry(tempDir); | 131 if (dirExists(tempDir)) deleteEntry(tempDir); |
104 } | 132 } |
105 } | 133 } |
OLD | NEW |