diff --git a/packages/flutter_tools/lib/executable.dart b/packages/flutter_tools/lib/executable.dart index cbe95ab56ba..b4b57444a64 100644 --- a/packages/flutter_tools/lib/executable.dart +++ b/packages/flutter_tools/lib/executable.dart @@ -199,7 +199,7 @@ List generateCommands({ signals: globals.signals, ), EmulatorsCommand(), - FormatCommand(verboseHelp: verboseHelp), + FormatCommand(), GenerateCommand(), GenerateLocalizationsCommand( fileSystem: globals.fs, diff --git a/packages/flutter_tools/lib/src/commands/format.dart b/packages/flutter_tools/lib/src/commands/format.dart index 816339a3759..c4ea165d145 100644 --- a/packages/flutter_tools/lib/src/commands/format.dart +++ b/packages/flutter_tools/lib/src/commands/format.dart @@ -4,19 +4,15 @@ import 'package:args/args.dart'; -import '../artifacts.dart'; import '../base/common.dart'; -import '../globals.dart' as globals; import '../runner/flutter_command.dart'; class FormatCommand extends FlutterCommand { - FormatCommand({required this.verboseHelp}); + FormatCommand(); @override ArgParser argParser = ArgParser.allowAnything(); - final bool verboseHelp; - @override final String name = 'format'; @@ -24,53 +20,20 @@ class FormatCommand extends FlutterCommand { List get aliases => const ['dartfmt']; @override - final String description = 'Format one or more Dart files.'; + String get description => deprecationWarning; @override - String get category => FlutterCommandCategory.project; - - @override - String get invocation => '${runner?.executableName} $name '; - - @override - final bool deprecated = true; + final bool hidden = true; @override String get deprecationWarning { - return '${globals.logger.terminal.warningMark} The "format" command is ' - 'deprecated and will be removed in a future version of Flutter. ' - 'Please use the "dart format" sub-command instead, which takes all ' - 'of the same command-line arguments as "flutter format".\n'; + return 'The "format" command is deprecated. Please use the "dart format" ' + 'sub-command instead, which has the same command-line usage as ' + '"flutter format".\n'; } @override Future runCommand() async { - final String dartBinary = globals.artifacts!.getArtifactPath(Artifact.engineDartBinary); - final List command = [ - dartBinary, - 'format', - ]; - final List rest = argResults?.rest ?? []; - if (rest.isEmpty) { - globals.printError( - 'No files specified to be formatted.' - ); - command.add('-h'); - } else { - command.addAll([ - for (String arg in rest) - if (arg == '--dry-run' || arg == '-n') - '--output=none' - else - arg, - ]); - } - - final int result = await globals.processUtils.stream(command); - if (result != 0) { - throwToolExit('Formatting failed: $result', exitCode: result); - } - - return FlutterCommandResult.success(); + throwToolExit(deprecationWarning); } } diff --git a/packages/flutter_tools/test/commands.shard/permeable/format_test.dart b/packages/flutter_tools/test/commands.shard/permeable/format_test.dart deleted file mode 100644 index f289440eb9a..00000000000 --- a/packages/flutter_tools/test/commands.shard/permeable/format_test.dart +++ /dev/null @@ -1,147 +0,0 @@ -// Copyright 2014 The Flutter 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 'package:args/command_runner.dart'; -import 'package:flutter_tools/src/base/file_system.dart'; -import 'package:flutter_tools/src/base/logger.dart'; -import 'package:flutter_tools/src/cache.dart'; -import 'package:flutter_tools/src/commands/format.dart'; -import 'package:flutter_tools/src/globals.dart' as globals; - -import '../../src/common.dart'; -import '../../src/context.dart'; -import '../../src/test_flutter_command_runner.dart'; - -void main() { - group('format', () { - late Directory tempDir; - late BufferLogger logger; - - setUp(() { - Cache.disableLocking(); - tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_format_test.'); - logger = BufferLogger.test(); - }); - - tearDown(() { - tryToDelete(tempDir); - }); - - testUsingContext('shows deprecation warning', () async { - final String projectPath = await createProject(tempDir); - - final File srcFile = globals.fs.file(globals.fs.path.join(projectPath, 'lib', 'main.dart')); - final String original = srcFile.readAsStringSync(); - srcFile.writeAsStringSync(original); - - final FormatCommand command = FormatCommand(verboseHelp: false); - final CommandRunner runner = createTestCommandRunner(command); - await runner.run(['format', srcFile.path]); - expect( - logger.warningText, - contains('The "format" command is deprecated and will be removed in a future version of Flutter'), - ); - }, overrides: { - Logger: () => logger, - }); - - testUsingContext('a file', () async { - final String projectPath = await createProject(tempDir); - - final File srcFile = globals.fs.file(globals.fs.path.join(projectPath, 'lib', 'main.dart')); - final String original = srcFile.readAsStringSync(); - srcFile.writeAsStringSync(original.replaceFirst('main()', 'main( )')); - - final FormatCommand command = FormatCommand(verboseHelp: false); - final CommandRunner runner = createTestCommandRunner(command); - await runner.run(['format', srcFile.path]); - - final String formatted = srcFile.readAsStringSync(); - expect(formatted, original); - }, overrides: { - Logger: () => logger, - }); - - testUsingContext('dry-run', () async { - final String projectPath = await createProject(tempDir); - - final File srcFile = globals.fs.file( - globals.fs.path.join(projectPath, 'lib', 'main.dart')); - final String nonFormatted = srcFile.readAsStringSync().replaceFirst( - 'main()', 'main( )'); - srcFile.writeAsStringSync(nonFormatted); - - final FormatCommand command = FormatCommand(verboseHelp: false); - final CommandRunner runner = createTestCommandRunner(command); - await runner.run(['format', '--dry-run', srcFile.path]); - - final String shouldNotFormatted = srcFile.readAsStringSync(); - expect(shouldNotFormatted, nonFormatted); - }, overrides: { - Logger: () => logger, - }); - - testUsingContext('dry-run with -n', () async { - final String projectPath = await createProject(tempDir); - - final File srcFile = globals.fs.file( - globals.fs.path.join(projectPath, 'lib', 'main.dart')); - final String nonFormatted = srcFile.readAsStringSync().replaceFirst( - 'main()', 'main( )'); - srcFile.writeAsStringSync(nonFormatted); - - final FormatCommand command = FormatCommand(verboseHelp: false); - final CommandRunner runner = createTestCommandRunner(command); - await runner.run(['format', '-n', srcFile.path]); - - final String shouldNotFormatted = srcFile.readAsStringSync(); - expect(shouldNotFormatted, nonFormatted); - }); - - testUsingContext('dry-run with set-exit-if-changed', () async { - final String projectPath = await createProject(tempDir); - - final File srcFile = globals.fs.file( - globals.fs.path.join(projectPath, 'lib', 'main.dart')); - final String nonFormatted = srcFile.readAsStringSync().replaceFirst( - 'main()', 'main( )'); - srcFile.writeAsStringSync(nonFormatted); - - final FormatCommand command = FormatCommand(verboseHelp: false); - final CommandRunner runner = createTestCommandRunner(command); - - expect(runner.run([ - 'format', '--dry-run', '--set-exit-if-changed', srcFile.path, - ]), throwsException); - - final String shouldNotFormatted = srcFile.readAsStringSync(); - expect(shouldNotFormatted, nonFormatted); - }); - - testUsingContext('line-length', () async { - const int lineLengthShort = 50; - const int lineLengthLong = 120; - final String projectPath = await createProject(tempDir); - - final File srcFile = globals.fs.file( - globals.fs.path.join(projectPath, 'lib', 'main.dart')); - final String nonFormatted = srcFile.readAsStringSync(); - srcFile.writeAsStringSync( - nonFormatted.replaceFirst('main()', - 'main(anArgument1, anArgument2, anArgument3, anArgument4, anArgument5)')); - - final String nonFormattedWithLongLine = srcFile.readAsStringSync(); - final FormatCommand command = FormatCommand(verboseHelp: false); - final CommandRunner runner = createTestCommandRunner(command); - - await runner.run(['format', '--line-length', '$lineLengthLong', srcFile.path]); - final String notFormatted = srcFile.readAsStringSync(); - expect(nonFormattedWithLongLine, notFormatted); - - await runner.run(['format', '--line-length', '$lineLengthShort', srcFile.path]); - final String shouldFormatted = srcFile.readAsStringSync(); - expect(nonFormattedWithLongLine, isNot(shouldFormatted)); - }); - }); -} diff --git a/packages/flutter_tools/test/general.shard/args_test.dart b/packages/flutter_tools/test/general.shard/args_test.dart index 7c1fb1fd2aa..0dc1b58b877 100644 --- a/packages/flutter_tools/test/general.shard/args_test.dart +++ b/packages/flutter_tools/test/general.shard/args_test.dart @@ -23,7 +23,7 @@ void main() { ).forEach(runner.addCommand); verifyCommandRunner(runner); for (final Command command in runner.commands.values) { - if(command.name == 'analyze') { + if (command.name == 'analyze') { final AnalyzeCommand analyze = command as AnalyzeCommand; expect(analyze.allProjectValidators().length, 2); }