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                 public static string opt_basedir = null;
32                                 
33                 [CCode (array_length = false, array_null_terminated = true)]
34                 private static string[]? opt_files = null;
35                 [CCode (array_length = false, array_null_terminated = true)]
36                 private static string[]? opt_files_from = null;
37                 public static bool opt_debug = false;
38                 public static bool opt_skip_scope = false;
39
40                 public static bool opt_keep_whitespace = false; 
41                 public static bool opt_dump_tokens = false;     
42                 public static bool opt_clean_cache = false;     
43                 
44                 const OptionEntry[] options = {
45                 
46                         { "jsfile", 'f', 0, OptionArg.FILENAME_ARRAY, ref opt_files ,"add a file to compile", null },
47                         { "target", 't', 0, OptionArg.STRING, ref opt_target, "Target File to write (eg. roojs.js)", null },
48                         { "debug-target", 'T', 0, OptionArg.STRING, ref opt_debug_target, "Target File to write debug code (eg. roojs-debug.js)", null },
49                         { "tmpdir", 'm', 0, OptionArg.STRING, ref opt_tmpdir, "Temporary Directory to use (defaults to /tmp)", null },
50                         { "basedir", 'b', 0, OptionArg.STRING, ref opt_basedir, "Base directory (where the files listed in index files are located.)", null },
51
52                         { "index-files", 'i', 0, OptionArg.FILENAME_ARRAY, ref opt_files_from ,"files that contain listing of files to compile", null },                 
53                         { "keep-whitespace", 'w', 0, OptionArg.NONE, ref opt_keep_whitespace, "Keep whitespace", null },
54                         { "skip-scope", 's', 0, OptionArg.NONE, ref opt_skip_scope, "Skip scope parsing and variable replacement", null },
55                         { "debug", 0, 0, OptionArg.NONE, ref opt_debug, "Show debug messages", null },
56                         { "dump-tokens", 'k', 0, OptionArg.NONE, ref opt_dump_tokens, "Dump the tokens from a file", null },
57                         { "clean-cache", 'c', 0, OptionArg.NONE, ref opt_clean_cache, "Clean up the cache after running (slower)", null },
58                         // fixme -- keepwhite.. cleanup 
59                         
60                         { null }
61                 };
62                 public static int main(string[] args) 
63                 {
64                         //foreach(var a in args) {
65                         //      debug("ARG: %s\n", a);
66                         //}
67                         
68                         var opt_context = new OptionContext ("JSDOC Packer");
69                         
70                                 try {
71                                         opt_context.set_help_enabled (true);
72                                         opt_context.add_main_entries (options, null);
73                                         if (!opt_context.parse ( ref args)) {
74                                                 print("options parse error");
75                                                 GLib.Process.exit(Posix.EXIT_FAILURE);
76                                         }
77
78                                 
79                                          
80                                 
81                                 } catch (OptionError e) {
82                                         stdout.printf ("error: %s\n", e.message);
83                                         stdout.printf ("Run '%s --help' to see a full list of available command line options.\n %s", 
84                                                                  args[0], opt_context.get_help(true,null));
85                                         GLib.Process.exit(Posix.EXIT_FAILURE);
86                                          
87                                 }
88                 
89                         new PackerRun(args);
90                         return 0;
91                 }
92
93
94         
95                 public PackerRun (string[] args)
96                 {
97 #if !HAVE_OLD_GLIB              
98                         Object(
99                             application_id: "org.roojs.jsdoc.packerrun",
100                                 flags: ApplicationFlags.HANDLES_COMMAND_LINE 
101                         );
102 #endif                                   
103                          
104                         
105                         // what's required...
106                         if (opt_debug) {
107                                 GLib.Log.set_handler(null, 
108                                         GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING, 
109                                         (dom, lvl, msg) => {
110                                         print("%s: %s\n", dom, msg);
111                                 });
112                         }
113                         
114                         // now run the Packer...
115                         var p = new Packer(
116                                 
117                                 );
118                         p.keepWhite = opt_keep_whitespace;
119                         p.skipScope = opt_skip_scope;
120                         p.dumpTokens = opt_dump_tokens;
121                         p.cleanup = opt_clean_cache;
122                         if (opt_tmpdir == null) {
123                                 p.cleanup = false;
124                         } else {
125                                 p.tmpDir = opt_tmpdir;
126                         }
127                         
128                         
129                         // set the base directory...
130                         var curdir = Environment.get_current_dir() + Path.DIR_SEPARATOR_S;
131                         if (opt_basedir == null) {
132                                 p.baseDir = curdir;
133                         } else if (opt_basedir[0] == '/') {     
134                                 p.baseDir = opt_basedir;
135                         } else {
136                                 p.baseDir = curdir + opt_basedir;
137                         }
138                         // suffix a slash..
139                         if (p.baseDir[p.baseDir.length-1].to_string() != Path.DIR_SEPARATOR_S) {
140                                 p.baseDir += Path.DIR_SEPARATOR_S;
141                         }
142                         
143                         print("BaseDir = '%s' : opt_basedir ='%s'\n", p.baseDir, opt_basedir);
144                         
145                         
146                         if (opt_files == null && opt_files_from == null) {
147                                 GLib.error("You must list some files with -f or -i to compile - see --help for more details");
148                                 GLib.Process.exit(1);
149                         }
150                         
151                         
152                         if (opt_files != null) {
153                          
154                                 foreach (var  f in opt_files) {
155                                         GLib.debug("Adding File %s", f);
156                                         p.loadFile(f);
157                                 }
158                         }  
159                         if (opt_files_from != null) {
160                          
161                                 foreach (var  f in opt_files_from) {
162                                         GLib.debug("Adding File %s", f);
163                                         p.loadSourceIndex(f);
164                                 }
165                         }  
166                         
167                         
168                         
169                         p.pack( opt_target == null ? "" : opt_target ,
170                                         opt_debug_target == null ? "" :  opt_debug_target );
171             
172                 if (p.outstr.length > 0 ) {
173                                 stdout.printf ("%s", p.outstr);
174                         }
175             
176                 }        
177                 
178         }
179         
180 }