channel.dart 5.13 KB
Newer Older
1 2 3 4 5 6
// 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';

7
import '../base/common.dart';
8 9 10 11
import '../base/process.dart';
import '../cache.dart';
import '../globals.dart';
import '../runner/flutter_command.dart';
12
import '../version.dart';
13 14

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

25 26 27 28 29 30 31 32 33
  @override
  final String name = 'channel';

  @override
  final String description = 'List or switch flutter channels.';

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

34 35 36
  @override
  Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{};

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

54
  Future<void> _listChannels({ bool showAll, bool verbose }) async {
55 56 57
    // Beware: currentBranch could contain PII. See getBranchName().
    final String currentChannel = FlutterVersion.instance.channel;
    final String currentBranch = FlutterVersion.instance.getBranchName();
58
    final Set<String> seenChannels = <String>{};
59
    final List<String> rawOutput = <String>[];
60 61

    showAll = showAll || currentChannel != currentBranch;
62 63

    printStatus('Flutter channels:');
64
    final int result = await processUtils.stream(
65 66 67
      <String>['git', 'branch', '-r'],
      workingDirectory: Cache.flutterRoot,
      mapFunction: (String line) {
68
        if (verbose) {
69
          rawOutput.add(line);
70
        }
71
        final List<String> split = line.split('/');
72
        if (split.length < 2) {
73
          return null;
74
        }
75
        final String branchName = split[1];
76 77 78 79
        if (seenChannels.contains(branchName)) {
          return null;
        }
        seenChannels.add(branchName);
80
        if (branchName == currentBranch) {
81
          return '* $branchName';
82
        }
83
        if (!branchName.startsWith('HEAD ') &&
84
            (showAll || FlutterVersion.officialChannels.contains(branchName))) {
85
          return '  $branchName';
86
        }
87
        return null;
88 89
      },
    );
90 91 92 93
    if (result != 0) {
      final String details = verbose ? '\n${rawOutput.join('\n')}' : '';
      throwToolExit('List channels failed: $result$details', exitCode: result);
    }
94 95
  }

96
  Future<void> _switchChannel(String branchName) {
97 98 99 100 101 102 103 104 105 106
    printStatus("Switching to flutter channel '$branchName'...");
    if (FlutterVersion.obsoleteBranches.containsKey(branchName)) {
      final String alternative = FlutterVersion.obsoleteBranches[branchName];
      printStatus("This channel is obsolete. Consider switching to the '$alternative' channel instead.");
    } else if (!FlutterVersion.officialChannels.contains(branchName)) {
      printStatus('This is not an official channel. For a list of available channels, try "flutter channel".');
    }
    return _checkout(branchName);
  }

107
  static Future<void> upgradeChannel() async {
108 109 110 111 112 113 114 115
    final String channel = FlutterVersion.instance.channel;
    if (FlutterVersion.obsoleteBranches.containsKey(channel)) {
      final String alternative = FlutterVersion.obsoleteBranches[channel];
      printStatus("Transitioning from '$channel' to '$alternative'...");
      return _checkout(alternative);
    }
  }

116
  static Future<void> _checkout(String branchName) async {
117
    // Get latest refs from upstream.
118
    int result = await processUtils.stream(
119
      <String>['git', 'fetch'],
120
      workingDirectory: Cache.flutterRoot,
121
      prefix: 'git: ',
122
    );
123

124
    if (result == 0) {
125
      result = await processUtils.stream(
126
        <String>['git', 'show-ref', '--verify', '--quiet', 'refs/heads/$branchName'],
127 128 129
        workingDirectory: Cache.flutterRoot,
        prefix: 'git: ',
      );
130 131
      if (result == 0) {
        // branch already exists, try just switching to it
132
        result = await processUtils.stream(
133
          <String>['git', 'checkout', branchName, '--'],
134 135 136 137 138
          workingDirectory: Cache.flutterRoot,
          prefix: 'git: ',
        );
      } else {
        // branch does not exist, we have to create it
139
        result = await processUtils.stream(
140 141 142 143 144
          <String>['git', 'checkout', '--track', '-b', branchName, 'origin/$branchName'],
          workingDirectory: Cache.flutterRoot,
          prefix: 'git: ',
        );
      }
145
    }
146
    if (result != 0) {
147
      throwToolExit('Switching channels failed with error code $result.', exitCode: result);
148 149 150 151 152
    } else {
      // Remove the version check stamp, since it could contain out-of-date
      // information that pertains to the previous channel.
      await FlutterVersion.resetFlutterVersionFreshnessCheck();
    }
153 154
  }
}