@@ -3,7 +3,6 @@ import 'dart:io';
3
3
4
4
import 'package:args/args.dart' ;
5
5
import 'package:glob/glob.dart' ;
6
- import 'package:glob/list_local_fs.dart' ;
7
6
import 'package:io/ansi.dart' ;
8
7
import 'package:io/io.dart' show ExitCode;
9
8
import 'package:logging/logging.dart' ;
@@ -140,6 +139,23 @@ class FormatTool extends DevTool {
140
139
return exitCode;
141
140
}
142
141
142
+ // Similar to listSync() but does not recurse into hidden directories.
143
+ static List <FileSystemEntity > _listSyncWithoutHidden (Directory dir,
144
+ {required bool recursive, required bool followLinks}) {
145
+ var allEntries = < FileSystemEntity > [];
146
+ dir.listSync (recursive: false , followLinks: followLinks).forEach ((element) {
147
+ final basename = p.basename (element.path);
148
+ if (basename.length > 1 && basename.startsWith ("." )) return ;
149
+
150
+ allEntries.add (element);
151
+ if (element is Directory ) {
152
+ allEntries.addAll (_listSyncWithoutHidden (element,
153
+ recursive: recursive, followLinks: followLinks));
154
+ }
155
+ });
156
+ return allEntries;
157
+ }
158
+
143
159
/// Builds and returns the object that contains:
144
160
/// - The file paths
145
161
/// - The paths that were excluded by an exclude glob
@@ -151,25 +167,22 @@ class FormatTool extends DevTool {
151
167
///
152
168
/// By default these globs are assumed to be relative to the current working
153
169
/// directory, but that can be overridden via [root] for testing purposes.
154
- ///
155
- /// If collapseDirectories is true, directories that contain no exclusions will wind up in the [FormatterInputs] ,
156
- /// rather than each file in that tree. You may get unexpected results if this and followLinks are both true.
157
170
static FormatterInputs getInputs ({
158
171
List <Glob >? exclude,
159
172
bool ? expandCwd,
160
173
bool ? followLinks,
161
174
String ? root,
162
- bool ? collapseDirectories,
175
+ @deprecated bool ? collapseDirectories,
163
176
}) {
177
+ if (collapseDirectories != null ) {
178
+ _log.warning (
179
+ 'ignoring deprecated option "collapseDirectories": argv limitations are now solved by parallel invocations' );
180
+ }
181
+
164
182
expandCwd ?? = false ;
165
183
followLinks ?? = false ;
166
- collapseDirectories ?? = false ;
167
184
168
185
final includedFiles = < String > {};
169
- final excludedFiles = < String > {};
170
- final skippedLinks = < String > {};
171
- final hiddenDirectories = < String > {};
172
-
173
186
exclude ?? = < Glob > [];
174
187
175
188
if (exclude.isEmpty && ! expandCwd) {
@@ -178,135 +191,51 @@ class FormatTool extends DevTool {
178
191
179
192
final dir = Directory (root ?? '.' );
180
193
181
- // Use Glob.listSync to get all directories which might include a matching file.
182
- var directoriesWithExcludes = < String > {};
183
-
184
- if (collapseDirectories) {
185
- for (var g in exclude) {
186
- List <FileSystemEntity >? matchingPaths;
187
- try {
188
- matchingPaths = g.listSync (followLinks: followLinks);
189
- } on FileSystemException catch (_) {
190
- _log.finer ("Glob '$g ' did not match any paths.\n " );
191
- }
192
- if (matchingPaths != null ) {
193
- for (var path in matchingPaths) {
194
- if (path is Directory ) {
195
- directoriesWithExcludes.add (path.path);
196
- } else {
197
- directoriesWithExcludes.add (path.parent.path);
198
- }
199
- }
200
- }
201
- }
202
-
203
- // This is all the directories that contain a match within them.
204
- _log.finer ("Directories with excludes:\n " );
205
- for (var dir in directoriesWithExcludes) {
206
- _log.finer (" $dir \n " );
207
- }
208
- _log.finer (
209
- "${directoriesWithExcludes .length } directories contain excludes\n " );
210
- }
211
-
212
- String currentDirectory = p.relative (dir.path, from: dir.path);
213
- bool skipFilesInDirectory = false ;
214
- for (final entry
215
- in dir.listSync (recursive: true , followLinks: followLinks)) {
216
- final relative = p.relative (entry.path, from: dir.path);
217
- _log.finest ('== Processing relative $relative ==\n ' );
218
-
219
- if (p.isWithin (currentDirectory, relative)) {
220
- if (skipFilesInDirectory) {
221
- _log.finest ('skipping child $entry \n ' );
222
- continue ;
223
- }
224
- } else {
225
- // the file/dir in not inside, cancel skipping.
226
- skipFilesInDirectory = false ;
227
- }
194
+ for (final entry in _listSyncWithoutHidden (dir,
195
+ recursive: true , followLinks: followLinks)) {
196
+ final filename = p.relative (entry.path, from: dir.path);
197
+ _log.finest ('== Processing relative $filename ==\n ' );
228
198
229
199
if (entry is Link ) {
230
- _log.finer ('skipping link $relative \n ' );
231
- skippedLinks.add (relative);
200
+ _log.finer ('skipping link $filename \n ' );
232
201
continue ;
233
202
}
234
203
235
204
if (entry is File && ! entry.path.endsWith ('.dart' )) {
236
- _log.finest ('skipping non-dart file $relative \n ' );
237
- continue ;
238
- }
239
-
240
- // If the path is in a subdirectory starting with ".", ignore it.
241
- final parts = p.split (relative);
242
- int ? hiddenIndex;
243
- for (var i = 0 ; i < parts.length; i++ ) {
244
- if (parts[i].startsWith ("." )) {
245
- hiddenIndex = i;
246
- break ;
247
- }
248
- }
249
-
250
- if (hiddenIndex != null ) {
251
- final hiddenDirectory = p.joinAll (parts.take (hiddenIndex + 1 ));
252
- hiddenDirectories.add (hiddenDirectory);
253
- _log.finest ('skipping file $relative in hidden dir $hiddenDirectory \n ' );
254
- if (collapseDirectories) {
255
- currentDirectory = hiddenDirectory;
256
- skipFilesInDirectory = true ;
257
- }
205
+ _log.finest ('skipping non-dart file $filename \n ' );
258
206
continue ;
259
207
}
260
208
261
- if (exclude.any ((glob) => glob.matches (relative))) {
262
- _log.finer ('excluding $relative \n ' );
263
- excludedFiles.add (relative);
264
- } else {
265
- if (collapseDirectories && entry is Directory ) {
266
- _log.finest ('directory: $entry \n ' );
267
- currentDirectory = relative;
268
- // It seems we can rely on the order of files coming from Directory.listSync.
269
- // If the entry does not contain an excluded file,
270
- // we skip adding any of its children files or directories.
271
- if (directoriesWithExcludes.any (
272
- (directoryWithExclude) =>
273
- p.isWithin (entry.path, directoryWithExclude) ||
274
- p.equals (entry.path, directoryWithExclude),
275
- )) {
276
- _log.finer ('$relative has excludes\n ' );
277
- } else {
278
- skipFilesInDirectory = true ;
279
- _log.finer ("$relative does not have excludes, skipping children\n " );
280
- includedFiles.add (relative);
281
- }
282
- }
283
-
284
- if (entry is File && ! skipFilesInDirectory) {
285
- _log.finest ("adding $relative \n " );
286
- includedFiles.add (relative);
287
- }
209
+ if (exclude.any ((glob) => glob.matches (filename))) {
210
+ _log.finer ('excluding $filename \n ' );
211
+ } else if (entry is File ) {
212
+ _log.finest ('adding $filename \n ' );
213
+ includedFiles.add (filename);
288
214
}
289
215
}
290
216
291
- _log.finer ("excluded ${excludedFiles .length } files\n " );
292
-
293
- return FormatterInputs (includedFiles,
294
- excludedFiles: excludedFiles,
295
- skippedLinks: skippedLinks,
296
- hiddenDirectories: hiddenDirectories);
217
+ return FormatterInputs (includedFiles);
297
218
}
298
219
}
299
220
300
221
class FormatterInputs {
301
222
FormatterInputs (this .includedFiles,
302
- {this .excludedFiles, this .hiddenDirectories, this .skippedLinks});
223
+ {@deprecated this .excludedFiles,
224
+ @deprecated this .hiddenDirectories,
225
+ @deprecated this .skippedLinks});
303
226
227
+ final Set <String > includedFiles;
228
+
229
+ // These fields are deprecated and are likely to be empty, due to
230
+ // performance optimizations made in
231
+ // https://github.com/Workiva/dart_dev/pull/424
232
+ @deprecated
304
233
final Set <String >? excludedFiles;
305
234
235
+ @deprecated
306
236
final Set <String >? hiddenDirectories;
307
237
308
- final Set <String > includedFiles;
309
-
238
+ @deprecated
310
239
final Set <String >? skippedLinks;
311
240
}
312
241
@@ -322,6 +251,7 @@ class FormatExecution {
322
251
FormatExecution .exitEarly (this .exitCode)
323
252
: formatProcess = null ,
324
253
directiveOrganization = null ;
254
+
325
255
FormatExecution .process (this .formatProcess, [this .directiveOrganization])
326
256
: exitCode = null ;
327
257
@@ -526,7 +456,6 @@ FormatExecution buildExecution(
526
456
: FormatTool .getInputs (
527
457
exclude: exclude,
528
458
root: path,
529
- collapseDirectories: true ,
530
459
);
531
460
532
461
if (inputs.includedFiles.isEmpty) {
@@ -536,21 +465,6 @@ FormatExecution buildExecution(
536
465
return FormatExecution .exitEarly (ExitCode .config.code);
537
466
}
538
467
539
- if (inputs.excludedFiles? .isNotEmpty ?? false ) {
540
- _log.fine ('Excluding these paths from formatting:\n '
541
- '${inputs .excludedFiles !.join ('\n ' )}' );
542
- }
543
-
544
- if (inputs.skippedLinks? .isNotEmpty ?? false ) {
545
- _log.fine ('Excluding these links from formatting:\n '
546
- '${inputs .skippedLinks !.join ('\n ' )}' );
547
- }
548
-
549
- if (inputs.hiddenDirectories? .isNotEmpty ?? false ) {
550
- _log.fine ('Excluding these hidden directories from formatting:\n '
551
- '${inputs .hiddenDirectories !.join ('\n ' )}' );
552
- }
553
-
554
468
final dartFormatter = buildFormatProcess (formatter);
555
469
Iterable <String > args;
556
470
if (formatter == Formatter .dartFormat) {
0 commit comments