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

import 'dart:async';
6

7 8
import 'package:meta/meta.dart';

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

class UpgradeCommand extends FlutterCommand {
22 23
  UpgradeCommand([UpgradeCommandRunner commandRunner])
    : _commandRunner = commandRunner ?? UpgradeCommandRunner() {
24 25 26 27 28 29 30 31 32 33 34
    argParser
      ..addFlag(
        'force',
        abbr: 'f',
        help: 'Force upgrade the flutter branch, potentially discarding local changes.',
        negatable: false,
      )
      ..addFlag(
        'continue',
        hide: true,
        negatable: false,
35 36 37
        help: 'For the second half of the upgrade flow requiring the new '
              'version of Flutter. Should not be invoked manually, but '
              're-entrantly by the standard upgrade command.',
38
      );
39 40
  }

41 42
  final UpgradeCommandRunner _commandRunner;

43
  @override
44
  final String name = 'upgrade';
45 46

  @override
47 48
  final String description = 'Upgrade your copy of Flutter.';

49 50 51
  @override
  bool get shouldUpdateCache => false;

52
  @override
53 54
  Future<FlutterCommandResult> runCommand() {
    return _commandRunner.runCommand(
55 56
      boolArg('force'),
      boolArg('continue'),
57 58 59
      GitTagVersion.determine(),
      FlutterVersion.instance,
    );
60 61 62 63 64
  }
}

@visibleForTesting
class UpgradeCommandRunner {
65 66 67 68 69 70 71 72 73 74 75
  Future<FlutterCommandResult> runCommand(
    bool force,
    bool continueFlow,
    GitTagVersion gitTagVersion,
    FlutterVersion flutterVersion,
  ) async {
    if (!continueFlow) {
      await runCommandFirstHalf(force, gitTagVersion, flutterVersion);
    } else {
      await runCommandSecondHalf(flutterVersion);
    }
76
    return FlutterCommandResult.success();
77 78 79 80 81 82 83
  }

  Future<void> runCommandFirstHalf(
    bool force,
    GitTagVersion gitTagVersion,
    FlutterVersion flutterVersion,
  ) async {
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    await verifyUpstreamConfigured();
    if (!force && gitTagVersion == const GitTagVersion.unknown()) {
      // If the commit is a recognized branch and not master,
      // explain that we are avoiding potential damage.
      if (flutterVersion.channel != 'master' && FlutterVersion.officialChannels.contains(flutterVersion.channel)) {
        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 '
          'command with --force.'
        );
      }
    }
Chris Bracken's avatar
Chris Bracken committed
103
    // If there are uncommitted changes we might be on the right commit but
104 105 106 107 108 109 110 111 112 113
    // we should still warn.
    if (!force && await hasUncomittedChanges()) {
      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 '
        'command with --force.'
      );
    }
114 115
    await resetChanges(gitTagVersion);
    await upgradeChannel(flutterVersion);
116 117 118
    final bool alreadyUpToDate = await attemptFastForward(flutterVersion);
    if (alreadyUpToDate) {
      // If the upgrade was a no op, then do not continue with the second half.
119 120
      globals.printStatus('Flutter is already up to date on channel ${flutterVersion.channel}');
      globals.printStatus('$flutterVersion');
121 122 123
    } else {
      await flutterUpgradeContinue();
    }
