OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:io'; | 5 import 'dart:io'; |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 | 7 |
8 import 'package:path/path.dart' as p; | 8 import 'package:path/path.dart' as p; |
9 | 9 |
10 import 'ast.dart'; | 10 import 'ast.dart'; |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 } | 307 } |
308 } | 308 } |
309 | 309 |
310 /// Lists all entities within [dir] matching this node or its children. | 310 /// Lists all entities within [dir] matching this node or its children. |
311 /// | 311 /// |
312 /// This may return duplicate entities. These will be filtered out in | 312 /// This may return duplicate entities. These will be filtered out in |
313 /// [ListTree.list]. | 313 /// [ListTree.list]. |
314 Stream<FileSystemEntity> list(String dir, {bool followLinks: true}) { | 314 Stream<FileSystemEntity> list(String dir, {bool followLinks: true}) { |
315 if (isRecursive) { | 315 if (isRecursive) { |
316 return new Directory(dir).list(recursive: true, followLinks: followLinks) | 316 return new Directory(dir).list(recursive: true, followLinks: followLinks) |
317 .where((entity) => _matches(entity.path.substring(dir.length + 1))); | 317 .where((entity) => _matches(p.relative(entity.path, from: dir))); |
Bob Nystrom
2016/03/08 21:40:31
What's the performance impact of this?
It might b
nweiz
2016/03/08 22:01:15
I doubt the performance impact is that high—it's h
| |
318 } | 318 } |
319 | 319 |
320 var resultPool = new StreamPool(); | 320 var resultPool = new StreamPool(); |
321 | 321 |
322 // Don't spawn extra [Directory.list] calls when we already know exactly | 322 // Don't spawn extra [Directory.list] calls when we already know exactly |
323 // which subdirectories we're interested in. | 323 // which subdirectories we're interested in. |
324 if (_isIntermediate) { | 324 if (_isIntermediate) { |
325 children.forEach((sequence, child) { | 325 children.forEach((sequence, child) { |
326 resultPool.add(child.list(p.join(dir, sequence.nodes.single.text), | 326 resultPool.add(child.list(p.join(dir, sequence.nodes.single.text), |
327 followLinks: followLinks)); | 327 followLinks: followLinks)); |
328 }); | 328 }); |
329 resultPool.closeWhenEmpty(); | 329 resultPool.closeWhenEmpty(); |
330 return resultPool.stream; | 330 return resultPool.stream; |
331 } | 331 } |
332 | 332 |
333 var resultController = new StreamController(sync: true); | 333 var resultController = new StreamController(sync: true); |
334 resultPool.add(resultController.stream); | 334 resultPool.add(resultController.stream); |
335 new Directory(dir).list(followLinks: followLinks).listen((entity) { | 335 new Directory(dir).list(followLinks: followLinks).listen((entity) { |
336 var basename = entity.path.substring(dir.length + 1); | 336 var basename = p.relative(entity.path, from: dir); |
337 if (_matches(basename)) resultController.add(entity); | 337 if (_matches(basename)) resultController.add(entity); |
338 | 338 |
339 children.forEach((sequence, child) { | 339 children.forEach((sequence, child) { |
340 if (entity is! Directory) return; | 340 if (entity is! Directory) return; |
341 if (!sequence.matches(basename)) return; | 341 if (!sequence.matches(basename)) return; |
342 var stream = child.list(p.join(dir, basename), followLinks: followLinks) | 342 var stream = child.list(p.join(dir, basename), followLinks: followLinks) |
343 .handleError((_) {}, test: (error) { | 343 .handleError((_) {}, test: (error) { |
344 // Ignore errors from directories not existing. We do this here so | 344 // Ignore errors from directories not existing. We do this here so |
345 // that we only ignore warnings below wild cards. For example, the | 345 // that we only ignore warnings below wild cards. For example, the |
346 // glob "foo/bar/*/baz" should fail if "foo/bar" doesn't exist but | 346 // glob "foo/bar/*/baz" should fail if "foo/bar" doesn't exist but |
(...skipping 14 matching lines...) Expand all Loading... | |
361 | 361 |
362 /// Synchronously lists all entities within [dir] matching this node or its | 362 /// Synchronously lists all entities within [dir] matching this node or its |
363 /// children. | 363 /// children. |
364 /// | 364 /// |
365 /// This may return duplicate entities. These will be filtered out in | 365 /// This may return duplicate entities. These will be filtered out in |
366 /// [ListTree.listSync]. | 366 /// [ListTree.listSync]. |
367 Iterable<FileSystemEntity> listSync(String dir, {bool followLinks: true}) { | 367 Iterable<FileSystemEntity> listSync(String dir, {bool followLinks: true}) { |
368 if (isRecursive) { | 368 if (isRecursive) { |
369 return new Directory(dir) | 369 return new Directory(dir) |
370 .listSync(recursive: true, followLinks: followLinks) | 370 .listSync(recursive: true, followLinks: followLinks) |
371 .where((entity) => _matches(entity.path.substring(dir.length + 1))); | 371 .where((entity) => _matches(p.relative(entity.path, from: dir))); |
372 } | 372 } |
373 | 373 |
374 // Don't spawn extra [Directory.listSync] calls when we already know exactly | 374 // Don't spawn extra [Directory.listSync] calls when we already know exactly |
375 // which subdirectories we're interested in. | 375 // which subdirectories we're interested in. |
376 if (_isIntermediate) { | 376 if (_isIntermediate) { |
377 return children.keys.expand((sequence) { | 377 return children.keys.expand((sequence) { |
378 return children[sequence].listSync( | 378 return children[sequence].listSync( |
379 p.join(dir, sequence.nodes.single.text), followLinks: followLinks); | 379 p.join(dir, sequence.nodes.single.text), followLinks: followLinks); |
380 }); | 380 }); |
381 } | 381 } |
382 | 382 |
383 return new Directory(dir).listSync(followLinks: followLinks) | 383 return new Directory(dir).listSync(followLinks: followLinks) |
384 .expand((entity) { | 384 .expand((entity) { |
385 var entities = []; | 385 var entities = []; |
386 var basename = entity.path.substring(dir.length + 1); | 386 var basename = p.relative(entity.path, from: dir); |
387 if (_matches(basename)) entities.add(entity); | 387 if (_matches(basename)) entities.add(entity); |
388 if (entity is! Directory) return entities; | 388 if (entity is! Directory) return entities; |
389 | 389 |
390 entities.addAll(children.keys | 390 entities.addAll(children.keys |
391 .where((sequence) => sequence.matches(basename)) | 391 .where((sequence) => sequence.matches(basename)) |
392 .expand((sequence) { | 392 .expand((sequence) { |
393 try { | 393 try { |
394 return children[sequence].listSync( | 394 return children[sequence].listSync( |
395 p.join(dir, basename), followLinks: followLinks).toList(); | 395 p.join(dir, basename), followLinks: followLinks).toList(); |
396 } on FileSystemException catch (error) { | 396 } on FileSystemException catch (error) { |
(...skipping 28 matching lines...) Expand all Loading... | |
425 SequenceNode _join(Iterable<AstNode> components) { | 425 SequenceNode _join(Iterable<AstNode> components) { |
426 var componentsList = components.toList(); | 426 var componentsList = components.toList(); |
427 var first = componentsList.removeAt(0); | 427 var first = componentsList.removeAt(0); |
428 var nodes = [first]; | 428 var nodes = [first]; |
429 for (var component in componentsList) { | 429 for (var component in componentsList) { |
430 nodes.add(new LiteralNode('/', caseSensitive: first.caseSensitive)); | 430 nodes.add(new LiteralNode('/', caseSensitive: first.caseSensitive)); |
431 nodes.add(component); | 431 nodes.add(component); |
432 } | 432 } |
433 return new SequenceNode(nodes, caseSensitive: first.caseSensitive); | 433 return new SequenceNode(nodes, caseSensitive: first.caseSensitive); |
434 } | 434 } |
OLD | NEW |