upgrade.dart 12.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'package:meta/meta.dart';

7
import '../base/common.dart';
8
import '../base/io.dart';
9
import '../base/os.dart';
10
import '../base/process.dart';
11
import '../cache.dart';
Devon Carew's avatar
Devon Carew committed
12
import '../dart/pub.dart';
13
import '../globals.dart' as globals;
14
import '../persistent_tool_state.dart';
15
import '../runner/flutter_command.dart';
16
import '../version.dart';
17
import 'channel.dart';
18

19 20 21
// The official docs to install Flutter.
const String _flutterInstallDocs = 'https://flutter.dev/docs/get-started/install';

22
class UpgradeCommand extends FlutterCommand {
23
  UpgradeCommand({
24 25
    required bool verboseHelp,
    UpgradeCommandRunner? commandRunner,
26
  })
27
    : _commandRunner = commandRunner ?? UpgradeCommandRunner() {
28 29 30 31 32 33 34 35 36
    argParser
      ..addFlag(
        'force',
        abbr: 'f',
        help: 'Force upgrade the flutter branch, potentially discarding local changes.',
        negatable: false,
      )
      ..addFlag(
        'continue',
37
        hide: !verboseHelp,
38
        negatable: false,
39 40 41 42
        help: 'Trigger the second half of the upgrade flow. This should not be invoked '
              'manually. It is used re-entrantly by the standard upgrade command after '
              'the new version of Flutter is available, to hand off the upgrade process '
              'from the old version to the new version.',
43 44 45
      )
      ..addOption(
        'working-directory',
46 47 48
        hide: !verboseHelp,
        help: 'Override the upgrade working directory. '
              'This is only intended to enable integration testing of the tool itself.'
49 50 51
      )
      ..addFlag(
        'verify-only',
52
        help: 'Checks for any new Flutter updates, without actually fetching them.',
53
        negatable: false,
54
      );
55 56
  }

57 58
  final UpgradeCommandRunner _commandRunner;

59
  @override
60
  final String name = 'upgrade';
61 62

  @override
63 64
  final String description = 'Upgrade your copy of Flutter.';

65
  @override
66 67 68
  final String category = FlutterCommandCategory.sdk;

  @override
69 70
  bool get shouldUpdateCache => false;

71
  @override
72
  Future<FlutterCommandResult> runCommand() {
73
    _commandRunner.workingDirectory = stringArgDeprecated('working-directory') ?? Cache.flutterRoot!;
74
    return _commandRunner.runCommand(
75 76
      force: boolArgDeprecated('force'),
      continueFlow: boolArgDeprecated('continue'),
77
      testFlow: stringArgDeprecated('working-directory') != null,
78
      gitTagVersion: GitTagVersion.determine(globals.processUtils, globals.platform),
79
      flutterVersion: stringArgDeprecated('working-directory') == null
80
        ? globals.flutterVersion
81
        : FlutterVersion(workingDirectory: _commandRunner.workingDirectory),
82
      verifyOnly: boolArgDeprecated('verify-only'),
83
    );
84 85 86 87 88
  }
}

@visibleForTesting
class UpgradeCommandRunner {
89

90
  String? workingDirectory;
91 92

