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