channel.dart 6.27 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
import '../base/common.dart';
6
import '../cache.dart';
7
import '../globals.dart' as globals;
8
import '../runner/flutter_command.dart';
9
import '../version.dart';
10 11

class ChannelCommand extends FlutterCommand {
12
  ChannelCommand({ bool verboseHelp = false }) {
13 14 15 16 17 18 19 20 21
    argParser.addFlag(
      'all',
      abbr: 'a',
      help: 'Include all the available branches (including local branches) when listing channels.',
      defaultsTo: false,
      hide: !verboseHelp,
    );
  }

22 23 24 25
  @override
  final String name = 'channel';

  @override
26
  final String description = 'List or switch Flutter channels.';
27 28 29 30

  @override
  String get invocation => '${runner.executableName} $name [<channel-name>]';

31 32 33
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{};

34
  @override
35
  Future<FlutterCommandResult> runCommand() async {
36 37
    switch (argResults.rest.length) {
      case 0:
38
        await _listChannels(
39 40
          showAll: boolArg('all'),
          verbose: globalResults['verbose'] as bool,
41
        );
42
        return FlutterCommandResult.success();
43
      case 1:
44
        await _switchChannel(argResults.rest[0]);
45
        return FlutterCommandResult.success();
46
      default:
47
        throw ToolExit('Too many arguments.\n$usage');
48 49 50
    }
  }

51
  Future<void> _listChannels({ bool showAll, bool verbose }) async {
52
    // Beware: currentBranch could contain PII. See getBranchName().
53 54
    final String currentChannel = globals.flutterVersion.channel;
    final String currentBranch = globals.flutterVersion.getBranchName();
55
    final Set<String> seenUnofficialChannels = <String>{};
56
    final List<String> rawOutput = <String>[];
57 58

    showAll = showAll || currentChannel != currentBranch;
59

60
    globals.printStatus('Flutter channels:');
61
    final int result = await globals.processUtils.stream(
62 63 64
      <String>['git', 'branch', '-r'],
      workingDirectory: Cache.flutterRoot,
      mapFunction: (String line) {
65
        rawOutput.add(line);
66
        return null;
67 68
      },
    );
69 70 71 72
    if (result != 0) {
      final String details = verbose ? '\n${rawOutput.join('\n')}' : '';
      throwToolExit('List channels failed: $result$details', exitCode: result);
    }
73

74
    final List<String> officialChannels = kOfficialChannels.toList();
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    final List<bool> availableChannels = List<bool>.filled(officialChannels.length, false);

    for (final String line in rawOutput) {
      final List<String> split = line.split('/');
      final String branch = split[1];
      if (split.length > 1) {
        final int index = officialChannels.indexOf(branch);

        if (index != -1) { // Mark all available channels official channels from output
          availableChannels[index] = true;
        } else if (showAll && !seenUnofficialChannels.contains(branch)) {
        // add other branches to seenUnofficialChannels if --all flag is given (to print later)
          seenUnofficialChannels.add(branch);
        }
      }
    }

    // print all available official channels in sorted manner
    for (int i = 0; i < officialChannels.length; i++) {
      // only print non-missing channels
      if (availableChannels[i]) {
        String currentIndicator = ' ';
        if (officialChannels[i] == currentChannel){
          currentIndicator = '*';
        }
        globals.printStatus('$currentIndicator ${officialChannels[i]}');
      }
    }

    // print all remaining channels if showAll is true
    if (showAll) {
      for (final String branch in seenUnofficialChannels) {
        if (currentBranch == branch) {
          globals.printStatus('* $branch');
        } else if (!branch.startsWith('HEAD ')) {
          globals.printStatus('  $branch');
        }
      }
    }
114 115
  }

116
  Future<void> _switchChannel(String branchName) async {
117
    globals.printStatus("Switching to flutter channel '$branchName'...");
118 119
    if (kObsoleteBranches.containsKey(branchName)) {
      final String alternative = kObsoleteBranches[branchName];
120
      globals.printStatus("This channel is obsolete. Consider switching to the '$alternative' channel instead.");
121
    } else if (!kOfficialChannels.contains(branchName)) {
122
      globals.printStatus('This is not an official channel. For a list of available channels, try "flutter channel".');
123
    }
124 125 126
    await _checkout(branchName);
    globals.printStatus("Successfully switched to flutter channel '$branchName'.");
    globals.printStatus("To ensure that you're on the latest build from this channel, run 'flutter upgrade'");
127 128
  }

129
  static Future<void> upgradeChannel() async {
130
    final String channel = globals.flutterVersion.channel;
131 132
    if (kObsoleteBranches.containsKey(channel)) {
      final String alternative = kObsoleteBranches[channel];
133
      globals.printStatus("Transitioning from '$channel' to '$alternative'...");
134 135 136 137
      return _checkout(alternative);
    }
  }

138
  static Future<void> _checkout(String branchName) async {
139
    // Get latest refs from upstream.
140
    int result = await globals.processUtils.stream(
141
      <String>['git', 'fetch'],
142
      workingDirectory: Cache.flutterRoot,
143
      prefix: 'git: ',
144
    );
145

146
    if (result == 0) {
147
      result = await globals.processUtils.stream(
148
        <String>['git', 'show-ref', '--verify', '--quiet', 'refs/heads/$branchName'],
149 150 151
        workingDirectory: Cache.flutterRoot,
        prefix: 'git: ',
      );
152 153
      if (result == 0) {
        // branch already exists, try just switching to it
154
        result = await globals.processUtils.stream(
155
          <String>['git', 'checkout', branchName, '--'],
156 157 158 159 160
          workingDirectory: Cache.flutterRoot,
          prefix: 'git: ',
        );
      } else {
        // branch does not exist, we have to create it
161
        result = await globals.processUtils.stream(
162 163 164 165 166
          <String>['git', 'checkout', '--track', '-b', branchName, 'origin/$branchName'],
          workingDirectory: Cache.flutterRoot,
          prefix: 'git: ',
        );
      }
167
    }
168
    if (result != 0) {
169
      throwToolExit('Switching channels failed with error code $result.', exitCode: result);
170 171 172 173 174
    } else {
      // Remove the version check stamp, since it could contain out-of-date
      // information that pertains to the previous channel.
      await FlutterVersion.resetFlutterVersionFreshnessCheck();
    }
175 176
  }
}