From 5fa7f18c21986b77138e292247cbfe4a85bd396a Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Sun, 16 Jan 2005 23:37:51 +0000 Subject: [PATCH] Adding scripting support to selectors. No personal need for this right now, just rounding off what you can now do with mappers via filterchains git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277368 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 4 + docs/manual/CoreTypes/selectors.html | 125 ++++++++++++++ .../types/selectors/scriptselector.xml | 123 +++++++++++++ .../tools/ant/taskdefs/optional/Script.java | 8 +- .../tools/ant/types/defaults.properties | 1 + .../ant/types/optional/ScriptFilter.java | 11 +- .../ant/types/optional/ScriptSelector.java | 161 ++++++++++++++++++ .../apache/tools/ant/util/ScriptRunner.java | 21 ++- .../types/optional/ScriptSelectorTest.java | 60 +++++++ 9 files changed, 497 insertions(+), 17 deletions(-) create mode 100644 src/etc/testcases/types/selectors/scriptselector.xml create mode 100644 src/main/org/apache/tools/ant/types/optional/ScriptSelector.java create mode 100644 src/testcases/org/apache/tools/ant/types/optional/ScriptSelectorTest.java diff --git a/build.xml b/build.xml index a5890d1ea..3a4091358 100644 --- a/build.xml +++ b/build.xml @@ -1537,6 +1537,10 @@ unless="bsf.present"/> + + + +

Script Selector

+

+ The <scriptselector> element enables you + to write a complex selection algorithm in any + Apache BSF + supported language.

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

+

+ This selector was added in Apache Ant 1.7. +

+ + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
languagelanguage of the script.yes
srcfilename of the scriptno
+

+ If no src attribute is supplied, the script must be nested + inside the selector declaration. +

+

The embedded script is invoked for every test, with + the bean self + is bound to the selector. It has an attribute selected + must can be set using setSelected(boolean) to select that + file. + +

+ + The following beans are configured for every script, alongside + the classic set of project, properties, and targets. + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
BeanDescriptionType
selfselector instanceorg.apache.tools.ant.types.optional
filenamefilename of the selectionString
filefile of the selectionjava.io.File
basedirFileset base directoryjava.io.File
+

+ The self bean maps to the selector, which has the following + attributes. Only the selected flag is writeable, the rest + are read only via their getter methods. + + + + + + + + + + + + + + + + + + + + + + + + + + + +
AttributeDescriptionType
selectedwriteable flag to select this fileboolean
filenamefilename of the selectionString
filefile of the selectionjava.io.File
basedirFileset base directoryjava.io.File
+ +

+ Example +

+
+    <scriptselector language="javascript">
+      self.setSelected(true);
+    </scriptselector>
+
+

+ Selects every file. +

+ +
+    <scriptselector language="javascript">
+      self.setSelected((filename.length%2)==0);
+    </scriptselector>
+
+Select files whose filename length is even. +

Selector Containers

