JSDOC/Options.js
[gnome.introspection-doc-generator] / JSDOC / Options.js
1 //<script type="text/javascript">
2 /**
3  * Options management...
4  */
5 XObject = imports.XObject.XObject;
6 File = imports.File.File;
7
8 Options = {
9
10     // generic stuff...
11     "--help"          : 'Show help',   // was h
12     "help": false,   // was h;se
13     
14     
15     // configurable settings.. - 
16     "usage" : "Usage seed jsdocbuild.js OPTIONS \n",
17     
18     
19     // options get defined like this..
20     "--src"           :  "source directory (either absolute - starts with "/" or relative " + 
21                         "- without, in which case it's added to baseDir",
22     "--baseDir"       :  'Base Directory (root directory of code)',
23     "--target"        :  'Target Directory (where html files go)',
24     "--cacheDirectory": 'Cached Files Directory',
25     "--conf"          : 'Read From a Configuration file',       // was c. - configuration file.. - parsed with JSON.parse
26     "--template"      : 'Template Directory',   // was t.
27     // "recurse": false,   // was r. - not supported..
28     "--ext"           :  'Extension of code files to read (normally js)',   // was x.
29     "--publishExt"    : 'Extension of html files to write (normally html)',
30     //"private": '',   // was p
31     //"allfunctions": '',   // was a
32     //"encoding": '',   // was e.
33     //"nocode": '',  // was n
34     //"out": '',   // was o.
35     //"suppress": '',  // was s ??? used?
36     "--outputSource" : 'Output the Source code to symbols/src/* (boolean)',
37     //"testmode": '',  // was t
38     
39     "--verbose"       : 'Show verbose messages',   // was v
40     //"disablecache": '',   // was C -- not needed? - see if cacheDirectory was set..
41     //"define" : [],   // was D.
42     //"handler" : [],  // was H -- not supported..
43
44     
45     // and now the defaults.. (which type can be infered from..)
46     "src" : [],
47     "baseDir" :  '',  // base directory - 
48     "target" : '',   // was d. ?? source directory (needed to put temporary files..)
49     "cacheDirectory" : '',
50     "conf" : '',       // was c. - configuration file.. - parsed with JSON.parse
51     "template": '',   // was t.
52     // "recurse": false,   // was r. - not supported..
53     "ext": 'js',   // was x.
54     "publishExt" : 'html',
55     "private": '',   // was p
56     "allfunctions": '',   // was a
57     "encoding": '',   // was e.
58     "nocode": '',  // was n
59     "out": '',   // was o.
60     "suppress": '',  // was s ??? used?
61     "outputSource" : true,
62     "testmode": '',  // was t
63     
64     "verbose": '',   // was v
65     "disablecache": '',   // was C
66     "define" : [],   // was D.
67     "handler" : [],  // was H -- not supported..
68     LOG : {
69         warn : function(str) {
70             print("Warn: " +str );
71         },
72         inform : function(str) {
73             print("Inform: " +str );
74         },
75         close : function() { },
76         flush : function() { },
77         out: false,
78         warnings : [],
79         verbose : false    
80     },
81     init : function()
82     {
83         
84         if (this.help) {
85             this.showHelp();
86           
87         }
88         
89         // the reset of this is specific to JSDOC - and can not be moved to a generic handler..
90         
91          
92         this.LOG.verbose = this.verbose;
93         
94         if (!this.baseDir) { // should we set this to cwd?
95             throw {
96                 name: "ArgumentError", 
97                 message: "No baseDir specified" 
98             };
99         }
100         
101         // this is most likely to come from the command line..
102         if (this.conf) {
103             var conf = this.conf[0] == '/' ? this.conf : this.baseDir + '/' + this.conf;
104         
105             XObject.extend(this, JSON.parse(File.read(conf)));;
106         }
107         // help ?? -- usage..
108        
109         if (!this.src.length) {
110             throw {
111                 name: "ArgumentError", 
112                 message: "No source directories specified" 
113             };
114         }
115         // append full path to source directories.
116         var _this= this;
117         var src = this.src;
118         this.src = [];
119         src.forEach(function(v, i) {
120             if (!v.length || v[0] != '/') {
121                 v = _this.baseDir + (v.length ?  '/' + v : '');
122             }
123             if (!File.isDirectory(v)) {
124                 throw {
125                     name: "ArgumentError", 
126                     message: "invalid Source Directory : " +  v
127                 };
128             }
129             _this.src.push(v);
130         });
131         
132         
133         if (!this.template) {
134             throw {
135                 name: "ArgumentError", 
136                 message: "No template Directory specified" 
137             };
138         }
139         if (this.template[0] !='/') {
140             this.template = this.baseDir + '/' + this.template;
141         }
142         
143         
144         if (!this.target) {
145             throw {
146                 name: "ArgumentError", 
147                 message: "No directory specified" 
148             };
149         }
150       
151         
152         // should cacheDirectory be a subdirectory of target??
153         // if not set..
154         //if (!this.cacheDirectory) {
155         //    throw {
156         //        name: "ArgumentError", 
157         //        message: "No cacheDirectory specified" 
158         //    };
159         // }
160         
161     },
162     /** 
163      *  this might be nice as a standard bit of code..
164      */
165        
166     parseArgv : function() 
167     {
168         
169         var args = Array.prototype.slice.call(Seed.argv);
170         args.shift(); //seed
171         args.shift(); // pack.js
172         
173         for(var i =0; i < args.length;i++) {
174             if (args[i].substring(0,2) != '--') {
175                 
176                 throw {
177                     name: "ArgumentError", 
178                     message: "Unknown argument: " + args[i] 
179                 };
180             }
181             var a = args[i].substring(2);
182             if (typeof(this[args[i]]) == 'undefined') {
183                 throw {
184                     name: "ArgumentError", 
185                     message: "Unknown argument: " + args[i] 
186                 };
187             }
188             // type!!?!?
189             if (typeof(this[a]) == 'string') {
190                 this[a] = args[i+1];
191                 i++;
192                 continue;
193             }
194             if (typeof(this[a]) == 'boolean') {
195                 if (['false', 'true'].indexOf(args[i+1]) < 0) {
196                     throw {
197                         name: "ArgumentError", 
198                         message: "Unknown value for : " + args[i] + ' : ' +  args[i+1] 
199                     };
200                 }
201                 this[a] = args[i+1] == 'true';
202                 i++;
203                 continue;
204             }
205             if (typeof(this[a]) == 'object') { // tecnically an array.
206                 i++;
207                 while(i < args.length)
208                 {
209                     if (args[i].substring(0,2) != '--'){
210                         
211                         break;
212                     }
213                     this[a].push(args[i]);
214                 }
215                 i--;
216             }
217             throw {
218                 name: "ArgumentError", 
219                 message: "Do not know how to handle: " + a
220             };  
221         }
222         
223         
224     },
225     
226     
227     showHelp: function()
228     {
229         print(this.usage);
230         for(var i in this) {
231             if (i.substring(0,2) != '--') {
232                 continue;
233             }
234             print( i + '  ARG  : ' + this[i]);
235             throw "DONE";
236         }
237     }
238 }