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