Builder/Provider/RooUsage.txt
[app.Builder.js] / Builder / Provider / Project / Base.js
1 //<Script type="text/javascript">
2
3 /**
4  * Project Object
5  * 
6  * Projects can only contain one directory... - it can import other projects..(later)
7  * 
8  * we need to sort out that - paths is currently a key/value array..
9  * 
10  * 
11  * 
12  */
13
14 Gio = imports.gi.Gio;
15 GLib = imports.gi.GLib; 
16
17
18 console = imports.console;
19 XObject = imports.XObject.XObject;
20 File = imports.File.File;
21
22 ProjectManager = imports.Builder.Provider.ProjectManager.ProjectManager;
23 Observable = imports.Observable.Observable;
24
25 Base = XObject.define( 
26     function(cfg) {
27         
28         
29         this.addEvents({ 'changed'  : true });
30         var _this = this;
31         this.on('changed' , function() {
32             Seed.print("Calling PM - changed");
33             
34             ProjectManager.fireEvent('changed');
35         });
36         XObject.extend(this, cfg);
37         this.files = { }; 
38         /*
39         if (this.files ){
40             for (var f in this.files) {
41                 var xt = this.xtype;
42                 var cls = imports.Builder.Provider.File[xt][xt];
43                 this.files[f] = cls(this.files[f]);
44             }
45         }
46         */
47         
48         
49         this.scanDirs();
50         
51     },
52     Observable, 
53     {
54         id : false,
55         fn:  false, // the md5 sum for this one..
56         paths : false,
57         files : false,
58         tree : false,
59         xtype : false,
60         
61         load : function (o)  // is this used??
62         {
63             if (!this.fetchTree) {
64                 console.log("Project.getTree tree called on base object?!?!");
65                 return false;
66             }
67             
68             if (this.files) {
69                 return o.success.apply(o.scope || this, [this]);
70             }
71             return this.fetchTree(o);
72             
73         },
74         getPalete : function()
75         {
76             print("GET PROVIDER FOR " + this.xtype);
77             return  ProjectManager.getPalete(this.xtype);
78         },
79         toJSON : function()
80         {
81             var ret = { };
82             var _this = this;
83             for (var k in _this) {
84                 
85                 if (['files', 'tree'].indexOf(k) > -1) {
86                     continue;
87                 }
88                 if (k != 'paths') {
89                     if ((typeof(_this[k]) == 'object') ||(typeof(_this[k]) == 'function')) {
90                         continue;
91                     }
92                 }
93                 print("Storing " + k);
94                 ret[k] = _this[k];
95                 
96             }
97             
98             
99             // deal with files..
100             //for (var f in this.files) {
101             //    print(f);
102             //    ret.files[f] = this.files[f].toJsonArray();
103            // }
104             
105             
106             return JSON.stringify(ret);
107           
108           
109         },
110         getName :function()
111         {
112             //returns the basename of the first path..
113             for (var i in this.paths) {
114                 return GLib.basename(i);
115             }
116         },
117         
118         toTree : function()
119         {
120             
121             
122             // normally this will build tree's based on info in the file stuff..
123             // but for the time being, we just return an array of all the files.
124             
125             
126             
127             //if (this.tree) {
128              //   return this.tree;
129             //}
130             
131             this.tree = [];
132             /*
133             for (var f in this.files) {
134                 this.tree.push(this.files[f]);
135             }
136             return this.tree;
137             */
138             
139             // have parents -> add them to parent...
140             var files = {};
141             var parents = {};
142             for (var k in this.files) {
143                 
144                 var f = this.files[k];
145                 if (!f) {
146                     continue;
147                    }
148                 ///console.dump(f);
149                 f.hasParent = false;
150                 f.cn = [];
151                 //console.log(f.name);
152                 if (f.fullname) {
153                     files[f.fullname] = f;
154                 }
155             };
156             
157             // add Parent pointer..
158             for (var k in this.files) {
159                 var f = this.files[k];
160                 console.log(f.parent + ":" + f.name);
161                 if (f.parent && typeof(files[f.parent]) != 'undefined') {
162                     // add it to parent;
163                     
164                     files[f.parent].cn.push(f);
165                     f.hasParent = true;
166                     
167                 }
168                 
169                 
170             };
171             
172               
173             
174             var ret = [];
175             for (var k in this.files) {
176                 var f = this.files[k];
177                 
178                 f.sortCn();
179                 if (f.hasParent) {
180                     continue;
181                 }
182                 if (typeof(files[f.fullname]) != 'undefined') {
183                     ret.push(f);
184                 }
185                 
186             };
187             
188             
189             ret.sort(function(a,b) {
190                 return a.path > b.path ? 1 : -1;
191             });
192             this.tree = ret;
193             return this.tree;
194              
195             
196         },
197         getById : function(id)
198         {
199             var ret = false;
200             for (var k in this.files) {
201                 var f = this.files[k];
202                 
203                 console.log(f.id + '?=' + id);
204                 if (f.id == id) {
205                     return f;
206                 }
207             };
208             return ret;
209         },
210         
211         loadFileOnly : function(path)
212         {
213             
214             var xt = this.xtype;
215             var cls = imports.Builder.Provider.File[xt][xt];
216             return  new cls({
217                 path : path,
218                 parent : '',
219                 project : this
220             });
221         },
222         create : function(filename)
223         {
224             var ret = this.loadFileOnly(filename);
225             ret.save();
226             this.addFile(ret);
227             return ret;
228             
229         },
230         
231          
232         addFile: function(pfile) { // add a single file, and trigger changed.
233             this.files[pfile.path] = pfile
234             this.fireEvent('changed', this);
235         },
236         
237         add: function(path, type)
238         {
239             this.paths = this.paths || { };
240             this.paths[path] = type;
241             //Seed.print(" type is '" + type + "'");
242             if (type == 'dir') {
243                 this.scanDir(path);
244             //    console.dump(this.files);
245             }
246             if (type == 'file' ) {
247                 
248                 this.files[path] = this.loadFileOnly( path );
249             }
250             
251             // console.dump(this.files);
252             this.fireEvent('changed', this);
253             
254         },
255         
256         scanDirs: function()
257         {
258             this.files = this.files  || { };
259             for (var d in this.paths) {
260                 if (this.paths[d] == 'dir') {
261                     this.scanDir(d);
262                 }
263                 // otherwise add a file..
264             }
265             //console.dump(this.files);
266             
267         },
268         
269         
270         // list files.
271         scanDir : function(dir, dp) 
272         {
273             dp = dp || 0;
274             Seed.print("Running scandir on " + dir);
275             if (dp > 5) { // no more than 5 deep?
276                 return;
277             }
278             // this should be done async -- but since we are getting the proto up ...
279             var dirs = File.list(dir);
280             var subs = [];
281             var _this = this;
282             dirs.forEach(function( fn ){ 
283                  
284                 console.log('trying ' + dir + '/' + fn);
285                 if (!fn) {
286                     subs.forEach( function(s) {
287                         _this.scanDir(s, dp+1);
288                     });
289                     return;
290                 }
291                 if (fn[0] == '.') { // skip hidden
292                     return;
293                 }
294                 
295                 if (GLib.file_test(dir  + '/' + fn, GLib.FileTest.IS_DIR)) {
296                     subs.push(dir  + '/' + fn);
297                     return;
298                 }
299                 
300                 if (!fn.match(/\.bjs$/)) {
301                     return;
302                 }
303                 var parent = '';
304                 //if (dp > 0 ) {
305                 var sp = dir.split('/');
306                 sp = sp.splice(sp.length - (dp +1), (dp +1));
307                 parent = sp.join('.');
308                 
309                 
310                 if (typeof(_this.files[dir  + '/' + fn]) != 'undefined') {
311                     // we already have it..
312                     _this.files[dir  + '/' + fn].parent = parent;
313                     return;
314                 }
315                 var xt = _this.xtype;
316                 var cls = imports.Builder.Provider.File[xt][xt];
317                 
318                 Seed.print("Adding file " + dir  + '/' + fn);
319                 _this.files[dir  + '/' + fn] = new cls({
320                     path : dir  + '/' + fn,
321                     parent : parent,
322                     project : _this
323                 });
324                 //console.log(this.files[dir  + '/' + fn] );
325                 /*
326                 var f = Gio.file_new_for_path(dir + '/' + fn);
327                 var inf = f.query_info('standard::*');
328                 var tv = new GLib.TimeVal();
329                 inf.get_modification_time(tv);
330                 
331                 // should it do something about this information..
332                 // fixme... time data is busted..
333                 this.files[dir  + '/' + fn] = '' + tv.tv_sec + '.' + tv.tv_usec;
334                 */
335             });
336              
337             
338         }
339         
340         
341         
342         
343         
344
345     }
346 ); 
347
348
349      
350