Use .pub-cache from Flutter root, if it exists. (#13358)

The purpose of this PR is to make it so that when the user runs 'flutter', if they have a .pub-cache directory in their flutter root, we use that instead of the default location for the pub cache. Otherwise, it should act as before.

The eventual goal is to support a pre-populated flutter .zip/.tar.gz file that has everything the developer needs in one bundle. In order for that to actually work, we need to have the pub cache be self-contained, and not in the user's home dir.

Another advantage of this is that if you have multiple flutter repos that you're switching between, then the versions in the pub cache will remain static when you switch between them.

This is an attempt to re-land: #13248. Includes a fix for the test that makes it work on bots in the presence of PUB_CACHE being set, and no other changes.
This commit is contained in:
Greg Spencer
2017-12-05 14:46:39 -08:00
committed by GitHub
parent c89cf6ccc6
commit f29ecba6de
8 changed files with 187 additions and 43 deletions

View File

@@ -148,16 +148,26 @@ List<String> _pubCommand(List<String> arguments) {
///
/// [context] provides extra information to package server requests to
/// understand usage. It must match the regular expression `[a-z][a-z_]*[a-z]`.
Map<String, String> _createPubEnvironment(String context) => <String, String>{
'FLUTTER_ROOT': Cache.flutterRoot,
_pubEnvironmentKey: _getPubEnvironmentValue(context),
};
Map<String, String> _createPubEnvironment(String context) {
final Map<String, String> environment = <String, String>{
'FLUTTER_ROOT': Cache.flutterRoot,
_pubEnvironmentKey: _getPubEnvironmentValue(context),
};
final String pubCache = _getRootPubCacheIfAvailable();
if (pubCache != null) {
environment[_pubCacheEnvironmentKey] = pubCache;
}
return environment;
}
final RegExp _analyzerWarning = new RegExp(r'^! \w+ [^ ]+ from path \.\./\.\./bin/cache/dart-sdk/lib/\w+$');
/// The console environment key used by the pub tool.
const String _pubEnvironmentKey = 'PUB_ENVIRONMENT';
/// The console environment key used by the pub tool to find the cache directory.
const String _pubCacheEnvironmentKey = 'PUB_CACHE';
final RegExp _validContext = new RegExp('[a-z][a-z_]*[a-z]');
/// Returns the environment value that should be used when running pub.
@@ -189,6 +199,21 @@ String _getPubEnvironmentValue(String pubContext) {
return values.join(':');
}
String _getRootPubCacheIfAvailable() {
if (platform.environment.containsKey(_pubCacheEnvironmentKey)) {
return platform.environment[_pubCacheEnvironmentKey];
}
final String cachePath = fs.path.join(Cache.flutterRoot, '.pub-cache');
if (fs.directory(cachePath).existsSync()) {
printTrace('Using $cachePath for the pub cache.');
return cachePath;
}
// Use pub's default location by returning null.
return null;
}
String _filterOverrideWarnings(String message) {
// This function filters out these three messages:
// Warning: You are using these overridden dependencies:

View File

@@ -8,7 +8,9 @@ import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
import 'package:quiver/testing/async.dart';
@@ -26,8 +28,8 @@ void main() {
expect(processMock.lastPubEnvironmment, isNull);
pubGet(context: 'flutter_tests', checkLastModified: false).then((Null value) {
error = 'test completed unexpectedly';
}, onError: (dynamic error) {
error = 'test failed unexpectedly';
}, onError: (dynamic thrownError) {
error = 'test failed unexpectedly: $thrownError';
});
expect(testLogger.statusText, '');
time.elapse(const Duration(milliseconds: 500));
@@ -36,6 +38,7 @@ void main() {
'pub get failed (69) -- attempting retry 1 in 1 second...\n'
);
expect(processMock.lastPubEnvironmment, contains('flutter_cli:ctx_flutter_tests'));
expect(processMock.lastPubCache, isNull);
time.elapse(const Duration(milliseconds: 500));
expect(testLogger.statusText,
'Running "flutter packages get" in /...\n'
@@ -79,6 +82,61 @@ void main() {
}, overrides: <Type, Generator>{
ProcessManager: () => new MockProcessManager(69),
FileSystem: () => new MockFileSystem(),
Platform: () => new FakePlatform(
environment: <String, String>{},
),
});
testUsingContext('pub cache in root is used', () async {
String error;
final MockProcessManager processMock = context.getVariable(ProcessManager);
new FakeAsync().run((FakeAsync time) {
MockDirectory.findCache = true;
expect(processMock.lastPubEnvironmment, isNull);
expect(processMock.lastPubCache, isNull);
pubGet(context: 'flutter_tests', checkLastModified: false).then((Null value) {
error = 'test completed unexpectedly';
}, onError: (dynamic thrownError) {
error = 'test failed unexpectedly: $thrownError';
});
time.elapse(const Duration(milliseconds: 500));
expect(processMock.lastPubCache, endsWith('flutter/.pub-cache'));
expect(error, isNull);
});
}, overrides: <Type, Generator>{
ProcessManager: () => new MockProcessManager(69),
FileSystem: () => new MockFileSystem(),
Platform: () => new FakePlatform(
environment: <String, String>{},
),
});
testUsingContext('pub cache in environment is used', () async {
String error;
final MockProcessManager processMock = context.getVariable(ProcessManager);
new FakeAsync().run((FakeAsync time) {
MockDirectory.findCache = false;
expect(processMock.lastPubEnvironmment, isNull);
expect(processMock.lastPubCache, isNull);
pubGet(context: 'flutter_tests', checkLastModified: false).then((Null value) {
error = 'test completed unexpectedly';
}, onError: (dynamic thrownError) {
error = 'test failed unexpectedly: $thrownError';
});
time.elapse(const Duration(milliseconds: 500));
expect(processMock.lastPubCache, equals('path/to/pub-cache'));
expect(error, isNull);
});
}, overrides: <Type, Generator>{
ProcessManager: () => new MockProcessManager(69),
FileSystem: () => new MockFileSystem(),
Platform: () => new FakePlatform(
environment: <String, String>{'PUB_CACHE': 'path/to/pub-cache'},
),
});
}
@@ -90,6 +148,7 @@ class MockProcessManager implements ProcessManager {
final int fakeExitCode;
String lastPubEnvironmment;
String lastPubCache;
@override
Future<Process> start(
@@ -101,6 +160,7 @@ class MockProcessManager implements ProcessManager {
ProcessStartMode mode: ProcessStartMode.NORMAL,
}) {
lastPubEnvironmment = environment['PUB_ENVIRONMENT'];
lastPubCache = environment['PUB_CACHE'];
return new Future<Process>.value(new MockProcess(fakeExitCode));
}
@@ -159,6 +219,11 @@ class MockFileSystem extends MemoryFileSystem {
File file(dynamic path) {
return new MockFile();
}
@override
Directory directory(dynamic path) {
return new MockDirectory(path);
}
}
class MockFile implements File {
@@ -177,4 +242,19 @@ class MockFile implements File {
dynamic noSuchMethod(Invocation invocation) => null;
}
class MockDirectory implements Directory {
static bool findCache = false;
MockDirectory(this.path);
@override
final String path;
@override
bool existsSync() => findCache && path.endsWith('.pub-cache');
@override
dynamic noSuchMethod(Invocation invocation) => null;
}
class MockRandomAccessFile extends Mock implements RandomAccessFile {}