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