Execute Groovy script with arguments from Grails app - grails

In reference to the question asked here
How to execute a Groovy Script from my Grails app?
which works except ...how do I pass arguments ?
def cmd = ['groovy.bat', 'C:\\my path\\mysript.groovy']
for a script that is run from the command line like
groovy myscript.groovy -name params.name -project params.name
using CliBuilder for arguments and params from form submitted

Groovy provides a simple way to execute command line processes. Yo can write the command line as a stringm and call the execute() method. Example:
"groovy myscript.groovy -name nancy -project testproj".execute()
More information in this link.
In case of arguments with whitespaces:
["groovy",
"my script with spaces.groovy",
"-name",
"nancy",
"-project",
"testproj"].execute()

Related

Acces groovy var in sh in jenkinsfile

I am using enviroment variable ARTIFACT_VERSION, and want to put it in shell script
sh "wget -q http://nexus.com/$polygon/$env.ARTIFACT_VERSION/$polygon-$env.ARTIFACT_VERSION.zip"
And got this errors
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.String zip
And if I input var with this style ${env.ARTIFACT_VERSION}
I get in output result like I am using \n (but version input is correct)
wget -q http://nexus.com/polygon/myversion
/polygon-myversion
.zip
The link is correct when i used it without vars - all is ok
myversion parameter was getting as a result from grep command
and it had \n at the end

How can I specify arguments to NUnit-console.exe in dotcover command line

I am working with nunit and dotCover for code coverage, if we run nunit-console.exe we need to provide arguments like :
& $nunit /nothread /noshadow /labels /domain=None /trace=Info /framework=net-4.0 /process=Separate
where $nunit is path to nunit-console.exe
but I am running nunit-console.exe with dotcover command line and I am providing following arguments
&$dotcover cover /TargetExecutable=$testRunner /TargetArguments=$test /Output="D:\JetBrains.dotCover.CommandLineTools.2019.3.4\TestReport\$testName.dcvr"
where testrunner is nunit-console.exe and $test has path to test.dll
but tests are not passing while running in Nunit.exe (UI app) they are passing.
is there any way I can pass required arguments to nunit in the dotcover script? so when dotcover covers nunit it will run with specified parameters.
I tried some workaround like this but it is not working &$dotcover cover /TargetExecutable=$testRunner /TargetArguments=$test /nothread /noshadow /labels /domain=None /trace=Info /framework=net-4.0 /process=Separate /Output="D:\JetBrains.dotCover.CommandLineTools.2019.3.4\TestReport\$testName.dcvr"
but it is showing this error:
[JetBrains dotCover] Not used command line parameter: 'nothread'
[JetBrains dotCover] Not used command line parameter: 'noshadow'
[JetBrains dotCover] Not used command line parameter: 'labels'
[JetBrains dotCover] Not used command line parameter: 'domain'
You should just need to quote the arguments for the NUnit Console.
&$dotcover cover /TargetExecutable=$testRunner /TargetArguments="$test /nothread /noshadow /labels /domain=None /trace=Info /framework=net-4.0 /process=Separate" /Output="D:\JetBrains.dotCover.CommandLineTools.2019.3.4\TestReport\$testName.dcvr"
Is there any chance that $test may contain quotes itself, or spaces? In which case, you need to quote it, and make sure that you escape the quotes inside quotes. Depends on exactly what command line you're using I think, but you'll probably want something a bit like this...
&$dotcover cover /TargetExecutable=$testRunner /TargetArguments="\"$test\" /nothread /noshadow /labels /domain=None /trace=Info /framework=net-4.0 /process=Separate" /Output="D:\JetBrains.dotCover.CommandLineTools.2019.3.4\TestReport\$testName.dcvr"

checking for existance of ";C:\Python27" before appending ";C:\Python27" to environment variable PATH

I am using following code to append ";C:\Python27" to environment variable PATH..
#echo off
Setx Path "%PATH%;C:\Python27" -M
PAUSE
but if i run this batch file more than once, it is appending ";C:\Python27" many times that should not happen.
SO i have to check for ;C:\Python27 before appending it to PATH variable.
Is there any command for this purpose?
The following Powershell should do it:
$needPython = $env:path | select-string -NotMatch -SimpleMatch "c:\python27"
if ($needPython) {
[Environment]::SetEnvironmentVariable("tstpath", $env:path + ";c:\python27", "User")
}
You can change User to Machine or Process to set a machine or process level environment variable.
You can run this directly from a powershell prompt.
If you're running this from a dos command line use (you need the full path to your script or .\ if it's in the current directory):
powershell "& '.\myscript.ps1'"

Calling Bash Commands With Multiple args From Ruby?

I write this code in my rails project.It's to execute a shell script ,But my shell script can catch #directdown only.
rails script
#cmd = "/downafile.sh #{#directdown} #{#file.id} #{#filename}"
`#{#cmd}`
shell script
echo $1 >> /tmp/ceshi.tmp
echo $2 >> /tmp/ceshi.tmp
echo $3 >> /tmp/ceshi.tmp
Thanks.
`` and system command works in similar manner.
But system method will return true on success.
Try with this:
#cmd = "/downafile.sh '#{#directdown}' '#{#file.id}' '#{#filename}'"
`#{#cmd}`
Try using puts #cmd to see the command generated and see whether the command is what you intended. If it is try executing it from the terminal to test whether your shell script works. I think the parameters #file.id and #filename value may be nil so on interpolation it will be replaced by "". puts their values also to check.
I don't know what is the meaning of `` in Ruby, but I think you can use the system function to invoke an external program. Something like,
#cmd = "/downafile.sh #{#directdown} #{#file.id} #{#filename}"
system(#cmd)
Hope it helps.

how to use execute() in groovy to run any command

I usually build my project using these two commands from command line (dos)
G:\> cd c:
C:\> cd c:\my\directory\where\ant\exists
C:\my\directory\where\ant\exists> ant -Mysystem
...
.....
build successful
What If I want to do the above from groovy instead? groovy has execute() method but following does not work for me:
def cd_command = "cd c:"
def proc = cd_command.execute()
proc.waitFor()
it gives error:
Caught: java.io.IOException: Cannot run program "cd": CreateProcess error=2, The
system cannot find the file specified
at ant_groovy.run(ant_groovy.groovy:2)
Or more explicitly, I think binil's solution should read
"your command".execute(null, new File("/the/dir/which/you/want/to/run/it/from"))
According to this thread (the 2nd part), "cd c:".execute() tries to run a program called cd which is not a program but a built-in shell command.
The workaround would be to change directory as below (not tested):
System.setProperty("user.dir", "c:")
"your command".execute(null, /the/dir/which/you/want/to/run/it/from)
should do what you wanted.
Thanks Noel and Binil, I had a similar problem with a build Maven.
projects = ["alpha", "beta", "gamma"]
projects.each{ project ->
println "*********************************************"
println "now compiling project " + project
println "cmd /c mvn compile".execute(null, new File(project)).text
}
I got to fix the issue by running a command as below:
i wanted to run git commands from git folder, so below is the code which worked for me.
println(["git","add","."].execute(null, new File("/Users/xyz/test-org/groovy-scripts/$GIT_REPOS/")).text)
println(["git","commit","-m","updated values.yaml"].execute(null, new File("/Users/xyz/test-org/groovy-scripts/$GIT_REPOS/")).text)
println(["git","push","--set-upstream","origin","master"].execute(null, new File("/Users/xyz/test-org/groovy-scripts/$GIT_REPOS/")).text)

Resources