Project/Project.vala
[app.Builder.js] / Project / Project.vala
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
15 public class Project.Project {
16     
17     public signal void on_changed (); 
18         
19     public string id = "";
20     public string fn = "";
21     public string name = "";
22     public Gee.HashMap<string,string> paths;
23     public GLib.List<JsRender.JsRender> files ;
24     //tree : false,
25     public string xtype = "";
26     
27     
28     public Project (string path) {
29         
30         //this.name = name;
31         
32         
33         this.paths = new Gee.HashMap<string,string>();
34         this.files = new GLib.List<JsRender.JsRender>();
35         //XObject.extend(this, cfg);
36         //this.files = { }; 
37         this.paths.set(path, "dir");
38         
39         this.scanDirs();
40         
41     }
42     
43     
44     /*
45     public load
46      
47         
48         load : function (o)  // is this used??
49         {
50             if (!this.fetchTree) {
51                 console.log("Project.getTree tree called on base object?!?!");
52                 return false;
53             }
54             
55             if (this.files) {
56                 return o.success.apply(o.scope || this, [this]);
57             }
58             return this.fetchTree(o);
59             
60         },
61     */
62    // public Palete  getPalete ()
63     //{
64             //print("Project.Base: GET PROVIDER FOR " + this.xtype);
65    //         return  ProjectManager.getPalete(this.xtype);
66    // }
67     
68     public string toJSON(bool show_all)
69     {
70         
71         var builder = new Json.Builder ();
72         
73         builder.begin_object ();
74         
75         builder.set_member_name ("name");
76         builder.add_string_value (this.name);
77
78         
79         builder.set_member_name ("fn");
80         builder.add_string_value (this.fn);
81
82         builder.set_member_name ("xtype");
83         builder.add_string_value (this.xtype);
84         
85         // file ??? try/false?
86         builder.set_member_name ("paths");
87         
88         
89         builder.begin_array ();
90         
91         
92         var iter = this.paths.map_iterator();
93         while (iter.next()) {
94             builder.add_string_value (iter.get_key());
95         }
96         builder.end_array ();
97         
98         builder.end_object ();
99                 
100         var  generator = new Json.Generator ();
101         var  root = builder.get_root ();
102         generator.set_root (root);
103
104         return  generator.to_data (null);
105               
106           
107     }
108     // returns the first path
109     public string getName()
110     {
111         var iter = this.paths.map_iterator();
112         while (iter.next()) {
113             return GLib.Path.get_basename(iter.get_key());
114         }
115       
116         return "";
117     }
118     /**
119      *
120      * to tree - > should
121      */
122  
123     public GLib.List<JsRender.JsRender> toTree ()
124     {
125             
126          
127          
128         var files = new Gee.HashMap<string,JsRender.JsRender>();
129          
130         for(var i = 0; i < this.files.length(); i++) {
131             var fo = this.files.nth_data(i);
132             
133             fo.hasParent = false;
134             fo.cn = new GLib.List<JsRender.JsRender>();
135             
136             if (this.files.nth_data(i).fullname.length > 0) {
137                 files.set(fo.fullname, fo);
138             }
139         }
140         
141         var iter = files.map_iterator();
142         while (iter.next()) {
143             var f = iter.get_value();
144             
145             var par = f.parent;
146             if (par.length < 1) {
147                 continue;
148             }
149             if (!files.has_key(par)) {
150                 continue;
151             }
152             files.get(par).cn.append(f);
153             f.hasParent = true;
154              
155         };
156             
157         var ret = new GLib.List<JsRender.JsRender>();
158         iter = files.map_iterator();
159         while (iter.next()) {
160             var f = iter.get_value();
161                 
162             //   f.sortCn();
163             if (f.hasParent) {
164                 continue;
165             }
166             if (files.has_key(f.fullname)) {
167             
168                 ret.append(f);
169             }
170         }
171         ret.sort( (a,b) => {
172             return a.path > b.path ? 1 : -1;
173         });
174         
175         
176         //print(JSON.stringify(ret,null,4));
177             
178         return ret;  
179              
180             
181     }
182     
183     
184     
185     public JsRender.JsRender? getById(string id)
186     {
187         
188        for(var i = 0; i < this.files.length(); i++) {
189             var f = this.files.nth_data(i);
190             
191             
192             //console.log(f.id + '?=' + id);
193             if (f.id == id) {
194                 return f;
195             }
196         };
197         return null;
198     }
199         
200     public JsRender.JsRender loadFileOnly (string path)
201     {
202         var xt = this.xtype;
203         return JsRender.JsRender.factory(xt, this, path);
204         
205     }
206     
207     public JsRender.JsRender create(string filename)
208     {
209         var ret = this.loadFileOnly(filename);
210         ret.save();
211         this.addFile(ret);
212         return ret;
213         
214     }
215         
216          
217     public void addFile(JsRender.JsRender pfile) { // add a single file, and trigger changed.
218         this.files.append(pfile); // duplicate check?
219         this.on_changed();
220     }
221     
222     public void add(string path, string type)
223     {
224         this.paths.set(path,type);
225         //Seed.print(" type is '" + type + "'");
226         if (type == "dir") {
227             this.scanDir(path);
228         //    console.dump(this.files);
229         }
230         if (type == "file" ) {
231             this.files.append(this.loadFileOnly( path ));
232         }
233         this.on_changed();
234         
235     }
236     public void  scanDirs()
237     {
238         var iter = this.paths.map_iterator();
239         while (iter.next()) {
240             if (iter.get_value() != "dir") {
241                 continue;
242             }
243             this.scanDir(iter.get_key());
244         }
245         //console.dump(this.files);
246         
247     }
248         // list files.
249     public void scanDir(string dir, int dp =0 ) 
250     {
251         //dp = dp || 0;
252         //Seed.print("Project.Base: Running scandir on " + dir);
253         if (dp > 5) { // no more than 5 deep?
254             return;
255         }
256         // this should be done async -- but since we are getting the proto up ...
257         
258         var subs = new GLib.List<string>();;            
259         var f = File.new_for_path(dir);
260         try {
261             var file_enum = f.enumerate_children(GLib.FileAttribute.STANDARD_DISPLAY_NAME, GLib.FileQueryInfoFlags.NONE, null);
262             
263              
264             FileInfo next_file; 
265             while ((next_file = file_enum.next_file(null)) != null) {
266                 var fn = next_file.get_display_name();
267         
268                  
269                 //console.log('trying ' + dir + '/' + fn);
270                 
271                 if (fn[0] == '.') { // skip hidden
272                     continue;
273                 }
274                 
275                 if (FileUtils.test(dir  + "/" + fn, GLib.FileTest.IS_DIR)) {
276                     subs.append(dir  + "/" + fn);
277                     continue;
278                 }
279                 
280                 if (!Regex.match_simple("\\.bjs$", fn)) {
281                     continue;
282                 }
283                 /*
284                 var parent = "";
285                 //if (dp > 0 ) {
286                 
287                 var sp = dir.split("/");
288                 var parent = "";
289                 for (var i = 0; i < sp.length; i++) {
290                     
291                 }
292                 
293                 /*
294                 sp = sp.splice(sp.length - (dp +1), (dp +1));
295                 parent = sp.join('.');
296                 
297                 
298                 if (typeof(_this.files[dir  + '/' + fn]) != 'undefined') {
299                     // we already have it..
300                     _this.files[dir  + '/' + fn].parent = parent;
301                     return;
302                 }
303                 */
304                 var xt = this.xtype;
305                 JsRender.JsRender.factory(xt,this, dir + "/" + fn);
306                 // parent ?? 
307                 
308                  
309             }
310         } catch (Error e) {
311             print("Project::scanDirs failed");
312         }
313         for (var i = 0; i < subs.length(); i++) {
314             
315              this.scanDir(subs.nth_data(i), dp+1);
316         }
317         
318     }
319       
320 }
321
322