Skip to content

Instantly share code, notes, and snippets.

@sdesalas
Created June 26, 2014 00:18
Show Gist options
  • Save sdesalas/79fd45cc6a9ad000d6c2 to your computer and use it in GitHub Desktop.
Save sdesalas/79fd45cc6a9ad000d6c2 to your computer and use it in GitHub Desktop.
Batch file for parsing input characters
@if (1==1) @if(1==0) @ELSE
@echo off
@SETLOCAL ENABLEEXTENSIONS
:: CHECK IF WE NEED TO MODIFY PATH
ECHO ;%PATH%; | find /C /I ";%~dp0tools\phantomjs;" > NUL
IF %ERRORLEVEL% GTR 0 (
ECHO Adding phantom and casper to PATH
SET "path=%path%;%~dp0tools\phantomjs;%~dp0tools\casperjs\batchbin"
)
:: EXECUTE AFTER REPARSING BATCH INPUT
IF "%SPECIALPARSE%"=="*%~f0" (
:: SECOND RUN - EXECUTE
ECHO casperjs.bat test "%targetdir%\run.js" --joyful-target=%target% --xunit=test-reports\latest.xml %~2 %~3 %~4 %~5 %~6 %~7 %~8 %~9
casperjs.bat test "%targetdir%\run.js" --joyful-target=%target% --xunit=test-reports\latest.xml %~2 %~3 %~4 %~5 %~6 %~7 %~8 %~9
) ELSE (
:: FIRST RUN - SETUP
SET target=%1
SET targetdir=%~dp0
SET "SPECIALPARSE=*%~f0"
cscript //E:JScript //nologo "%~f0" %*
)
@goto :EOF
@end @ELSE
w=WScript,wa=w.Arguments,al=wa.length,Sh=w.CreateObject("WScript.Shell"),p="";
for(i=0;i<al;++i)p+="\"--"+wa.Item(i)+"\" ";
function PipeStream(i,o){for(;!i.AtEndOfStream;)o.Write(i.Read(1))}
function Exec(cmd,e){
try{
e=Sh.Exec(cmd);
while(e.Status==0){
w.Sleep(99);
PipeStream(e.StdOut,w.StdOut);
PipeStream(e.StdErr,w.StdErr);
}
return e.ExitCode;
}catch(e){return e.number;}
}
w.Quit(Exec("\""+WScript.ScriptFullName+"\" "+p));
@end
@sdesalas
Copy link
Author

sdesalas commented Jul 2, 2014

Usage as follows

 run.bat suites\1.Login.test @username=johns @password=mysecret @url=http://dev.app.local/

This in turn get transformed into:

 casperjs.bat test "%cwd%\run.js" --joyful-target=suites\1.Login.test --@username=johns --@password=mysecret --@url=http://dev.app.local

The reason for all the messy WScript code is to ensure we dont lose the equals = sign for each input parameter in the process. Otherwise the input is only available as:

 run.bat suites\1.Login.test @username johns @password mysecret @url http://dev.app.local/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment