OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Read the contents of a file [fileName] into a [List] of [String]s. |
| 7 * If the file does not exist and [errorIfNoFile] is true, throw an |
| 8 * exception, else return an empty list. |
| 9 */ |
| 10 List<String> getFileContents(String filename, bool errorIfNoFile) { |
| 11 File f = new File(filename); |
| 12 if (!f.existsSync()) { |
| 13 if (errorIfNoFile) { |
| 14 throw new Exception('Config file $filename not found.'); |
| 15 } else { |
| 16 return new List(); |
| 17 } |
| 18 } |
| 19 return f.readAsLinesSync(); |
| 20 } |
| 21 |
| 22 /** Copy a file with path [sourceName] to destination [destName]. */ |
| 23 void copyFile(String sourceName, String destName) { |
| 24 var sourceFile = new File(sourceName); |
| 25 var destFile = new File(destName); |
| 26 var istream = sourceFile.openInputStream(); |
| 27 var ostream = destFile.openOutputStream(FileMode.WRITE); |
| 28 istream.pipe(ostream); |
| 29 // TODO(gram) - make sure both streams are closed. |
| 30 // I think they are but the docs are not clear on this point. |
| 31 } |
| 32 |
| 33 /** Create a file [fileName] and populate it with [contents]. */ |
| 34 void createFile(String fileName, String contents) { |
| 35 var file = new File(fileName); |
| 36 var ostream = file.openOutputStream(FileMode.WRITE); |
| 37 ostream.writeString(contents); |
| 38 ostream.close(); |
| 39 } |
| 40 |
| 41 /** |
| 42 * Given a file path [path], make it absolute if it is relative, |
| 43 * and return the result. |
| 44 */ |
| 45 String makePathAbsolute(String path) { |
| 46 var p = new Path(path).canonicalize(); |
| 47 if (p.isAbsolute) { |
| 48 return p.toString(); |
| 49 } else { |
| 50 var cwd = new Path((new Directory.current()).path); |
| 51 return cwd.join(p).toString(); |
| 52 } |
| 53 } |
| 54 |
| 55 /** |
| 56 * Create the list of all the files in a set of directories |
| 57 * ([dirs]) whose names match [filePat]. If [recurse] is true |
| 58 * look at subdirectories too. Once they have all been enumerated, |
| 59 * call [onComplete]. |
| 60 */ |
| 61 void buildFileList(List dirs, RegExp filePat, bool recurse, |
| 62 Function onComplete) { |
| 63 var files = new List(); |
| 64 var dirCount = 1; |
| 65 for (var i = 0; i < dirs.length; i++) { |
| 66 var path = dirs[i]; |
| 67 // Is this a regular file? |
| 68 File f = new File(path); |
| 69 if (f.existsSync()) { |
| 70 if (filePat.hasMatch(path)) { |
| 71 files.add(path); |
| 72 } |
| 73 } else { // Try treat it as a directory. |
| 74 Directory d = new Directory(path); |
| 75 if (d.existsSync()) { |
| 76 ++dirCount; |
| 77 var lister = d.list(recursive: recurse); |
| 78 lister.onFile = (file) { |
| 79 if (filePat.hasMatch(file)) { |
| 80 files.add(file); |
| 81 } |
| 82 }; |
| 83 lister.onDone = (complete) { |
| 84 if (complete && --dirCount == 0) { |
| 85 onComplete(files); |
| 86 } |
| 87 }; |
| 88 } else { // Does not exist. |
| 89 print('$path does not exist.'); |
| 90 } |
| 91 } |
| 92 } |
| 93 if (--dirCount == 0) { |
| 94 onComplete(files); |
| 95 } |
| 96 } |
| 97 |
| 98 /** Delete a file. */ |
| 99 void deleteFile(String fname) { |
| 100 var f = new File(fname); |
| 101 f.deleteSync(); |
| 102 } |
OLD | NEW |