Browse Source

Adding scripting support to conditions.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277374 13f79535-47bb-0310-9956-ffa450edef68
master
Steve Loughran 20 years ago
parent
commit
891ef3586b
6 changed files with 300 additions and 6 deletions
  1. +56
    -4
      docs/manual/CoreTasks/conditions.html
  2. +77
    -0
      src/etc/testcases/types/scriptcondition.xml
  3. +0
    -2
      src/etc/testcases/types/selectors/scriptselector.xml
  4. +2
    -0
      src/main/org/apache/tools/ant/types/defaults.properties
  5. +104
    -0
      src/main/org/apache/tools/ant/types/optional/ScriptCondition.java
  6. +61
    -0
      src/testcases/org/apache/tools/ant/types/optional/ScriptConditionTest.java

+ 56
- 4
docs/manual/CoreTasks/conditions.html View File

@@ -334,7 +334,7 @@ that is "true","yes", or "on"</p>
existence of any signature
</p>
<p>
This condition has been added in Apache Ant 1.7.
This condition was added in Apache Ant 1.7.
</p>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
@@ -361,7 +361,7 @@ that is "true","yes", or "on"</p>
Test whether a file passes an embedded selector.
</p>
<p>
This condition has been added in Apache Ant 1.7.
This condition was added in Apache Ant 1.7.
</p>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
@@ -398,7 +398,7 @@ that is "true","yes", or "on"</p>
its implementation class can be loaded. Types include
tasks, datatypes, scriptdefs, macrodefs and presetdefs.</p>

<p>This condition has been added in Apache Ant 1.7.</p>
<p>This condition was added in Apache Ant 1.7.</p>

<table border="1" cellpadding="2" cellspacing="0">
<tr>
@@ -413,8 +413,60 @@ tasks, datatypes, scriptdefs, macrodefs and presetdefs.</p>
</tr>
</table>

<h4>scriptcondition</h4>

<p>Evaluate a condition based on a script in any
<a href="http://jakarta.apache.org/bsf" target="_top">Apache BSF</a>
supported language.</p>
<p>
See the <a href="../OptionalTasks/script.html">Script</a> task for
an explanation of scripts and dependencies.
</p>

<p>This condition was added in Apache Ant 1.7.</p>

<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">language</td>
<td valign="top">script language</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">value</td>
<td valign="top">default boolean value</td>
<td valign="top" align="center">No -default is "false"</td>
</tr>
<tr>
<td valign="top">src</td>
<td valign="top">filename of script source</td>
<td valign="top" align="center">No</td>
</tr>
</table>
<p>
The script supports script language inline, this script has access to the
same beans as the <code>&lt;script&gt;</code> task, and to the <code>
self</code> bean, which refers back to the condition itself. The
<code>value</code> property of this bean sets the return value:
</p>
<p>
Example:
</p>
<pre>
&lt;scriptcondition language=&quot;javascript&quot;
value=&quot;true&quot;&gt;
self.setValue(false);
&lt;/scriptcondition&gt;
</pre>

Sets the default value of the condition to true, then in the script,
sets the value to false. This condition always evaluates to "false"
<hr>
<p align="center">Copyright &copy; 2001-2004 Apache Software
<p align="center">Copyright &copy; 2001-2005 Apache Software
Foundation. All rights Reserved.</p>

</body>


+ 77
- 0
src/etc/testcases/types/scriptcondition.xml View File

@@ -0,0 +1,77 @@
<project name="testscriptcondition" >


<macrodef name="t">
<element name="test" implicit="yes" />
<attribute name="message"/>
<sequential>
<fail message="query @{message} failed; result was false">
<condition>
<not>
<test />
</not>
</condition>
</fail>
</sequential>
</macrodef>

<macrodef name="f">
<element name="test" implicit="yes" />
<attribute name="message"/>
<sequential>
<fail message="test @{message} failed; result was true">
<condition>
<test/>
</condition>
</fail>
</sequential>
</macrodef>
<!-- this is here to test the macro is well coded -->
<target name="testMacro">
<t message="testMacro" >
<istrue value="true"/>
</t>
<f message="testMacro2" >
<istrue value="false"/>
</f>
</target>

<target name="testNolanguage">
<t message="testNolanguage" >
<scriptcondition >
self.setValue(true);
</scriptcondition>
</t>
</target>

