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