Fix #6894 - debugging generator
[roojspacker] / roojspacker / PackerRun.vala
1
2 /** 
3
4   THIS IS THE ENTRY POINT...
5
6   the application
7   -- in theory this code  can be used as a library... but this is the standard command line version...
8   
9   
10   valac  --vapidir=/usr/share/vala/vapi 
11      --vapidir=/usr/share/vala/vapi 
12     --vapidir=/usr/share/vala-0.30/vapi 
13         --thread  -g  
14       JSDOC/Lang.vala JSDOC/TextStream.vala JSDOC/TokenReader.vala JSDOC/Token.vala JSDOC/TokenStream.vala JSDOC/Packer.vala 
15       JSDOC/Collapse.vala JSDOC/ScopeParser.vala JSDOC/Scope.vala JSDOC/Identifier.vala JSDOC/CompressWhite.vala 
16        JSDOC/PackerRun.vala --pkg glib-2.0 --pkg gee-1.0 --pkg gio-2.0 --pkg posix -o /tmp/jspack --target-glib=2.32  -X -lm
17
18   
19 */
20
21 JSDOC.PackerRun _PackerRun;
22
23 namespace JSDOC
24 {
25         // --------------- <<<<<<< <MAIN HERE....
26         
27 #if HAVE_OLD_GLIB
28         public class PackerRun : Object 
29 #else
30         public class PackerRun : Application    
31 #endif
32         {
33                 public  string opt_target = null;
34                 public string opt_debug_target = null;
35 //              public  string opt_tmpdir = null;
36                 private  string opt_basedir = null;
37                 
38                 /**
39                 * @cfg baseDir -- prefix the files listed in indexfiles with this.
40                 */
41                  
42                 public  string opt_real_basedir = null; // USE this one it's calcuated based on current path..
43                 
44                 public  string opt_doc_target = null;
45                 public  string opt_doc_template_dir = null;
46                 public  bool opt_doc_include_private = false;           
47                 public  bool opt_doc_dump_tree = false;
48                                 
49                 [CCode (array_length = false, array_null_terminated = true)]
50                 private string[]? opt_files = null;
51                 [CCode (array_length = false, array_null_terminated = true)]
52                 private  string[]? opt_files_from = null;
53                  
54                 
55                 public  bool opt_debug = false;
56                 
57                  /**
58                  * @cfg {Boolean} opt_skip_scope (optional) skip Scope parsing and replacement.
59                  *    usefull for debugging...
60                  */
61                 public  bool opt_skip_scope = false;
62                 
63                 /**
64                  * @cfg {Boolean} opt_keep_whitespace (optional) do not remove white space in output.
65                  *    usefull for debugging compressed files.
66                  */
67                 
68                 public  bool opt_keep_whitespace = false;       
69                 
70                         /**
71                  * @cfg {Boolean} opt_dump_tokens (optional) read the first file and dump the tokens.
72                  *    usefull for debugging...
73                  */
74                 
75                 public  bool opt_dump_tokens = false;   
76                 
77                    
78                 /**
79                  * @cfg {Boolean} opt_clean_cache  (optional) clean up temp files after done - 
80                  *    Defaults to false if you set tmpDir, otherwise true.
81                  */
82                 
83                 public  bool opt_clean_cache = true;    
84                 
85                 // not actually an option yet..
86                 
87                 public  string opt_doc_ext = "html";
88                 
89                 public static PackerRun singleton()
90                 {
91                         if (_PackerRun == null) {
92                                 _PackerRun = new PackerRun();
93                         }
94                         return _PackerRun;
95                 }
96   
97                 public PackerRun ()
98                 {
99 #if !HAVE_OLD_GLIB              
100                         Object(
101                             application_id: "org.roojs.jsdoc.packerrun",
102                                 flags: ApplicationFlags.HANDLES_COMMAND_LINE 
103                         );
104 #endif          
105                         
106
107                 }
108                 
109                 
110                 public void parseArgs(string[] args)
111                 {
112                         GLib.OptionEntry[] options       = {
113                                 OptionEntry() {
114                                         long_name = "jsfile",
115                                         short_name = 'f',
116                                         flags = 0,
117                                         arg =  OptionArg.FILENAME_ARRAY,
118                                         arg_data = &opt_files,
119                                         description = "add a file to compile",
120                                         arg_description = null
121                                 },
122                                 OptionEntry() {
123                                         long_name = "target",
124                                         short_name = 't',
125                                         flags = 0,
126                                         arg =  OptionArg.STRING,
127                                         arg_data = &opt_target,
128                                         description = "Target File to write (eg. roojs.js)",
129                                         arg_description = null
130                                 },
131                                 OptionEntry() {
132                                         long_name = "debug-target",
133                                         short_name = 'T',
134                                         flags = 0,
135                                         arg =  OptionArg.STRING,
136                                         arg_data = &opt_debug_target,
137                                         description = "Target File to write debug code (eg. roojs-debug.js)",
138                                         arg_description = null
139                                 },
140                                 //{ "tmpdir", 'm', 0, OptionArg.STRING, ref opt_tmpdir, "Temporary Directory to use (defaults to /tmp)", null },
141                                 /*
142                                 OptionEntry() {
143                                         long_name = "tmpdir",
144                                         short_name = 'm',
145                                         flags = 0,
146                                         arg =  OptionArg.STRING,
147                                         arg_data = &opt_tmpdir,
148                                         description = "Temporary Directory - used by documentation tool?",
149                                         arg_description = null
150                                 }, 
151                                 */
152
153                                 OptionEntry() {
154                                         long_name = "basedir",
155                                         short_name = 'b',
156                                         flags = 0,
157                                         arg =  OptionArg.STRING,
158                                         arg_data = &opt_basedir,
159                                         description = "Base directory (where the files listed in index files are located.)",
160                                         arg_description = null
161                                 }, 
162
163                                 OptionEntry() {
164                                         long_name = "index-files",
165                                         short_name = 'i',
166                                         flags = 0,
167                                         arg =  OptionArg.FILENAME_ARRAY,
168                                         arg_data = &opt_files_from,
169                                         description = "files that contain listing of files to compile",
170                                         arg_description = null
171                                 }, 
172
173                                 OptionEntry() {
174                                         long_name = "keep-whitespace",
175                                         short_name = 'w',
176                                         flags = 0,
177                                         arg =  OptionArg.NONE,
178                                         arg_data = &opt_keep_whitespace,
179                                         description = "Keep whitespace",
180                                         arg_description = null
181                                 }, 
182                          
183                                 OptionEntry() {
184                                         long_name = "skip-scope",
185                                         short_name = 's',
186                                         flags = 0,
187                                         arg =  OptionArg.NONE,
188                                         arg_data = &opt_skip_scope,
189                                         description = "Skip scope parsing and variable replacement",
190                                         arg_description = null
191                                 }, 
192                                 OptionEntry() {
193                                         long_name = "debug",
194                                         short_name = 'D',
195                                         flags = 0,
196                                         arg =  OptionArg.NONE,
197                                         arg_data = &opt_debug,
198                                         description = "Show debug messages",
199                                         arg_description = null
200                                 }, 
201
202                                 OptionEntry() {
203                                         long_name = "dump-tokens",
204                                         short_name = 'k',
205                                         flags = 0,
206                                         arg =  OptionArg.NONE,
207                                         arg_data = &opt_dump_tokens,
208                                         description = "Dump the tokens from a file",
209                                         arg_description = null
210                                 }, 
211
212                                 OptionEntry() {
213                                         long_name = "clean-cache",
214                                         short_name = 'c',
215                                         flags = 0,
216                                         arg =  OptionArg.NONE,
217                                         arg_data = &opt_clean_cache,
218                                         description = "Clean up the cache after running (slower)",
219                                         arg_description = null
220                                 }, 
221
222
223                         // fixme -- keepwhite.. cleanup 
224                         
225                         // documentation options
226                         // usage: roojspacker --basedir roojs1 \
227                         //       --doc-target roojs1/docs \
228                         //       --index-files roojs1/buildSDK/dependancy_core.txt  \
229                         //       --index-files roojs1/buildSDK/dependancy_ui.txt  \
230                         //       --index-files roojs1/buildSDK/dependancy_bootstrap.txt  \
231                         //       --doc-template-dir \
232                         
233                                 OptionEntry() {
234                                         long_name = "doc-target",
235                                         short_name = 'd',
236                                         flags = 0,
237                                         arg =  OptionArg.STRING,
238                                         arg_data = &opt_doc_target,
239                                         description = "Target location for documentation",
240                                         arg_description = null
241                                 }, 
242
243                                 OptionEntry() {
244                                         long_name = "doc-template-dir",
245                                         short_name = 'p',
246                                         flags = 0,
247                                         arg =  OptionArg.STRING,
248                                         arg_data = &opt_doc_template_dir,
249                                         description = "Template directory for documentation",
250                                         arg_description = null
251                                 },                      
252
253
254                                 OptionEntry() {
255                                         long_name = "doc-dump-tree",
256                                         short_name = 'E',
257                                         flags = 0,
258                                         arg =  OptionArg.NONE,
259                                         arg_data = &opt_doc_dump_tree,
260                                         description = "Dump the documenation json tree",
261                                         arg_description = null
262                                 }, 
263                                 
264                                 OptionEntry() {
265                                         long_name = "doc-private",
266                                         short_name = 'P',
267                                         flags = 0,
268                                         arg =  OptionArg.NONE,
269                                         arg_data = &opt_doc_include_private,
270                                         description = "Document Private functions",
271                                         arg_description = null
272                                 }
273                         };
274                         var opt_context = new OptionContext ("JSDOC Packer");
275
276                         try {
277                                 opt_context.set_help_enabled (true);
278                                 opt_context.add_main_entries (options, null);
279                                 if (!opt_context.parse ( ref args)) {
280                                         print("options parse error");
281                                         GLib.Process.exit(Posix.EXIT_FAILURE);
282                                 }
283
284
285                                  
286
287                         } catch (OptionError e) {
288                                 stdout.printf ("error: %s\n", e.message);
289                                 stdout.printf ("Run '%s --help' to see a full list of available command line options.\n %s", 
290                                                          args[0], opt_context.get_help(true,null));
291                                 GLib.Process.exit(Posix.EXIT_FAILURE);
292                                  
293                         }
294                         
295                                          
296                          
297                 }
298                 public void  runPack()
299                 {       
300                         // what's required...
301                         if (opt_debug) {
302                                 GLib.Log.set_handler(null, 
303                                         GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING, 
304                                         (dom, lvl, msg) => {
305                                         print("%s: %s\n", dom, msg);
306                                 });
307                         }
308                         
309                          
310   
311                          
312                         // set the base directory...
313                         var curdir = Environment.get_current_dir() + Path.DIR_SEPARATOR_S;
314                         if (opt_basedir == null) {
315
316                                 opt_real_basedir = curdir;
317                         } else if (opt_basedir[0] == '/') {     
318                                 opt_real_basedir  = opt_basedir;
319                         } else {
320                                 opt_real_basedir  = curdir + opt_basedir;
321                         }
322                         // suffix a slash..
323                         if (opt_real_basedir [opt_real_basedir .length-1].to_string() != Path.DIR_SEPARATOR_S) {
324                                 opt_real_basedir  += Path.DIR_SEPARATOR_S;
325                         }
326                         
327                         GLib.debug("real_base_dir  = '%s' : opt_basedir ='%s'\n", opt_real_basedir , opt_basedir);
328                         
329                         
330                         if (opt_files == null && opt_files_from == null) {
331                                 GLib.error("You must list some files with -f or -i to compile - see --help for more details");
332                                 GLib.Process.exit(1);
333                         }
334                         
335                         
336                                 // initialize the Packer (does not parse anything..)
337                         var p = new Packer(     this );
338                         
339                         
340                         if (opt_files != null) {
341                          
342                                 foreach (var  f in opt_files) {
343                                         GLib.debug("Adding File %s", f);
344                                         p.loadFile(f);  // just adds to list of files to parse (no parsing yet..)
345                                 }
346                         }  
347                         if (opt_files_from != null) {
348                          
349                                 foreach (var  f in opt_files_from) {
350                                         GLib.debug("Adding File %s", f);
351                                         p.loadSourceIndex(f);
352                                 }
353                         }  
354                         
355                         var run_pack = false;
356                         if (opt_target != null || opt_debug_target != null || opt_dump_tokens) {
357                                 // do the actual packing...
358                                 p.pack( opt_target == null ? "" : opt_target ,
359                                                 opt_debug_target == null ? "" :  opt_debug_target );
360                         
361                         if (p.outstr.length > 0 ) {
362                                         stdout.printf ("%s", p.outstr);
363                                 }
364                                 return;
365                 }
366                 if (opt_doc_target != null) {
367                                 // remove trailing /
368                         opt_doc_target = opt_doc_target.has_suffix("/") ? 
369                                         opt_doc_target.substring(0, opt_doc_target.length-1) : opt_doc_target;
370                         var d = new JSDOC.DocBuilder(p);
371                         return;
372                 } 
373                 GLib.error("either select output target or doc output target");
374                 
375                 
376                 
377                 }        
378                 
379         }
380         
381 }