OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 return tempDir.path; | 125 return tempDir.path; |
126 } | 126 } |
127 | 127 |
128 // TODO(nweiz): rename includeHiddenFiles to includeHidden. | 128 // TODO(nweiz): rename includeHiddenFiles to includeHidden. |
129 /// Lists the contents of [dir]. If [recursive] is `true`, lists subdirectory | 129 /// Lists the contents of [dir]. If [recursive] is `true`, lists subdirectory |
130 /// contents (defaults to `false`). If [includeHiddenFiles] is `true`, includes | 130 /// contents (defaults to `false`). If [includeHiddenFiles] is `true`, includes |
131 /// files and directories beginning with `.` (defaults to `false`). | 131 /// files and directories beginning with `.` (defaults to `false`). |
132 /// | 132 /// |
133 /// The returned paths are guaranteed to begin with [dir]. | 133 /// The returned paths are guaranteed to begin with [dir]. |
134 List<String> listDir(String dir, {bool recursive: false, | 134 List<String> listDir(String dir, {bool recursive: false, |
135 bool includeHiddenFiles: false}) { | 135 bool includeHidden: false}) { |
136 List<String> doList(String dir, Set<String> listedDirectories) { | 136 List<String> doList(String dir, Set<String> listedDirectories) { |
137 var contents = <String>[]; | 137 var contents = <String>[]; |
138 | 138 |
139 // Avoid recursive symlinks. | 139 // Avoid recursive symlinks. |
140 var resolvedPath = new File(dir).fullPathSync(); | 140 var resolvedPath = new File(dir).fullPathSync(); |
141 if (listedDirectories.contains(resolvedPath)) return []; | 141 if (listedDirectories.contains(resolvedPath)) return []; |
142 | 142 |
143 listedDirectories = new Set<String>.from(listedDirectories); | 143 listedDirectories = new Set<String>.from(listedDirectories); |
144 listedDirectories.add(resolvedPath); | 144 listedDirectories.add(resolvedPath); |
145 | 145 |
146 log.io("Listing directory $dir."); | 146 log.io("Listing directory $dir."); |
147 | 147 |
148 var children = []; | 148 var children = []; |
149 for (var entity in new Directory(dir).listSync(followLinks: false)) { | 149 for (var entity in new Directory(dir).listSync(followLinks: false)) { |
| 150 var entityPath = entity.path; |
| 151 if (!includeHidden && path.basename(entityPath).startsWith('.')) continue; |
| 152 |
150 // TODO(nweiz): remove this when issue 4928 is fixed. | 153 // TODO(nweiz): remove this when issue 4928 is fixed. |
151 if (entity is Link) { | 154 if (entity is Link) { |
152 var link = entity.path; | |
153 // We treat broken symlinks as files, in that we don't want to recurse | 155 // We treat broken symlinks as files, in that we don't want to recurse |
154 // into them. | 156 // into them. |
155 entity = dirExists(link) ? new Directory(link) : new File(link); | 157 entity = dirExists(entityPath) |
| 158 ? new Directory(entityPath) |
| 159 : new File(entityPath); |
156 } | 160 } |
157 | 161 |
158 if (entity is File) { | 162 if (entity is File) { |
159 var file = entity.path; | 163 contents.add(entityPath); |
160 if (!includeHiddenFiles && path.basename(file).startsWith('.')) { | |
161 continue; | |
162 } | |
163 contents.add(file); | |
164 } else if (entity is Directory) { | 164 } else if (entity is Directory) { |
165 var file = entity.path; | 165 contents.add(entityPath); |
166 if (!includeHiddenFiles && path.basename(file).startsWith('.')) { | |
167 continue; | |
168 } | |
169 contents.add(file); | |
170 // TODO(nweiz): don't manually recurse once issue 4794 is fixed. | 166 // TODO(nweiz): don't manually recurse once issue 4794 is fixed. |
171 // Note that once we remove the manual recursion, we'll need to | 167 // Note that once we remove the manual recursion, we'll need to |
172 // explicitly filter out files in hidden directories. | 168 // explicitly filter out files in hidden directories. |
173 if (recursive) { | 169 if (recursive) { |
174 children.addAll(doList(file, listedDirectories)); | 170 children.addAll(doList(entityPath, listedDirectories)); |
175 } | 171 } |
176 } | 172 } |
177 } | 173 } |
178 | 174 |
179 log.fine("Listed directory $dir:\n${contents.join('\n')}"); | 175 log.fine("Listed directory $dir:\n${contents.join('\n')}"); |
180 contents.addAll(children); | 176 contents.addAll(children); |
181 return contents; | 177 return contents; |
182 } | 178 } |
183 | 179 |
184 return doList(dir, new Set<String>()); | 180 return doList(dir, new Set<String>()); |
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 const PubProcessResult(this.stdout, this.stderr, this.exitCode); | 735 const PubProcessResult(this.stdout, this.stderr, this.exitCode); |
740 | 736 |
741 bool get success => exitCode == 0; | 737 bool get success => exitCode == 0; |
742 } | 738 } |
743 | 739 |
744 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 740 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
745 Uri _getUri(uri) { | 741 Uri _getUri(uri) { |
746 if (uri is Uri) return uri; | 742 if (uri is Uri) return uri; |
747 return Uri.parse(uri); | 743 return Uri.parse(uri); |
748 } | 744 } |
OLD | NEW |