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 { ...@@ -665,7 +665,7 @@ class Cache {
} }
/// Update the cache to contain all `requiredArtifacts`. /// 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) { if (!_lockEnabled) {
return; return;
} }
...@@ -678,7 +678,7 @@ class Cache { ...@@ -678,7 +678,7 @@ class Cache {
continue; continue;
} }
try { try {
await artifact.update(_artifactUpdater, _logger, _fileSystem, _osUtils); await artifact.update(_artifactUpdater, _logger, _fileSystem, _osUtils, offline: offline);
} on SocketException catch (e) { } on SocketException catch (e) {
if (_hostsBlockedInChina.contains(e.address?.host)) { if (_hostsBlockedInChina.contains(e.address?.host)) {
_logger.printError( _logger.printError(
...@@ -744,6 +744,7 @@ abstract class ArtifactSet { ...@@ -744,6 +744,7 @@ abstract class ArtifactSet {
Logger logger, Logger logger,
FileSystem fileSystem, FileSystem fileSystem,
OperatingSystemUtils operatingSystemUtils, OperatingSystemUtils operatingSystemUtils,
{bool offline = false}
); );
/// The canonical name of the artifact. /// The canonical name of the artifact.
...@@ -797,6 +798,7 @@ abstract class CachedArtifact extends ArtifactSet { ...@@ -797,6 +798,7 @@ abstract class CachedArtifact extends ArtifactSet {
Logger logger, Logger logger,
FileSystem fileSystem, FileSystem fileSystem,
OperatingSystemUtils operatingSystemUtils, OperatingSystemUtils operatingSystemUtils,
{bool offline = false}
) async { ) async {
if (!location.existsSync()) { if (!location.existsSync()) {
try { try {
......
...@@ -115,10 +115,12 @@ class PubDependencies extends ArtifactSet { ...@@ -115,10 +115,12 @@ class PubDependencies extends ArtifactSet {
Logger logger, Logger logger,
FileSystem fileSystem, FileSystem fileSystem,
OperatingSystemUtils operatingSystemUtils, OperatingSystemUtils operatingSystemUtils,
{bool offline = false}
) async { ) async {
await _pub().get( await _pub().get(
context: PubContext.pubGet, context: PubContext.pubGet,
directory: fileSystem.path.join(_flutterRoot(), 'packages', 'flutter_tools'), directory: fileSystem.path.join(_flutterRoot(), 'packages', 'flutter_tools'),
offline: offline
); );
} }
} }
...@@ -418,6 +420,7 @@ class AndroidMavenArtifacts extends ArtifactSet { ...@@ -418,6 +420,7 @@ class AndroidMavenArtifacts extends ArtifactSet {
Logger logger, Logger logger,
FileSystem fileSystem, FileSystem fileSystem,
OperatingSystemUtils operatingSystemUtils, OperatingSystemUtils operatingSystemUtils,
{bool offline = false}
) async { ) async {
if (globals.androidSdk == null) { if (globals.androidSdk == null) {
return; return;
......
...@@ -1301,8 +1301,15 @@ abstract class FlutterCommand extends Command<void> { ...@@ -1301,8 +1301,15 @@ abstract class FlutterCommand extends Command<void> {
if (shouldUpdateCache) { if (shouldUpdateCache) {
// First always update universal artifacts, as some of these (e.g. // First always update universal artifacts, as some of these (e.g.
// ios-deploy on macOS) are required to determine `requiredArtifacts`. // ios-deploy on macOS) are required to determine `requiredArtifacts`.
await globals.cache.updateAll(<DevelopmentArtifact>{DevelopmentArtifact.universal}); bool offline;
await globals.cache.updateAll(await requiredArtifacts); 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(); globals.cache.releaseLock();
......
...@@ -9,17 +9,50 @@ import 'package:flutter_tools/src/base/file_system.dart'; ...@@ -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/cache.dart';
import 'package:flutter_tools/src/commands/create.dart'; import 'package:flutter_tools/src/commands/create.dart';
import 'package:flutter_tools/src/convert.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.dart';
import 'package:flutter_tools/src/doctor_validator.dart'; import 'package:flutter_tools/src/doctor_validator.dart';
import 'package:flutter_tools/src/globals.dart' as globals; import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:test/fake.dart';
import '../../src/context.dart'; import '../../src/context.dart';
import '../../src/test_flutter_command_runner.dart'; import '../../src/test_flutter_command_runner.dart';
import '../../src/testbed.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() { void main() {
group('usageValues', () { group('usageValues', () {
Testbed testbed; Testbed testbed;
FakePub fakePub;
setUpAll(() { setUpAll(() {
Cache.disableLocking(); Cache.disableLocking();
...@@ -28,6 +61,7 @@ void main() { ...@@ -28,6 +61,7 @@ void main() {
setUp(() { setUp(() {
testbed = Testbed(setup: () { testbed = Testbed(setup: () {
fakePub = FakePub(globals.fs);
Cache.flutterRoot = 'flutter'; Cache.flutterRoot = 'flutter';
final List<String> filePaths = <String>[ final List<String> filePaths = <String>[
globals.fs.path.join('flutter', 'packages', 'flutter', 'pubspec.yaml'), globals.fs.path.join('flutter', 'packages', 'flutter', 'pubspec.yaml'),
...@@ -139,6 +173,18 @@ void main() { ...@@ -139,6 +173,18 @@ void main() {
]); ]);
expect((await command.usageValues).commandCreateAndroidLanguage, 'java'); 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 { ...@@ -445,7 +445,7 @@ class FakeCache extends Fake implements Cache {
Future<bool> isUpToDate() async => isUpToDateValue; Future<bool> isUpToDate() async => isUpToDateValue;
@override @override
Future<void> updateAll(Set<DevelopmentArtifact> requiredArtifacts) async { Future<void> updateAll(Set<DevelopmentArtifact> requiredArtifacts, {bool offline = false}) async {
artifacts = requiredArtifacts; artifacts = requiredArtifacts;
} }
......
...@@ -1098,7 +1098,7 @@ class FakeSecondaryCachedArtifact extends Fake implements CachedArtifact { ...@@ -1098,7 +1098,7 @@ class FakeSecondaryCachedArtifact extends Fake implements CachedArtifact {
Future<bool> isUpToDate(FileSystem fileSystem) async => upToDate; Future<bool> isUpToDate(FileSystem fileSystem) async => upToDate;
@override @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) { if (updateException != null) {
throw updateException; throw updateException;
} }
......
...@@ -768,7 +768,7 @@ class FakeCache extends Fake implements Cache { ...@@ -768,7 +768,7 @@ class FakeCache extends Fake implements Cache {
List<Set<DevelopmentArtifact>> artifacts = <Set<DevelopmentArtifact>>[]; List<Set<DevelopmentArtifact>> artifacts = <Set<DevelopmentArtifact>>[];
@override @override
Future<void> updateAll(Set<DevelopmentArtifact> requiredArtifacts) async { Future<void> updateAll(Set<DevelopmentArtifact> requiredArtifacts, {bool offline = false}) async {
artifacts.add(requiredArtifacts.toSet()); artifacts.add(requiredArtifacts.toSet());
} }
......
...@@ -32,7 +32,7 @@ class FakeDyldEnvironmentArtifact extends ArtifactSet { ...@@ -32,7 +32,7 @@ class FakeDyldEnvironmentArtifact extends ArtifactSet {
String get name => 'fake'; String get name => 'fake';
@override @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