Skip to content

Commit 7b54a30

Browse files
authored
Notify about existing caches when preloading (#122592)
1 parent a72a8ba commit 7b54a30

File tree

2 files changed

+459
-281
lines changed

2 files changed

+459
-281
lines changed

packages/flutter_tools/lib/src/dart/pub.dart

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,6 @@ const String _kPubCacheEnvironmentKey = 'PUB_CACHE';
3434

3535
typedef MessageFilter = String? Function(String message);
3636

37-
/// Load any package-files stored in [preloadCacheDir] into the pub cache if it
38-
/// exists.
39-
///
40-
/// Deletes the [preloadCacheDir].
41-
@visibleForTesting
42-
void preloadPubCache({
43-
required Directory preloadCacheDir,
44-
required ProcessManager processManager,
45-
required Logger logger,
46-
required List<String> pubCommand,
47-
}) {
48-
if (preloadCacheDir.existsSync()) {
49-
final Iterable<String> cacheFiles =
50-
preloadCacheDir
51-
.listSync()
52-
.map((FileSystemEntity f) => f.path)
53-
.where((String path) => path.endsWith('.tar.gz'));
54-
processManager.runSync(<String>[...pubCommand, 'cache', 'preload',...cacheFiles]);
55-
_tryDeleteDirectory(preloadCacheDir, logger);
56-
}
57-
}
58-
5937
bool _tryDeleteDirectory(Directory directory, Logger logger) {
6038
try {
6139
if (directory.existsSync()) {
@@ -614,13 +592,76 @@ class _DefaultPub implements Pub {
614592
if (_platform.environment.containsKey(_kPubCacheEnvironmentKey)) {
615593
return _platform.environment[_kPubCacheEnvironmentKey];
616594
}
595+
_preloadPubCache();
596+
// Use pub's default location by returning null.
597+
return null;
598+
}
617599

600+
/// Load any package-files stored in FLUTTER_ROOT/.pub-preload-cache into the
601+
/// pub cache if it exists.
602+
///
603+
/// Deletes the [preloadCacheDir].
604+
void _preloadPubCache() {
618605
final String flutterRootPath = Cache.flutterRoot!;
619606
final Directory flutterRoot = _fileSystem.directory(flutterRootPath);
620607
final Directory preloadCacheDir = flutterRoot.childDirectory('.pub-preload-cache');
621-
preloadPubCache(preloadCacheDir: preloadCacheDir,logger: _logger,processManager: _processManager, pubCommand: _pubCommand);
622-
// Use pub's default location by returning null.
623-
return null;
608+
if (preloadCacheDir.existsSync()) {
609+
/// We only want to inform about existing caches on first run of a freshly
610+
/// downloaded Flutter SDK. Therefore it is conditioned on the existence
611+
/// of the .pub-preload-cache dir.
612+
_informAboutExistingCaches();
613+
final Iterable<String> cacheFiles =
614+
preloadCacheDir
615+
.listSync()
616+
.map((FileSystemEntity f) => f.path)
617+
.where((String path) => path.endsWith('.tar.gz'));
618+
_processManager.runSync(<String>[..._pubCommand, 'cache', 'preload', ...cacheFiles]);
619+
_tryDeleteDirectory(preloadCacheDir, _logger);
620+
}
621+
}
622+
623+
/// Issues a log message if there is an existing pub cache and or an existing
624+
/// Dart Analysis Server cache.
625+
void _informAboutExistingCaches() {
626+
final String? pubCachePath = _pubCacheDefaultLocation();
627+
if (pubCachePath != null) {
628+
final Directory pubCacheDirectory = _fileSystem.directory(pubCachePath);
629+
if (pubCacheDirectory.existsSync()) {
630+
_logger.printStatus('''
631+
Found an existing Pub cache at $pubCachePath.
632+
It can be repaired by running `dart pub cache repair`.
633+
It can be reset by running `dart pub cache clear`.''');
634+
}
635+
}
636+
final String? home = _platform.environment['HOME'];
637+
if (home != null) {
638+
final String dartServerCachePath = _fileSystem.path.join(home, '.dartServer');
639+
if (_fileSystem.directory(dartServerCachePath).existsSync()) {
640+
_logger.printStatus('''
641+
Found an existing Dart Analysis Server cache at $dartServerCachePath.
642+
It can be reset by deleting $dartServerCachePath.''');
643+
}
644+
}
645+
}
646+
647+
/// The default location of the Pub cache if the PUB_CACHE environment variable
648+
/// is not set.
649+
///
650+
/// Returns null if the appropriate environment variables are unset.
651+
String? _pubCacheDefaultLocation () {
652+
if (_platform.isWindows) {
653+
final String? localAppData = _platform.environment['LOCALAPPDATA'];
654+
if (localAppData == null) {
655+
return null;
656+
}
657+
return _fileSystem.path.join(localAppData, 'Pub', 'Cache');
658+
} else {
659+
final String? home = _platform.environment['HOME'];
660+
if (home == null) {
661+
return null;
662+
}
663+
return _fileSystem.path.join(home, '.pub-cache');
664+
}
624665
}
625666

626667
/// The full environment used when running pub.

0 commit comments

Comments
 (0)