upgrade.dart 7.44 KB
Newer Older
1 2 3 4 5
// Copyright 2015 The Chromium Authors. All rights reserved.
// 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/file_system.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';
16
import '../runner/flutter_command.dart';
17
import '../version.dart';
18
import 'channel.dart';
19 20

class UpgradeCommand extends FlutterCommand {
21 22 23 24 25 26 27 28 29
  UpgradeCommand() {
    argParser.addFlag(
      'force',
      abbr: 'f',
      help: 'force upgrade the flutter branch, potentially discarding local changes.',
      negatable: false,
    );
  }

30
  @override
31
  final String name = 'upgrade';
32 33

  @override
34 35
  final String description = 'Upgrade your copy of Flutter.';

36 37 38
  @override
  bool get shouldUpdateCache => false;

39 40 41 42 43
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => <DevelopmentArtifact>{
    DevelopmentArtifact.universal,
  };

44
  @override
45
  Future<FlutterCommandResult> runCommand() async {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    final UpgradeCommandRunner upgradeCommandRunner = UpgradeCommandRunner();
    await upgradeCommandRunner.runCommand(argResults['force'], GitTagVersion.determine(), FlutterVersion.instance);
    return null;
  }
}


@visibleForTesting
class UpgradeCommandRunner {
  Future<FlutterCommandResult> runCommand(bool force, GitTagVersion gitTagVersion, FlutterVersion flutterVersion) async {
    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.'
        );
      }
    }
75 76 77 78 79 80 81 82 83 84 85
    // If there are uncomitted changes we might be on the right commit but
    // 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.'
      );
    }
86 87 88 89 90 91 92 93 94
    await resetChanges(gitTagVersion);
    await upgradeChannel(flutterVersion);
    await attemptFastForward();
    await precacheArtifacts();
    await updatePackages(flutterVersion);
    await runDoctor();
    return null;
  }

95 96 97 98 99 100 101 102 103 104 105 106
  Future<bool> hasUncomittedChanges() async {
    try {
      final RunResult result = await runCheckedAsync(<String>[
        'git', 'status', '-s'
      ], workingDirectory: Cache.flutterRoot);
      return result.stdout.trim().isNotEmpty;
    } catch (e) {
      throwToolExit('git status failed: $e');
    }
    return false;
  }

107 108 109 110
  /// Check if there is an upstream repository configured.
  ///
  /// Exits tool if there is no upstream.
  Future<void> verifyUpstreamConfigured() async {
111
    try {
112
      await runCheckedAsync(<String>[
113
        'git', 'rev-parse', '@{u}',
114
      ], workingDirectory: Cache.flutterRoot);
115
    } catch (e) {
116 117 118 119 120
      throwToolExit(
        'Unable to upgrade Flutter: no origin repository configured. '
        'Run \'git remote add origin '
        'https://github.com/flutter/flutter\' in ${Cache.flutterRoot}',
      );
121
    }
122
  }
123

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  /// Attempts to reset to the last known tag or branch. This should restore the
  /// history to something that is compatible with the regular upgrade
  /// process.
  Future<void> resetChanges(GitTagVersion gitTagVersion) async {
    // We only got here by using --force.
    String tag;
    if (gitTagVersion == const GitTagVersion.unknown()) {
      tag = 'v0.0.0';
    } else {
      tag = 'v${gitTagVersion.x}.${gitTagVersion.y}.${gitTagVersion.z}';
    }
    final RunResult runResult = await runCheckedAsync(<String>[
      'git', 'reset', '--hard', tag,
    ], workingDirectory: Cache.flutterRoot);
    if (runResult.exitCode != 0) {
      throwToolExit('Failed to restore branch from hotfix.');
    }
  }
142

143 144 145 146 147
  /// 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 {
148
    printStatus('Upgrading Flutter from ${Cache.flutterRoot}...');
149
    await ChannelCommand.upgradeChannel();
150
  }
151

152 153 154 155 156 157 158
  /// 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.
  Future<void> attemptFastForward() async {
    final int code = await runCommandAndStreamOutput(
      <String>['git', 'pull', '--ff'],
159
      workingDirectory: Cache.flutterRoot,
160
      mapFunction: (String line) => matchesGitLine(line) ? null : line,
161
    );
162
    if (code != 0) {
163
      throwToolExit(null, exitCode: code);
164 165
    }
  }
166

167 168 169 170 171 172
  /// 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 {
Devon Carew's avatar
Devon Carew committed
173
    printStatus('');
174
    printStatus('Upgrading engine...');
175
    final int code = await runCommandAndStreamOutput(
176
      <String>[
177
        fs.path.join('bin', 'flutter'), '--no-color', '--no-version-check', 'precache',
178 179
      ],
      workingDirectory: Cache.flutterRoot,
180
      allowReentrantFlutter: true,
181
    );
182 183 184 185
    if (code != 0) {
      throwToolExit(null, exitCode: code);
    }
  }
186

187 188
  /// Update the user's packages.
  Future<void> updatePackages(FlutterVersion flutterVersion) async {
189
    printStatus('');
190
    printStatus(flutterVersion.toString());
191 192
    final String projectRoot = findProjectRoot();
    if (projectRoot != null) {
Devon Carew's avatar
Devon Carew committed
193
      printStatus('');
194
      await pubGet(context: PubContext.pubUpgrade, directory: projectRoot, upgrade: true, checkLastModified: false);
Devon Carew's avatar
Devon Carew committed
195
    }
196
  }
197

198 199
  /// Run flutter doctor in case requirements have changed.
  Future<void> runDoctor() async {
200 201
    printStatus('');
    printStatus('Running flutter doctor...');
202
    await runCommandAndStreamOutput(
203
      <String>[
204
        fs.path.join('bin', 'flutter'), '--no-version-check', 'doctor',
205 206 207 208
      ],
      workingDirectory: Cache.flutterRoot,
      allowReentrantFlutter: true,
    );
209
  }
210 211

  //  dev/benchmarks/complex_layout/lib/main.dart        |  24 +-
212
  static final RegExp _gitDiffRegex = RegExp(r' (\S+)\s+\|\s+\d+ [+-]+');
213 214 215

  //  rename {packages/flutter/doc => dev/docs}/styles.html (92%)
  //  delete mode 100644 doc/index.html
216
  //  create mode 100644 examples/flutter_gallery/lib/gallery/demo.dart
217
  static final RegExp _gitChangedRegex = RegExp(r' (rename|delete mode|create mode) .+');
218 219 220 221 222 223

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