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         class PackerRun : Object 
24 #else
25         class PackerRun : Application   
26 #endif
27         {
28                 public static string opt_target = null;
29                 public static string opt_debug_target = null;
30 //              public static string opt_tmpdir = null;
31                 private static string opt_basedir = null;
32                 
33                 public static string opt_doc_target = null;
34                 public static string opt_doc_template_dir = null;
35                 
36                                 
37                 [CCode (array_length = false, array_null_terminated = true)]
38                 private static string[]? opt_files = null;
39                 [CCode (array_length = false, array_null_terminated = true)]
40                 private static string[]? opt_files_from = null;
41                 
42                 
43                 public static bool opt_debug = false;
44                 
45                  /**
46                  * @cfg {Boolean} opt_skip_scope (optional) skip Scope parsing and replacement.
47                  *    usefull for debugging...
48                  */
49                 public static bool opt_skip_scope = false;
50                 
51                 /**
52                  * @cfg {Boolean} opt_keep_whitespace (optional) do not remove white space in output.
53                  *    usefull for debugging compressed files.
54                  */
55                 
56                 public static bool opt_keep_whitespace = false; 
57                 
58                         /**
59                  * @cfg {Boolean} opt_dump_tokens (optional) read the first file and dump the tokens.
60                  *    usefull for debugging...
61                  */
62                 
63                 public static bool opt_dump_tokens = false;     
64                 
65                   
66                 /**
67                  * @cfg {Boolean} opt_clean_cache  (optional) clean up temp files after done - 
68                  *    Defaults to false if you set tmpDir, otherwise true.
69                  */
70                 
71                 public static bool opt_clean_cache = true;      
72                 
73                 const OptionEntry[] options = {
74                 
75                         { "jsfile", 'f', 0, OptionArg.FILENAME_ARRAY, ref opt_files ,"add a file to compile", null },
76                         { "target", 't', 0, OptionArg.STRING, ref opt_target, "Target File to write (eg. roojs.js)", null },
77                         { "debug-target", 'T', 0, OptionArg.STRING, ref opt_debug_target, "Target File to write debug code (eg. roojs-debug.js)", null },
78                         //{ "tmpdir", 'm', 0, OptionArg.STRING, ref opt_tmpdir, "Temporary Directory to use (defaults to /tmp)", null },
79                         { "basedir", 'b', 0, OptionArg.STRING, ref opt_basedir, "Base directory (where the files listed in index files are located.)", null },
80
81                         { "index-files", 'i', 0, OptionArg.FILENAME_ARRAY, ref opt_files_from ,"files that contain listing of files to compile", null },                 
82                         { "keep-whitespace", 'w', 0, OptionArg.NONE, ref opt_keep_whitespace, "Keep whitespace", null },
83                         { "skip-scope", 's', 0, OptionArg.NONE, ref opt_skip_scope, "Skip scope parsing and variable replacement", null },
84                         { "debug", 0, 0, OptionArg.NONE, ref opt_debug, "Show debug messages", null },
85                         { "dump-tokens", 'k', 0, OptionArg.NONE, ref opt_dump_tokens, "Dump the tokens from a file", null },
86                         { "clean-cache", 'c', 0, OptionArg.NONE, ref opt_clean_cache, "Clean up the cache after running (slower)", null },
87                         // fixme -- keepwhite.. cleanup 
88                         
89                         // documentation options
90                         // usage: roojspacker --basedir roojs1 \
91                         //       --doc-target roojs1/docs \
92                         //       --index-files roojs1/buildSDK/dependancy_core.txt  \
93                         //       --index-files roojs1/buildSDK/dependancy_ui.txt  \
94                         //       --index-files roojs1/buildSDK/dependancy_bootstrap.txt  \
95                         //       --doc-template-dir\
96                         
97                         
98                         { "doc-target", 'd', 0, OptionArg.STRING, ref opt_doc_target, "Documentation Directory target", null },
99                         { "doc-template-dir", 'p', 0, OptionArg.STRING, ref opt_doc_template_dir, "Documentation Directory target", null },
100                         
101                         
102                         
103                         
104                         { null }
105                 };
106                 public static int main(string[] args) 
107                 {
108                         //foreach(var a in args) {
109                         //      debug("ARG: %s\n", a);
110                         //}
111                         
112                         var opt_context = new OptionContext ("JSDOC Packer");
113                         
114                                 try {
115                                         opt_context.set_help_enabled (true);
116                                         opt_context.add_main_entries (options, null);
117                                         if (!opt_context.parse ( ref args)) {
118                                                 print("options parse error");
119                                                 GLib.Process.exit(Posix.EXIT_FAILURE);
120                                         }
121
122                                 
123                                          
124                                 
125                                 } catch (OptionError e) {
126                                         stdout.printf ("error: %s\n", e.message);
127                                         stdout.printf ("Run '%s --help' to see a full list of available command line options.\n %s", 
128                                                                  args[0], opt_context.get_help(true,null));
129                                         GLib.Process.exit(Posix.EXIT_FAILURE);
130                                          
131                                 }
132                 
133                         new PackerRun(args);
134                         return 0;
135                 }
136
137
138         
139                 public PackerRun (string[] args)
140                 {
141 #if !HAVE_OLD_GLIB              
142                         Object(
143                             application_id: "org.roojs.jsdoc.packerrun",
144                                 flags: ApplicationFlags.HANDLES_COMMAND_LINE 
145                         );
146 #endif                                   
147                          
148                         
149                         // what's required...
150                         if (opt_debug) {
151                                 GLib.Log.set_handler(null, 
152                                         GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING, 
153                                         (dom, lvl, msg) => {
154                                         print("%s: %s\n", dom, msg);
155                                 });
156                         }
157                         
158                         
159                         // now run the Packer...
160                         var p = new Packer(
161                                 
162                         );
163   
164   
165                         //if (opt_tmpdir == null) {
166                         //      p.cleanup = false;
167                         //} else {
168                         //      p.tmpDir = opt_tmpdir;
169                         //}
170                         
171                         
172                         // set the base directory...
173                         var curdir = Environment.get_current_dir() + Path.DIR_SEPARATOR_S;
174                         if (opt_basedir == null) {
175                                 p.baseDir = curdir;
176                         } else if (opt_basedir[0] == '/') {     
177                                 p.baseDir = opt_basedir;
178                         } else {
179                                 p.baseDir = curdir + opt_basedir;
180                         }
181                         // suffix a slash..
182                         if (p.baseDir[p.baseDir.length-1].to_string() != Path.DIR_SEPARATOR_S) {
183                                 p.baseDir += Path.DIR_SEPARATOR_S;
184                         }
185                         
186                         print("BaseDir = '%s' : opt_basedir ='%s'\n", p.baseDir, opt_basedir);
187                         
188                         
189                         if (opt_files == null && opt_files_from == null) {
190                                 GLib.error("You must list some files with -f or -i to compile - see --help for more details");
191                                 GLib.Process.exit(1);
192                         }
193                         
194                         
195                         if (opt_files != null) {
196                          
197                                 foreach (var  f in opt_files) {
198                                         GLib.debug("Adding File %s", f);
199                                         p.loadFile(f);
200                                 }
201                         }  
202                         if (opt_files_from != null) {
203                          
204                                 foreach (var  f in opt_files_from) {
205                                         GLib.debug("Adding File %s", f);
206                                         p.loadSourceIndex(f);
207                                 }
208                         }  
209                         
210                         
211                         
212                         p.pack( opt_target == null ? "" : opt_target ,
213                                         opt_debug_target == null ? "" :  opt_debug_target );
214             
215                 if (p.outstr.length > 0 ) {
216                                 stdout.printf ("%s", p.outstr);
217                         }
218             
219                 }        
220                 
221         }
222         
223 }