e704ae920e10a3fd998105b9c7777c413bd1ec92
[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         class PackerRun : Object 
24 #else
25         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                 void parseArgs(string[] args)
96                 {
97                         GLib.OptionEntry[] options       = {
98                                 OptionEntry() {
99                                         long_name = "jsfile",
100                                         short_name = 'f',
101                                         flags = 0,
102                                         arg =  OptionArg.FILENAME_ARRAY,
103                                         arg_data = &opt_files,
104                                         description = "add a file to compile",
105                                         arg_description = null
106                                 },
107                                 OptionEntry() {
108                                         long_name = "target",
109                                         short_name = 't',
110                                         flags = 0,
111                                         arg =  OptionArg.STRING,
112                                         arg_data = &opt_target,
113                                         description = "Target File to write (eg. roojs.js)",
114                                         arg_description = null
115                                 },
116                                 OptionEntry() {
117                                         long_name = "debug-target",
118                                         short_name = 'T',
119                                         flags = 0,
120                                         arg =  OptionArg.STRING,
121                                         arg_data = &opt_debug_target,
122                                         description = "Target File to write debug code (eg. roojs-debug.js)",
123                                         arg_description = null
124                                 },
125                                 //{ "tmpdir", 'm', 0, OptionArg.STRING, ref opt_tmpdir, "Temporary Directory to use (defaults to /tmp)", null },
126                                 
127                                 OptionEntry() {
128                                         long_name = "basedir",
129                                         short_name = 'b',
130                                         flags = 0,
131                                         arg =  OptionArg.STRING,
132                                         arg_data = &opt_basedir,
133                                         description = "Base directory (where the files listed in index files are located.)",
134                                         arg_description = null
135                                 }, 
136
137                                 OptionEntry() {
138                                         long_name = "index-files",
139                                         short_name = 'i',
140                                         flags = 0,
141                                         arg =  OptionArg.FILENAME_ARRAY,
142                                         arg_data = &opt_files_from,
143                                         description = "files that contain listing of files to compile",
144                                         arg_description = null
145                                 }, 
146
147                                 OptionEntry() {
148                                         long_name = "keep-whitespace",
149                                         short_name = 'w',
150                                         flags = 0,
151                                         arg =  OptionArg.NONE,
152                                         arg_data = &opt_keep_whitespace,
153                                         description = "Keep whitespace",
154                                         arg_description = null
155                                 }, 
156                          
157                                 OptionEntry() {
158                                         long_name = "skip-scope",
159                                         short_name = 's',
160                                         flags = 0,
161                                         arg =  OptionArg.NONE,
162                                         arg_data = &opt_skip_scope,
163                                         description = "Skip scope parsing and variable replacement",
164                                         arg_description = null
165                                 }, 
166                                 OptionEntry() {
167                                         long_name = "debug",
168                                         short_name = 'D',
169                                         flags = 0,
170                                         arg =  OptionArg.NONE,
171                                         arg_data = &opt_debug,
172                                         description = "Show debug messages",
173                                         arg_description = null
174                                 }, 
175
176                                 OptionEntry() {
177                                         long_name = "dump-tokens",
178                                         short_name = 'k',
179                                         flags = 0,
180                                         arg =  OptionArg.NONE,
181                                         arg_data = &opt_dump_tokens,
182                                         description = "Dump the tokens from a file",
183                                         arg_description = null
184                                 }, 
185
186                                 OptionEntry() {
187                                         long_name = "clean-cache",
188                                         short_name = 'c',
189                                         flags = 0,
190                                         arg =  OptionArg.NONE,
191                                         arg_data = &opt_clean_cache,
192                                         description = "Clean up the cache after running (slower)",
193                                         arg_description = null
194                                 }, 
195
196
197                         // fixme -- keepwhite.. cleanup 
198                         
199                         // documentation options
200                         // usage: roojspacker --basedir roojs1 \
201                         //       --doc-target roojs1/docs \
202                         //       --index-files roojs1/buildSDK/dependancy_core.txt  \
203                         //       --index-files roojs1/buildSDK/dependancy_ui.txt  \
204                         //       --index-files roojs1/buildSDK/dependancy_bootstrap.txt  \
205                         //       --doc-template-dir \
206                         
207                                 OptionEntry() {
208                                         long_name = "doc-target",
209                                         short_name = 'd',
210                                         flags = 0,
211                                         arg =  OptionArg.STRING,
212                                         arg_data = &opt_doc_target,
213                                         description = "Clean up the cache after running (slower)",
214                                         arg_description = null
215                                 }, 
216
217                                 OptionEntry() {
218                                         long_name = "doc-template-dir",
219                                         short_name = 'p',
220                                         flags = 0,
221                                         arg =  OptionArg.STRING,
222                                         arg_data = &opt_doc_template_dir,
223                                         description = "Documentation Directory target",
224                                         arg_description = null
225                                 },                      
226
227
228                                 OptionEntry() {
229                                         long_name = "doc-private",
230                                         short_name = 'P',
231                                         flags = 0,
232                                         arg =  OptionArg.NONE,
233                                         arg_data = &opt_doc_include_private,
234                                         description = "Document Private functions",
235                                         arg_description = null
236                                 }
237                         };
238                         var opt_context = new OptionContext ("JSDOC Packer");
239
240                         try {
241                                 opt_context.set_help_enabled (true);
242                                 opt_context.add_main_entries (options, null);
243                                 if (!opt_context.parse ( ref args)) {
244                                         print("options parse error");
245                                         GLib.Process.exit(Posix.EXIT_FAILURE);
246                                 }
247
248
249                                  
250
251                         } catch (OptionError e) {
252                                 stdout.printf ("error: %s\n", e.message);
253                                 stdout.printf ("Run '%s --help' to see a full list of available command line options.\n %s", 
254                                                          args[0], opt_context.get_help(true,null));
255                                 GLib.Process.exit(Posix.EXIT_FAILURE);
256                                  
257                         }
258                         
259                                          
260                          
261                 }
262                 public void  runPack()
263                 {       
264                         // what's required...
265                         if (opt_debug) {
266                                 GLib.Log.set_handler(null, 
267                                         GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING, 
268                                         (dom, lvl, msg) => {
269                                         print("%s: %s\n", dom, msg);
270                                 });
271                         }
272                         
273                         
274                 
275   
276   
277                          
278                         // set the base directory...
279                         var curdir = Environment.get_current_dir() + Path.DIR_SEPARATOR_S;
280                         if (opt_basedir == null) {
281
282                                 opt_real_basedir = curdir;
283                         } else if (opt_basedir[0] == '/') {     
284                                 opt_real_basedir  = opt_basedir;
285                         } else {
286                                 opt_real_basedir  = curdir + opt_basedir;
287                         }
288                         // suffix a slash..
289                         if (opt_real_basedir [opt_real_basedir .length-1].to_string() != Path.DIR_SEPARATOR_S) {
290                                 opt_real_basedir  += Path.DIR_SEPARATOR_S;
291                         }
292                         
293                         GLib.debug("real_base_dir  = '%s' : opt_basedir ='%s'\n", opt_real_basedir , opt_basedir);
294                         
295                         
296                         if (opt_files == null && opt_files_from == null) {
297                                 GLib.error("You must list some files with -f or -i to compile - see --help for more details");
298                                 GLib.Process.exit(1);
299                         }
300                         
301                         
302                                 // now run the Packer...
303                         var p = new Packer(     this );
304                         
305                         
306                         if (opt_files != null) {
307                          
308                                 foreach (var  f in opt_files) {
309                                         GLib.debug("Adding File %s", f);
310                                         p.loadFile(f);
311                                 }
312                         }  
313                         if (opt_files_from != null) {
314                          
315                                 foreach (var  f in opt_files_from) {
316                                         GLib.debug("Adding File %s", f);
317                                         p.loadSourceIndex(f);
318                                 }
319                         }  
320                         
321                         var run_pack = false;
322                         if (opt_target != null || opt_debug_target != null || opt_dump_tokens) {
323                         
324                                 p.pack( opt_target == null ? "" : opt_target ,
325                                                 opt_debug_target == null ? "" :  opt_debug_target );
326                         
327                         if (p.outstr.length > 0 ) {
328                                         stdout.printf ("%s", p.outstr);
329                                 }
330                 }
331                 if (opt_doc_target != null) {
332                         //var d = new DocBuilder(p);
333                 }
334                 
335                 
336                 
337                 
338                 }        
339                 
340         }
341         
342 }