| 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 | | |
|---|
| | 108 | |
|---|