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