OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /// This is a helper library to make working with io easier. | |
6 library docgen.io; | |
7 | |
8 // TODO(janicejl): listDir, canonicalize, resolveLink, and linkExists are from | |
9 // pub/lib/src/io.dart. If the io.dart file becomes a package, should remove | |
10 // copy of the functions. | |
11 | |
12 import 'dart:io'; | |
13 import 'package:path/path.dart' as path; | |
14 | |
15 /// Lists the contents of [dir]. | |
16 /// | |
17 /// If [recursive] is `true`, lists subdirectory contents (defaults to `false`). | |
18 /// | |
19 /// Excludes files and directories beginning with `.` | |
20 /// | |
21 /// The returned paths are guaranteed to begin with [dir]. | |
22 List<String> listDir(String dir, {bool recursive: false, | |
23 List<FileSystemEntity> listDir(Directory dir)}) { | |
24 if (listDir == null) listDir = (Directory dir) => dir.listSync(); | |
25 | |
26 return _doList(dir, new Set<String>(), recursive, listDir); | |
27 } | |
28 | |
29 List<String> _doList(String dir, Set<String> listedDirectories, bool recurse, | |
30 List<FileSystemEntity> listDir(Directory dir)) { | |
31 var contents = <String>[]; | |
32 | |
33 // Avoid recursive symlinks. | |
34 var resolvedPath = new Directory(dir).resolveSymbolicLinksSync(); | |
35 if (listedDirectories.contains(resolvedPath)) return []; | |
36 | |
37 listedDirectories = new Set<String>.from(listedDirectories); | |
38 listedDirectories.add(resolvedPath); | |
39 | |
40 var children = <String>[]; | |
41 for (var entity in listDir(new Directory(dir))) { | |
42 // Skip hidden files and directories | |
43 if (path.basename(entity.path).startsWith('.')) { | |
44 continue; | |
45 } | |
46 | |
47 contents.add(entity.path); | |
48 if (entity is Directory) { | |
49 // TODO(nweiz): don't manually recurse once issue 4794 is fixed. | |
50 // Note that once we remove the manual recursion, we'll need to | |
51 // explicitly filter out files in hidden directories. | |
52 if (recurse) { | |
53 children.addAll(_doList(entity.path, listedDirectories, recurse, | |
54 listDir)); | |
55 } | |
56 } | |
57 } | |
58 | |
59 contents.addAll(children); | |
60 return contents; | |
61 } | |
OLD | NEW |