-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_paths.dart
More file actions
128 lines (112 loc) · 3.28 KB
/
cache_paths.dart
File metadata and controls
128 lines (112 loc) · 3.28 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
import 'dart:io';
import 'package:crypto/crypto.dart';
import 'package:path/path.dart' as p;
import 'platform_info.dart';
/// Resolves and builds drx cache and lock paths.
final class DrxPaths {
DrxPaths(this.homeDirectory);
final Directory homeDirectory;
Directory get cacheDirectory =>
Directory(p.join(homeDirectory.path, 'cache'));
Directory get lockDirectory => Directory(p.join(homeDirectory.path, 'locks'));
/// Resolves the drx home directory from environment and host defaults.
static DrxPaths resolve({
Map<String, String>? environment,
HostPlatform? platform,
}) {
final env = environment ?? Platform.environment;
final host = platform ?? HostPlatform.detect();
final configured = env['DRX_HOME'];
if (configured != null && configured.trim().isNotEmpty) {
return DrxPaths(Directory(configured));
}
if (host.isWindows) {
final appData = env['LOCALAPPDATA'];
if (appData != null && appData.trim().isNotEmpty) {
return DrxPaths(Directory(p.join(appData, 'drx')));
}
}
final home = env['HOME'];
if (home != null && home.trim().isNotEmpty) {
return DrxPaths(Directory(p.join(home, '.drx')));
}
return DrxPaths(Directory(p.join(Directory.systemTemp.path, 'drx')));
}
/// Cache location for pub dependency sandboxes.
Directory pubSandboxDir(String package, String versionKey) {
final key = stableKey(['pub', package, versionKey]);
return Directory(p.join(cacheDirectory.path, 'pub', key, 'sandbox'));
}
/// Cache location for compiled AOT pub executables.
Directory pubAotDir(
String package,
String versionKey,
String command,
HostPlatform platform,
String sdkVersion,
) {
final key = stableKey([
'pub-aot',
package,
versionKey,
command,
platform.os,
platform.arch,
sdkVersion,
]);
return Directory(p.join(cacheDirectory.path, 'pub-aot', key));
}
/// Cache location for downloaded GH release assets.
Directory ghAssetDir(
String owner,
String repo,
String tag,
String assetName,
HostPlatform platform,
) {
final key = stableKey([
'gh',
owner,
repo,
tag,
assetName,
platform.os,
platform.arch,
]);
return Directory(p.join(cacheDirectory.path, 'gh', key));
}
/// Cache location for GitHub source-mode Dart installs.
Directory ghSourceDir(
String owner,
String repo,
String ref,
String gitPath,
HostPlatform platform,
) {
final key = stableKey([
'gh-source',
owner,
repo,
ref,
gitPath,
platform.os,
platform.arch,
]);
return Directory(p.join(cacheDirectory.path, 'gh-source', key));
}
/// Lock file path derived from a deterministic key.
File lockFileFor(String key) {
final hash = stableKey([key]);
return File(p.join(lockDirectory.path, '$hash.lock'));
}
/// Creates a stable SHA-256 key from path components.
String stableKey(List<String> parts) {
final payload = parts.join('|');
return sha256.convert(payload.codeUnits).toString();
}
/// Ensures base cache and lock directories exist.
Future<void> ensureBaseDirectories() async {
await cacheDirectory.create(recursive: true);
await lockDirectory.create(recursive: true);
}
}