diff --git a/pkg/dartfix/lib/src/driver.dart b/pkg/dartfix/lib/src/driver.dart index fa4c91aa072..ee9347675bf 100644 --- a/pkg/dartfix/lib/src/driver.dart +++ b/pkg/dartfix/lib/src/driver.dart @@ -16,6 +16,7 @@ import 'package:dartfix/listener/bad_message_listener.dart'; import 'package:dartfix/src/context.dart'; import 'package:dartfix/src/options.dart'; import 'package:dartfix/src/util.dart'; +import 'package:path/path.dart' as path; import 'package:pub_semver/pub_semver.dart'; class Driver { @@ -127,6 +128,14 @@ class Driver { if (options.pedanticFixes) { params.includePedanticFixes = true; } + String dir = options.outputDir; + if (dir != null) { + if (!path.isAbsolute(dir)) { + dir = path.absolute(dir); + } + dir = path.canonicalize(dir); + params.outputDir = dir; + } Map json = await server.send(EDIT_REQUEST_DARTFIX, params.toJson()); diff --git a/pkg/dartfix/lib/src/options.dart b/pkg/dartfix/lib/src/options.dart index 545196a9bb1..02bcf3a2a82 100644 --- a/pkg/dartfix/lib/src/options.dart +++ b/pkg/dartfix/lib/src/options.dart @@ -9,19 +9,20 @@ import 'package:cli_util/cli_logging.dart'; import 'package:dartfix/src/context.dart'; import 'package:path/path.dart' as path; +const excludeFixOption = 'excludeFix'; const forceOption = 'force'; const includeFixOption = 'fix'; -const excludeFixOption = 'excludeFix'; +const outputDirOption = 'outputDir'; const overwriteOption = 'overwrite'; const pedanticOption = 'pedantic'; const requiredOption = 'required'; const _binaryName = 'dartfix'; const _colorOption = 'color'; -const _serverSnapshot = 'server'; +const _helpOption = 'help'; // options only supported by server 1.22.2 and greater -const _helpOption = 'help'; +const _serverSnapshot = 'server'; const _verboseOption = 'verbose'; /// Command line options for `dartfix`. @@ -40,6 +41,7 @@ class Options { final bool force; final bool showHelp; + final String outputDir; final bool overwrite; final bool useColor; final bool verbose; @@ -48,6 +50,7 @@ class Options { : force = results[forceOption] as bool, includeFixes = (results[includeFixOption] as List ?? []).cast(), excludeFixes = (results[excludeFixOption] as List ?? []).cast(), + outputDir = results[outputDirOption] as String, overwrite = results[overwriteOption] as bool, pedanticFixes = results[pedanticOption] as bool, requiredFixes = results[requiredOption] as bool, @@ -104,7 +107,12 @@ class Options { negatable: false) ..addFlag(_colorOption, help: 'Use ansi colors when printing messages.', - defaultsTo: Ansi.terminalSupportsAnsi); + defaultsTo: Ansi.terminalSupportsAnsi) + // + // Hidden options. + // + ..addOption(outputDirOption, + help: 'Path to the output directory', hide: true); context ??= Context(); diff --git a/pkg/dartfix/test/src/options_test.dart b/pkg/dartfix/test/src/options_test.dart index c6c87edad73..beb90d0230d 100644 --- a/pkg/dartfix/test/src/options_test.dart +++ b/pkg/dartfix/test/src/options_test.dart @@ -30,6 +30,7 @@ main() { String normalOut, bool pedanticFixes = false, bool requiredFixes = false, + String outputDir, bool overwrite = false, String serverSnapshot, List targetSuffixes, @@ -55,6 +56,7 @@ main() { expect(options.force, force); expect(options.pedanticFixes, pedanticFixes); expect(options.requiredFixes, requiredFixes); + expect(options.outputDir, outputDir); expect(options.overwrite, overwrite); expect(options.serverSnapshot, serverSnapshot); expect(options.showHelp, showHelp); @@ -115,6 +117,10 @@ main() { errorOut: 'Expected directory, but found', exitCode: 21); }); + test('outputDir', () { + parse(['--outputDir=bar', 'foo'], outputDir: 'bar'); + }); + test('overwrite', () { parse(['--overwrite', 'foo'], overwrite: true, targetSuffixes: ['foo']); }); @@ -145,12 +151,6 @@ main() { }); } -void expectOneFileTarget(Options options, String fileName) { - expect(options.targets, hasLength(1)); - final target = options.targets[0]; - expect(target.endsWith(fileName), isTrue); -} - void expectContains(Iterable collection, String suffix) { for (String elem in collection) { if (elem.endsWith(suffix)) { @@ -159,3 +159,9 @@ void expectContains(Iterable collection, String suffix) { } fail('Expected one of $collection\n to end with "$suffix"'); } + +void expectOneFileTarget(Options options, String fileName) { + expect(options.targets, hasLength(1)); + final target = options.targets[0]; + expect(target.endsWith(fileName), isTrue); +}