X-Git-Url: http://git.roojs.org/?p=gnome.introspection-doc-generator;a=blobdiff_plain;f=File.js;h=77777ba32653bbf87a032ea3a8c9b0f696d16a3c;hp=f0da9ff4d55ec1a63fb35a75668915e7b3bb7ffd;hb=a67931117fc1f532da5016752f7dd3c625ce25d3;hpb=82b17ee1cb07377fb1a2ceac634ad0c3757180a6 diff --git a/File.js b/File.js index f0da9ff..77777ba 100755 --- a/File.js +++ b/File.js @@ -2,8 +2,21 @@ GLib = imports.gi.GLib; Gio = imports.gi.Gio; -imports['String.js'].load(String); - +String = imports.String.String; +/** +* @namespace File +* +* Library to wrap GLib and Gio basic File related methods +* +* usage: +* +* File = import.File.File; +* +* var contents = File.read("/tmp/test.txt"); +* +* +* +*/ var File = { SEPARATOR : '/', @@ -74,45 +87,112 @@ var File = { var can = f.resolve_relative_path(''); return can.get_path(); }, - + + /** + * write + * @arg path {String} File to write to + * @arg string {String} Contents of file. + * + */ write : function (path, string) { var f = Gio.file_new_for_path(String(path)); var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)}); data_out.put_string(string, null); data_out.close(null); }, - - // copy files recursively from fromDir, silently ignore them if they already exist in toDir - silentRecursiveCopy : function (fromDir, toDir) { + /** + * append + * @arg path {String} File to write to + * @arg string {String} string to append to file. + * + */ + append : function (path, string) { + var f = Gio.file_new_for_path(String(path)); + var data_out = new Gio.DataOutputStream({ + base_stream:f.append_to(Gio.FileCreateFlags.NONE, null) + }); + data_out.put_string(string, null); + data_out.close(null); + }, + /** + * remove + * Delete a file. + * @arg path {String} File to remove + * + * + */ + remove : function (path) + { + var f = Gio.file_new_for_path(String(path)); + return f['delete'](); + }, + /** + * silentRecursiveCopy + * copy files recursively from fromDir, silently ignore them if they already exist in toDir + * unless you select overwrite.. + * @arg {String} src source path + * @arg {String} dest destination path + * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to + * otherwise they will not be copied + * + */ + silentRecursiveCopy : function (fromDir, toDir, opts) { + var filesToCopy = File.recursiveListing(fromDir); var srcPath, destPath, src, dest; - + if (typeof(opts) =='undefined') { + opts = Gio.FileCopyFlags.NONE; + } + for (var index in filesToCopy) { - srcPath = File.join(String(fromDir), filesToCopy[index]); - destPath = File.join(String(toDir), filesToCopy[index]); - - if (File.isFile(srcPath) && !File.isFile(destPath)) { - File.copyFile(srcPath, destPath); - } - else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) { - File.mkdir(destPath); - } + srcPath = File.join(String(fromDir), filesToCopy[index]); + destPath = File.join(String(toDir), filesToCopy[index]); + if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) { + File.mkdir(destPath); + continue; + } + // source is not file..?!?!? + if (!File.isFile(srcPath)) { + continue; + } + if (File.isFile(destPath) && opts == Gio.FileCopyFlags.NONE) { + // do not overwrite.. - if file exists and we are not flaged to overwrite. + continue; + } + + File.copyFile(srcPath, destPath, opts); + } }, - + + /** + * mkdir + * make a directory.. + * @arg {String} dstPath directory to make + */ mkdir : function (destPath) { var dest = Gio.file_new_for_path(String(destPath)); + return dest.make_directory(null, null); }, - - copyFile : function (srcPath, destPath) { + /** + * copyFile + * @arg {String} src source path + * @arg {String} dest destination path + * @arg {Gio.FileCopyFlags} options (optional) - use Gio.FileCopyFlags.OVERWRITE to .. overwrite.. + * + */ + copyFile : function (srcPath, destPath, opts) { + if (typeof(opts) =='undefined') { + opts = Gio.FileCopyFlags.NONE; + } var dest = Gio.file_new_for_path(String(destPath)); var src = Gio.file_new_for_path(String(srcPath)); // a bit of a hack for the fact that Gio.File.copy arguments // can be nulled, but not according to the GIR file - return src.copy(dest, Gio.FileCopyFlags.NONE); + return src.copy(dest, opts); }, recursiveListing : function (dir) {