meson.build.o7QLX02
[roobuilder] / src / Project / Gtk.vala
1 //<Script type="text/javascript">
2 /**
3  * Gtk projects - normally vala based now..
4  * 
5  * should have a few extra features..
6  * 
7  * like:
8  *   compile flags etc..
9  *   different versions (eg. different files can compile different versions - eg. for testing.
10  *   
11  * If we model this like adjuta - then we would need a 'project' file that is actually in 
12  * the directory somewhere... - and is revision controlled etc..
13  * 
14  * builder.config ??
15  * 
16  * 
17  * 
18  * 
19  */
20  
21
22 namespace Project 
23 {
24  
25         
26
27         public class Gtk : Project
28         {
29                 /**
30                 * Gir cache - it's local as we might want clear it if we modify the packages...
31                 *
32                 */
33                 // public Gee.HashMap<string,Palete.GirObject> gir_cache = null; ?? 
34                 
35                 public bool gir_cache_loaded = false;  /// set this to false to force a relaod of vapi's?
36                 
37                 // these are loaded / created by the palete.. but are project specific.
38                 public Gee.HashMap<string,Gee.ArrayList<string>>? dropList = null;  
39             public Gee.HashMap<string,Gee.ArrayList<string>> child_list_cache;   // what child can on on what node
40                 public Gee.HashMap<string,Gee.ArrayList<string>> child_list_cache_props; // what child can go on what node (with properties included)
41                 
42              public string compile_flags = ""; // generic to all.       
43                 public Gee.ArrayList<string> packages; // list of vapi's that are used by this project. 
44                  
45                 //pblic Gee.ArrayList<string> hidden; // list of dirs to be hidden from display...
46                 
47                 public GtkValaSettings? active_cg = null;
48                 public Gee.HashMap<string,GtkValaSettings> compilegroups;
49                 public Meson meson;
50                 
51                 
52                 public Palete.Gtk gpalete {
53                         get {
54                                 return (Palete.Gtk) this.palete;
55                         }
56                         set {}
57                 }
58                  
59                 
60                 public Gtk(string path) {
61                   
62                   
63                         base(path);
64                         
65                         this.initChildCache();
66                    
67                         this.palete = new Palete.Gtk(this);
68                         
69                         this.gir_cache = new Gee.HashMap<string,Palete.GirObject>();
70                         this.xtype = "Gtk";
71                         //var gid = "project-gtk-%d".printf(gtk_id++);
72                         //this.id = gid;
73                         this.packages = new Gee.ArrayList<string>();
74                         //this.hidden = new Gee.ArrayList<string>();
75                         this.compilegroups = new  Gee.HashMap<string,GtkValaSettings>();
76                         this.meson = new Meson(this);
77                 
78                 }
79                 
80                 public  void initChildCache()
81                 {
82                         this.child_list_cache = new Gee.HashMap<string,Gee.ArrayList<string>>();
83                         this.child_list_cache_props = new Gee.HashMap<string,Gee.ArrayList<string>>();
84                         this.dropList = null;
85                 }
86                 
87                 
88                 
89                 public override void loadJson(Json.Object obj)  
90                 {
91                         // load a builder.config JSON file.
92                         // 
93                         this.compilegroups = new  Gee.HashMap<string,GtkValaSettings>();
94                         
95                         if (obj.has_member("packages")) {
96                                 this.packages = this.readArray(obj.get_array_member("packages"));
97                         }
98                         if (obj.has_member("compiler_flags")) {
99                                 this.compile_flags = obj.get_string_member("compile_flags");
100                         }
101                         
102                          if (!obj.has_member("compilegroups") || obj.get_member("compilegroups").get_node_type () != Json.NodeType.ARRAY) {
103                                 // make _default_ ?
104                                  return;
105                          }
106                         
107                         //this.hidden = this.readArray(obj.get_array_member("hidden"));
108                         var ar = obj.get_array_member("compilegroups");
109                         for(var i= 0;i<ar.get_length();i++) {
110                                 var el = ar.get_object_element(i);
111                                 var vs = new GtkValaSettings.from_json(this,el);
112                 if (vs == null) {
113                     print("problem loading json file");
114                     continue;
115                 }
116                                  
117                                 this.compilegroups.set(vs.name,vs);
118                         }
119                                                 
120                          
121                         //GLib.debug("%s\n",this.configToString ());
122                         
123                 }
124                 public override void saveJson(Json.Object obj)
125                 {
126                         var ar = new Json.Array();
127                         foreach(var cg in this.compilegroups.values) {
128                                  ar.add_object_element(cg.toJson());
129                         }
130                         obj.set_array_member("compilegroups", ar);
131                         
132                         obj.set_string_member("compile_flags", this.compile_flags);
133                         var par = new Json.Array();
134                         foreach(var p in this.packages) {
135                                 par.add_string_element(p);
136                         }
137                         obj.set_array_member("packages", par);
138                         //var hi = new Json.Array();
139                         //foreach(var p in this.hidden) {
140                         //      hi.add_string_element(p);
141                         //}
142                         //obj.set_array_member("hidden", hi);
143                         
144                         this.gir_cache_loaded = false; // force reload of the cache if we change the packages.
145                         this.gpalete.loaded = false;
146                         this.initChildCache();
147                         this.gir_cache = null;
148                 }
149                 
150                 public override void onSave()
151                 {
152                         this.meson.save();
153                         var vl = this.language_servers.get("vala");
154                         if (vl != null) {
155                                 vl.initialize_server(); // hopefully better than exit?
156                         }
157                 }
158          
159                 /**
160                  *  perhaps we should select the default in the window somewhere...
161                  */ 
162                 public string firstBuildModule()
163                 {
164                         
165                         foreach(var cg in this.compilegroups.values) {
166                                 return cg.name;
167                                 
168                         }
169                         return "";
170                          
171                 }
172                 public string firstBuildModuleWith(JsRender.JsRender file)
173                 {
174                         foreach(var cg in this.compilegroups.values) {
175                                  if (cg.has_file(file)) {
176                                         return cg.name;
177                                  }
178                                  
179                                  
180                         }
181                         return this.firstBuildModule();
182                          
183                 }
184                 
185                 public void loadVapiIntoStore(GLib.ListStore ls) 
186                 {
187                         ls.remove_all();
188     
189                          
190                         var pal = (Palete.Gtk) this.palete;
191                         var pkgs = pal.packages(this);
192                         foreach (var p in pkgs) {
193                                 ls.append(new VapiSelection(this.packages, p));
194                         }
195                         
196                 }
197                 
198                 public void loadTargetsIntoStore(GLib.ListStore ls) 
199                 {
200                         ls.remove_all();
201                         foreach(var cg in this.compilegroups.values) {
202                                 ls.append(cg);
203                         }
204                 }
205                 
206                  
207                 
208                  
209                 
210                 public string[] vapidirs()
211                 {
212                         return this.pathsMatching("vapi", false);
213                 }
214                 
215                 
216                  
217                 public override Palete.LanguageClient getLanguageServer(string lang)
218                 {
219                         if (this.language_servers.has_key(lang)) {
220                                 return this.language_servers.get(lang);
221                         }
222                         switch( lang ) {
223                                 case "vala":
224                                         var ls = new Palete.LanguageClientVala(this);
225                                         ls.log.connect((act, msg) => {
226                                                 //GLib.debug("log %s: %s", act.to_string(), msg);
227                                                 BuilderApplication.showSpinnerLspLog(act,msg);
228                                         });
229                                         this.language_servers.set(lang, ls);
230                                         break;
231                                 default :
232                                          return this.language_servers.get("dummy");
233                                          
234                                 }
235                                 return this.language_servers.get(lang);
236                 
237                 }
238                  
239                  
240                  
241                  
242                  
243                  
244                  // ------------------  new project stufff
245                 public override void initialize()
246                 {
247                         string[] dirs = {
248                                 "src",
249                                 "src/ui"
250                                 // ?? docs ?
251                                 //   
252                         };
253                         
254                         
255                         string[] vapis = {
256                                 "gtk4",
257                                 "gee-0.8",
258                                 "gio-2.0",
259                                  
260                                 "glib-2.0",
261                                 "gobject-2.0",
262                                  
263                                 // "json-glib-1.0",
264                                  
265                                 //"libadwaita-1",
266                                 //"libxml-2.0",
267                                 "posix"
268                                  
269  
270                         };
271                         for(var i = 0;  i < dirs.length; i++) {
272                                 this.makeProjectSubdir( dirs[i]);
273                         }
274                         for(var i = 0;  i < vapis.length; i++) {
275                                 this.packages.add(vapis[i]);
276                         
277                         }
278                         // create/// some dummy files?
279                         // application
280                         
281                         this.makeMain();
282                         this.makeApplication();
283                         this.makeWindow();
284                         this.makeGitIgnore();
285
286                         var cg =  new GtkValaSettings(this, this.name);
287                         this.compilegroups.set(this.name, cg);
288                         cg.sources.add("src/Main.vala");
289                         cg.sources.add("src/%sApplication.vala".printf(this.name));
290                         cg.sources.add("src/ui/ui.Window.bjs");
291                         // rescan... not needed as it get's selected after initialization.
292                         this.load();
293                         var fn = this.getByPath(this.path + "/src/ui/ui.Window.bjs");
294                         try {
295                                 fn.loadItems();
296                         } catch (GLib.Error e) { } // do nothing?
297                         
298                         fn.save();
299                         
300                         
301                         
302                 }
303                 
304                 
305                 void makeTemplatedFile(string name, string[] str, string replace) 
306                 {
307                         var o = "";
308                         for(var i=0;i< str.length;i++) {
309                                 o += str[i].replace("%s", replace) + "\n";
310                         }
311                         this.writeFile(name, o);
312                 }
313                 public void writeFile(string name, string o) 
314                 {
315                         var f = GLib.File.new_for_path(this.path + "/" + name);
316                         try {
317                                 var data_out = new GLib.DataOutputStream( f.replace(null, false, GLib.FileCreateFlags.NONE, null) );
318                                 data_out.put_string(o, null);
319                                 data_out.close(null);
320                         } catch (GLib.Error e) {
321                                 GLib.debug("Error writing file %s", e.message);
322                         }
323                         
324                 } 
325                 
326                 void makeMain()
327                 {
328                         string[] str = {
329                                 "int main (string[] args)",
330                                 "{",
331                                 "       var app = new  %sApplication(  args);",
332                                 "       Gtk.init ();",
333                                 "       GLib.Log.set_always_fatal(LogLevelFlags.LEVEL_ERROR ); ",
334                                 "       app.activate.connect(() => {",
335                                 "               var w = new ui.Window();",   // ?? main window as UI window?
336                                 "               w.el.application  = app;",
337                                 "               w.ref();",
338                                 "               w.show();",
339                                 "       });",
340                                 "       var ret = app.run(args);",
341                                 "       return ret; ",
342                                 "}"
343                         };
344                         this.makeTemplatedFile("src/Main.vala", str, this.name); // fixme name needs to be code friendly!
345                 }
346                 void makeApplication()
347                 {
348                         string[] str = {
349                         
350                         
351                                 "public class %sApplication : Gtk.Application",
352                                 "{",
353                                 "       public %sApplication (string[] args) ",
354                                 "       {",
355                                 "               Object(",
356                                 "                       application_id: \"org.roojs.%s\",",
357                                 "                       flags: ApplicationFlags.FLAGS_NONE",
358                                 "               );",
359                                 "       }",
360                                 "}"
361                         };
362                                 
363                         
364                         this.makeTemplatedFile("src/%sApplication.vala".printf(this.name), str, this.name); // fixme name needs to be code friendly!
365                 }
366
367                 
368                 void makeWindow()
369                 {
370                         this.writeFile("src/ui/ui.Window.bjs", """{
371  "build_module" : "",
372  "items" : [
373   {
374    "$ xns" : "Gtk",
375    "| void show" : "() { this.el.show(); }",
376    "items" : [
377     {
378      "$ xns" : "Gtk",
379      "* prop" : "child",
380      "Gtk.Orientation orientation" : "Gtk.Orientation.HORIZONTAL",
381      "int spacing" : 0,
382      "items" : [
383       {
384        "$ xns" : "Gtk",
385        "string label" : "Hello World",
386        "xtype" : "Label"
387       }
388      ],
389      "xtype" : "Box"
390     }
391    ],
392    "xtype" : "Window"
393   }
394  ],
395  "name" : "ui.Window",
396  "gen_extended" : false
397 }
398 """);
399         }
400         void makeGitIgnore()
401         {
402                         this.writeFile(".gitignore", """
403 build/
404 """);
405         }
406                         
407                 
408                  public override void   initDatabase()
409                 {
410                      // nOOP
411                 }
412         }
413          
414    
415 }