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