| 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'package:process/process.dart' ; |
| 6 | |
| 7 | import '../base/common.dart'; |
| 8 | import '../base/file_system.dart'; |
| 9 | import '../base/io.dart'; |
| 10 | import '../base/logger.dart'; |
| 11 | import '../base/process.dart'; |
| 12 | import '../base/terminal.dart'; |
| 13 | import '../cache.dart'; |
| 14 | import '../globals.dart' as globals; |
| 15 | import '../persistent_tool_state.dart'; |
| 16 | import '../runner/flutter_command.dart'; |
| 17 | import '../version.dart'; |
| 18 | |
| 19 | /// The flutter downgrade command returns the SDK to the last recorded version |
| 20 | /// for a particular branch. |
| 21 | /// |
| 22 | /// For example, suppose a user on the beta channel upgrades from 1.2.3 to 1.4.6. |
| 23 | /// The tool will record that sha "abcdefg" was the last active beta channel in the |
| 24 | /// persistent tool state. If the user is still on the beta channel and runs |
| 25 | /// flutter downgrade, this will take the user back to "abcdefg". They will not be |
| 26 | /// able to downgrade again, since the tool only records one prior version. |
| 27 | /// Additionally, if they had switched channels to stable before trying to downgrade, |
| 28 | /// the command would fail since there was no previously recorded stable version. |
| 29 | class DowngradeCommand extends FlutterCommand { |
| 30 | DowngradeCommand({ |
| 31 | bool verboseHelp = false, |
| 32 | PersistentToolState? persistentToolState, |
| 33 | required Logger logger, |
| 34 | ProcessManager? processManager, |
| 35 | FlutterVersion? flutterVersion, |
| 36 | Terminal? terminal, |
| 37 | Stdio? stdio, |
| 38 | FileSystem? fileSystem, |
| 39 | }) : _terminal = terminal, |
| 40 | _flutterVersion = flutterVersion, |
| 41 | _persistentToolState = persistentToolState, |
| 42 | _processManager = processManager, |
| 43 | _stdio = stdio, |
| 44 | _logger = logger, |
| 45 | _fileSystem = fileSystem { |
| 46 | argParser.addOption( |
| 47 | 'working-directory' , |
| 48 | hide: !verboseHelp, |
| 49 | help: |
| 50 | 'Override the downgrade working directory. ' |
| 51 | 'This is only intended to enable integration testing of the tool itself. ' |
| 52 | 'It allows one to use the flutter tool from one checkout to downgrade a ' |
| 53 | 'different checkout.' , |
| 54 | ); |
| 55 | argParser.addFlag( |
| 56 | 'prompt' , |
| 57 | defaultsTo: true, |
| 58 | hide: !verboseHelp, |
| 59 | help: 'Show the downgrade prompt.' , |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | Terminal? _terminal; |
| 64 | FlutterVersion? _flutterVersion; |
| 65 | PersistentToolState? _persistentToolState; |
| 66 | ProcessUtils? _processUtils; |
| 67 | ProcessManager? _processManager; |
| 68 | final Logger _logger; |
| 69 | Stdio? _stdio; |
| 70 | FileSystem? _fileSystem; |
| 71 | |
| 72 | @override |
| 73 | String get description => 'Downgrade Flutter to the last active version for the current channel.' ; |
| 74 | |
| 75 | @override |
| 76 | String get name => 'downgrade' ; |
| 77 | |
| 78 | @override |
| 79 | final String category = FlutterCommandCategory.sdk; |
| 80 | |
| 81 | @override |
| 82 | Future<FlutterCommandResult> runCommand() async { |
| 83 | // Commands do not necessarily have access to the correct zone injected |
| 84 | // values when being created. Fields must be lazily instantiated in runCommand, |
| 85 | // at least until the zone injection is refactored. |
| 86 | _terminal ??= globals.terminal; |
| 87 | _flutterVersion ??= globals.flutterVersion; |
| 88 | _persistentToolState ??= globals.persistentToolState; |
| 89 | _processManager ??= globals.processManager; |
| 90 | _processUtils ??= ProcessUtils(processManager: _processManager!, logger: _logger); |
| 91 | _stdio ??= globals.stdio; |
| 92 | _fileSystem ??= globals.fs; |
| 93 | String workingDirectory = Cache.flutterRoot!; |
| 94 | if (argResults!.wasParsed('working-directory' )) { |
| 95 | workingDirectory = stringArg('working-directory' )!; |
| 96 | _flutterVersion = FlutterVersion(fs: _fileSystem!, flutterRoot: workingDirectory); |
| 97 | } |
| 98 | |
| 99 | final String currentChannel = _flutterVersion!.channel; |
| 100 | final Channel? channel = getChannelForName(currentChannel); |
| 101 | if (channel == null) { |
| 102 | throwToolExit( |
| 103 | 'Flutter is not currently on a known channel. ' |
| 104 | 'Use "flutter channel" to switch to an official channel. ' , |
| 105 | ); |
| 106 | } |
| 107 | final PersistentToolState persistentToolState = _persistentToolState!; |
| 108 | final String? lastFlutterVersion = persistentToolState.lastActiveVersion(channel); |
| 109 | final String? currentFlutterVersion = _flutterVersion?.frameworkRevision; |
| 110 | if (lastFlutterVersion == null || currentFlutterVersion == lastFlutterVersion) { |
| 111 | final String trailing = await _createErrorMessage(workingDirectory, channel); |
| 112 | throwToolExit( |
| 113 | "It looks like you haven't run " |
| 114 | '"flutter upgrade" on channel " $currentChannel".\n' |
| 115 | '\n' |
| 116 | '"flutter downgrade" undoes the last "flutter upgrade".\n' |
| 117 | '\n' |
| 118 | 'To switch to a specific Flutter version, see: ' |
| 119 | 'https://flutter.dev/to/switch-flutter-version' |
| 120 | ' $trailing' , |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | // Detect unknown versions. |
| 125 | final ProcessUtils processUtils = _processUtils!; |
| 126 | final RunResult parseResult = await processUtils.run(<String>[ |
| 127 | 'git' , |
| 128 | 'describe' , |
| 129 | '--tags' , |
| 130 | lastFlutterVersion, |
| 131 | ], workingDirectory: workingDirectory); |
| 132 | if (parseResult.exitCode != 0) { |
| 133 | throwToolExit('Failed to parse version for downgrade:\n ${parseResult.stderr}' ); |
| 134 | } |
| 135 | final String humanReadableVersion = parseResult.stdout; |
| 136 | |
| 137 | // If there is a terminal attached, prompt the user to confirm the downgrade. |
| 138 | final Stdio stdio = _stdio!; |
| 139 | final Terminal terminal = _terminal!; |
| 140 | if (stdio.hasTerminal && boolArg('prompt' )) { |
| 141 | terminal.usesTerminalUi = true; |
| 142 | final String result = await terminal.promptForCharInput( |
| 143 | const <String>['y' , 'n' ], |
| 144 | prompt: 'Downgrade flutter to version $humanReadableVersion?' , |
| 145 | logger: _logger, |
| 146 | ); |
| 147 | if (result == 'n' ) { |
| 148 | return FlutterCommandResult.success(); |
| 149 | } |
| 150 | } else { |
| 151 | _logger.printStatus('Downgrading Flutter to version $humanReadableVersion' ); |
| 152 | } |
| 153 | |
| 154 | // To downgrade the tool, we perform a git checkout --hard, and then |
| 155 | // switch channels. The version recorded must have existed on that branch |
| 156 | // so this operation is safe. |
| 157 | try { |
| 158 | await processUtils.run( |
| 159 | <String>['git' , 'reset' , '--hard' , lastFlutterVersion], |
| 160 | throwOnError: true, |
| 161 | workingDirectory: workingDirectory, |
| 162 | ); |
| 163 | } on ProcessException catch (error) { |
| 164 | throwToolExit( |
| 165 | 'Unable to downgrade Flutter: The tool could not update to the version ' |
| 166 | ' $humanReadableVersion.\n' |
| 167 | 'Error: $error' , |
| 168 | ); |
| 169 | } |
| 170 | try { |
| 171 | await processUtils.run( |
| 172 | // The `--` bit (because it's followed by nothing) means that we don't actually change |
| 173 | // anything in the working tree, which avoids the need to first go into detached HEAD mode. |
| 174 | <String>['git' , 'checkout' , currentChannel, '--' ], |
| 175 | throwOnError: true, |
| 176 | workingDirectory: workingDirectory, |
| 177 | ); |
| 178 | } on ProcessException catch (error) { |
| 179 | throwToolExit( |
| 180 | 'Unable to downgrade Flutter: The tool could not switch to the channel ' |
| 181 | ' $currentChannel.\n' |
| 182 | 'Error: $error' , |
| 183 | ); |
| 184 | } |
| 185 | await FlutterVersion.resetFlutterVersionFreshnessCheck(); |
| 186 | _logger.printStatus('Success' ); |
| 187 | return FlutterCommandResult.success(); |
| 188 | } |
| 189 | |
| 190 | // Formats an error message that lists the currently stored versions. |
| 191 | Future<String> _createErrorMessage(String workingDirectory, Channel currentChannel) async { |
| 192 | final buffer = StringBuffer(); |
| 193 | for (final Channel channel in Channel.values) { |
| 194 | if (channel == currentChannel) { |
| 195 | continue; |
| 196 | } |
| 197 | final String? sha = _persistentToolState?.lastActiveVersion(channel); |
| 198 | if (sha == null) { |
| 199 | continue; |
| 200 | } |
| 201 | final RunResult parseResult = await _processUtils!.run(<String>[ |
| 202 | 'git' , |
| 203 | 'describe' , |
| 204 | '--tags' , |
| 205 | sha, |
| 206 | ], workingDirectory: workingDirectory); |
| 207 | if (parseResult.exitCode == 0) { |
| 208 | if (buffer.isEmpty) { |
| 209 | buffer.writeln(); |
| 210 | } |
| 211 | buffer.writeln(); |
| 212 | buffer.writeln( |
| 213 | 'Channel " ${getNameForChannel(channel)}" was previously on: ' |
| 214 | ' ${parseResult.stdout}.' , |
| 215 | ); |
| 216 | } |
| 217 | } |
| 218 | return buffer.toString(); |
| 219 | } |
| 220 | } |
| 221 | |