Improve 'flutter downgrade' error message (#154434)

`flutter downgrade` fails if you haven't used `flutter upgrade`:

```
$ flutter downgrade
There is no previously recorded version for channel "stable".
```

It's not clear what actions a user should take from this error message. Here's the new error message:

```
$ flutter downgrade
It looks like you haven't run "flutter upgrade" on channel "stable".

"flutter downgrade" undoes the last "flutter upgrade".

To switch to a specific Flutter version, see: https://flutter.dev/to/switch-flutter-version
```

Depends on https://github.com/flutter/website/pull/11098
This commit is contained in:
Loïc Sharma 2024-09-04 15:06:31 -07:00 committed by GitHub
parent 6abef22251
commit 3bda455f4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 11 deletions

View File

@ -110,9 +110,18 @@ class DowngradeCommand extends FlutterCommand {
final String? lastFlutterVersion = persistentToolState.lastActiveVersion(channel);
final String? currentFlutterVersion = _flutterVersion?.frameworkRevision;
if (lastFlutterVersion == null || currentFlutterVersion == lastFlutterVersion) {
final String trailing = await _createErrorMessage(workingDirectory, channel);
final String trailing = await _createErrorMessage(
workingDirectory,
channel,
);
throwToolExit(
'There is no previously recorded version for channel "$currentChannel".\n'
"It looks like you haven't run "
'"flutter upgrade" on channel "$currentChannel".\n'
'\n'
'"flutter downgrade" undoes the last "flutter upgrade".\n'
'\n'
'To switch to a specific Flutter version, see: '
'https://flutter.dev/to/switch-flutter-version'
'$trailing'
);
}
@ -181,7 +190,10 @@ class DowngradeCommand extends FlutterCommand {
}
// Formats an error message that lists the currently stored versions.
Future<String> _createErrorMessage(String workingDirectory, Channel currentChannel) async {
Future<String> _createErrorMessage(
String workingDirectory,
Channel currentChannel,
) async {
final StringBuffer buffer = StringBuffer();
for (final Channel channel in Channel.values) {
if (channel == currentChannel) {
@ -191,11 +203,19 @@ class DowngradeCommand extends FlutterCommand {
if (sha == null) {
continue;
}
final RunResult parseResult = await _processUtils!.run(<String>[
'git', 'describe', '--tags', sha,
], workingDirectory: workingDirectory);
final RunResult parseResult = await _processUtils!.run(
<String>['git', 'describe', '--tags', sha],
workingDirectory: workingDirectory,
);
if (parseResult.exitCode == 0) {
buffer.writeln('Channel "${getNameForChannel(channel)}" was previously on: ${parseResult.stdout}.');
if (buffer.isEmpty) {
buffer.writeln();
}
buffer.writeln();
buffer.writeln(
'Channel "${getNameForChannel(channel)}" was previously on: '
'${parseResult.stdout}.'
);
}
}
return buffer.toString();

View File

@ -77,10 +77,16 @@ void main() {
logger: bufferLogger,
);
expect(createTestCommandRunner(command).run(const <String>['downgrade']),
throwsToolExit(message:
'There is no previously recorded version for channel "beta".\n'
'Channel "master" was previously on: v1.2.3.'
expect(
createTestCommandRunner(command).run(const <String>['downgrade']),
throwsToolExit(message: '''
It looks like you haven't run "flutter upgrade" on channel "beta".
"flutter downgrade" undoes the last "flutter upgrade".
To switch to a specific Flutter version, see: https://flutter.dev/to/switch-flutter-version
Channel "master" was previously on: v1.2.3.''',
),
);
});