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