root/tags/jsolait2.0/jsolait/jsolait.wsf

Revision 65, 7.4 kB (checked in by Jan-Klaas Kollhof, 2 years ago)

fixing ticket:16 ticket:22 adn some missing ;

  • Property svn:keywords set to LastChangedBy LastChangedDate Date Revision Rev
Line 
1 <?xml version="1.0" standalone="yes" ?>
2 <job id="jsolait">
3     <?job error="true" debug="false"?>
4     <description>
5         JavaScript O Lait for WSH.
6        
7        Copyright (c) 2005 Jan-Klaas Kollhof
8        
9        This file is part of jsolait
10      
11        jsolait is free software; you can redistribute it and/or modify
12        it under the terms of the GNU Lesser General Public License as published by
13        the Free Software Foundation; either version 2.1 of the License, or
14        (at your option) any later version.
15      
16        This software is distributed in the hope that it will be useful,
17        but WITHOUT ANY WARRANTY; without even the implied warranty of
18        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19        GNU Lesser General Public License for more details.
20      
21        You should have received a copy of the GNU Lesser General Public License
22        along with this software; if not, write to the Free Software
23        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA     
24     </description>
25
26     <runtime>
27         <named name="compile" helpstring="Specifies to run a the lang.parser on it." many="false" required="0" />
28         <unnamed name="filename" helpstring="The scriptfiles to run." many="false" required="1" />
29         <named name="script-args" helpstring="Specifies arguments to pass to the module/script being run. This may be text or a JSON encoded object" many="false" required="0" />
30     </runtime>
31    
32     <script language="JavaScript" src="./jsolait.js" />
33    
34     <script language="JavaScript"> <![CDATA[
35    
36     Module("jsolaitws", "0.0.1", function(mod){
37            
38         var fs= new ActiveXObject("Scripting.FileSystemObject");
39         var wshShell= new ActiveXObject("WScript.Shell");
40         var ForReading = 1, ForWriting = 2;
41        
42         print = function(m){
43             var s=[];
44             for(var i=0;i<arguments.length;i++){
45                 s.push(''+arguments[i]);
46             }
47            
48             WScript.echo(s.join(" "));
49         }
50          
51         pprint=function(m, indent){
52             var m = m.split("\n");
53            
54             indent =(indent === undefined) ? 0 : indent;
55            
56             if(indent<0){
57                 pprint.indent+=indent;
58             }
59            
60             var s=[];
61             for(var i=0;i<pprint.indent;i++){
62                 s.push(' ');
63             }
64             s=s.join('');
65             for(var i=0;i<m.length;i++){
66                 print(s + m[i]);
67             }
68            
69             if(indent>0){
70                 pprint.indent += indent;
71             }
72         }
73         pprint.indent=0;
74        
75         Error.prototype.toString=function(){
76             return this.name +": " + this.message;
77         }
78                
79         LogError=1;
80         LogWarn=2;
81         LogInfo=4;
82         Error.prototype.toTraceString=function(){
83             return this.message
84         }
85        
86         log=function(msg, level){
87             level = level==null?LogInfo:level;
88             if(typeof msg !="string"){
89                 level = LogError;
90                
91                 msg = msg.toTraceString();
92             }
93            
94             if(level & log.level){
95                 print(msg);
96             }
97         }
98         log.level = LogInfo | LogWarn | LogError;   
99        
100         mod.run=function(){
101             mod.__sourceURI__ = 'file://' + WScript.scriptFullName;
102             jsolait.__sourceURI__ = 'file://' + WScript.scriptFullName.slice(0,-3) + "js";
103                        
104             ///The location where jsolait is installed.
105             jsolait.baseURI = 'file://' + fs.getParentFolderName(WScript.scriptFullName);
106        
107             if (WScript.arguments.unnamed.length==0){
108                 WScript.Arguments.ShowUsage();                                             
109                 return;
110             }else{
111                 //var fileName=fs.getAbsolutePathName(WScript.arguments.unnamed.item(0));
112                 var fileName=WScript.arguments.unnamed.item(0);
113                 //todo:check if file exists
114             }
115                        
116             //get the base of the file to execute
117             var fileBase= fs.getParentFolderName(fileName);
118            
119             //make sure the search path is updated to include the fileBase
120             jsolait.moduleSearchURIs = [fileBase,  "%(baseURI)s/lib"];
121            
122             //change working dir to the file's location
123             //todo:is it OK to change cwd?
124             if(fileBase.slice(0, 'file://'.length) == 'file://'){
125                 wshShell.currentDirectory = fileBase.slice('file://'.length);
126             }
127            
128            
129             if(WScript.arguments.named.exists("compile")){
130                 var lang = imprt('lang');
131                 var s = jsolait.loadURI(fileName);
132                 var p = new lang.Parser(s);
133                    
134                 try{
135                     p.parseStatements(p.next());
136                 }catch(e){
137                     var l=p.getPosition();
138                     throw fileName + '(' + (l[0] ) + ',' +l[1] + ') ' +   e + ' near:\n' + p._working.slice(0,200);
139                 }
140    
141             }else{
142                 try{//load the script if it is not the main jsolait or jsolaitws module that has already been loaded.
143                     if(fileName.toLowerCase() != jsolait.__sourceURI__.toLowerCase()  &&
144                                 fileName.toLowerCase() != mod.__sourceURI__.toLowerCase()){
145                         var src = jsolait.loadURI(fileName);
146                         src = 'Module.currentURI="%s";\n%s\nModule.currentURI=null;\n'.format(src.__sourceURI__.replace(/\\/g, '\\\\'), src);
147                         var f=new Function("",src); //todo should it use globalEval ?
148                         f();
149                     }
150                 }catch(e){
151                     log(e);
152                     return;
153                 }
154                
155                 //if the loaded file contained a module then run it's __main__ method
156                 for(var mn in jsolait.modules){
157                     var modl =jsolait.modules[mn];
158                     if(modl.__sourceURI__.toLowerCase() == fileName.toLowerCase()){
159                         if(modl.__main__){
160                             //todo find arguments
161                             if(WScript.arguments.named.exists("script-args")){
162                                 var args = WScript.arguments.named.Item("script-args");
163                                 switch(args.slice(0,1)){
164                                     case "{": case "'":  case "[":  case '"':
165                                         break;
166                                     default:
167                                         args = repr(args);
168                                 }
169                                 f=new Function( "return " + args);
170                                 args = [f()];
171                             }else{
172                                 var args =[];
173                             }
174                            
175                             try{
176                                 modl.__main__.apply(modl,args);
177                             }catch(e){
178                                 throw new mod.Exception("runing %s  __main__()  failed\n".format(modl),e)
179                             }
180                         }
181                         return;
182                     }
183                 }
184             }
185         }
186     });
187
188     try{
189         imprt("jsolaitws").run();
190     }catch(e){
191         log(e,LogError);
192     }
193        
194     ]]>
195     </script>
196    
197 </job>
Note: See TracBrowser for help on using the browser.