From 891ef3586b19bbddafa2cc9e052eb6ba21ca57b5 Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Mon, 17 Jan 2005 21:54:34 +0000 Subject: [PATCH] Adding scripting support to conditions. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277374 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/conditions.html | 60 +++++++++- src/etc/testcases/types/scriptcondition.xml | 77 +++++++++++++ .../types/selectors/scriptselector.xml | 2 - .../tools/ant/types/defaults.properties | 2 + .../ant/types/optional/ScriptCondition.java | 104 ++++++++++++++++++ .../types/optional/ScriptConditionTest.java | 61 ++++++++++ 6 files changed, 300 insertions(+), 6 deletions(-) create mode 100644 src/etc/testcases/types/scriptcondition.xml create mode 100644 src/main/org/apache/tools/ant/types/optional/ScriptCondition.java create mode 100644 src/testcases/org/apache/tools/ant/types/optional/ScriptConditionTest.java diff --git a/docs/manual/CoreTasks/conditions.html b/docs/manual/CoreTasks/conditions.html index 2e5081510..f0ec8172d 100644 --- a/docs/manual/CoreTasks/conditions.html +++ b/docs/manual/CoreTasks/conditions.html @@ -334,7 +334,7 @@ that is "true","yes", or "on"

existence of any signature

- This condition has been added in Apache Ant 1.7. + This condition was added in Apache Ant 1.7.

@@ -361,7 +361,7 @@ that is "true","yes", or "on"

Test whether a file passes an embedded selector.

- This condition has been added in Apache Ant 1.7. + This condition was added in Apache Ant 1.7.

@@ -398,7 +398,7 @@ that is "true","yes", or "on"

its implementation class can be loaded. Types include tasks, datatypes, scriptdefs, macrodefs and presetdefs.

-

This condition has been added in Apache Ant 1.7.

+

This condition was added in Apache Ant 1.7.

@@ -413,8 +413,60 @@ tasks, datatypes, scriptdefs, macrodefs and presetdefs.

+

scriptcondition

+ +

Evaluate a condition based on a script in any +Apache BSF +supported language.

+

+See the Script task for +an explanation of scripts and dependencies. +

+ +

This condition was added in Apache Ant 1.7.

+ + + + + + + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
languagescript languageYes
valuedefault boolean valueNo -default is "false"
srcfilename of script sourceNo
+

+The script supports script language inline, this script has access to the +same beans as the <script> task, and to the +self bean, which refers back to the condition itself. The +value property of this bean sets the return value: +

+

+Example: +

+
+    <scriptcondition language="javascript"
+            value="true">
+        self.setValue(false);
+    </scriptcondition>
+
+ +Sets the default value of the condition to true, then in the script, +sets the value to false. This condition always evaluates to "false"
-

Copyright © 2001-2004 Apache Software +

Copyright © 2001-2005 Apache Software Foundation. All rights Reserved.

diff --git a/src/etc/testcases/types/scriptcondition.xml b/src/etc/testcases/types/scriptcondition.xml new file mode 100644 index 000000000..f1b6770d0 --- /dev/null +++ b/src/etc/testcases/types/scriptcondition.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + self.setValue(true); + + + + + + + + + + + + + + + + + + + + + self.setValue(true); + + + + + + + + self.setValue(false); + + + + diff --git a/src/etc/testcases/types/selectors/scriptselector.xml b/src/etc/testcases/types/selectors/scriptselector.xml index 879b73a61..3299410a2 100644 --- a/src/etc/testcases/types/selectors/scriptselector.xml +++ b/src/etc/testcases/types/selectors/scriptselector.xml @@ -46,8 +46,6 @@ - - diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties index b06bf4c94..0f444fecf 100644 --- a/src/main/org/apache/tools/ant/types/defaults.properties +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -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 + diff --git a/src/main/org/apache/tools/ant/types/optional/ScriptCondition.java b/src/main/org/apache/tools/ant/types/optional/ScriptCondition.java new file mode 100644 index 000000000..42bba4f26 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/optional/ScriptCondition.java @@ -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; + } +} diff --git a/src/testcases/org/apache/tools/ant/types/optional/ScriptConditionTest.java b/src/testcases/org/apache/tools/ant/types/optional/ScriptConditionTest.java new file mode 100644 index 000000000..1e93ad2ae --- /dev/null +++ b/src/testcases/org/apache/tools/ant/types/optional/ScriptConditionTest.java @@ -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"); + } + +}