Unverified Commit 3394fb4a authored by Jesús S Guerrero's avatar Jesús S Guerrero Committed by GitHub

[flutter_tools] fix flutter create --offline (#100941)

Co-authored-by: 's avatarChristopher Fujino <christopherfujino@gmail.com>
parent 730e4971
......@@ -665,7 +665,7 @@ class Cache {
}
/// Update the cache to contain all `requiredArtifacts`.
Future<void> updateAll(Set<DevelopmentArtifact> requiredArtifacts) async {
Future<void> updateAll(Set<DevelopmentArtifact> requiredArtifacts, {bool offline = false}) async {
if (!_lockEnabled) {
return;
}
......@@ -678,7 +678,7 @@ class Cache {
continue;
}
try {
await artifact.update(_artifactUpdater, _logger, _fileSystem, _osUtils);
await artifact.update(_artifactUpdater, _logger, _fileSystem, _osUtils, offline: offline);
} on SocketException catch (e) {
if (_hostsBlockedInChina.contains(e.address?.host)) {
_logger.printError(
......@@ -744,6 +744,7 @@ abstract class ArtifactSet {
Logger logger,
FileSystem fileSystem,
OperatingSystemUtils operatingSystemUtils,
{bool offline = false}
);
/// The canonical name of the artifact.
......@@ -797,6 +798,7 @@ abstract class CachedArtifact extends ArtifactSet {
Logger logger,
FileSystem fileSystem,
OperatingSystemUtils operatingSystemUtils,
{bool offline = false}
) async {
if (!location.existsSync()) {
try {
......
......@@ -115,10 +115,12 @@ class PubDependencies extends ArtifactSet {
Logger logger,
FileSystem fileSystem,
OperatingSystemUtils operatingSystemUtils,
{bool offline = false}
) async {
await _pub().get(
context: PubContext.pubGet,
directory: fileSystem.path.join(_flutterRoot(), 'packages', 'flutter_tools'),
offline: offline
);
}
}
......@@ -418,6 +420,7 @@ class AndroidMavenArtifacts extends ArtifactSet {
Logger logger,
FileSystem fileSystem,
OperatingSystemUtils operatingSystemUtils,
{bool offline = false}
) async {
if (globals.androidSdk == null) {
return;
......
......@@ -1301,8 +1301,15 @@ abstract class FlutterCommand extends Command<void> {
if (shouldUpdateCache) {
// First always update universal artifacts, as some of these (e.g.
// ios-deploy on macOS) are required to determine `requiredArtifacts`.
await globals.cache.updateAll(<DevelopmentArtifact>{DevelopmentArtifact.universal});
await globals.cache.updateAll(await requiredArtifacts);
bool offline;
if (argParser.options.containsKey('offline')) {
offline = boolArg('offline');
}
else {
offline = false;
}
await globals.cache.updateAll(<DevelopmentArtifact>{DevelopmentArtifact.universal}, offline: offline);
await globals.cache.updateAll(await requiredArtifacts, offline: offline);
}
globals.cache.releaseLock();
......
......@@ -9,17 +9,50 @@ import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/create.dart';
import 'package:flutter_tools/src/convert.dart';
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/doctor.dart';
import 'package:flutter_tools/src/doctor_validator.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:test/fake.dart';
import '../../src/context.dart';
import '../../src/test_flutter_command_runner.dart';
import '../../src/testbed.dart';
class FakePub extends Fake implements Pub {
FakePub(this.fs);
final FileSystem fs;
int calledGetOffline = 0;
int calledOnline = 0;
@override
Future<void> get({
PubContext context,
String directory,
bool skipIfAbsent = false,
bool upgrade = false,
bool offline = false,
bool generateSyntheticPackage = false,
String flutterRootOverride,
bool checkUpToDate = false,
bool shouldSkipThirdPartyGenerator = true,
bool printProgress = true,
}) async {
fs.directory(directory).childFile('.packages').createSync();
if (offline == true){
calledGetOffline += 1;
}
else {
calledOnline += 1;
}
}
}
void main() {
group('usageValues', () {
Testbed testbed;
FakePub fakePub;
setUpAll(() {
Cache.disableLocking();
......@@ -28,6 +61,7 @@ void main() {
setUp(() {
testbed = Testbed(setup: () {
fakePub = FakePub(globals.fs);
Cache.flutterRoot = 'flutter';
final List<String> filePaths = <String>[
globals.fs.path.join('flutter', 'packages', 'flutter', 'pubspec.yaml'),
......@@ -139,6 +173,18 @@ void main() {
]);
expect((await command.usageValues).commandCreateAndroidLanguage, 'java');
}));
testUsingContext('create --offline', () => testbed.run(() async {
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['create', 'testy', '--offline']);
expect(fakePub.calledOnline, 0);
expect(fakePub.calledGetOffline, 1);
expect(command.argParser.options.containsKey('offline'), true);
expect(command.shouldUpdateCache, true);
}, overrides: <Type, Generator>{
Pub: () => fakePub,
}));
});
}
......
......@@ -445,7 +445,7 @@ class FakeCache extends Fake implements Cache {
Future<bool> isUpToDate() async => isUpToDateValue;
@override
Future<void> updateAll(Set<DevelopmentArtifact> requiredArtifacts) async {
Future<void> updateAll(Set<DevelopmentArtifact> requiredArtifacts, {bool offline = false}) async {
artifacts = requiredArtifacts;
}
......
......@@ -1098,7 +1098,7 @@ class FakeSecondaryCachedArtifact extends Fake implements CachedArtifact {
Future<bool> isUpToDate(FileSystem fileSystem) async => upToDate;
@override
Future<void> update(ArtifactUpdater artifactUpdater, Logger logger, FileSystem fileSystem, OperatingSystemUtils operatingSystemUtils) async {
Future<void> update(ArtifactUpdater artifactUpdater, Logger logger, FileSystem fileSystem, OperatingSystemUtils operatingSystemUtils, {bool offline = false}) async {
if (updateException != null) {
throw updateException;
}
......
......@@ -768,7 +768,7 @@ class FakeCache extends Fake implements Cache {
List<Set<DevelopmentArtifact>> artifacts = <Set<DevelopmentArtifact>>[];
@override
Future<void> updateAll(Set<DevelopmentArtifact> requiredArtifacts) async {
Future<void> updateAll(Set<DevelopmentArtifact> requiredArtifacts, {bool offline = false}) async {
artifacts.add(requiredArtifacts.toSet());
}
......
......@@ -32,7 +32,7 @@ class FakeDyldEnvironmentArtifact extends ArtifactSet {
String get name => 'fake';
@override
Future<void> update(ArtifactUpdater artifactUpdater, Logger logger, FileSystem fileSystem, OperatingSystemUtils operatingSystemUtils) async {
Future<void> update(ArtifactUpdater artifactUpdater, Logger logger, FileSystem fileSystem, OperatingSystemUtils operatingSystemUtils, {bool offline = false}) async {
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment