OLD | NEW |
1 // Copyright 2013 Google Inc. All Rights Reserved. | 1 // Copyright 2013 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 library quiver.io; | 15 library quiver.io; |
16 | 16 |
17 import 'dart:async'; | 17 import 'dart:async'; |
18 import 'dart:convert'; | 18 import 'dart:convert'; |
19 import 'dart:io'; | 19 import 'dart:io'; |
20 | 20 |
21 import 'package:quiver/async.dart'; | 21 import 'package:quiver/async.dart'; |
22 | 22 |
23 /** | 23 /// Converts a [Stream] of byte lists to a [String]. |
24 * Converts a [Stream] of byte lists to a [String]. | |
25 */ | |
26 Future<String> byteStreamToString(Stream<List<int>> stream, | 24 Future<String> byteStreamToString(Stream<List<int>> stream, |
27 {Encoding encoding: UTF8}) { | 25 {Encoding encoding: UTF8}) { |
28 return stream.transform(encoding.decoder).join(); | 26 return stream.transform(encoding.decoder).join(); |
29 } | 27 } |
30 | 28 |
31 /** | 29 /// Gets the full path of [path] by using [File.fullPathSync]. |
32 * Gets the full path of [path] by using [File.fullPathSync]. | |
33 */ | |
34 String getFullPath(path) => new File(path).resolveSymbolicLinksSync(); | 30 String getFullPath(path) => new File(path).resolveSymbolicLinksSync(); |
35 | 31 |
36 /** | 32 /// Lists the sub-directories and files of this Directory, optionally recursing |
37 * Lists the sub-directories and files of this Directory, optionally recursing | 33 /// into sub-directories based on the return value of [visit]. |
38 * into sub-directories based on the return value of [visit]. | 34 /// |
39 * | 35 /// [visit] is called with a [File], [Directory] or [Link] to a directory, |
40 * [visit] is called with a [File], [Directory] or [Link] to a directory, | 36 /// never a Symlink to a File. If [visit] returns true, then it's argument is |
41 * never a Symlink to a File. If [visit] returns true, then it's argument is | 37 /// listed recursively. |
42 * listed recursively. | |
43 */ | |
44 Future visitDirectory(Directory dir, Future<bool> visit(FileSystemEntity f)) { | 38 Future visitDirectory(Directory dir, Future<bool> visit(FileSystemEntity f)) { |
45 var futureGroup = new FutureGroup(); | 39 var futureGroup = new FutureGroup(); |
46 | 40 |
47 void _list(Directory dir) { | 41 void _list(Directory dir) { |
48 var completer = new Completer(); | 42 var completer = new Completer(); |
49 futureGroup.add(completer.future); | 43 futureGroup.add(completer.future); |
50 dir.list(followLinks: false).listen((FileSystemEntity entity) { | 44 dir.list(followLinks: false).listen((FileSystemEntity entity) { |
51 var future = visit(entity); | 45 var future = visit(entity); |
52 if (future != null) { | 46 if (future != null) { |
53 futureGroup.add(future.then((bool recurse) { | 47 futureGroup.add(future.then((bool recurse) { |
(...skipping 11 matching lines...) Expand all Loading... |
65 } else { | 59 } else { |
66 _list(entity); | 60 _list(entity); |
67 } | 61 } |
68 } | 62 } |
69 })); | 63 })); |
70 } | 64 } |
71 }, onDone: () { | 65 }, onDone: () { |
72 completer.complete(null); | 66 completer.complete(null); |
73 }, cancelOnError: true); | 67 }, cancelOnError: true); |
74 } | 68 } |
| 69 |
75 _list(dir); | 70 _list(dir); |
76 | 71 |
77 return futureGroup.future; | 72 return futureGroup.future; |
78 } | 73 } |
OLD | NEW |