flutter/packages/flutter_tools/lib/src/commands/channel.dart
Dan Rubel 1821238279 Refactor flutter command execution (#5892)
* refactor _run to runCmd

* replace requiresProjectRoot getter with call to commandValidator

* replace requiresDevice getter with call to findTargetDevice

* trace command requires a debug connection, not a device

* inline androidOnly getter

* rename command methods to verifyTheRunCmd and runCmd

* move common verification into BuildSubCommand

* rename deviceForCommand to device

* rename methods to verifyThenRunCommand and runCommand
2016-09-16 09:13:35 -04:00

64 lines
1.8 KiB
Dart

// 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';
import '../base/process.dart';
import '../cache.dart';
import '../globals.dart';
import '../runner/flutter_command.dart';
class ChannelCommand extends FlutterCommand {
@override
final String name = 'channel';
@override
final String description = 'List or switch flutter channels.';
@override
String get invocation => '${runner.executableName} $name [<channel-name>]';
@override
Future<int> runCommand() async {
switch (argResults.rest.length) {
case 0:
return await _listChannels();
case 1:
return await _switchChannel(argResults.rest[0]);
default:
printStatus('Too many arguments.');
printStatus(usage);
return 2;
}
}
Future<int> _listChannels() async {
String currentBranch = runSync(
<String>['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
workingDirectory: Cache.flutterRoot);
printStatus('Flutter channels:');
return runCommandAndStreamOutput(
<String>['git', 'branch', '-r'],
workingDirectory: Cache.flutterRoot,
mapFunction: (String line) {
List<String> split = line.split('/');
if (split.length < 2) return null;
String branchName = split[1];
if (branchName.startsWith('HEAD')) return null;
if (branchName == currentBranch) return '* $branchName';
return ' $branchName';
},
);
}
Future<int> _switchChannel(String branchName) {
printStatus('Switching to flutter channel named $branchName');
return runCommandAndStreamOutput(
<String>['git', 'checkout', branchName],
workingDirectory: Cache.flutterRoot,
);
}
}