124 125 126
  }

  Future<void> flutterUpgradeContinue() async {
127
    final int code = await processUtils.stream(
128
      <String>[
129
        globals.fs.path.join('bin', 'flutter'),
130 131 132 133 134 135
        'upgrade',
        '--continue',
        '--no-version-check',
      ],
      workingDirectory: Cache.flutterRoot,
      allowReentrantFlutter: true,
136
      environment: Map<String, String>.of(globals.platform.environment),
137 138 139 140 141 142 143 144 145
    );
    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 {
146 147
    // Make sure the welcome message re-display is delayed until the end.
    persistentToolState.redisplayWelcomeMessage = false;
148 149 150
    await precacheArtifacts();
    await updatePackages(flutterVersion);
    await runDoctor();
151 152
    // Force the welcome message to re-display following the upgrade.
    persistentToolState.redisplayWelcomeMessage = true;
153 154
  }

155 156
  Future<bool> hasUncomittedChanges() async {
    try {
157 158 159 160 161
      final RunResult result = await processUtils.run(
        <String>['git', 'status', '-s'],
        throwOnError: true,
        workingDirectory: Cache.flutterRoot,
      );
162
      return result.stdout.trim().isNotEmpty;
163 164 165 166 167 168 169 170
    } on ProcessException catch (error) {
      throwToolExit(
        'The tool could not verify the status of the current flutter checkout. '
        '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'
        'command with --force.'
        '\nError: $error.'
      );
171 172 173 174
    }
    return false;
  }

175 176 177 178
  /// Check if there is an upstream repository configured.
  ///
  /// Exits tool if there is no upstream.
  Future<void> verifyUpstreamConfigured() async {
179
    try {
180 181 182 183 184
      await processUtils.run(
        <String>[ 'git', 'rev-parse', '@{u}'],
        throwOnError: true,
        workingDirectory: Cache.flutterRoot,
      );
185
    } catch (e) {
186 187 188 189 190
      throwToolExit(
        'Unable to upgrade Flutter: no origin repository configured. '
        'Run \'git remote add origin '
        'https://github.com/flutter/flutter\' in ${Cache.flutterRoot}',
      );
191
    }
192
  }
193

194 195 196 197 198
  /// Attempts to reset to the last non-hotfix tag.
  ///
  /// If the git history is on a hotfix, doing a fast forward will not pick up
  /// major or minor version upgrades. By resetting to the point before the
  /// hotfix, doing a git fast forward should succeed.
199 200 201 202 203 204 205
  Future<void> resetChanges(GitTagVersion gitTagVersion) async {
    String tag;
    if (gitTagVersion == const GitTagVersion.unknown()) {
      tag = 'v0.0.0';
    } else {
      tag = 'v${gitTagVersion.x}.${gitTagVersion.y}.${gitTagVersion.z}';
    }
206
    try {
207 208 209 210 211
      await processUtils.run(
        <String>['git', 'reset', '--hard', tag],
        throwOnError: true,
        workingDirectory: Cache.flutterRoot,
      );
212 213 214 215 216 217 218
    } on ProcessException catch (error) {
      throwToolExit(
        'Unable to upgrade Flutter: The tool could not update to the version $tag. '
        'This may be due to git not being installed or an internal error.'
        'Please ensure that git is installed on your computer and retry again.'
        '\nError: $error.'
      );
219 220
    }
  }
221

222 223 224 225 226
  /// Attempts to upgrade the channel.
  ///
  /// If the user is on a deprecated channel, attempts to migrate them off of
  /// it.
  Future<void> upgradeChannel(FlutterVersion flutterVersion) async {
227
    globals.printStatus('Upgrading Flutter from ${Cache.flutterRoot}...');
228
    await ChannelCommand.upgradeChannel();
229
  }
230

231 232 233 234
  /// Attempts to rebase the upstream onto the local branch.
  ///
  /// If there haven't been any hot fixes or local changes, this is equivalent
  /// to a fast-forward.
235 236 237 238
  ///
  /// If the fast forward lands us on the same channel and revision, then
  /// returns true, otherwise returns false.
  Future<bool> attemptFastForward(FlutterVersion oldFlutterVersion) async {
239
    final int code = await processUtils.stream(
240
      <String>['git', 'pull', '--ff'],
241
      workingDirectory: Cache.flutterRoot,
242
      mapFunction: (String line) => matchesGitLine(line) ? null : line,
243
    );
244
    if (code != 0) {
245
      throwToolExit(null, exitCode: code);
246
    }
247 248 249 250 251 252 253 254

    // Check if the upgrade did anything.
    bool alreadyUpToDate = false;
    try {
      final FlutterVersion newFlutterVersion = FlutterVersion();
      alreadyUpToDate = newFlutterVersion.channel == oldFlutterVersion.channel &&
        newFlutterVersion.frameworkRevision == oldFlutterVersion.frameworkRevision;
    } catch (e) {
255
      globals.printTrace('Failed to determine FlutterVersion after upgrade fast-forward: $e');
256 257
    }
    return alreadyUpToDate;
258
  }
259

260 261 262 263 264 265
  /// 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 {
266 267
    globals.printStatus('');
    globals.printStatus('Upgrading engine...');
268
    final int code = await processUtils.stream(
269
      <String>[
270
        globals.fs.path.join('bin', 'flutter'), '--no-color', '--no-version-check', 'precache',
271 272
      ],
      workingDirectory: Cache.flutterRoot,
273
      allowReentrantFlutter: true,
274
      environment: Map<String, String>.of(globals.platform.environment),
275
    );
276 277 278 279
    if (code != 0) {
      throwToolExit(null, exitCode: code);
    }
  }
280

281 282
  /// Update the user's packages.
  Future<void> updatePackages(FlutterVersion flutterVersion) async {
283 284
    globals.printStatus('');
    globals.printStatus(flutterVersion.toString());
285 286
    final String projectRoot = findProjectRoot();
    if (projectRoot != null) {
287
      globals.printStatus('');
288
      await pub.get(context: PubContext.pubUpgrade, directory: projectRoot, upgrade: true, checkLastModified: false);
Devon Carew's avatar
Devon Carew committed
289
    }
290
  }
291

292 293
  /// Run flutter doctor in case requirements have changed.
  Future<void> runDoctor() async {
294 295
    globals.printStatus('');
    globals.printStatus('Running flutter doctor...');
296
    await processUtils.stream(
297
      <String>[
298
        globals.fs.path.join('bin', 'flutter'), '--no-version-check', 'doctor',
299 300 301 302
      ],
      workingDirectory: Cache.flutterRoot,
      allowReentrantFlutter: true,
    );
303
  }
304 305

  //  dev/benchmarks/complex_layout/lib/main.dart        |  24 +-
306
  static final RegExp _gitDiffRegex = RegExp(r' (\S+)\s+\|\s+\d+ [+-]+');
307 308 309

  //  rename {packages/flutter/doc => dev/docs}/styles.html (92%)
  //  delete mode 100644 doc/index.html
310
  //  create mode 100644 examples/flutter_gallery/lib/gallery/demo.dart
311
  static final RegExp _gitChangedRegex = RegExp(r' (rename|delete mode|create mode) .+');
312 313 314 315 316 317

  static bool matchesGitLine(String line) {
    return _gitDiffRegex.hasMatch(line)
      || _gitChangedRegex.hasMatch(line)
      || line == 'Fast-forward';
  }
318
}