Fix #5829 - Messing around with flutter API
[roobuilder] / src / Application.vala
1
2  
3         public class AppSettings : Object
4         {
5
6                 
7                 
8                 // what are we going to have as settings?
9                 public string roo_html_dir { get; set; }
10
11                 public AppSettings()
12                 {
13                         this.notify.connect(() => {
14                                 this.save();
15                         });
16                 }
17
18                 public static AppSettings factory()
19                 {
20                          
21                         var setting_file = BuilderApplication.configDirectory() + "/builder.settings";
22                         
23                         if (!FileUtils.test(setting_file, FileTest.EXISTS)) {
24                                  return new AppSettings();
25                         }
26                         string data; 
27                         FileUtils.get_contents(setting_file, out data);
28                         return Json.gobject_from_data (typeof (AppSettings), data) as AppSettings;
29                 }
30                 public void save()
31                 {
32                         var dirname = GLib.Environment.get_home_dir() + "/.Builder";
33                         var setting_file = dirname + "/builder.settings";
34                         string data = Json.gobject_to_data (this, null);
35                         print("saving application settings\n");
36                         FileUtils.set_contents(setting_file,   data);
37                 }
38
39                 
40         }
41         
42         
43         public static BuilderApplication application = null;
44         
45         public class BuilderApplication : Gtk.Application
46         {
47                 
48                 // options - used when builder is run as a compiler
49                 // we have to spawn ourself as a compiler as just running libvala
50                 // as a task to check syntax causes memory leakage..
51                 // 
52                 const OptionEntry[] options = {
53                 
54                         
55                         { "project", 0, 0, OptionArg.STRING, ref opt_compile_project, "Compile a project", null },
56                         { "target", 0, 0, OptionArg.STRING, ref opt_compile_target, "Target to build", null },
57                         { "skip-file", 0, 0, OptionArg.STRING, ref opt_compile_skip ,"For test compiles do not add this (usually used in conjunction with add-file ", null },
58                         { "add-file", 0, 0, OptionArg.STRING, ref opt_compile_add, "Add this file to compile list", null },
59                         { "output", 0, 0, OptionArg.STRING, ref opt_compile_output, "output binary file path", null },
60                         { "debug", 0, 0, OptionArg.NONE, ref opt_debug, "Show debug messages", null },
61                         { "pull-resources", 0, 0, OptionArg.NONE, ref opt_pull_resources, "Fetch the online resources", null },                 
62             
63             // some testing code.
64             { "list-projects", 0, 0,  OptionArg.NONE, ref opt_list_projects, "List Projects", null },
65             { "list-files", 0, 0,  OptionArg.NONE, ref  opt_list_files, "List Files (in a project", null},
66             { "bjs", 0, 0, OptionArg.STRING, ref opt_bjs_compile, "convert bjs file", null },
67             { "bjs-target", 0, 0, OptionArg.STRING, ref opt_bjs_compile_target, "convert bjs file to tareet  : vala / js", null },
68             { "test", 0, 0, OptionArg.STRING, ref opt_test, "run a test use 'help' to list the available tests", null },
69             
70                         { null }
71                 };
72                 public static string opt_compile_project;
73                 public static string opt_compile_target;
74                 public static string opt_compile_skip;
75                 public static string opt_compile_add;
76                 public static string opt_compile_output;
77         public static string opt_bjs_compile;
78         public static string opt_bjs_compile_target;
79         public static string opt_test;        
80                 public static bool opt_debug = false;
81                 public static bool opt_list_projects = false;
82                 public static bool opt_list_files = false;
83                 public static bool opt_pull_resources = false;          
84                 
85                 public static string _self;
86                 
87                 enum Target {
88                     INT32,
89                     STRING,
90                     ROOTWIN
91                 }
92
93
94                 public const Gtk.TargetEntry[] targetList = {
95                     { "INTEGER",    0, Target.INT32 },
96                     { "STRING",     0, Target.STRING },
97                     { "application/json",     0, Target.STRING },                       
98                     { "text/plain", 0, Target.STRING },
99                     { "application/x-rootwindow-drop", 0, Target.ROOTWIN }
100                 };
101                 public AppSettings settings = null;
102
103         
104                 public BuilderApplication (  string[] args)
105                 {
106                         
107                         _self = FileUtils.read_link("/proc/self/exe");
108                         GLib.debug("SELF = %s", _self);
109                         
110                         Object(
111                                application_id: "org.roojs.app-builder",
112                                 flags: ApplicationFlags.FLAGS_NONE
113                         );
114                                          
115                         configDirectory();
116                         this.settings = AppSettings.factory();  
117                         var opt_context = new OptionContext ("Application Builder");
118                         
119                         try {
120                                 opt_context.set_help_enabled (true);
121                                 opt_context.add_main_entries (options, null);
122                                 opt_context.parse (ref args);
123                                  
124                                 
125                         } catch (OptionError e) {
126                                 stdout.printf ("error: %s\n", e.message);
127                                 stdout.printf ("Run '%s --help' to see a full list of available command line options.\n %s", 
128                                                          args[0], opt_context.get_help(true,null));
129                                 GLib.Process.exit(Posix.EXIT_FAILURE);
130                                  
131                         }
132                         this.initDebug();
133                         this.runTests();                        
134                         this.pullResources();
135                         
136                 Project.Project.loadAll();
137                         this.listProjects();
138                         var cur_project = this.compileProject();
139                         this.listFiles(cur_project);
140                         this.compileBjs(cur_project);
141                         this.compileVala();
142
143                 }
144
145
146                 
147                 public static BuilderApplication  singleton(  string[] args)
148                 {
149                         if (application==null) {
150                                 application = new BuilderApplication(  args);
151  
152                         
153                         }
154                         return application;
155                 }
156
157                 
158                 public static string configDirectory()
159                 {
160                         var dirname = GLib.Environment.get_home_dir() + "/.Builder";
161                 
162                         if (!FileUtils.test(dirname,FileTest.IS_DIR)) {
163                                 var dir = File.new_for_path(dirname);
164                                 dir.make_directory();    
165                         }
166                         if (!FileUtils.test(dirname + "/resources",FileTest.IS_DIR)) {
167                                 var dir = File.new_for_path(dirname + "/resources");
168                                 dir.make_directory();    
169                         }
170
171                 
172                         return dirname;
173                 }
174                 
175                 
176                 // --------------- non static...
177                 
178                 void initDebug() 
179                 {
180                 
181                         if (BuilderApplication.opt_debug  || BuilderApplication.opt_compile_project == null) {
182                                 GLib.Log.set_handler(null, 
183                                         GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING, 
184                                         (dom, lvl, msg) => {
185                                         print("%s: %s\n", dom, msg);
186                                 });
187                         }
188                         
189                 
190                 }
191                 void listProjects()
192                 {
193                         if (!BuilderApplication.opt_list_projects) {
194                                 return;
195                         }
196                         print("Projects\n %s\n", Project.Project.listAllToString());
197                         GLib.Process.exit(Posix.EXIT_SUCCESS);
198                 
199                 }
200                 Project.Project? compileProject()
201                 {
202                         
203                         if (BuilderApplication.opt_compile_project == null) {
204                                 return null;
205                          }
206                         Project.Project cur_project = null;
207                         cur_project = Project.Project.getProjectByHash( BuilderApplication.opt_compile_project);
208                         
209                         if (cur_project == null) {
210                                 GLib.error("invalid project %s, use --list-projects to show project ids",BuilderApplication.opt_compile_project);
211                         }
212                         cur_project.scanDirs();
213                                 
214                         return cur_project;
215                 
216                 }
217                 void listFiles(Project.Project? cur_project)
218                 {
219                         if (!BuilderApplication.opt_list_files) {
220                                 return;
221                         }
222                         if (cur_project == null) {
223                                 GLib.error("missing project, use --project to select which project");
224                         }
225                         print("Files for %s\n %s\n", cur_project.name, cur_project.listAllFilesToString());
226                         GLib.Process.exit(Posix.EXIT_SUCCESS);
227                         
228                 }
229                 void compileBjs(Project.Project? cur_project)
230                 {
231                         if (BuilderApplication.opt_bjs_compile == null) {
232                                 return;
233                         }
234                         if (cur_project == null) {
235                                 GLib.error("missing project, use --project to select which project");
236                         }       
237                         var file = cur_project.getByName(BuilderApplication.opt_bjs_compile);
238                         if (file == null) {
239                                 GLib.error("missing file %s in project %s", BuilderApplication.opt_bjs_compile, cur_project.name);
240                         }
241                         //BuilderApplication.compileBjs();
242                         file.loadItems();
243                         var str = file.toSourceCode();
244                           
245                           
246                         if (!BuilderApplication.opt_debug) {
247                                 print("%s", str);
248                                 GLib.Process.exit(Posix.EXIT_SUCCESS);
249                         }
250                         
251                         // dump the node tree
252                         file.tree.dumpProps();
253                         
254                         
255                         var str_ar = str.split("\n");
256                         for(var i =0;i<str_ar.length;i++) {
257                                 var node = file.tree.lineToNode(i+1);
258                                 var prop = node == null ? null : node.lineToProp(i+1);
259                                 print("%d: %s   :  %s\n", 
260                                         i+1, 
261                                         node == null ? "......"  : (prop == null ? "????????" : prop),
262                                         str_ar[i]
263                                 );
264                         }
265                         
266                         GLib.Process.exit(Posix.EXIT_SUCCESS);
267                 }
268                 
269                 void compileVala()
270                 {
271                         if (BuilderApplication.opt_compile_target == null) {
272                                 return;
273                         }
274                         Palete.ValaSourceCompiler.buildApplication();
275                 
276                         GLib.Process.exit(Posix.EXIT_SUCCESS);
277         
278                 }
279                 void pullResources()
280                 {
281                         if (!opt_pull_resources) {
282                                 return;
283                         }
284                         var loop = new MainLoop();
285                         Resources.singleton().updateProgress.connect((p,t) => {
286                                 print("Got %d/%d", (int) p,(int)t);
287                                 if (p == t) {
288                                         loop.quit();
289                                 }
290                         });
291                         Resources.singleton().fetchStart();     
292                         loop.run();
293                         GLib.Process.exit(Posix.EXIT_SUCCESS);
294                 }
295                 
296                 
297                 void runTests()
298                 {
299                         if (opt_test == null) {
300                                 return;
301                         }
302                         switch(opt_test) {
303                                 case "help":
304                                         print("""
305 help             - list available tests
306 flutter-project  - create a flutter project in /tmp/test-flutter
307 """);           
308                                         break;
309                                 case "flutter-project":
310                                 Project.Project.loadAll();
311                                         var p =   Project.Project.factory("Flutter", "/tmp/test-flutter");
312                                         var pa = p.palete as Palete.Flutter;
313                                         pa.dumpusage();
314                                          var ar = pa.getChildList("material.Scaffold");
315                                         GLib.debug("childlist for material.Scaffold is %s", 
316                                                 string.joinv( "\n-- ", ar)
317                                         );
318                                         ar = pa.getDropList("material.MaterialApp");
319                                         GLib.debug("droplist for material.MaterialApp is %s", 
320                                                 string.joinv( "\n-- ", ar)
321                                         );
322                                         break;
323                                         
324                                 default:
325                                         print("Invalid test\n");
326                                         break;
327
328
329                         }
330                         GLib.Process.exit(Posix.EXIT_SUCCESS);          
331                 }
332                 
333          
334         } 
335
336  
337
338