<target name="testClearByDefault">
<f message="testClearByDefault" >
<scriptcondition language="javascript">
</scriptcondition>
</f>
</target>
<target name="testValueWorks">
<t message="testValueWorks" >
<scriptcondition language="javascript"
value="true" />
</t>
</target>
<target name="testSetWorks">
<t message="testSetWorks" >
<scriptcondition language="javascript" value="false">
self.setValue(true);
</scriptcondition>
</t>
</target>

<target name="testClearWorks">
<f message="testClearWorks">
<scriptcondition language="javascript" value="true">
self.setValue(false);
</scriptcondition>
</f>
</target>
</project>

+ 0
- 2
src/etc/testcases/types/selectors/scriptselector.xml View File

@@ -46,8 +46,6 @@
</scriptselector>
</selector>
</testselected>
<scriptdef name="nolang">
</scriptdef>
</target>

<target name="testSelectionSetByDefault">


+ 2
- 0
src/main/org/apache/tools/ant/types/defaults.properties View File

@@ -40,3 +40,5 @@ isfileselected=org.apache.tools.ant.taskdefs.condition.IsFileSelected
ispingable=org.apache.tools.ant.taskdefs.optional.condition.IsPingable
mavenrepository=org.apache.tools.ant.taskdefs.repository.MavenRepository
scriptselector=org.apache.tools.ant.types.optional.ScriptSelector
scriptcondition=org.apache.tools.ant.types.optional.ScriptCondition


+ 104
- 0
src/main/org/apache/tools/ant/types/optional/ScriptCondition.java View File

@@ -0,0 +1,104 @@
/*
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.tools.ant.types.optional;

import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.util.ScriptRunner;
import org.apache.tools.ant.taskdefs.condition.Condition;

import java.io.File;

/**
* A condition that lets you include script.
* The condition component sets a bean "self", whose attribute "result"
* must be set to true for the condition to succeed, false to fail.
* The default is 'false'
*/
public class ScriptCondition extends ProjectComponent implements Condition {

/**
* script runner
*/
private ScriptRunner runner = new ScriptRunner();

/**
* result field
*/
private boolean value = false;

/**
* Load the script from an external file ; optional.
*
* @param file the file containing the script source.
*/
public void setSrc(File file) {
runner.setSrc(file);
}

/**
* The script text.
*
* @param text a component of the script text to be added.
*/
public void addText(String text) {
runner.addText(text);
}

/**
* Defines the language (required).
*
* @param language the scripting language name for the script.
*/
public void setLanguage(String language) {
runner.setLanguage(language);
}


/**
* Is this condition true?
*
* @return true if the condition is true
*
* @throws org.apache.tools.ant.BuildException
* if an error occurs
*/
public boolean eval() throws BuildException {
runner.bindToComponent(this);
runner.executeScript("ant_condition");
return getValue();
}

/**
* get the current value of the conditon
* @return true if the condition
*/
public boolean getValue() {
return value;
}

/**
* set the value of the condition.
* This is used by the script to pass the return value.
* It can be used by an attribute, in which case it sets the default
* value
* @param value
*/
public void setValue(boolean value) {
this.value = value;
}
}

+ 61
- 0
src/testcases/org/apache/tools/ant/types/optional/ScriptConditionTest.java View File

@@ -0,0 +1,61 @@
/*
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.tools.ant.types.optional;

import org.apache.tools.ant.BuildFileTest;

/**
*/
public class ScriptConditionTest extends BuildFileTest {
/**
* Constructor for the BuildFileTest object
*
* @param name string to pass up to TestCase constructor
*/
public ScriptConditionTest(String name) {
super(name);
}

public void setUp() {
configureProject("src/etc/testcases/types/scriptcondition.xml");
}

public void testNolanguage() {
expectBuildExceptionContaining("testNolanguage",
"Absence of language attribute not detected",
"script language must be specified");
}

public void testMacro() {
executeTarget("testMacro");
}
public void testClearByDefault() {
executeTarget("testClearByDefault");
}
public void testValueWorks() {
executeTarget("testValueWorks");
}

public void testSetWorks() {
executeTarget("testSetWorks");
}

public void testClearWorks() {
executeTarget("testClearWorks");
}

}

Loading…
Cancel
Save