@@ -74,30 +74,28 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
74
74
return 0 ;
75
75
}
76
76
77
- var arguments = args.rest;
78
- var argumentCount = arguments.length;
79
- if (argumentCount > 1 ) {
80
- usageException ('Only one file or directory is expected.' );
77
+ var target = _getTarget ( args.rest) ;
78
+ if ( ! target. existsSync ()) {
79
+ var entity = target.isDirectory ? 'Directory' : 'File' ;
80
+ usageException ("$ entity doesn't exist: ${ target . path }" );
81
81
}
82
82
83
- var dir =
84
- argumentCount == 0 ? io.Directory .current : io.Directory (arguments[0 ]);
85
- if (! dir.existsSync ()) {
86
- usageException ("Directory doesn't exist: ${dir .path }" );
83
+ if (inTestMode && ! target.isDirectory) {
84
+ usageException ('Golden comparison requires a directory argument.' );
87
85
}
88
- dir = io. Directory (path. canonicalize (path. normalize (dir.absolute.path)));
89
- var dirPath = dir .path;
86
+
87
+ var fixPath = target .path;
90
88
91
89
var modeText = dryRun ? ' (dry run)' : '' ;
92
90
93
- final projectName = path.basename (dirPath );
91
+ final targetName = path.basename (fixPath );
94
92
Progress ? computeFixesProgress = log.progress (
95
- 'Computing fixes in ${log .ansi .emphasized (projectName )}$modeText ' );
93
+ 'Computing fixes in ${log .ansi .emphasized (targetName )}$modeText ' );
96
94
97
95
var server = AnalysisServer (
98
96
null ,
99
97
io.Directory (sdk.sdkPath),
100
- [dir ],
98
+ [target ],
101
99
commandName: 'fix' ,
102
100
argResults: argResults,
103
101
);
@@ -123,7 +121,7 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
123
121
List <SourceFileEdit > edits;
124
122
var pass = 0 ;
125
123
do {
126
- var fixes = await server.requestBulkFixes (dirPath , inTestMode);
124
+ var fixes = await server.requestBulkFixes (fixPath , inTestMode);
127
125
_mergeDetails (detailsMap, fixes.details);
128
126
edits = fixes.edits;
129
127
_applyEdits (server, edits);
@@ -142,6 +140,7 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
142
140
computeFixesProgress = null ;
143
141
}
144
142
143
+ var dir = target.isDirectory ? target as io.Directory : target.parent;
145
144
if (inTestMode) {
146
145
var result = _compareFixesInDirectory (dir);
147
146
log.stdout ('Passed: ${result .passCount }, Failed: ${result .failCount }' );
@@ -267,6 +266,18 @@ To use the tool, run either ['dart fix --dry-run'] for a preview of the proposed
267
266
String _compressWhitespace (String code) =>
268
267
code.replaceAll (RegExp (r'\s+' ), ' ' );
269
268
269
+ io.FileSystemEntity _getTarget (List <String > arguments) {
270
+ var argumentCount = arguments.length;
271
+ if (argumentCount == 0 ) return io.Directory .current.absolute;
272
+ if (argumentCount > 1 ) {
273
+ usageException ('Only one file or directory is expected.' );
274
+ }
275
+ var normalizedPath = path.canonicalize (path.normalize (arguments[0 ]));
276
+ return io.FileSystemEntity .isDirectorySync (normalizedPath)
277
+ ? io.Directory (normalizedPath)
278
+ : io.File (normalizedPath);
279
+ }
280
+
270
281
/// Merge the fixes from the current round's [details] into the [detailsMap] .
271
282
void _mergeDetails (Map <String , BulkFix > detailsMap, List <BulkFix > details) {
272
283
for (var detail in details) {
@@ -361,3 +372,7 @@ class _TestResult {
361
372
/// Initialize a newly created result object.
362
373
_TestResult ();
363
374
}
375
+
376
+ extension on io.FileSystemEntity {
377
+ bool get isDirectory => this is io.Directory ;
378
+ }
0 commit comments