mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 13:08:10 +00:00
Replace dartfix confirm apply changes with --force
Change-Id: I1d0777e90dd47b13463c3d063f668df859d7995e Reviewed-on: https://dart-review.googlesource.com/c/78880 Commit-Queue: Dan Rubel <danrubel@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
parent
619a06370e
commit
5561e69f4e
|
@ -6,8 +6,6 @@ import 'dart:io' as io;
|
|||
|
||||
/// The context for dartfix.
|
||||
class Context {
|
||||
Stream<List<int>> get stdin => io.stdin;
|
||||
|
||||
StringSink get stdout => io.stdout;
|
||||
StringSink get stderr => io.stderr;
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
// for details. 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 'dart:convert';
|
||||
import 'dart:io' show File;
|
||||
|
||||
import 'package:analysis_server/protocol/protocol_constants.dart';
|
||||
|
@ -24,6 +23,7 @@ class Driver {
|
|||
Completer serverConnected;
|
||||
Completer analysisComplete;
|
||||
bool dryRun;
|
||||
bool force;
|
||||
bool verbose;
|
||||
List<String> targets;
|
||||
|
||||
|
@ -34,6 +34,7 @@ class Driver {
|
|||
final options = Options.parse(args, context);
|
||||
|
||||
dryRun = options.dryRun;
|
||||
force = options.force;
|
||||
verbose = options.verbose;
|
||||
targets = options.targets;
|
||||
|
||||
|
@ -127,18 +128,17 @@ class Driver {
|
|||
for (SourceFileEdit fileEdit in result.fixes) {
|
||||
context.print(fileEdit.file);
|
||||
}
|
||||
if (dryRun || !(await confirmApplyChanges(result))) {
|
||||
return;
|
||||
}
|
||||
for (SourceFileEdit fileEdit in result.fixes) {
|
||||
final file = new File(fileEdit.file);
|
||||
String code = await file.readAsString();
|
||||
for (SourceEdit edit in fileEdit.edits) {
|
||||
code = edit.apply(code);
|
||||
if (shouldApplyChanges(result)) {
|
||||
for (SourceFileEdit fileEdit in result.fixes) {
|
||||
final file = new File(fileEdit.file);
|
||||
String code = await file.readAsString();
|
||||
for (SourceEdit edit in fileEdit.edits) {
|
||||
code = edit.apply(code);
|
||||
}
|
||||
await file.writeAsString(code);
|
||||
}
|
||||
await file.writeAsString(code);
|
||||
context.print('Changes applied.');
|
||||
}
|
||||
context.print('Changes applied.');
|
||||
}
|
||||
|
||||
void showDescriptions(List<String> descriptions, String title) {
|
||||
|
@ -152,33 +152,18 @@ class Driver {
|
|||
}
|
||||
}
|
||||
|
||||
Future<bool> confirmApplyChanges(EditDartfixResult result) async {
|
||||
bool shouldApplyChanges(EditDartfixResult result) {
|
||||
context.print();
|
||||
if (result.hasErrors) {
|
||||
context.print('WARNING: The analyzed source contains errors'
|
||||
' that may affect the accuracy of these changes.');
|
||||
}
|
||||
const prompt = 'Would you like to apply these changes (y/n)? ';
|
||||
context.stdout.write(prompt);
|
||||
final response = new Completer<bool>();
|
||||
final subscription = context.stdin
|
||||
.transform(utf8.decoder)
|
||||
.transform(new LineSplitter())
|
||||
.listen((String line) {
|
||||
line = line.trim().toLowerCase();
|
||||
if (line == 'y' || line == 'yes') {
|
||||
response.complete(true);
|
||||
} else if (line == 'n' || line == 'no') {
|
||||
response.complete(false);
|
||||
} else {
|
||||
context.stdout
|
||||
.writeln(' Unrecognized response. Please type "yes" or "no".');
|
||||
context.stdout.write(prompt);
|
||||
context.print();
|
||||
if (!force) {
|
||||
context.print('Rerun with --$forceOption to apply these changes.');
|
||||
return false;
|
||||
}
|
||||
});
|
||||
bool applyChanges = await response.future;
|
||||
await subscription.cancel();
|
||||
return applyChanges;
|
||||
}
|
||||
return !dryRun;
|
||||
}
|
||||
|
||||
/// Dispatch the notification named [event], and containing parameters
|
||||
|
|
|
@ -13,6 +13,7 @@ class Options {
|
|||
|
||||
List<String> targets;
|
||||
bool dryRun;
|
||||
bool force;
|
||||
String sdkPath;
|
||||
bool verbose;
|
||||
|
||||
|
@ -30,10 +31,14 @@ class Options {
|
|||
' but exit before applying them',
|
||||
defaultsTo: false,
|
||||
negatable: false)
|
||||
..addFlag(forceOption,
|
||||
abbr: 'f',
|
||||
help: 'Apply the recommended changes even if there are errors.',
|
||||
defaultsTo: false,
|
||||
negatable: false)
|
||||
..addFlag(_helpOption,
|
||||
abbr: 'h',
|
||||
help:
|
||||
'Display this help message. Add --verbose to show hidden options.',
|
||||
help: 'Display this help message.',
|
||||
defaultsTo: false,
|
||||
negatable: false)
|
||||
..addFlag(_verboseOption,
|
||||
|
@ -106,6 +111,7 @@ class Options {
|
|||
void _fromArgs(ArgResults results) {
|
||||
targets = results.rest;
|
||||
dryRun = results[_dryRunOption] as bool;
|
||||
force = results[forceOption] as bool;
|
||||
sdkPath = results[_sdkPathOption] as String;
|
||||
verbose = results[_verboseOption] as bool;
|
||||
}
|
||||
|
@ -127,6 +133,7 @@ class Options {
|
|||
|
||||
const _binaryName = 'dartfix';
|
||||
const _dryRunOption = 'dry-run';
|
||||
const forceOption = 'force';
|
||||
const _helpOption = 'help';
|
||||
const _sdkPathOption = 'dart-sdk';
|
||||
const _verboseOption = 'verbose';
|
||||
|
|
|
@ -18,6 +18,7 @@ main() {
|
|||
{bool dryRun = false,
|
||||
String errorOut,
|
||||
int exitCode,
|
||||
bool force = false,
|
||||
String normalOut,
|
||||
List<String> targetSuffixes,
|
||||
bool verbose = false}) {
|
||||
|
@ -39,6 +40,7 @@ main() {
|
|||
expect(actualExitCode, isNull, reason: 'exit code');
|
||||
}
|
||||
expect(options.dryRun, dryRun);
|
||||
expect(options.force, force);
|
||||
expect(options.verbose, verbose);
|
||||
expect(path.isAbsolute(options.sdkPath), isTrue, reason: options.sdkPath);
|
||||
for (String target in options.targets) {
|
||||
|
@ -61,6 +63,10 @@ main() {
|
|||
parse(['--dry-run', 'foo'], dryRun: true, targetSuffixes: ['foo']);
|
||||
});
|
||||
|
||||
test('force', () {
|
||||
parse(['--force', 'foo'], force: true, targetSuffixes: ['foo']);
|
||||
});
|
||||
|
||||
test('invalid option', () {
|
||||
parse(['--foo'],
|
||||
errorOut: 'Could not find an option named "foo"', exitCode: 15);
|
||||
|
@ -75,7 +81,7 @@ main() {
|
|||
parse(['foo'], targetSuffixes: ['foo']);
|
||||
});
|
||||
|
||||
test('two files', () {
|
||||
test('two targets', () {
|
||||
parse([p('one/foo'), p('two/bar')],
|
||||
targetSuffixes: [p('one/foo'), p('two/bar')]);
|
||||
});
|
||||
|
|
|
@ -17,9 +17,6 @@ class TestContext with ResourceProviderMixin implements Context {
|
|||
@override
|
||||
final stderr = new StringBuffer();
|
||||
|
||||
@override
|
||||
Stream<List<int>> get stdin => stdinController.stream;
|
||||
|
||||
@override
|
||||
String get workingDir => convertPath('/usr/some/non/existing/directory');
|
||||
|
||||
|
|
Loading…
Reference in a new issue