Skip to content

Commit

Permalink
[flutter_tools] cache flutter sdk version to disk (#124558)
Browse files Browse the repository at this point in the history
Fixes flutter/flutter#112833

Most of the actual changes here are in [packages/flutter_tools/lib/src/version.dart](https://github.com/flutter/flutter/pull/124558/files#diff-092e00109d9e1589fbc7c6de750e29a6ae512b2dd44e85d60028953561201605), while the rest is largely just addressing changes to the constructor of `FlutterVersion` which now has different dependencies.

This change makes `FlutterVersion` an interface with two concrete implementations:

1. `_FlutterVersionGit` which is mostly the previous implementation, and
2. `_FlutterVersionFromFile` which will read a new `.version.json` file from the root of the repo

The [`FlutterVersion` constructor](https://github.com/flutter/flutter/pull/124558/files#diff-092e00109d9e1589fbc7c6de750e29a6ae512b2dd44e85d60028953561201605R70) is now a factory that first checks if `.version.json` exists, and if so returns an instance of `_FlutterVersionFromGit` else it returns the fallback `_FlutterVersionGit` which will end up writing `.version.json` so that we don't need to re-calculate the version on the next invocation.

`.version.json` will be deleted in the bash/batch entrypoints any time we need to rebuild he tool (this will usually be because the user did `flutter upgrade` or `flutter channel`, or manually changed the commit with git).
  • Loading branch information
christopherfujino authored Jun 15, 2023
1 parent 6d2b5ea commit 3246808
Show file tree
Hide file tree
Showing 19 changed files with 707 additions and 224 deletions.
1 change: 1 addition & 0 deletions bin/internal/shared.bat
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ GOTO :after_subroutine

:do_snapshot
IF EXIST "%FLUTTER_ROOT%\version" DEL "%FLUTTER_ROOT%\version"
IF EXIST "%FLUTTER_ROOT%\bin\cache\flutter.version.json" DEL "%FLUTTER_ROOT%\bin\cache\flutter.version.json"
ECHO: > "%cache_dir%\.dartignore"
ECHO Building flutter tool... 1>&2
PUSHD "%flutter_tools_dir%"
Expand Down
6 changes: 5 additions & 1 deletion bin/internal/shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ function upgrade_flutter () (
# * STAMP_PATH is an empty file, or
# * Contents of STAMP_PATH is not what we are going to compile, or
# * pubspec.yaml last modified after pubspec.lock
if [[ ! -f "$SNAPSHOT_PATH" || ! -s "$STAMP_PATH" || "$(cat "$STAMP_PATH")" != "$compilekey" || "$FLUTTER_TOOLS_DIR/pubspec.yaml" -nt "$FLUTTER_TOOLS_DIR/pubspec.lock" ]]; then
if [[ ! -f "$SNAPSHOT_PATH" || \
! -s "$STAMP_PATH" || \
"$(cat "$STAMP_PATH")" != "$compilekey" || \
"$FLUTTER_TOOLS_DIR/pubspec.yaml" -nt "$FLUTTER_TOOLS_DIR/pubspec.lock" ]]; then
# Waits for the update lock to be acquired. Placing this check inside the
# conditional allows the majority of flutter/dart installations to bypass
# the lock entirely, but as a result this required a second verification that
Expand All @@ -137,6 +140,7 @@ function upgrade_flutter () (

# Fetch Dart...
rm -f "$FLUTTER_ROOT/version"
rm -f "$FLUTTER_ROOT/bin/cache/flutter.version.json"
touch "$FLUTTER_ROOT/bin/cache/.dartignore"
"$FLUTTER_ROOT/bin/internal/update_dart_sdk.sh"

Expand Down
1 change: 0 additions & 1 deletion packages/flutter_tools/lib/src/commands/doctor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class DoctorCommand extends FlutterCommand {

@override
Future<FlutterCommandResult> runCommand() async {
globals.flutterVersion.fetchTagsAndUpdate();
if (argResults?.wasParsed('check-for-remote-artifacts') ?? false) {
final String engineRevision = stringArg('check-for-remote-artifacts')!;
if (engineRevision.startsWith(RegExp(r'[a-f0-9]{1,40}'))) {
Expand Down
5 changes: 4 additions & 1 deletion packages/flutter_tools/lib/src/commands/downgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ class DowngradeCommand extends FlutterCommand {
String workingDirectory = Cache.flutterRoot!;
if (argResults!.wasParsed('working-directory')) {
workingDirectory = stringArg('working-directory')!;
_flutterVersion = FlutterVersion(workingDirectory: workingDirectory);
_flutterVersion = FlutterVersion(
fs: _fileSystem!,
flutterRoot: workingDirectory,
);
}

final String currentChannel = _flutterVersion!.channel;
Expand Down
8 changes: 6 additions & 2 deletions packages/flutter_tools/lib/src/commands/upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class UpgradeCommand extends FlutterCommand {
gitTagVersion: GitTagVersion.determine(globals.processUtils, globals.platform),
flutterVersion: stringArg('working-directory') == null
? globals.flutterVersion
: FlutterVersion(workingDirectory: _commandRunner.workingDirectory),
: FlutterVersion(flutterRoot: _commandRunner.workingDirectory!, fs: globals.fs),
verifyOnly: boolArg('verify-only'),
);
}
Expand Down Expand Up @@ -297,7 +297,11 @@ class UpgradeCommandRunner {
'for instructions.'
);
}
return FlutterVersion(workingDirectory: workingDirectory, frameworkRevision: revision);
return FlutterVersion.fromRevision(
flutterRoot: workingDirectory!,
frameworkRevision: revision,
fs: globals.fs,
);
}

/// Attempts a hard reset to the given revision.
Expand Down
5 changes: 4 additions & 1 deletion packages/flutter_tools/lib/src/context_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ Future<T> runInContext<T>(
config: globals.config,
platform: globals.platform,
),
FlutterVersion: () => FlutterVersion(),
FlutterVersion: () => FlutterVersion(
fs: globals.fs,
flutterRoot: Cache.flutterRoot!,
),
FuchsiaArtifacts: () => FuchsiaArtifacts.find(),
FuchsiaDeviceTools: () => FuchsiaDeviceTools(),
FuchsiaSdk: () => FuchsiaSdk(),
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tools/lib/src/doctor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class _DefaultDoctorValidatorsProvider implements DoctorValidatorsProvider {
FlutterValidator(
fileSystem: globals.fs,
platform: globals.platform,
flutterVersion: () => globals.flutterVersion,
flutterVersion: () => globals.flutterVersion.fetchTagsAndGetVersion(clock: globals.systemClock),
devToolsVersion: () => globals.cache.devToolsVersion,
processManager: globals.processManager,
userMessages: userMessages,
Expand Down
5 changes: 4 additions & 1 deletion packages/flutter_tools/lib/src/project_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ class VariableDumpMachineProjectValidator extends MachineProjectValidator {
));

// FlutterVersion
final FlutterVersion version = FlutterVersion(workingDirectory: project.directory.absolute.path);
final FlutterVersion version = FlutterVersion(
flutterRoot: Cache.flutterRoot!,
fs: fileSystem,
);
result.add(ProjectValidatorResult(
name: 'FlutterVersion.frameworkRevision',
value: _toJsonValue(version.frameworkRevision),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import '../cache.dart';
import '../convert.dart';
import '../globals.dart' as globals;
import '../tester/flutter_tester.dart';
import '../version.dart';
import '../web/web_device.dart';

/// Common flutter command line options.
Expand Down Expand Up @@ -318,14 +319,16 @@ class FlutterCommandRunner extends CommandRunner<void> {

if ((topLevelResults[FlutterGlobalOptions.kVersionFlag] as bool?) ?? false) {
globals.flutterUsage.sendCommand(FlutterGlobalOptions.kVersionFlag);
globals.flutterVersion.fetchTagsAndUpdate();
String status;
final FlutterVersion version = globals.flutterVersion.fetchTagsAndGetVersion(
clock: globals.systemClock,
);
final String status;
if (machineFlag) {
final Map<String, Object> jsonOut = globals.flutterVersion.toJson();
final Map<String, Object> jsonOut = version.toJson();
jsonOut['flutterRoot'] = Cache.flutterRoot!;
status = const JsonEncoder.withIndent(' ').convert(jsonOut);
} else {
status = globals.flutterVersion.toString();
status = version.toString();
}
globals.printStatus(status);
return;
Expand Down
Loading

0 comments on commit 3246808

Please sign in to comment.