File.js
[app.Builder.js] / File.js
1 // <script type ="text/Javascript">
2 GLib = imports.gi.GLib;
3 Gio = imports.gi.Gio;
4
5
6
7 /**
8 * @namespace File
9
10 * Library to wrap GLib and Gio basic File related methods
11
12 * usage:
13
14 * File = import.File.File;
15
16 * var contents = File.read("/tmp/test.txt");
17 *
18
19
20 */
21 var File = {
22
23     SEPARATOR : '/',
24
25     // fixme - this needs a bitter location.. 
26     // they where in a string class before, but  overriding String methods is not a good normally a good idea..
27        
28     rtrim : function (s,toTrim) {
29         if (s.substr(s.length - toTrim.length) == toTrim) {
30             return s.slice(0, s.length - toTrim.length);
31         }
32    
33         return s;
34     },
35    trim : function (s,toTrim) {
36         var out = s.ltrim(toTrim);
37         out = out.rtrim(toTrim);
38         return out;
39     },
40     
41     ltrim : function (s, toTrim) {
42         if (s.substr(0, toTrim.length) == toTrim) {
43             return s.slice(toTrim.length);
44         }
45         
46         return s;
47     },
48     
49     join : function () {
50         
51        
52        
53         
54         var out = "";
55         for (var i = 0; i < arguments.length; i++) {
56             if (i == 0) {
57               out += arguments[i].rtrim(File.SEPARATOR);
58             }
59             else if (i == arguments.length - 1) {
60               out += File.SEPARATOR + arguments[i].ltrim(File.SEPARATOR);
61             }
62             else {
63               out += File.SEPARATOR + arguments[i].trim(File.SEPARATOR);
64             }
65         }
66         return out;
67     },
68
69     read : function (path) {
70         var out = {};
71         GLib.file_get_contents(path, out, null, null);
72         return out['value'];
73     },
74
75     isFile : function (path) {
76       return GLib.file_test(path, GLib.FileTest.IS_REGULAR);
77     },
78     exists : function (path) {
79       return GLib.file_test(path, GLib.FileTest.EXISTS);
80     },
81     isDirectory : function (path) {
82       return GLib.file_test(path, GLib.FileTest.IS_DIR);
83     },
84
85     list : function (path) {
86         var listing = [];
87
88         var f = Gio.file_new_for_path(String(path));
89         var file_enum = f.enumerate_children(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, Gio.FileQueryInfoFlags.NONE, null);
90
91         var next_file = null;
92
93         while ((next_file = file_enum.next_file(null)) != null) {
94           listing.push(next_file.get_display_name());
95         }
96
97         file_enum.close(null);
98
99         listing.sort();
100
101         return listing;
102     },
103
104     mtime : function (path) {
105         var f = Gio.file_new_for_path(String(path));
106         var mtime = new GLib.TimeVal();
107
108         var info = f.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, null);
109         info.get_modification_time(mtime);
110
111         return new Date(mtime.tv_sec * 1000);
112     },
113
114     /**
115      * resolve the real path
116      * @arg path {String} Path to resolve
117      * @returns {String} the resolved path path.
118      * 
119      */
120     realpath :  function (path) { 
121         return this.canonical(path);
122     },
123     canonical : function (path) { 
124         var f = Gio.file_new_for_path(String(path));
125         var can = f.resolve_relative_path('');
126         return can.get_path();
127     },
128     /**
129      * write a string to a file
130      * @arg path {String} File to write to alwasy overwrites.
131      * @arg string {String} Contents of file.
132      * 
133      */
134     write : function (path, string) {
135         var d = new Date();
136         var f = Gio.file_new_for_path(String(path));
137         var data_out = new Gio.DataOutputStream({base_stream:f.replace(null, false, Gio.FileCreateFlags.NONE, null)});
138         data_out.put_string(string, null);
139         data_out.close(null);
140         print("WRITE : " + path + " in " + ((new Date()) - d) + 'ms');
141         
142     },
143     /**
144      * append
145      * @arg path {String} File to write to
146      * @arg string {String} string to append to file.
147      * 
148      */
149     append : function (path, string) {
150         var f = Gio.file_new_for_path(String(path));
151         var data_out = new Gio.DataOutputStream({
152                 base_stream:f.append_to(Gio.FileCreateFlags.NONE, null)
153         });
154         data_out.put_string(string, null);
155         data_out.close(null);
156     },
157     /**
158      * remove 
159      * Delete a file.
160      * @arg path {String} File to remove
161      * 
162      * 
163      */
164     remove : function (path)
165     {
166         var f = Gio.file_new_for_path(String(path));
167         return f['delete']();
168     },
169     // copy files recursively from fromDir, silently ignore them if they already exist in toDir
170     silentRecursiveCopy : function (fromDir, toDir) {
171         var filesToCopy = File.recursiveListing(fromDir);
172         var srcPath, destPath, src, dest;
173
174         for (var index in filesToCopy) {
175           srcPath = File.join(String(fromDir), filesToCopy[index]);
176           destPath = File.join(String(toDir), filesToCopy[index]);
177
178           if (File.isFile(srcPath) && !File.isFile(destPath)) {
179             File.copyFile(srcPath, destPath);
180           }
181           else if (File.isDirectory(srcPath) && !File.isDirectory(destPath)) {
182             File.mkdir(destPath);
183           }
184
185         }
186     },
187     /**
188      * Make a symbolic link
189      * @arg  new_link {String} The new link
190      * @arg  target    {String} Where it links to.
191      */
192     link : function (new_link, target) {
193         var dest = Gio.file_new_for_path(String(new_link));
194         return dest.make_symbolic_link(target, null);
195     },
196     /**
197      * Make a directory
198      * FIXME - needs perms setting..
199      * 
200      * @arg  directory  {String} Directory to make
201      */
202
203     mkdir : function (destPath) {
204         var dest = Gio.file_new_for_path(String(destPath));
205         return dest.make_directory(null);
206     },
207
208     /**
209      * Copy a file or (directory maybe?)
210      * @arg  srcPath {String} source file
211      * @arg  destPath {String} destination file
212      * @arg  flags {Gio.FileCopyFlags} to overwrite etc...  Gio.FileCopyFlags.OVERWRITE
213      */
214     copy : function (srcPath, destPath, flags) {
215         return this.copyFile(srcPath, destPath, flags);
216     },
217     copyFile : function (srcPath, destPath, flags) {
218         
219         flags = typeof(flags) == 'undefined' ? Gio.FileCopyFlags.NONE : flags;
220         var dest = Gio.file_new_for_path(String(destPath));
221         var src = Gio.file_new_for_path(String(srcPath));
222
223         // a bit of a hack for the fact that Gio.File.copy arguments
224         // can be nulled, but not according to the GIR file
225         return src.copy(dest, flags);
226     },
227     
228     
229     
230
231     recursiveListing : function (dir) {
232
233         function recursiveListingInternal(prefix, listing, dir) {
234           var entries = File.list(dir);
235           var next, fullPath;
236
237           for (var index in entries) {
238             next = entries[index];
239             fullPath = File.join(prefix, dir, next);
240
241             if (File.isDirectory(fullPath)) {
242               listing.push(next);
243               listing = listing.concat(recursiveListingInternal(next, [], fullPath));
244             }
245             else {
246               if (prefix) {
247                 next = File.join(prefix, next);
248               }
249               listing.push(next);
250             }
251           }
252
253           return listing;
254         }
255
256         return recursiveListingInternal('', [], dir);
257     }
258
259 };