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 p; | 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; |
(...skipping 29 matching lines...) Expand all Loading... |
40 } | 40 } |
41 })(); | 41 })(); |
42 | 42 |
43 /// The registry for sources used by this system cache. | 43 /// The registry for sources used by this system cache. |
44 /// | 44 /// |
45 /// New sources registered here will be available through the [source] | 45 /// New sources registered here will be available through the [source] |
46 /// function. | 46 /// function. |
47 final sources = new SourceRegistry(); | 47 final sources = new SourceRegistry(); |
48 | 48 |
49 /// The sources bound to this cache. | 49 /// The sources bound to this cache. |
50 final _boundSources = <String, BoundSource>{}; | 50 final _boundSources = <Source, BoundSource>{}; |
51 | 51 |
52 /// The built-in Git source bound to this cache. | 52 /// The built-in Git source bound to this cache. |
53 BoundGitSource get git => _boundSources["git"] as BoundGitSource; | 53 BoundGitSource get git => _boundSources[sources.git] as BoundGitSource; |
54 | 54 |
55 /// The built-in hosted source bound to this cache. | 55 /// The built-in hosted source bound to this cache. |
56 BoundHostedSource get hosted => _boundSources["hosted"] as BoundHostedSource; | 56 BoundHostedSource get hosted => |
| 57 _boundSources[sources.hosted] as BoundHostedSource; |
57 | 58 |
58 /// The built-in path source bound to this cache. | 59 /// The built-in path source bound to this cache. |
59 BoundPathSource get path => _boundSources["path"] as BoundPathSource; | 60 BoundPathSource get path => _boundSources[sources.path] as BoundPathSource; |
60 | 61 |
61 /// The default source bound to this cache. | 62 /// The default source bound to this cache. |
62 BoundSource get defaultSource => source(null); | 63 BoundSource get defaultSource => source(sources[null]); |
63 | 64 |
64 /// Creates a system cache and registers all sources in [sources]. | 65 /// Creates a system cache and registers all sources in [sources]. |
65 /// | 66 /// |
66 /// If [isOffline] is `true`, then the offline hosted source will be used. | 67 /// If [isOffline] is `true`, then the offline hosted source will be used. |
67 /// Defaults to `false`. | 68 /// Defaults to `false`. |
68 SystemCache({String rootDir, bool isOffline: false}) | 69 SystemCache({String rootDir, bool isOffline: false}) |
69 : rootDir = rootDir == null ? SystemCache.defaultDir : rootDir { | 70 : rootDir = rootDir == null ? SystemCache.defaultDir : rootDir { |
70 for (var source in sources.all) { | 71 for (var source in sources.all) { |
71 if (source is HostedSource) { | 72 if (source is HostedSource) { |
72 _boundSources[source.name] = source.bind(this, isOffline: isOffline); | 73 _boundSources[source] = source.bind(this, isOffline: isOffline); |
73 } else { | 74 } else { |
74 _boundSources[source.name] = source.bind(this); | 75 _boundSources[source] = source.bind(this); |
75 } | 76 } |
76 } | 77 } |
77 } | 78 } |
78 | 79 |
79 /// Returns the source named [name] bound to this cache. | 80 /// Returns the version of [source] bound to this cache. |
80 /// | 81 BoundSource source(Source source) => |
81 /// Returns a bound version of [UnknownSource] if no source with that name has | 82 _boundSources.putIfAbsent(source, () => source.bind(this)); |
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 | 83 |
86 /// Loads the package identified by [id]. | 84 /// Loads the package identified by [id]. |
87 /// | 85 /// |
88 /// Throws an [ArgumentError] if [id] has an invalid source. | 86 /// Throws an [ArgumentError] if [id] has an invalid source. |
89 Package load(PackageId id) { | 87 Package load(PackageId id) { |
90 var source = this.source(id.source); | 88 if (id.source is UnknownSource) { |
91 if (source.source is UnknownSource) { | |
92 throw new ArgumentError("Unknown source ${id.source}."); | 89 throw new ArgumentError("Unknown source ${id.source}."); |
93 } | 90 } |
94 | 91 |
95 var dir = source.getDirectory(id); | 92 return new Package.load( |
96 return new Package.load(id.name, dir, sources); | 93 id.name, source(id.source).getDirectory(id), sources); |
97 } | 94 } |
98 | 95 |
99 /// Determines if the system cache contains the package identified by [id]. | 96 /// Determines if the system cache contains the package identified by [id]. |
100 bool contains(PackageId id) { | 97 bool contains(PackageId id) { |
101 var source = this.source(id.source); | 98 var source = this.source(id.source); |
102 | 99 |
103 if (source is! CachedSource) { | 100 if (source is! CachedSource) { |
104 throw new ArgumentError("Package $id is not cacheable."); | 101 throw new ArgumentError("Package $id is not cacheable."); |
105 } | 102 } |
106 | 103 |
(...skipping 10 matching lines...) Expand all Loading... |
117 var temp = ensureDir(tempDir); | 114 var temp = ensureDir(tempDir); |
118 return io.createTempDir(temp, 'dir'); | 115 return io.createTempDir(temp, 'dir'); |
119 } | 116 } |
120 | 117 |
121 /// Deletes the system cache's internal temp directory. | 118 /// Deletes the system cache's internal temp directory. |
122 void deleteTempDir() { | 119 void deleteTempDir() { |
123 log.fine('Clean up system cache temp directory $tempDir.'); | 120 log.fine('Clean up system cache temp directory $tempDir.'); |
124 if (dirExists(tempDir)) deleteEntry(tempDir); | 121 if (dirExists(tempDir)) deleteEntry(tempDir); |
125 } | 122 } |
126 } | 123 } |
OLD | NEW |