70c5496aa60e96f0f9a772cbaca42939ce5dd2e5
[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                 public   OptionEntry[] options;
85         
86                 public PackerRun ()
87                 {
88 #if !HAVE_OLD_GLIB              
89                         Object(
90                             application_id: "org.roojs.jsdoc.packerrun",
91                                 flags: ApplicationFlags.HANDLES_COMMAND_LINE 
92                         );
93 #endif          
94                         this.options     = {
95                                 OptionEntry() {
96                                         long_name = "jsfile",
97                                         short_name = 'f',
98                                         flags = 0,
99                                         arg =  OptionArg.FILENAME_ARRAY,
100                                         arg_data = &opt_files,
101                                         description = "add a file to compile",
102                                         arg_description = null
103                                 },
104                                 OptionEntry() {
105                                         long_name = "target",
106                                         short_name = 't',
107                                         flags = 0,
108                                         arg =  OptionArg.STRING,
109                                         arg_data = &opt_target,
110                                         description = "Target File to write (eg. roojs.js)",
111                                         arg_description = null
112                                 },
113                                 OptionEntry() {
114                                         long_name = "debug-target",
115                                         short_name = 'T',
116                                         flags = 0,
117                                         arg =  OptionArg.STRING,
118                                         arg_data = &opt_debug_target,
119                                         description = "Target File to write debug code (eg. roojs-debug.js)",
120                                         arg_description = null
121                                 },
122                                 //{ "tmpdir", 'm', 0, OptionArg.STRING, ref opt_tmpdir, "Temporary Directory to use (defaults to /tmp)", null },
123                                 
124                                 OptionEntry() {
125                                         long_name = "basedir",
126                                         short_name = 'b',
127                                         flags = 0,
128                                         arg =  OptionArg.STRING,
129                                         arg_data = &opt_basedir,
130                                         description = "Base directory (where the files listed in index files are located.)",
131                                         arg_description = null
132                                 }, 
133
134                                 OptionEntry() {
135                                         long_name = "index-files",
136                                         short_name = 'i',
137                                         flags = 0,
138                                         arg =  OptionArg.FILENAME_ARRAY,
139                                         arg_data = &opt_files_from,
140                                         description = "files that contain listing of files to compile",
141                                         arg_description = null
142                                 }, 
143
144                                 OptionEntry() {
145                                         long_name = "keep-whitespace",
146                                         short_name = 'w',
147                                         flags = 0,
148                                         arg =  OptionArg.NONE,
149                                         arg_data = &opt_keep_whitespace,
150                                         description = "Keep whitespace",
151                                         arg_description = null
152                                 }, 
153                          
154                                 OptionEntry() {
155                                         long_name = "skip-scope",
156                                         short_name = 's',
157                                         flags = 0,
158                                         arg =  OptionArg.NONE,
159                                         arg_data = &opt_skip_scope,
160                                         description = "Skip scope parsing and variable replacement",
161                                         arg_description = null
162                                 }, 
163
164                         { "keep-whitespace", 'w', 0, OptionArg.NONE, ref opt_keep_whitespace, "Keep whitespace", null },
165                         { "skip-scope", 's', 0, OptionArg.NONE, ref opt_skip_scope, "Skip scope parsing and variable replacement", null },
166                         { "debug", 0, 0, OptionArg.NONE, ref opt_debug, "Show debug messages", null },
167                         { "dump-tokens", 'k', 0, OptionArg.NONE, ref opt_dump_tokens, "Dump the tokens from a file", null },
168                         { "clean-cache", 'c', 0, OptionArg.NONE, ref opt_clean_cache, "Clean up the cache after running (slower)", null },
169
170                         // fixme -- keepwhite.. cleanup 
171                         
172                         // documentation options
173                         // usage: roojspacker --basedir roojs1 \
174                         //       --doc-target roojs1/docs \
175                         //       --index-files roojs1/buildSDK/dependancy_core.txt  \
176                         //       --index-files roojs1/buildSDK/dependancy_ui.txt  \
177                         //       --index-files roojs1/buildSDK/dependancy_bootstrap.txt  \
178                         //       --doc-template-dir\
179                         
180                         
181                         { "doc-target", 'd', 0, OptionArg.STRING, ref opt_doc_target, "Documentation Directory target", null },
182                         { "doc-template-dir", 'p', 0, OptionArg.STRING, ref opt_doc_template_dir, "Documentation Directory target", null },
183                         { "doc-private", 'p', 0, OptionArg.NONE, ref opt_doc_include_private, "Document Private functions", null },                                             
184                         
185                         
186                         
187                         { null }
188                 };
189                                          
190                          
191                 }
192                 public void  run()
193                 {       
194                         // what's required...
195                         if (opt_debug) {
196                                 GLib.Log.set_handler(null, 
197                                         GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING, 
198                                         (dom, lvl, msg) => {
199                                         print("%s: %s\n", dom, msg);
200                                 });
201                         }
202                         
203                         
204                 
205   
206   
207                          
208                         // set the base directory...
209                         var curdir = Environment.get_current_dir() + Path.DIR_SEPARATOR_S;
210                         if (opt_basedir == null) {
211
212                                 opt_real_basedir = curdir;
213                         } else if (opt_basedir[0] == '/') {     
214                                 opt_real_basedir  = opt_basedir;
215                         } else {
216                                 opt_real_basedir  = curdir + opt_basedir;
217                         }
218                         // suffix a slash..
219                         if (opt_real_basedir [opt_real_basedir .length-1].to_string() != Path.DIR_SEPARATOR_S) {
220                                 opt_real_basedir  += Path.DIR_SEPARATOR_S;
221                         }
222                         
223                         GLib.debug("real_base_dir  = '%s' : opt_basedir ='%s'\n", opt_real_basedir , opt_basedir);
224                         
225                         
226                         if (opt_files == null && opt_files_from == null) {
227                                 GLib.error("You must list some files with -f or -i to compile - see --help for more details");
228                                 GLib.Process.exit(1);
229                         }
230                         
231                         
232                                 // now run the Packer...
233                         var p = new Packer(     this );
234                         
235                         
236                         if (opt_files != null) {
237                          
238                                 foreach (var  f in opt_files) {
239                                         GLib.debug("Adding File %s", f);
240                                         p.loadFile(f);
241                                 }
242                         }  
243                         if (opt_files_from != null) {
244                          
245                                 foreach (var  f in opt_files_from) {
246                                         GLib.debug("Adding File %s", f);
247                                         p.loadSourceIndex(f);
248                                 }
249                         }  
250                         
251                         var run_pack = false;
252                         if (opt_target != null || opt_debug_target != null || opt_dump_tokens) {
253                         
254                                 p.pack( opt_target == null ? "" : opt_target ,
255                                                 opt_debug_target == null ? "" :  opt_debug_target );
256                         
257                         if (p.outstr.length > 0 ) {
258                                         stdout.printf ("%s", p.outstr);
259                                 }
260                 }
261                 if (opt_doc_target != null) {
262                         //var d = new DocBuilder(p);
263                 }
264                 
265                 
266                 
267                 
268                 }        
269                 
270         }
271         
272 }