forked from dart-lang/pub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_cache.dart
More file actions
354 lines (309 loc) · 11.4 KB
/
Copy pathsystem_cache.dart
File metadata and controls
354 lines (309 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:collection/collection.dart';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
import 'authentication/token_store.dart';
import 'exceptions.dart';
import 'io.dart';
import 'io.dart' as io show createTempDir;
import 'log.dart' as log;
import 'package.dart';
import 'package_name.dart';
import 'pubspec.dart';
import 'source.dart';
import 'source/cached.dart';
import 'source/git.dart';
import 'source/hosted.dart';
import 'source/path.dart';
import 'source/sdk.dart';
import 'source/unknown.dart';
import 'utils.dart';
/// The system-wide cache of downloaded packages.
///
/// This cache contains all packages that are downloaded from the internet.
/// Packages that are available locally (e.g. path dependencies) don't use this
/// cache.
class SystemCache {
/// The root directory where this package cache is located.
final String rootDir;
String rootDirForSource(CachedSource source) => p.join(rootDir, source.name);
String get tempDir => p.join(rootDir, '_temp');
static String defaultDir = (() {
if (Platform.environment.containsKey('PUB_CACHE')) {
return Platform.environment['PUB_CACHE']!;
} else if (Platform.isWindows) {
// %LOCALAPPDATA% is used as the cache location over %APPDATA%, because
// the latter is synchronised between devices when the user roams between
// them, whereas the former is not.
final localAppData = Platform.environment['LOCALAPPDATA'];
if (localAppData == null) {
dataError('''
Could not find the pub cache. No `LOCALAPPDATA` environment variable exists.
Consider setting the `PUB_CACHE` variable manually.
''');
}
return p.join(localAppData, 'Pub', 'Cache');
} else {
final home = Platform.environment['HOME'];
if (home == null) {
dataError('''
Could not find the pub cache. No `HOME` environment variable exists.
Consider setting the `PUB_CACHE` variable manually.
''');
}
return p.join(home, '.pub-cache');
}
})();
/// The available sources.
late final _sources =
Map.fromIterable([hosted, git, path, sdk], key: (source) => source.name);
Source sources(String? name) {
return name == null
? defaultSource
: (_sources[name] ?? UnknownSource(name));
}
Source get defaultSource => hosted;
/// The built-in Git source.
GitSource get git => GitSource.instance;
/// The built-in hosted source.
HostedSource get hosted => HostedSource.instance;
/// The built-in path source bound to this cache.
PathSource get path => PathSource.instance;
/// The built-in SDK source bound to this cache.
SdkSource get sdk => SdkSource.instance;
/// The default credential store.
final TokenStore tokenStore;
/// If true, cached sources will attempt to use the cached packages for
/// resolution.
final bool isOffline;
/// Creates a system cache and registers all sources in [sources].
///
/// If [isOffline] is `true`, then the offline hosted source will be used.
/// Defaults to `false`.
SystemCache({String? rootDir, this.isOffline = false})
: rootDir = rootDir ?? SystemCache.defaultDir,
tokenStore = TokenStore(dartConfigDir);
/// Loads the package identified by [id].
///
/// Throws an [ArgumentError] if [id] has an invalid source.
Package load(PackageId id) {
return Package.load(id.name, getDirectory(id), sources);
}
Package loadCached(PackageId id) {
final source = id.description.description.source;
if (source is CachedSource) {
return Package.load(
id.name,
source.getDirectoryInCache(id, this),
sources,
);
} else {
throw ArgumentError('Call only on Cached ids.');
}
}
/// Determines if the system cache contains the package identified by [id].
bool contains(PackageId id) {
final source = id.source;
if (source is CachedSource) {
return source.isInSystemCache(id, this);
}
throw ArgumentError('Package $id is not cacheable.');
}
/// Create a new temporary directory within the system cache.
///
/// The system cache maintains its own temporary directory that it uses to
/// stage packages into while downloading. It uses this instead of the OS's
/// system temp directory to ensure that it's on the same volume as the pub
/// system cache so that it can move the directory from it.
String createTempDir() {
var temp = ensureDir(tempDir);
return io.createTempDir(temp, 'dir');
}
/// Deletes the system cache's internal temp directory.
void deleteTempDir() {
log.fine('Clean up system cache temp directory $tempDir.');
if (dirExists(tempDir)) deleteEntry(tempDir);
}
/// An in-memory cache of pubspecs described by [describe].
final cachedPubspecs = <PackageId, Pubspec>{};
/// Loads the (possibly remote) pubspec for the package version identified by
/// [id].
///
/// This may be called for packages that have not yet been downloaded during
/// the version resolution process. Its results are automatically memoized.
///
/// Throws a [DataException] if the pubspec's version doesn't match [id]'s
/// version.
Future<Pubspec> describe(PackageId id) async {
var pubspec = cachedPubspecs[id] ??= await id.source.doDescribe(id, this);
if (pubspec.version != id.version) {
throw PackageNotFoundException(
'the pubspec for $id has version ${pubspec.version}',
);
}
return pubspec;
}
/// Get the IDs of all versions that match [ref].
///
/// Note that this does *not* require the packages to be downloaded locally,
/// which is the point. This is used during version resolution to determine
/// which package versions are available to be downloaded (or already
/// downloaded).
///
/// By default, this assumes that each description has a single version and
/// uses [describe] to get that version.
///
/// If [maxAge] is given answers can be taken from cache - up to that age old.
///
/// If given, the [allowedRetractedVersion] is the only version which can be
/// selected even if it is marked as retracted. Otherwise, all the returned
/// IDs correspond to non-retracted versions.
Future<List<PackageId>> getVersions(
PackageRef ref, {
Duration? maxAge,
Version? allowedRetractedVersion,
}) async {
if (ref.isRoot) {
throw ArgumentError('Cannot get versions for the root package.');
}
var versions = await ref.source.doGetVersions(ref, maxAge, this);
versions = (await Future.wait(
versions.map((id) async {
final packageStatus = await ref.source.status(
id.toRef(),
id.version,
this,
maxAge: maxAge,
);
if (!packageStatus.isRetracted ||
id.version == allowedRetractedVersion) {
return id;
}
return null;
}),
))
.whereNotNull()
.toList();
return versions;
}
/// Returns the directory where this package can (or could) be found locally.
///
/// If the source is cached, this will be a path in the system cache.
///
/// If id is a relative path id, the directory will be relative from
/// [relativeFrom]. Returns an absolute path if [relativeFrom] is not passed.
String getDirectory(PackageId id, {String? relativeFrom}) {
return id.source.doGetDirectory(id, this, relativeFrom: relativeFrom);
}
/// Downloads a cached package identified by [id] to the cache.
///
/// [id] must refer to a cached package.
///
/// If [allowOutdatedHashChecks] is `true` we use a cached version listing
/// response if present instead of probing the server. Not probing allows for
/// `pub get` with a filled cache to be a fast case that doesn't require any
/// new version-listings.
///
/// Returns [id] with an updated [ResolvedDescription], this can be different
/// if the content-hash changed while downloading.
Future<PackageId> downloadPackage(PackageId id) async {
final source = id.source;
assert(source is CachedSource);
final result = await (source as CachedSource).downloadToSystemCache(
id,
this,
);
// We only update the README.md in the cache when a change to the cache has
// happened. This is:
// * to avoid failing if used with a read-only cache, and
// * because the cost of writing a single file is negligible compared to
// downloading a package, but might be significant in the fast-case where
// a the cache is already valid.
if (result.didUpdate) {
_ensureReadme();
}
return result.packageId;
}
/// Get the latest version of [package].
///
/// Will consider _prereleases_ if:
/// * [allowPrereleases] is true, or,
/// * If [version] is non-null and is a prerelease version and there are no
/// later stable version we return a prerelease version if it exists.
///
/// Returns `null`, if unable to find the package or if [package] is `null`.
Future<PackageId?> getLatest(
PackageRef? package, {
Version? version,
bool allowPrereleases = false,
}) async {
if (package == null) {
return null;
}
final List<PackageId> available;
try {
// TODO: Pass some maxAge to getVersions
available = await getVersions(package);
} on PackageNotFoundException {
return null;
}
if (available.isEmpty) {
return null;
}
available.sort(
allowPrereleases
? (x, y) => x.version.compareTo(y.version)
: (x, y) => Version.prioritize(x.version, y.version),
);
var latest = available.last;
if (version != null && version.isPreRelease && version > latest.version) {
available.sort((x, y) => x.version.compareTo(y.version));
latest = available.last;
}
// There should be exactly one entry in [available] matching [latest]
assert(available.where((id) => id.version == latest.version).length == 1);
return latest;
}
/// Removes all contents of the system cache.
///
/// Rewrites the README.md.
void clean() {
deleteEntry(rootDir);
ensureDir(rootDir);
_ensureReadme();
}
/// Write a README.md file in the root of the cache directory to document the
/// contents of the folder.
///
/// This should only be called when we are doing another operation that is
/// modifying the `PUB_CACHE`. This ensures that users won't experience
/// permission errors because we writing a `README.md` file, in a flow that
/// the user expected wouldn't have issues with a read-only `PUB_CACHE`.
void _ensureReadme() {
/// We only want to do this once per run.
if (_hasEnsuredReadme) return;
_hasEnsuredReadme = true;
final readmePath = p.join(rootDir, 'README.md');
try {
writeTextFile(readmePath, '''
Pub Package Cache
=================
This folder is used by Pub to store cached packages used in Dart / Flutter
projects.
The contents of this folder should only be modified using the `dart pub` and
`flutter pub` commands.
Modifying this folder manually can lead to inconsistent behavior.
For details on how manage the `PUB_CACHE`, see:
https://dart.dev/go/pub-cache
''');
} on Exception catch (e) {
// Failing to write the README.md should not disrupt other operations.
log.fine('Failed to write README.md in PUB_CACHE: $e');
}
}
bool _hasEnsuredReadme = false;
}
typedef SourceRegistry = Source Function(String? name);