diff --git a/src/etc/testcases/types/selectors/scriptselector.xml b/src/etc/testcases/types/selectors/scriptselector.xml new file mode 100644 index 000000000..879b73a61 --- /dev/null +++ b/src/etc/testcases/types/selectors/scriptselector.xml @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + self.setSelected(true); + + + + + + + + + + + + + + + + + + + + + + self.setSelected(false); + self.setSelected(true); + + + + + + + + + + self.setSelected(false); + + + + + + + + + + self.setSelected(file.equals(self.getFile())); + + + + + + + + + + self.setSelected(filename.equals(self.getFilename())); + + + + + + + + + + self.setSelected(basedir.equals(self.getBasedir())); + + + + + + + + + + self.setSelected((filename.length%2)==0); + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/Script.java b/src/main/org/apache/tools/ant/taskdefs/optional/Script.java index a08b19621..ffdb69581 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/Script.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/Script.java @@ -49,13 +49,7 @@ public class Script extends Task { runner.addText(text); } - runner.addBeans(getProject().getProperties()); - runner.addBeans(getProject().getUserProperties()); - runner.addBeans(getProject().getTargets()); - runner.addBeans(getProject().getReferences()); - - runner.addBean("project", getProject()); - runner.addBean("self", this); + runner.bindToComponent(this); runner.executeScript("ANT"); } diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties index bfa453c69..b06bf4c94 100644 --- a/src/main/org/apache/tools/ant/types/defaults.properties +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -39,3 +39,4 @@ issigned=org.apache.tools.ant.taskdefs.condition.IsSigned 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 diff --git a/src/main/org/apache/tools/ant/types/optional/ScriptFilter.java b/src/main/org/apache/tools/ant/types/optional/ScriptFilter.java index 37ce0d3d8..041d3af5c 100644 --- a/src/main/org/apache/tools/ant/types/optional/ScriptFilter.java +++ b/src/main/org/apache/tools/ant/types/optional/ScriptFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2004 The Apache Software Foundation + * Copyright 2003-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. @@ -60,14 +60,7 @@ public class ScriptFilter extends TokenFilter.ChainableReaderFilter { return; } initialized = true; - - runner.addBeans(getProject().getProperties()); - runner.addBeans(getProject().getUserProperties()); - runner.addBeans(getProject().getTargets()); - runner.addBeans(getProject().getReferences()); - - runner.addBean("project", getProject()); - runner.addBean("self", this); + runner.bindToComponent(this); } /** diff --git a/src/main/org/apache/tools/ant/types/optional/ScriptSelector.java b/src/main/org/apache/tools/ant/types/optional/ScriptSelector.java new file mode 100644 index 000000000..1b67afb0a --- /dev/null +++ b/src/main/org/apache/tools/ant/types/optional/ScriptSelector.java @@ -0,0 +1,161 @@ +/* + * 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.types.selectors.BaseSelector; +import org.apache.tools.ant.util.ScriptRunner; +import org.apache.tools.ant.BuildException; + +import java.io.File; + +/** + * Selector that lets you run a script with selection logic inline + * @since Ant1.7 + */ +public class ScriptSelector extends BaseSelector { + + /** + * Has this object been initialized ? + */ + private boolean initialized = false; + + /** + * script runner + */ + private ScriptRunner runner = new ScriptRunner(); + + /** + * fields updated for every selection + */ + private File basedir; + private String filename; + private File file; + + /** + * selected flag + */ + private boolean selected; + + /** + * Defines the language (required). + * + * @param language the scripting language name for the script. + */ + public void setLanguage(String language) { + runner.setLanguage(language); + } + + /** + * Initialize on demand. + * + * @throws org.apache.tools.ant.BuildException + * if someting goes wrong + */ + private void init() throws BuildException { + if (initialized) { + return; + } + initialized = true; + runner.bindToComponent(this); + } + + + /** + * 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); + } + + /** + * Method that each selector will implement to create their selection + * behaviour. If there is a problem with the setup of a selector, it can + * throw a BuildException to indicate the problem. + * + * @param basedir A java.io.File object for the base directory + * @param filename The name of the file to check + * @param file A File object for this filename + * + * @return whether the file should be selected or not + */ + public boolean isSelected(File basedir, String filename, File file) { + init(); + setSelected(true); + this.file=file; + this.basedir=basedir; + this.filename=filename; + runner.addBean("basedir", basedir); + runner.addBean("filename", filename); + runner.addBean("file", file); + runner.executeScript(""); + return isSelected(); + } + + + /** + * get the base directory + * @return + */ + public File getBasedir() { + return basedir; + } + + /** + * get the filename of the file + * @return + */ + public String getFilename() { + return filename; + } + + /** + * get the file that is currently to be tested + * @return + */ + public File getFile() { + return file; + } + + /** + * get state of selected flag + * @return + */ + public boolean isSelected() { + return selected; + } + + /** + * set the selected state + * Intended for script use, not as an Ant attribute + * @param selected + */ + public void setSelected(boolean selected) { + this.selected = selected; + } + +} diff --git a/src/main/org/apache/tools/ant/util/ScriptRunner.java b/src/main/org/apache/tools/ant/util/ScriptRunner.java index 9c644fbaa..ed2326bae 100644 --- a/src/main/org/apache/tools/ant/util/ScriptRunner.java +++ b/src/main/org/apache/tools/ant/util/ScriptRunner.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2004 The Apache Software Foundation + * Copyright 2003-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. @@ -22,6 +22,9 @@ import java.io.IOException; import org.apache.bsf.BSFException; import org.apache.bsf.BSFManager; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.ProjectComponent; +import org.apache.tools.ant.Project; + import java.util.Map; import java.util.HashMap; import java.util.Iterator; @@ -190,4 +193,20 @@ public class ScriptRunner { public void addText(String text) { this.script += text; } + + /** + * Bind the runner to a project component. + * Properties, targets and references are all added as beans; + * project is bound to project, and self to the component. + * @param component to become self + */ + public void bindToComponent(ProjectComponent component) { + Project project=component.getProject(); + addBeans(project.getProperties()); + addBeans(project.getUserProperties()); + addBeans(project.getTargets()); + addBeans(project.getReferences()); + addBean("project", project); + addBean("self", component); + } } diff --git a/src/testcases/org/apache/tools/ant/types/optional/ScriptSelectorTest.java b/src/testcases/org/apache/tools/ant/types/optional/ScriptSelectorTest.java new file mode 100644 index 000000000..1376ea81f --- /dev/null +++ b/src/testcases/org/apache/tools/ant/types/optional/ScriptSelectorTest.java @@ -0,0 +1,60 @@ +/* + * 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; + +/** + * Test that scripting selection works. Needs scripting support to work + */ +public class ScriptSelectorTest extends BuildFileTest { + + + public ScriptSelectorTest(String name) { + super(name); + } + + public void setUp() { + configureProject("src/etc/testcases/types/selectors/scriptselector.xml"); + } + + public void testNolanguage() { + expectBuildExceptionContaining("testNolanguage", + "Absence of language attribute not detected", + "script language must be specified"); + } + + public void testSelectionSetByDefault() { + executeTarget("testSelectionSetByDefault"); + } + public void testSelectionSetWorks() { + executeTarget("testSelectionSetWorks"); + } + public void testSelectionClearWorks() { + executeTarget("testSelectionClearWorks"); + } + public void testFilenameAttribute() { + executeTarget("testFilenameAttribute"); + } + public void testFileAttribute() { + executeTarget("testFileAttribute"); + } + public void testBasedirAttribute() { + executeTarget("testBasedirAttribute"); + } + +}