  Future<FlutterCommandResult> runCommand({
93 94 95 96 97 98
    required bool force,
    required bool continueFlow,
    required bool testFlow,
    required GitTagVersion gitTagVersion,
    required FlutterVersion flutterVersion,
    required bool verifyOnly,
99
  }) async {
100
    if (!continueFlow) {
101 102 103 104 105
      await runCommandFirstHalf(
        force: force,
        gitTagVersion: gitTagVersion,
        flutterVersion: flutterVersion,
        testFlow: testFlow,
106
        verifyOnly: verifyOnly,
107
      );
108 109 110
    } else {
      await runCommandSecondHalf(flutterVersion);
    }
111
    return FlutterCommandResult.success();
112 113
  }

114
  Future<void> runCommandFirstHalf({
115 116 117 118 119
    required bool force,
    required GitTagVersion gitTagVersion,
    required FlutterVersion flutterVersion,
    required bool testFlow,
    required bool verifyOnly,
120
  }) async {
121
    final FlutterVersion upstreamVersion = await fetchLatestVersion(localVersion: flutterVersion);
122
    if (flutterVersion.frameworkRevision == upstreamVersion.frameworkRevision) {
123 124 125
      globals.printStatus('Flutter is already up to date on channel ${flutterVersion.channel}');
      globals.printStatus('$flutterVersion');
      return;
126 127
    } else if (verifyOnly) {
      globals.printStatus('A new version of Flutter is available on channel ${flutterVersion.channel}\n');
128 129
      globals.printStatus('The latest version: ${upstreamVersion.frameworkVersion} (revision ${upstreamVersion.frameworkRevisionShort})', emphasis: true);
      globals.printStatus('Your current version: ${flutterVersion.frameworkVersion} (revision ${flutterVersion.frameworkRevisionShort})\n');
130 131 132 133 134 135
      globals.printStatus('To upgrade now, run "flutter upgrade".');
      if (flutterVersion.channel == 'stable') {
        globals.printStatus('\nSee the announcement and release notes:');
        globals.printStatus('https://flutter.dev/docs/development/tools/sdk/release-notes');
      }
      return;
136
    }
137 138 139
    if (!force && gitTagVersion == const GitTagVersion.unknown()) {
      // If the commit is a recognized branch and not master,
      // explain that we are avoiding potential damage.
140
      if (flutterVersion.channel != 'master' && kOfficialChannels.contains(flutterVersion.channel)) {
141 142 143 144 145 146 147 148 149 150
        throwToolExit(
          'Unknown flutter tag. Abandoning upgrade to avoid destroying local '
          'changes. It is recommended to use git directly if not working on '
          'an official channel.'
        );
      // Otherwise explain that local changes can be lost.
      } else {
        throwToolExit(
          'Unknown flutter tag. Abandoning upgrade to avoid destroying local '
          'changes. If it is okay to remove local changes, then re-run this '
151
          'command with "--force".'
152 153 154
        );
      }
    }
Chris Bracken's avatar
Chris Bracken committed
155
    // If there are uncommitted changes we might be on the right commit but
156
    // we should still warn.
157
    if (!force && await hasUncommittedChanges()) {
158 159 160 161 162
      throwToolExit(
        'Your flutter checkout has local changes that would be erased by '
        'upgrading. If you want to keep these changes, it is recommended that '
        'you stash them via "git stash" or else commit the changes to a local '
        'branch. If it is okay to remove local changes, then re-run this '
163
        'command with "--force".'
164 165
      );
    }
166
    recordState(flutterVersion);
167
    await ChannelCommand.upgradeChannel(flutterVersion);
168
    globals.printStatus('Upgrading Flutter to ${upstreamVersion.frameworkVersion} from ${flutterVersion.frameworkVersion} in $workingDirectory...');
169
    await attemptReset(upstreamVersion.frameworkRevision);
170
    if (!testFlow) {
171 172
      await flutterUpgradeContinue();
    }
173 174
  }

175
  void recordState(FlutterVersion flutterVersion) {
176
    final Channel? channel = getChannelForName(flutterVersion.channel);
177 178 179
    if (channel == null) {
      return;
    }
180
    globals.persistentToolState!.updateLastActiveVersion(flutterVersion.frameworkRevision, channel);
181 182
  }

183
  Future<void> flutterUpgradeContinue() async {
184
    final int code = await globals.processUtils.stream(
185
      <String>[
186
        globals.fs.path.join('bin', 'flutter'),
187 188 189 190
        'upgrade',
        '--continue',
        '--no-version-check',
      ],
191
      workingDirectory: workingDirectory,
192
      allowReentrantFlutter: true,
193
      environment: Map<String, String>.of(globals.platform.environment),
194 195 196 197 198 199 200 201 202
    );
    if (code != 0) {
      throwToolExit(null, exitCode: code);
    }
  }

