• Dan Rubel's avatar
    Refactor flutter command exit code - part 3 of 3 (#6838) · 34e466f1
    Dan Rubel authored
    * Remove the workaround that pinned args to v0.13.6
    This reverts most of the changes in commit 6331b6c8
    * throw exception if exit code is not an integer
    * rework command infrastructure to throw ToolExit when non-zero exitCode
    * convert commands to return Future<Null>
    * cleanup remaining commands to use throwToolExit for non-zero exit code
    * remove isUnusual exception message
    * add type annotations for updated args package
    34e466f1
format.dart 1.3 KB
// Copyright 2016 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 'package:path/path.dart' as path;

import '../base/common.dart';
import '../base/process.dart';
import '../cache.dart';
import '../runner/flutter_command.dart';

class FormatCommand extends FlutterCommand {
  @override
  final String name = 'format';

  @override
  List<String> get aliases => const <String>['dartfmt'];

  @override
  final String description = 'Format one or more dart files.';

  @override
  String get invocation => "${runner.executableName} $name <one or more paths>";

  @override
  Future<Null> runCommand() async {
    if (argResults.rest.isEmpty) {
      throwToolExit(
        'No files specified to be formatted.\n'
        '\n'
        'To format all files in the current directory tree:\n'
        '${runner.executableName} $name .\n'
        '\n'
        '$usage'
      );
    }

    String dartfmt = path.join(
        Cache.flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', 'dartfmt');
    List<String> cmd = <String>[dartfmt, '-w']..addAll(argResults.rest);
    int result = await runCommandAndStreamOutput(cmd);
    if (result != 0)
      throwToolExit('Formatting failed: $result', exitCode: result);
  }
}