OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /// Helper functionality to make working with IO easier. | 5 /// Helper functionality to make working with IO easier. |
6 library io; | 6 library io; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 dir.delete(recursive: true))); | 172 dir.delete(recursive: true))); |
173 } | 173 } |
174 | 174 |
175 /// Asynchronously lists the contents of [dir], which can be a [String] | 175 /// Asynchronously lists the contents of [dir], which can be a [String] |
176 /// directory path or a [Directory]. If [recursive] is `true`, lists | 176 /// directory path or a [Directory]. If [recursive] is `true`, lists |
177 /// subdirectory contents (defaults to `false`). If [includeHiddenFiles] is | 177 /// subdirectory contents (defaults to `false`). If [includeHiddenFiles] is |
178 /// `true`, includes files and directories beginning with `.` (defaults to | 178 /// `true`, includes files and directories beginning with `.` (defaults to |
179 /// `false`). | 179 /// `false`). |
180 /// | 180 /// |
181 /// If [dir] is a string, the returned paths are guaranteed to begin with it. | 181 /// If [dir] is a string, the returned paths are guaranteed to begin with it. |
182 /// | |
183 /// Note that on Windows, only directories may be symlinked to. | |
nweiz
2013/02/05 03:26:14
Is this note supposed to be on createSymlink?
Bob Nystrom
2013/02/05 04:21:53
Done.
| |
182 Future<List<String>> listDir(dir, | 184 Future<List<String>> listDir(dir, |
183 {bool recursive: false, bool includeHiddenFiles: false}) { | 185 {bool recursive: false, bool includeHiddenFiles: false}) { |
184 Future<List<String>> doList(Directory dir, Set<String> listedDirectories) { | 186 Future<List<String>> doList(Directory dir, Set<String> listedDirectories) { |
185 var contents = <String>[]; | 187 var contents = <String>[]; |
186 var completer = new Completer<List<String>>(); | 188 var completer = new Completer<List<String>>(); |
187 | 189 |
188 // Avoid recursive symlinks. | 190 // Avoid recursive symlinks. |
189 var resolvedPath = new File(dir.path).fullPathSync(); | 191 var resolvedPath = new File(dir.path).fullPathSync(); |
190 if (listedDirectories.contains(resolvedPath)) { | 192 if (listedDirectories.contains(resolvedPath)) { |
191 return new Future.immediate([]); | 193 return new Future.immediate([]); |
192 } | 194 } |
195 | |
193 listedDirectories = new Set<String>.from(listedDirectories); | 196 listedDirectories = new Set<String>.from(listedDirectories); |
194 listedDirectories.add(resolvedPath); | 197 listedDirectories.add(resolvedPath); |
195 | 198 |
196 log.io("Listing directory ${dir.path}."); | 199 log.io("Listing directory ${dir.path}."); |
197 var lister = dir.list(); | 200 var lister = dir.list(); |
198 | 201 |
199 lister.onDone = (done) { | 202 lister.onDone = (done) { |
200 // TODO(rnystrom): May need to sort here if it turns out onDir and onFile | 203 // TODO(rnystrom): May need to sort here if it turns out onDir and onFile |
201 // aren't guaranteed to be called in a certain order. So far, they seem to . | 204 // aren't guaranteed to be called in a certain order. So far, they seem to . |
202 if (done) { | 205 if (done) { |
(...skipping 10 matching lines...) Expand all Loading... | |
213 } catch (_, localStackTrace) { | 216 } catch (_, localStackTrace) { |
214 stackTrace = localStackTrace; | 217 stackTrace = localStackTrace; |
215 } | 218 } |
216 | 219 |
217 var children = []; | 220 var children = []; |
218 lister.onError = (error) => completer.completeError(error, stackTrace); | 221 lister.onError = (error) => completer.completeError(error, stackTrace); |
219 lister.onDir = (file) { | 222 lister.onDir = (file) { |
220 if (!includeHiddenFiles && basename(file).startsWith('.')) return; | 223 if (!includeHiddenFiles && basename(file).startsWith('.')) return; |
221 file = join(dir, basename(file)); | 224 file = join(dir, basename(file)); |
222 contents.add(file); | 225 contents.add(file); |
223 | |
224 // TODO(nweiz): don't manually recurse once issue 7358 is fixed. Note that | 226 // TODO(nweiz): don't manually recurse once issue 7358 is fixed. Note that |
225 // once we remove the manual recursion, we'll need to explicitly filter | 227 // once we remove the manual recursion, we'll need to explicitly filter |
226 // out files in hidden directories. | 228 // out files in hidden directories. |
227 if (recursive) { | 229 if (recursive) { |
228 children.add(doList(new Directory(file), listedDirectories)); | 230 children.add(doList(new Directory(file), listedDirectories)); |
229 } | 231 } |
230 }; | 232 }; |
233 | |
231 lister.onFile = (file) { | 234 lister.onFile = (file) { |
232 if (!includeHiddenFiles && basename(file).startsWith('.')) return; | 235 if (!includeHiddenFiles && basename(file).startsWith('.')) return; |
233 contents.add(join(dir, basename(file))); | 236 contents.add(join(dir, basename(file))); |
234 }; | 237 }; |
235 | 238 |
236 return completer.future.then((contents) { | 239 return completer.future.then((contents) { |
237 return Future.wait(children).then((childContents) { | 240 return Future.wait(children).then((childContents) { |
238 contents.addAll(flatten(childContents)); | 241 contents.addAll(flatten(childContents)); |
239 return contents; | 242 return contents; |
240 }); | 243 }); |
(...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
929 Directory _getDirectory(entry) { | 932 Directory _getDirectory(entry) { |
930 if (entry is Directory) return entry; | 933 if (entry is Directory) return entry; |
931 return new Directory(entry); | 934 return new Directory(entry); |
932 } | 935 } |
933 | 936 |
934 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 937 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
935 Uri _getUri(uri) { | 938 Uri _getUri(uri) { |
936 if (uri is Uri) return uri; | 939 if (uri is Uri) return uri; |
937 return Uri.parse(uri); | 940 return Uri.parse(uri); |
938 } | 941 } |
OLD | NEW |