Author: hibou
Date: Mon Feb 22 21:57:47 2010
New Revision: 915083
URL: http://svn.apache.org/viewvc?rev=915083&view=rev
Log:
Introduce the 'anteval' function to deal with evaluation of properties with
dashed or dotted names
Modified:
ant/sandbox/groovyfront/src/main/java/org/apache/ant/groovyfront/GroovyFrontScriptMetaClass.java
ant/sandbox/groovyfront/src/test/antunit/varMappingTest.groovy
ant/sandbox/groovyfront/xml2groovy.xsl
Modified:
ant/sandbox/groovyfront/src/main/java/org/apache/ant/groovyfront/GroovyFrontScriptMetaClass.java
URL:
http://svn.apache.org/viewvc/ant/sandbox/groovyfront/src/main/java/org/apache/ant/groovyfront/GroovyFrontScriptMetaClass.java?rev=915083&r1=915082&r2=915083&view=diff
==============================================================================
---
ant/sandbox/groovyfront/src/main/java/org/apache/ant/groovyfront/GroovyFrontScriptMetaClass.java
(original)
+++
ant/sandbox/groovyfront/src/main/java/org/apache/ant/groovyfront/GroovyFrontScriptMetaClass.java
Mon Feb 22 21:57:47 2010
@@ -30,6 +30,7 @@
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Location;
import org.apache.tools.ant.Project;
+import org.apache.tools.ant.PropertyHelper;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.taskdefs.AntlibDefinition;
import org.apache.tools.ant.taskdefs.condition.Condition;
@@ -97,6 +98,14 @@
return new SimpleNamespaceBuilder(groovyFrontBuilder, prefix, uri);
}
+ if ("anteval".equals(methodName)) {
+ if (arguments.length != 1 && !(arguments[0] instanceof String)) {
+ throw new BuildException("Invalid method signature for
'anteval'");
+ }
+ logDebug("caught", methodName);
+ return
PropertyHelper.getPropertyHelper(project).parseProperties((String)
arguments[0]);
+ }
+
if (!groovyFrontBuilder.isNotCondition(methodName, arguments)) {
Condition condition =
groovyFrontBuilder.createCondition(methodName, arguments);
logDebug("caught", methodName);
Modified: ant/sandbox/groovyfront/src/test/antunit/varMappingTest.groovy
URL:
http://svn.apache.org/viewvc/ant/sandbox/groovyfront/src/test/antunit/varMappingTest.groovy?rev=915083&r1=915082&r2=915083&view=diff
==============================================================================
--- ant/sandbox/groovyfront/src/test/antunit/varMappingTest.groovy (original)
+++ ant/sandbox/groovyfront/src/test/antunit/varMappingTest.groovy Mon Feb 22
21:57:47 2010
@@ -43,10 +43,10 @@
if (fileExist) {
au.assertPropertyEquals(name: 'fileExist', value: 'true')
} else {
- au.testFail(message: 'the if branching of "fileExist" failed')
+ au.fail(message: 'the if branching of "fileExist" failed')
}
if (!fileExist) {
- au.testFail(message: 'the else branching of "fileExist" failed')
+ au.fail(message: 'the else branching of "fileExist" failed')
} else {
au.assertPropertyEquals(name: 'fileExist', value: 'true')
}
@@ -57,10 +57,10 @@
if (project.properties['fileExistNoAlternative'] != null &&
fileExistNoAlternative) {
au.assertPropertyEquals(name: 'fileExistNoAlternative', value: 'true')
} else {
- au.testFail(message: 'the if branching of "fileExistNoAlternative"
failed')
+ au.fail(message: 'the if branching of "fileExistNoAlternative" failed')
}
if (project.properties['fileExistNoAlternative'] == null ||
!fileExistNoAlternative) {
- au.testFail(message: 'the else branching of "fileExistNoAlternative"
failed')
+ au.fail(message: 'the else branching of "fileExistNoAlternative"
failed')
} else {
au.assertPropertyEquals(name: 'fileExistNoAlternative', value: 'true')
}
@@ -71,7 +71,7 @@
}
}
if (project.properties['fileNotExistNoAlternative'] != null &&
fileNotExistNoAlternative) {
- au.testFail(message: 'the else branching of
"fileNotExistNoAlternative" failed')
+ au.fail(message: 'the else branching of "fileNotExistNoAlternative"
failed')
} else {
au.assertTrue() {
not() {
@@ -86,7 +86,7 @@
}
}
} else {
- au.testFail(message: 'the if branching of "fileNotExistNoAlternative"
failed')
+ au.fail(message: 'the if branching of "fileNotExistNoAlternative"
failed')
}
}
@@ -209,3 +209,14 @@
}
au.assertEquals(message: 'Calling echo should have failed', expected:
'true', actual: fail)
}
+
+target(name: 'testAntEval') {
+ property(name: 'fooproperty', value: 'foovalue')
+ def value = anteval('${fooproperty}')
+ au.assertEquals(expected: 'foovalue', actual: value)
+
+ boolproperty = 'true'
+ if (!anteval('${boolproperty}')) {
+ au.fail(message: 'the anteval did not properly evaluate the boolean
property')
+ }
+}
Modified: ant/sandbox/groovyfront/xml2groovy.xsl
URL:
http://svn.apache.org/viewvc/ant/sandbox/groovyfront/xml2groovy.xsl?rev=915083&r1=915082&r2=915083&view=diff
==============================================================================
--- ant/sandbox/groovyfront/xml2groovy.xsl (original)
+++ ant/sandbox/groovyfront/xml2groovy.xsl Mon Feb 22 21:57:47 2010
@@ -77,29 +77,34 @@
<apply-templates select="child::*" mode="condition" />
<text>) {
</text>
- <value-of select="@property" />
- <!-- TODO handle the case of a property with a dot -->
- <text> = "</text><value-of select="@value" /><text>"
+ <call-template name="setprop">
+ <with-param name="name" select="@property" />
+ <with-param name="value" select="@value" />
+ </call-template>
+ <text>
</text>
<if test="@else">
<text>} else {
</text>
- <value-of select="@property" />
- <!-- TODO handle the case of a property with a dot -->
- <text> = "</text><value-of select="@else" /><text>"
-</text>
+ <call-template name="setprop">
+ <with-param name="name" select="@property" />
+ <with-param name="value" select="@else" />
+ </call-template>
</if>
<text>}</text>
</template>
<!-- 'pure' condition handling -->
<template match="*[local-name() = 'istrue']" mode="condition">
- <!-- TODO handle the case of a property with a dot -->
- <text>"</text><value-of select="@value" /><text>"</text>
+ <call-template name="anteval">
+ <with-param name="value" select="@value" />
+ </call-template>
</template>
<template match="*[local-name() = 'isfalse']" mode="condition">
- <!-- TODO handle the case of a property with a dot -->
- <text>!"</text><value-of select="@value" /><text>"</text>
+ <text>!</text>
+ <call-template name="anteval">
+ <with-param name="value" select="@value" />
+ </call-template>
</template>
<template match="*[local-name() = 'not']" mode="condition">
<text>!</text><apply-templates select="child::*" />
@@ -159,7 +164,7 @@
<text>: '</text>
<choose>
<when test="local-name(..) = 'macrodef' and local-name() =
'name' and contains(., '-')">
- <!-- try to handle macrodefs with a dash name -->
+ <!-- try to handle macrodefs with a dashed name -->
<call-template name="replace">
<with-param name="input" select="." />
<with-param name="match" select="'-'" />
@@ -265,4 +270,39 @@
</if>
</template>
+ <!-- try to be smart by avoiding a call anteval when it is not necessary
-->
+ <template name="anteval">
+ <param name="value" />
+ <choose>
+ <when test="contains($value, '$')">
+ <text>anteval('</text><value-of select="$value"
/><text>')</text>
+ </when>
+ <otherwise>
+ <text>'</text><value-of select="$value"
/><text>'</text>
+ </otherwise>
+ </choose>
+ </template>
+
+ <!-- try to handle properly setting a ant property -->
+ <template name="setprop">
+ <param name="name" />
+ <param name="value" />
+ <choose>
+ <when test="contains($name, '-') or contains($name, '.') ">
+ <text>property(name: '</text>
+ <value-of select="$name" />
+ <text>', value: '</text>
+ <value-of select="$value" />
+ <text>')
+</text>
+ </when>
+ <otherwise>
+ <value-of select="$name" />
+ <text> = </text>
+ <call-template name="anteval">
+ <with-param name="value" select="$value" />
+ </call-template>
+ </otherwise>
+ </choose>
+ </template>
</stylesheet>