OLD | NEW |
1 library io; | 1 library io; |
2 /// This is a helper library to make working with io easier. | 2 /// This is a helper library to make working with io easier. |
3 // TODO(janicejl): listDir, canonicalize, resolveLink, and linkExists are from | 3 // TODO(janicejl): listDir, canonicalize, resolveLink, and linkExists are from |
4 // pub/lib/src/io.dart. If the io.dart file becomes a package, should remove | 4 // pub/lib/src/io.dart. If the io.dart file becomes a package, should remove |
5 // copy of the functions. | 5 // copy of the functions. |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'package:pathos/path.dart' as path; | 9 import 'package:path/path.dart' as path; |
10 | 10 |
11 /// Lists the contents of [dir]. If [recursive] is `true`, lists subdirectory | 11 /// Lists the contents of [dir]. If [recursive] is `true`, lists subdirectory |
12 /// contents (defaults to `false`). If [includeHidden] is `true`, includes files | 12 /// contents (defaults to `false`). If [includeHidden] is `true`, includes files |
13 /// and directories beginning with `.` (defaults to `false`). | 13 /// and directories beginning with `.` (defaults to `false`). |
14 /// | 14 /// |
15 /// The returned paths are guaranteed to begin with [dir]. | 15 /// The returned paths are guaranteed to begin with [dir]. |
16 List<String> listDir(String dir, {bool recursive: false, | 16 List<String> listDir(String dir, {bool recursive: false, |
17 bool includeHidden: false}) { | 17 bool includeHidden: false}) { |
18 List<String> doList(String dir, Set<String> listedDirectories) { | 18 List<String> doList(String dir, Set<String> listedDirectories) { |
19 var contents = <String>[]; | 19 var contents = <String>[]; |
20 | 20 |
21 // Avoid recursive symlinks. | 21 // Avoid recursive symlinks. |
22 var resolvedPath = canonicalize(dir); | 22 var resolvedPath = canonicalize(dir); |
23 if (listedDirectories.contains(resolvedPath)) return []; | 23 if (listedDirectories.contains(resolvedPath)) return []; |
24 | 24 |
25 listedDirectories = new Set<String>.from(listedDirectories); | 25 listedDirectories = new Set<String>.from(listedDirectories); |
26 listedDirectories.add(resolvedPath); | 26 listedDirectories.add(resolvedPath); |
27 | 27 |
28 var children = <String>[]; | 28 var children = <String>[]; |
29 for (var entity in new Directory(dir).listSync()) { | 29 for (var entity in new Directory(dir).listSync()) { |
30 if (!includeHidden && path.basename(entity.path).startsWith('.')) { | 30 if (!includeHidden && path.basename(entity.path).startsWith('.')) { |
31 continue; | 31 continue; |
32 } | 32 } |
33 | 33 |
34 contents.add(entity.path); | 34 contents.add(entity.path); |
35 if (entity is Directory) { | 35 if (entity is Directory) { |
36 // TODO(nweiz): don't manually recurse once issue 4794 is fixed. | 36 // TODO(nweiz): don't manually recurse once issue 4794 is fixed. |
37 // Note that once we remove the manual recursion, we'll need to | 37 // Note that once we remove the manual recursion, we'll need to |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 seen.add(link); | 142 seen.add(link); |
143 link = path.normalize(path.join( | 143 link = path.normalize(path.join( |
144 path.dirname(link), new Link(link).targetSync())); | 144 path.dirname(link), new Link(link).targetSync())); |
145 } | 145 } |
146 return link; | 146 return link; |
147 } | 147 } |
148 | 148 |
149 /// Returns whether [link] exists on the file system. This will return `true` | 149 /// Returns whether [link] exists on the file system. This will return `true` |
150 /// for any symlink, regardless of what it points at or whether it's broken. | 150 /// for any symlink, regardless of what it points at or whether it's broken. |
151 bool linkExists(String link) => new Link(link).existsSync(); | 151 bool linkExists(String link) => new Link(link).existsSync(); |
OLD | NEW |