  // This method should only be called if the upgrade command is invoked
  // re-entrantly with the `--continue` flag
  Future<void> runCommandSecondHalf(FlutterVersion flutterVersion) async {
203
    // Make sure the welcome message re-display is delayed until the end.
204 205
    final PersistentToolState persistentToolState = globals.persistentToolState!;
    persistentToolState.setShouldRedisplayWelcomeMessage(false);
206 207 208
    await precacheArtifacts();
    await updatePackages(flutterVersion);
    await runDoctor();
209
    // Force the welcome message to re-display following the upgrade.
210
    persistentToolState.setShouldRedisplayWelcomeMessage(true);
211 212
  }

213
  Future<bool> hasUncommittedChanges() async {
214
    try {
215
      final RunResult result = await globals.processUtils.run(
216 217
        <String>['git', 'status', '-s'],
        throwOnError: true,
218
        workingDirectory: workingDirectory,
219
      );
220
      return result.stdout.trim().isNotEmpty;
221 222 223
    } on ProcessException catch (error) {
      throwToolExit(
        'The tool could not verify the status of the current flutter checkout. '
224 225
        'This might be due to git not being installed or an internal error. '
        'If it is okay to ignore potential local changes, then re-run this '
226 227
        'command with "--force".\n'
        'Error: $error.'
228
      );
229 230 231
    }
  }

232
  /// Returns the remote HEAD flutter version.
233
  ///
234
  /// Exits tool if HEAD isn't pointing to a branch, or there is no upstream.
235
  Future<FlutterVersion> fetchLatestVersion({
236
    required FlutterVersion localVersion,
237
  }) async {
238
    String revision;
239
    try {
240
      // Fetch upstream branch's commits and tags
241
      await globals.processUtils.run(
242
        <String>['git', 'fetch', '--tags'],
243
        throwOnError: true,
244
        workingDirectory: workingDirectory,
245
      );
246
      // Get the latest commit revision of the upstream
247
      final RunResult result = await globals.processUtils.run(
248
          <String>['git', 'rev-parse', '--verify', kGitTrackingUpstream],
249 250 251 252
          throwOnError: true,
          workingDirectory: workingDirectory,
      );
      revision = result.stdout.trim();
253 254 255 256
    } on Exception catch (e) {
      final String errorString = e.toString();
      if (errorString.contains('fatal: HEAD does not point to a branch')) {
        throwToolExit(
257 258 259 260
          'Unable to upgrade Flutter: Your Flutter checkout is currently not '
          'on a release branch.\n'
          'Use "flutter channel" to switch to an official channel, and retry. '
          'Alternatively, re-install Flutter by going to $_flutterInstallDocs.'
261 262 263
        );
      } else if (errorString.contains('fatal: no upstream configured for branch')) {
        throwToolExit(
264 265 266 267
          'Unable to upgrade Flutter: The current Flutter branch/channel is '
          'not tracking any remote repository.\n'
          'Re-install Flutter by going to $_flutterInstallDocs.'
        );
268 269 270
      } else {
        throwToolExit(errorString);
      }
271
    }
272 273 274 275 276 277 278 279 280 281 282
    // At this point the current checkout should be on HEAD of a branch having
    // an upstream. Check whether this upstream is "standard".
    final VersionCheckError? error = VersionUpstreamValidator(version: localVersion, platform: globals.platform).run();
    if (error != null) {
      throwToolExit(
        'Unable to upgrade Flutter: '
        '${error.message}\n'
        'Reinstalling Flutter may fix this issue. Visit $_flutterInstallDocs '
        'for instructions.'
      );
    }
283
    return FlutterVersion(workingDirectory: workingDirectory, frameworkRevision: revision);
284
  }
285

286
  /// Attempts a hard reset to the given revision.
287
  ///
288 289 290
  /// This is a reset instead of fast forward because if we are on a release
  /// branch with cherry picks, there may not be a direct fast-forward route
  /// to the next release.
291 292
  Future<void> attemptReset(String newRevision) async {
    try {
293
      await globals.processUtils.run(
294 295 296 297 298 299
        <String>['git', 'reset', '--hard', newRevision],
        throwOnError: true,
        workingDirectory: workingDirectory,
      );
    } on ProcessException catch (e) {
      throwToolExit(e.message, exitCode: e.errorCode);
300
    }
301
  }
302

303 304 305 306 307 308
  /// Update the engine repository and precache all artifacts.
  ///
  /// Check for and download any engine and pkg/ updates. We run the 'flutter'
  /// shell script re-entrantly here so that it will download the updated
  /// Dart and so forth if necessary.
  Future<void> precacheArtifacts() async {
309 310
    globals.printStatus('');
    globals.printStatus('Upgrading engine...');
311
    final int code = await globals.processUtils.stream(
312
      <String>[
313
        globals.fs.path.join('bin', 'flutter'), '--no-color', '--no-version-check', 'precache',
314
      ],
315
      workingDirectory: workingDirectory,
316
      allowReentrantFlutter: true,
317
      environment: Map<String, String>.of(globals.platform.environment),
318
    );
319 320 321 322
    if (code != 0) {
      throwToolExit(null, exitCode: code);
    }
  }
323

324 325
  /// Update the user's packages.
  Future<void> updatePackages(FlutterVersion flutterVersion) async {
326 327
    globals.printStatus('');
    globals.printStatus(flutterVersion.toString());
328
    final String? projectRoot = findProjectRoot(globals.fs);
329
    if (projectRoot != null) {
330
      globals.printStatus('');
331 332 333 334 335
      await pub.get(
        context: PubContext.pubUpgrade,
        directory: projectRoot,
        upgrade: true,
      );
Devon Carew's avatar
Devon Carew committed
336
    }
337
  }
338

339 340
  /// Run flutter doctor in case requirements have changed.
  Future<void> runDoctor() async {
341 342
    globals.printStatus('');
    globals.printStatus('Running flutter doctor...');
343
    await globals.processUtils.stream(
344
      <String>[
345
        globals.fs.path.join('bin', 'flutter'), '--no-version-check', 'doctor',
346
      ],
347
      workingDirectory: workingDirectory,
348 349
      allowReentrantFlutter: true,
    );
350 351
  }
}