Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: lib/src/system_cache.dart

Issue 2044253003: Refactor Source and SourceRegistry. (Closed) Base URL: git@github.com:dart-lang/pub.git@master
Patch Set: Rename LiveSource to BoundSource. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/source_registry.dart ('k') | lib/src/validator/dependency.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 the [source]
48 : rootDir = rootDir == null ? SystemCache.defaultDir : rootDir; 46 /// function.
47 final sources = new SourceRegistry();
49 48
50 /// Creates a system cache and registers the standard set of sources. 49 /// The sources bound to this cache.
50 final _boundSources = <String, BoundSource>{};
51
52 /// The built-in Git source bound to this cache.
53 BoundGitSource get git => _boundSources["git"] as BoundGitSource;
54
55 /// The built-in hosted source bound to this cache.
56 BoundHostedSource get hosted => _boundSources["hosted"] as BoundHostedSource;
57
58 /// The built-in path source bound to this cache.
59 BoundPathSource get path => _boundSources["path"] as BoundPathSource;
60
61 /// The default source bound to this cache.
62 BoundSource get defaultSource => source(null);
63
64 /// Creates a system cache and registers all sources in [sources].
51 /// 65 ///
52 /// If [isOffline] is `true`, then the offline hosted source will be used. 66 /// If [isOffline] is `true`, then the offline hosted source will be used.
53 /// Defaults to `false`. 67 /// Defaults to `false`.
54 factory SystemCache.withSources({String rootDir, bool isOffline: false}) { 68 SystemCache({String rootDir, bool isOffline: false})
55 var cache = new SystemCache(rootDir); 69 : rootDir = rootDir == null ? SystemCache.defaultDir : rootDir {
56 cache.register(new GitSource()); 70 for (var source in sources.all) {
71 if (source is HostedSource) {
72 _boundSources[source.name] = source.bind(this, isOffline: isOffline);
73 } else {
74 _boundSources[source.name] = source.bind(this);
75 }
76 }
77 }
57 78
58 if (isOffline) { 79 /// Returns the source named [name] bound to this cache.
59 cache.register(new OfflineHostedSource()); 80 ///
60 } else { 81 /// Returns a bound version of [UnknownSource] if no source with that name has
61 cache.register(new HostedSource()); 82 /// been registered. If [name] is null, returns the default source.
83 BoundSource source(String name) =>
84 _boundSources.putIfAbsent(name, () => sources[name].bind(this));
85
86 /// Loads the package identified by [id].
87 ///
88 /// Throws an [ArgumentError] if [id] has an invalid source.
89 Package load(PackageId id) {
90 var source = this.source(id.source);
91 if (source.source is UnknownSource) {
92 throw new ArgumentError("Unknown source ${id.source}.");
62 } 93 }
63 94
64 cache.register(new PathSource()); 95 var dir = source.getDirectory(id);
65 cache.sources.setDefault('hosted'); 96 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 } 97 }
77 98
78 /// Determines if the system cache contains the package identified by [id]. 99 /// Determines if the system cache contains the package identified by [id].
79 bool contains(PackageId id) { 100 bool contains(PackageId id) {
80 var source = sources[id.source]; 101 var source = this.source(id.source);
81 102
82 if (source is! CachedSource) { 103 if (source is! CachedSource) {
83 throw new ArgumentError("Package $id is not cacheable."); 104 throw new ArgumentError("Package $id is not cacheable.");
84 } 105 }
85 106
86 return source.isInSystemCache(id); 107 return source.isInSystemCache(id);
87 } 108 }
88 109
89 /// Create a new temporary directory within the system cache. 110 /// Create a new temporary directory within the system cache.
90 /// 111 ///
91 /// The system cache maintains its own temporary directory that it uses to 112 /// 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 113 /// 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 114 /// 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. 115 /// system cache so that it can move the directory from it.
95 String createTempDir() { 116 String createTempDir() {
96 var temp = ensureDir(tempDir); 117 var temp = ensureDir(tempDir);
97 return io.createTempDir(temp, 'dir'); 118 return io.createTempDir(temp, 'dir');
98 } 119 }
99 120
100 /// Deletes the system cache's internal temp directory. 121 /// Deletes the system cache's internal temp directory.
101 void deleteTempDir() { 122 void deleteTempDir() {
102 log.fine('Clean up system cache temp directory $tempDir.'); 123 log.fine('Clean up system cache temp directory $tempDir.');
103 if (dirExists(tempDir)) deleteEntry(tempDir); 124 if (dirExists(tempDir)) deleteEntry(tempDir);
104 } 125 }
105 } 126 }
OLDNEW
« no previous file with comments | « lib/src/source_registry.dart ('k') | lib/src/validator/dependency.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698