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