1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 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);
}
}