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