Skip to content

Commit 7cdf26d

Browse files
[flutter_tools] forward flutter format to dart format and deprecate (flutter#57829)
1 parent 4f0c82b commit 7cdf26d

File tree

3 files changed

+56
-149
lines changed

3 files changed

+56
-149
lines changed

packages/flutter_tools/lib/src/commands/format.dart

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,18 @@
44

55
import 'dart:async';
66

7+
import 'package:args/args.dart';
8+
9+
import '../artifacts.dart';
710
import '../base/common.dart';
811
import '../base/process.dart';
9-
import '../dart/sdk.dart';
12+
import '../globals.dart' as globals;
1013
import '../runner/flutter_command.dart';
1114

1215
class FormatCommand extends FlutterCommand {
13-
FormatCommand() {
14-
argParser.addFlag('dry-run',
15-
abbr: 'n',
16-
help: 'Show which files would be modified but make no changes.',
17-
defaultsTo: false,
18-
negatable: false,
19-
);
20-
argParser.addFlag('set-exit-if-changed',
21-
help: 'Return exit code 1 if there are any formatting changes.',
22-
defaultsTo: false,
23-
negatable: false,
24-
);
25-
argParser.addFlag('machine',
26-
abbr: 'm',
27-
help: 'Produce machine-readable JSON output.',
28-
defaultsTo: false,
29-
negatable: false,
30-
);
31-
argParser.addOption('line-length',
32-
abbr: 'l',
33-
help: 'Wrap lines longer than this length. Defaults to 80 characters.',
34-
defaultsTo: '80',
35-
);
36-
}
16+
@override
17+
ArgParser get argParser => _argParser;
18+
final ArgParser _argParser = ArgParser.allowAnything();
3719

3820
@override
3921
final String name = 'format';
@@ -48,34 +30,25 @@ class FormatCommand extends FlutterCommand {
4830
String get invocation => '${runner.executableName} $name <one or more paths>';
4931

5032
@override
51-
Future<FlutterCommandResult> runCommand() async {
52-
if (argResults.rest.isEmpty) {
53-
throwToolExit(
54-
'No files specified to be formatted.\n'
55-
'\n'
56-
'To format all files in the current directory tree:\n'
57-
'${runner.executableName} $name .\n'
58-
'\n'
59-
'$usage'
60-
);
61-
}
33+
bool get shouldUpdateCache => false;
6234

63-
final String dartfmt = sdkBinaryName('dartfmt');
35+
@override
36+
Future<FlutterCommandResult> runCommand() async {
37+
final String dartBinary = globals.artifacts.getArtifactPath(Artifact.engineDartBinary);
38+
globals.printError(
39+
'"flutter format" is deprecated and will be removed in a'
40+
' future release, use "dart format" instead.'
41+
);
6442
final List<String> command = <String>[
65-
dartfmt,
66-
if (boolArg('dry-run')) '-n',
67-
if (boolArg('machine')) '-m',
68-
if (argResults['line-length'] != null) '-l ${argResults['line-length']}',
69-
if (!boolArg('dry-run') && !boolArg('machine')) '-w',
70-
if (boolArg('set-exit-if-changed')) '--set-exit-if-changed',
43+
dartBinary,
44+
'format',
7145
...argResults.rest,
7246
];
7347

7448
final int result = await processUtils.stream(command);
7549
if (result != 0) {
76-
throwToolExit('Formatting failed: $result', exitCode: result);
50+
throwToolExit('', exitCode: result);
7751
}
78-
7952
return FlutterCommandResult.success();
8053
}
8154
}

packages/flutter_tools/test/commands.shard/permeable/format_test.dart

Lines changed: 0 additions & 104 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:args/command_runner.dart';
6+
import 'package:file/memory.dart';
7+
import 'package:flutter_tools/src/artifacts.dart';
8+
import 'package:flutter_tools/src/base/file_system.dart';
9+
import 'package:flutter_tools/src/commands/format.dart';
10+
import 'package:flutter_tools/src/globals.dart' as globals;
11+
12+
import '../../src/common.dart';
13+
import '../../src/context.dart';
14+
15+
void main() {
16+
testUsingContext('flutter format forward all arguments to dart format and '
17+
'prints deprecation warning', () async {
18+
final CommandRunner<void> runner = CommandRunner<void>('flutter', 'test')
19+
..addCommand(FormatCommand());
20+
await runner.run(<String>['format', 'a', 'b', 'c']);
21+
22+
expect(testLogger.errorText, contains('"flutter format" is deprecated'));
23+
expect((globals.processManager as FakeProcessManager).hasRemainingExpectations, false);
24+
}, overrides: <Type, Generator>{
25+
FileSystem: () => MemoryFileSystem.test(),
26+
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
27+
FakeCommand(
28+
command: <String>[
29+
globals.artifacts.getArtifactPath(Artifact.engineDartBinary),
30+
'format',
31+
'a',
32+
'b',
33+
'c',
34+
],
35+
)
36+
])
37+
});
38+
}

0 commit comments

Comments
 (0)