Unverified Commit 10840352 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] switch flutter sdk to dart format (#84273)

parent 07921b56
...@@ -160,7 +160,7 @@ List<FlutterCommand> generateCommands({ ...@@ -160,7 +160,7 @@ List<FlutterCommand> generateCommands({
platform: globals.platform, platform: globals.platform,
), ),
EmulatorsCommand(), EmulatorsCommand(),
FormatCommand(), FormatCommand(verboseHelp: verboseHelp),
GenerateCommand(), GenerateCommand(),
GenerateLocalizationsCommand( GenerateLocalizationsCommand(
fileSystem: globals.fs, fileSystem: globals.fs,
......
...@@ -4,37 +4,21 @@ ...@@ -4,37 +4,21 @@
// @dart = 2.8 // @dart = 2.8
import 'package:args/args.dart';
import 'package:meta/meta.dart';
import '../artifacts.dart'; import '../artifacts.dart';
import '../base/common.dart'; import '../base/common.dart';
import '../globals_null_migrated.dart' as globals; import '../globals_null_migrated.dart' as globals;
import '../runner/flutter_command.dart'; import '../runner/flutter_command.dart';
class FormatCommand extends FlutterCommand { class FormatCommand extends FlutterCommand {
FormatCommand() { FormatCommand({@required this.verboseHelp});
argParser.addFlag('dry-run',
abbr: 'n', @override
help: 'Show which files would be modified but make no changes.', ArgParser argParser = ArgParser.allowAnything();
defaultsTo: false,
negatable: false, final bool verboseHelp;
);
argParser.addFlag('set-exit-if-changed',
help: 'Return exit code 1 if there are any formatting changes.',
defaultsTo: false,
negatable: false,
);
argParser.addFlag('machine',
abbr: 'm',
help: 'Produce machine-readable JSON output.',
defaultsTo: false,
negatable: false,
);
argParser.addOption('line-length',
abbr: 'l',
help: 'Wrap lines longer than this length.',
valueHelp: 'characters',
defaultsTo: '80',
);
}
@override @override
final String name = 'format'; final String name = 'format';
...@@ -50,29 +34,25 @@ class FormatCommand extends FlutterCommand { ...@@ -50,29 +34,25 @@ class FormatCommand extends FlutterCommand {
@override @override
Future<FlutterCommandResult> runCommand() async { Future<FlutterCommandResult> 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'
);
}
final String dartSdk = globals.artifacts.getHostArtifact(HostArtifact.engineDartSdkPath).path;
final String dartBinary = globals.artifacts.getHostArtifact(HostArtifact.engineDartBinary).path; final String dartBinary = globals.artifacts.getHostArtifact(HostArtifact.engineDartBinary).path;
final List<String> command = <String>[ final List<String> command = <String>[
dartBinary, dartBinary,
globals.fs.path.join(dartSdk, 'bin', 'snapshots', 'dartfmt.dart.snapshot'), 'format',
if (boolArg('dry-run')) '-n',
if (boolArg('machine')) '-m',
if (argResults['line-length'] != null) '-l ${argResults['line-length']}',
if (!boolArg('dry-run') && !boolArg('machine')) '-w',
if (boolArg('set-exit-if-changed')) '--set-exit-if-changed',
...argResults.rest,
]; ];
if (argResults.rest.isEmpty) {
globals.printError(
'No files specified to be formatted.'
);
command.add('-h');
} else {
command.addAll(<String>[
for (String arg in argResults.rest)
if (arg == '--dry-run')
'--output=show'
else
arg
]);
}
final int result = await globals.processUtils.stream(command); final int result = await globals.processUtils.stream(command);
if (result != 0) { if (result != 0) {
......
...@@ -1099,17 +1099,13 @@ void main() { ...@@ -1099,17 +1099,13 @@ void main() {
final String original = file.readAsStringSync(); final String original = file.readAsStringSync();
final Process process = await Process.start( final Process process = await Process.start(
globals.fs.path.join( globals.artifacts.getHostArtifact(HostArtifact.engineDartBinary).path,
globals.artifacts.getHostArtifact(HostArtifact.engineDartSdkPath).path, <String>['format', '--output=show', file.path],
'bin',
globals.platform.isWindows ? 'dartfmt.bat' : 'dartfmt',
),
<String>[file.path],
workingDirectory: projectDir.path, workingDirectory: projectDir.path,
); );
final String formatted = await process.stdout.transform(utf8.decoder).join(); final String formatted = await process.stdout.transform(utf8.decoder).join();
expect(original, formatted, reason: file.path); expect(formatted, contains(original), reason: file.path);
} }
} }
...@@ -1205,17 +1201,13 @@ void main() { ...@@ -1205,17 +1201,13 @@ void main() {
final String original = file.readAsStringSync(); final String original = file.readAsStringSync();
final Process process = await Process.start( final Process process = await Process.start(
globals.fs.path.join( globals.artifacts.getHostArtifact(HostArtifact.engineDartBinary).path,
globals.artifacts.getHostArtifact(HostArtifact.engineDartSdkPath).path, <String>['format', '--output=show', file.path],
'bin',
globals.platform.isWindows ? 'dartfmt.bat' : 'dartfmt',
),
<String>[file.path],
workingDirectory: projectDir.path, workingDirectory: projectDir.path,
); );
final String formatted = await process.stdout.transform(utf8.decoder).join(); final String formatted = await process.stdout.transform(utf8.decoder).join();
expect(original, formatted, reason: file.path); expect(formatted, contains(original), reason: file.path);
} }
} }
......
...@@ -34,7 +34,7 @@ void main() { ...@@ -34,7 +34,7 @@ void main() {
final String original = srcFile.readAsStringSync(); final String original = srcFile.readAsStringSync();
srcFile.writeAsStringSync(original.replaceFirst('main()', 'main( )')); srcFile.writeAsStringSync(original.replaceFirst('main()', 'main( )'));
final FormatCommand command = FormatCommand(); final FormatCommand command = FormatCommand(verboseHelp: false);
final CommandRunner<void> runner = createTestCommandRunner(command); final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['format', srcFile.path]); await runner.run(<String>['format', srcFile.path]);
...@@ -51,7 +51,7 @@ void main() { ...@@ -51,7 +51,7 @@ void main() {
'main()', 'main( )'); 'main()', 'main( )');
srcFile.writeAsStringSync(nonFormatted); srcFile.writeAsStringSync(nonFormatted);
final FormatCommand command = FormatCommand(); final FormatCommand command = FormatCommand(verboseHelp: false);
final CommandRunner<void> runner = createTestCommandRunner(command); final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['format', '--dry-run', srcFile.path]); await runner.run(<String>['format', '--dry-run', srcFile.path]);
...@@ -68,7 +68,7 @@ void main() { ...@@ -68,7 +68,7 @@ void main() {
'main()', 'main( )'); 'main()', 'main( )');
srcFile.writeAsStringSync(nonFormatted); srcFile.writeAsStringSync(nonFormatted);
final FormatCommand command = FormatCommand(); final FormatCommand command = FormatCommand(verboseHelp: false);
final CommandRunner<void> runner = createTestCommandRunner(command); final CommandRunner<void> runner = createTestCommandRunner(command);
expect(runner.run(<String>[ expect(runner.run(<String>[
...@@ -92,7 +92,7 @@ void main() { ...@@ -92,7 +92,7 @@ void main() {
'main(anArgument1, anArgument2, anArgument3, anArgument4, anArgument5)')); 'main(anArgument1, anArgument2, anArgument3, anArgument4, anArgument5)'));
final String nonFormattedWithLongLine = srcFile.readAsStringSync(); final String nonFormattedWithLongLine = srcFile.readAsStringSync();
final FormatCommand command = FormatCommand(); final FormatCommand command = FormatCommand(verboseHelp: false);
final CommandRunner<void> runner = createTestCommandRunner(command); final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['format', '--line-length', '$lineLengthLong', srcFile.path]); await runner.run(<String>['format', '--line-length', '$lineLengthLong', srcFile.path]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment