Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit fc1d2a8

Browse files
devoncarewcommit-bot@chromium.org
authored andcommitted
[dartfix] remove the 'dartfix upgrade sdk' command (prefer 'dartdev migrate')
Change-Id: I981e2b1805a81210cfb0ed7af809a07eb3eabb5f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/137221 Commit-Queue: Devon Carew <[email protected]> Reviewed-by: Janice Collins <[email protected]>
1 parent ed1091a commit fc1d2a8

File tree

12 files changed

+149
-234
lines changed

12 files changed

+149
-234
lines changed

pkg/dartfix/lib/src/driver.dart

Lines changed: 74 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ import 'package:dartfix/listener/bad_message_listener.dart';
1616
import 'package:dartfix/src/context.dart';
1717
import 'package:dartfix/src/options.dart';
1818
import 'package:dartfix/src/util.dart';
19+
import 'package:path/path.dart' as path;
1920
import 'package:pub_semver/pub_semver.dart';
2021

22+
import 'migrate/display.dart';
23+
import 'util.dart';
24+
2125
class Driver {
2226
Context context;
2327
_Handler handler;
@@ -140,20 +144,7 @@ class Driver {
140144

141145
/// Return `true` if the changes should be applied.
142146
bool shouldApplyFixes(EditDartfixResult result) {
143-
if (result.edits.isEmpty) {
144-
logger.stdout('');
145-
logger.stdout(result.otherSuggestions.isNotEmpty
146-
? 'None of the recommended changes can be automatically applied.'
147-
: 'There are no recommended changes.');
148-
return false;
149-
}
150-
if (overwrite || force) {
151-
return true;
152-
}
153-
logger.stdout('');
154-
logger.stdout('Would you like to apply these changes? (yes/no)');
155-
var response = stdin.readLineSync();
156-
return response.toLowerCase() == 'yes';
147+
return overwrite || force;
157148
}
158149

159150
void showDescriptions(String title, List<DartFixSuggestion> suggestions) {
@@ -241,8 +232,6 @@ These fixes can be enabled using --$includeFixOption:''');
241232
Progress progress;
242233
if (options.showHelp) {
243234
progress = logger.progress('${ansi.emphasized('Listing fixes')}');
244-
} else if (options.isUpgrade) {
245-
progress = logger.progress('${ansi.emphasized('Calculating changes')}');
246235
} else {
247236
progress = logger.progress('${ansi.emphasized('Calculating fixes')}');
248237
}
@@ -280,57 +269,28 @@ These fixes can be enabled using --$includeFixOption:''');
280269
editCount += fileEdit.edits.length;
281270
}
282271
logger.stdout('Found $editCount changes in ${fileEdits.length} files.');
272+
273+
previewFixes(logger, result);
274+
283275
//
284-
// Print instructions for opening the preview tool.
276+
// Stop the server.
285277
//
286-
var urls = result.urls;
287-
if (urls != null) {
288-
// Server has already started the preview server.
289-
if (result.hasErrors) {
290-
// TODO(brianwilkerson) When we have previews for fixes, tailor the
291-
// message to be appropriate for fixes.
292-
logger.stdout('');
293-
String warning = ansi.emphasized('WARNING');
294-
logger.stdout('$warning: The unmodified code contains errors that '
295-
'might affect the accuracy of the upgrade.');
296-
options.overwrite = false;
297-
}
298-
if (options.isUpgrade && options.upgradeOptions.preview) {
299-
logger.stdout('');
300-
String open;
301-
if (urls.length == 1) {
302-
open = ansi.emphasized('Please open this URL');
303-
} else {
304-
open = ansi.emphasized('Please open these URLs');
305-
}
306-
logger.stdout('$open in your browser to see the changes:');
307-
for (var url in urls) {
308-
logger.stdout(' $url');
309-
}
310-
logger.stdout('');
311-
String enter = ansi.emphasized('ENTER');
312-
logger.stdout('When done previewing, press $enter.');
313-
stdin.readLineSync();
314-
}
315-
//
316-
// Stop the server.
317-
//
318-
serverStopped = server.stop();
319-
if (shouldApplyFixes(result)) {
320-
applyFixes();
321-
logger.stdout('Changes have been applied.');
322-
} else {
323-
logger.stdout('No changes applied.');
324-
}
325-
await serverStopped;
278+
serverStopped = server.stop();
279+
280+
logger.stdout('');
281+
282+
// Check if we should apply fixes.
283+
if (result.edits.isEmpty) {
284+
logger.stdout(result.otherSuggestions.isNotEmpty
285+
? 'None of the recommended changes can be automatically applied.'
286+
: 'There are no recommended changes.');
287+
} else if (shouldApplyFixes(result)) {
288+
applyFixes();
289+
logger.stdout('Changes have been applied.');
326290
} else {
327-
//
328-
// Stop the server.
329-
//
330-
serverStopped = server.stop();
331-
await printAndApplyFixes();
332-
await serverStopped;
291+
logger.stdout('Re-run with --overwrite to apply the above changes.');
333292
}
293+
await serverStopped;
334294
} finally {
335295
// If we didn't already try to stop the server, then stop it now.
336296
if (serverStopped == null) {
@@ -398,6 +358,53 @@ analysis server
398358
The --$option option is not supported by analysis server version $version.
399359
Please upgrade to a newer version of the Dart SDK to use this option.''');
400360
}
361+
362+
void previewFixes(
363+
Logger logger,
364+
EditDartfixResult results,
365+
) {
366+
final Ansi ansi = logger.ansi;
367+
368+
Map<String, List<DartFixSuggestion>> fileSuggestions = {};
369+
for (DartFixSuggestion suggestion in results.suggestions) {
370+
String file = suggestion.location.file;
371+
fileSuggestions.putIfAbsent(file, () => <DartFixSuggestion>[]);
372+
fileSuggestions[file].add(suggestion);
373+
}
374+
375+
// present a diff-like view
376+
for (SourceFileEdit sourceFileEdit in results.edits) {
377+
String file = sourceFileEdit.file;
378+
String relPath = path.relative(file);
379+
int count = sourceFileEdit.edits.length;
380+
381+
logger.stdout('');
382+
logger.stdout('${ansi.emphasized(relPath)} '
383+
'($count ${pluralize('change', count)}):');
384+
385+
String source;
386+
try {
387+
source = File(file).readAsStringSync();
388+
} catch (_) {}
389+
390+
if (source == null) {
391+
logger.stdout(' (unable to retrieve source for file)');
392+
} else {
393+
SourcePrinter sourcePrinter = SourcePrinter(source);
394+
395+
List<SourceEdit> edits = sortEdits(sourceFileEdit);
396+
397+
// Apply edits.
398+
sourcePrinter.applyEdits(edits);
399+
400+
// Render the changed lines.
401+
sourcePrinter.processChangedLines((lineNumber, lineText) {
402+
String prefix = ' line ${lineNumber.toString().padRight(3)} •';
403+
logger.stdout('$prefix ${lineText.trim()}');
404+
});
405+
}
406+
}
407+
}
401408
}
402409

403410
class _Handler
@@ -454,15 +461,15 @@ class _Handler
454461
if (version > expectedVersion) {
455462
logger.stdout('''
456463
This version of dartfix is incompatible with the current Dart SDK.
457-
Try installing a newer version of dartfix by running
464+
Try installing a newer version of dartfix by running:
458465
459466
pub global activate dartfix
460467
''');
461468
} else {
462469
logger.stdout('''
463-
This version of dartfix is too new to be used with the current Dart SDK.
464-
Try upgrading the Dart SDK to a newer version
465-
or installing an older version of dartfix using
470+
This version of dartfix is too new to be used with the current Dart SDK. Try
471+
upgrading the Dart SDK to a newer version or installing an older version of
472+
dartfix using:
466473
467474
pub global activate dartfix <version>
468475
''');

pkg/dartfix/lib/src/migrate/apply.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44

55
import 'package:analysis_server_client/protocol.dart';
66

7+
import '../util.dart';
8+
79
/// Perform the indicated source edits to the given source, returning the
810
/// resulting transformed text.
911
String applyEdits(SourceFileEdit sourceFileEdit, String source) {
10-
// Sort edits in reverse offset order.
11-
List<SourceEdit> edits = sourceFileEdit.edits.toList();
12-
edits.sort((a, b) {
13-
return b.offset - a.offset;
14-
});
15-
12+
List<SourceEdit> edits = sortEdits(sourceFileEdit);
1613
return SourceEdit.applySequence(source, edits);
1714
}

pkg/dartfix/lib/src/migrate/display.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,21 @@ class SourcePrinter {
4141

4242
SourcePrinter(this.source);
4343

44-
void deleteRange(int offset, int length) {
45-
// \u001b[31m - red
44+
void applyEdits(List<SourceEdit> edits) {
45+
for (SourceEdit edit in edits) {
46+
if (edit.replacement.isNotEmpty) {
47+
// an addition
48+
insertText(edit.offset + edit.length, edit.replacement);
49+
}
4650

51+
if (edit.length != 0) {
52+
// a removal
53+
deleteRange(edit.offset, edit.length);
54+
}
55+
}
56+
}
57+
58+
void deleteRange(int offset, int length) {
4759
source = source.substring(0, offset) +
4860
red +
4961
reversed +

pkg/dartfix/lib/src/migrate/migrate.dart

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:args/command_runner.dart';
1313
import 'package:cli_util/cli_logging.dart';
1414
import 'package:path/path.dart' as path;
1515

16+
import '../util.dart';
1617
import 'apply.dart';
1718
import 'display.dart';
1819
import 'options.dart';
@@ -106,7 +107,7 @@ class MigrateCommand extends Command {
106107
int issueCount =
107108
fileErrors.values.map((list) => list.length).reduce((a, b) => a + b);
108109
logger.stdout(
109-
'$issueCount analysis ${_pluralize('issue', issueCount)} found:');
110+
'$issueCount analysis ${pluralize('issue', issueCount)} found:');
110111
_displayIssues(
111112
logger,
112113
options.directory,
@@ -163,8 +164,8 @@ class MigrateCommand extends Command {
163164
migrationResults.edits.map((edit) => edit.file).toList();
164165

165166
logger.stdout('Found ${allEdits.length} '
166-
'suggested ${_pluralize('change', allEdits.length)} in '
167-
'${files.length} ${_pluralize('file', files.length)}.');
167+
'suggested ${pluralize('change', allEdits.length)} in '
168+
'${files.length} ${pluralize('file', files.length)}.');
168169

169170
logger.stdout('');
170171

@@ -175,7 +176,7 @@ class MigrateCommand extends Command {
175176

176177
logger.stdout('');
177178
logger.stdout(
178-
'Applied ${allEdits.length} ${_pluralize('edit', allEdits.length)}.');
179+
'Applied ${allEdits.length} ${pluralize('edit', allEdits.length)}.');
179180

180181
return 0;
181182
}
@@ -284,7 +285,7 @@ class MigrateCommand extends Command {
284285

285286
logger.stdout('');
286287
logger.stdout('${ansi.emphasized(relPath)} '
287-
'($count ${_pluralize('change', count)}):');
288+
'($count ${pluralize('change', count)}):');
288289

289290
String source;
290291
try {
@@ -296,24 +297,10 @@ class MigrateCommand extends Command {
296297
} else {
297298
SourcePrinter sourcePrinter = SourcePrinter(source);
298299

299-
// Sort edits in reverse offset order.
300-
List<SourceEdit> edits = sourceFileEdit.edits;
301-
edits.sort((a, b) {
302-
return b.offset - a.offset;
303-
});
300+
List<SourceEdit> edits = sortEdits(sourceFileEdit);
304301

305-
for (SourceEdit edit in sourceFileEdit.edits) {
306-
if (edit.replacement.isNotEmpty) {
307-
// an addition
308-
sourcePrinter.insertText(
309-
edit.offset + edit.length, edit.replacement);
310-
}
311-
312-
if (edit.length != 0) {
313-
// a removal
314-
sourcePrinter.deleteRange(edit.offset, edit.length);
315-
}
316-
}
302+
// Apply edits.
303+
sourcePrinter.applyEdits(edits);
317304

318305
// Render the changed lines.
319306
sourcePrinter.processChangedLines((lineNumber, lineText) {
@@ -333,7 +320,7 @@ class MigrateCommand extends Command {
333320
for (SourceFileEdit sourceFileEdit in migrationResults.edits) {
334321
String relPath = path.relative(sourceFileEdit.file, from: directory);
335322
int count = sourceFileEdit.edits.length;
336-
logger.stdout(' $relPath ($count ${_pluralize('change', count)})');
323+
logger.stdout(' $relPath ($count ${pluralize('change', count)})');
337324

338325
String source;
339326
try {
@@ -441,5 +428,3 @@ String get _dartSdkVersion {
441428

442429
return version;
443430
}
444-
445-
String _pluralize(String word, int count) => count == 1 ? word : '${word}s';

0 commit comments

Comments
 (0)