ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rebhan, Gilbert" <Gilbert.Reb...@huk-coburg.de>
Subject RE: Using the script tag, wanting to return a value
Date Thu, 12 Jun 2008 08:01:11 GMT
 
Hi, Jeremy

-----Original Message-----
From: Jeremy Hanna [mailto:jeremy_hanna@mac.com] 
Sent: Thursday, June 12, 2008 3:56 AM
To: user@ant.apache.org
Subject: Using the script tag, wanting to return a value

/*
I'm using the script tag to run a ruby script and would like to return  
a value from the script.  Is that possible?  I see that I can use the  
setbeans attribute to have access to the build.xml variables from  
within the ruby script.  I can output them and even change those  
variables within the ruby script, but those changes disappear once the  
script returns.
Is there any way to return the value?
Really I'm trying to get the result of `svn info` which is retrieved  
in my ruby script.  I haven't seen ant libraries out there that do  
that - most don't implement the info function.  I tried using the exec  
task but that seems to need the path information and that gets too  
messy.
*/

if you need to return value(s) out of <script> and make them available
for further processing by ant you need to put them into the project
scope
of your running ant script.

therefore you make use of the ant api =
$project.setProperty "propertyname", value
$project.setNewProperty "propertyname", value

difference = setProperty allows also overwriting of existing properties.
Overwriting of properties collides with ant rules - means immutability
of properties - but sometimes it's a must.

beside using the ant api a 'puts' or 'print'  in your script prints on
stdout
and gets logged

and finally your script may write/append to a file for persistance
f.e. to make values available for other scripts that run isolated

what i'd try with your svn info command, is using
the backtick operator as i did with nslookup in
the first example.

two small examples =

1.getting the servername from nslookup on windows

<scriptdef name="getservername" language="ruby">
 <attribute name="alias"/>
 <attribute name="property"/>
 <![CDATA[
   getserver=`nslookup "#{$attributes.get('alias')}"`
   $project.setProperty $attributes.get('property'),
"//#{getserver.scan(/Name:\s+(\w+)\./)}"
 ]]>
 </scriptdef>

<getservername alias="test1" property="test.deploy"/>

...

<echo>$${test.deploy} = ${test.deploy}</echo>

2. sleep random

<script language="ruby">
<![CDATA[
  a = ['15','30','60','90','120']    
  $project.setProperty "sleep.time", a[rand(a.size)
]]]>
</script>
    
<sleep seconds="${sleep.time}"/>


Regards, Gilbert

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message