From a7a552988f0a5661339f8edf0d7c024a15392238 Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Tue, 20 Jun 2006 21:44:32 +0000 Subject: [PATCH] extending scriptdef 1. nested text gets passed to self.text 2. a fail(String) method takes on the work of throwing exceptions even in languages that dont make it easy to throw BuildExceptions I have antunit tests for these in the antbook source tree, but not moved them into ant's codebase yet. Issue: should nested text be something that must be enabled explicitly via a nestedText attribute in scriptdef? git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@415817 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 ++ docs/manual/OptionalTasks/scriptdef.html | 50 +++++++++++++++++-- .../taskdefs/optional/script/ScriptDef.java | 2 +- .../optional/script/ScriptDefBase.java | 36 ++++++++++++- 4 files changed, 85 insertions(+), 6 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index efaca6099..6df5f23cb 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -436,6 +436,9 @@ Other changes: * New deleteonexit attribute for the task. Bugzilla report 39842. +* -created scripts have support for nested text. All text + passed to a scripted task can be accessed via self.text. + Changes from Ant 1.6.4 to Ant 1.6.5 =================================== diff --git a/docs/manual/OptionalTasks/scriptdef.html b/docs/manual/OptionalTasks/scriptdef.html index 4fea0b4ed..076b54297 100755 --- a/docs/manual/OptionalTasks/scriptdef.html +++ b/docs/manual/OptionalTasks/scriptdef.html @@ -27,6 +27,8 @@ Ant distribution. See Library Dependencies for more information.

+ +

The attributes and nested elements supported by the task may be defined using <attribute> and <element> nested elements. These are available to the script that implements the task as two collection style @@ -42,8 +44,20 @@ lowercase names, so even if you use name="SomeAttribute", you'll have to use "someattribute" to retrieve the attribute's value from the attributes collection.

-

The name "self" (since Ant 1.6.3) is a pre-defined reference to the script def task instance. - It can be used for logging purposes

+

The name "self" (since Ant 1.6.3) is a pre-defined reference to the + script def task instance. + It can be used for logging, or for integration with the rest of + ant. the self.text attribute contains + any nested text passed to the script

+ +

If an attribute or element is not passed in, +then attributes.get() or elements.get() will +return null. It is up to the script to perform any checks and validation. +self.fail(String message)can be used to raise a +BuildException. +

+ +

The name "project" is a pre-defined reference to the Ant Project. For more information on writing scripts, please refer to the <script> task @@ -196,10 +210,40 @@ statement (scriptdef <scripttest2>; line 10)

Script errors are only detected when a script task is actually executed.

+

+ The next example does uses nested text in Jython. It also declares + the script in a new xml namespace, which must be used to refer to + the task. Declaring scripts in a new namespace guarantees that Ant will + not create a task of the same (namespace,localname) name pair. +

+ +
+    <target name="echo-task-jython">
+  <scriptdef language="jython"
+      name="echo"
+      uri="http://example.org/script">
+      <![CDATA[
+self.log("text: " +self.text)
+    ]]>
+    </scriptdef>
+</target>
+
+<target name="testEcho" depends="echo-task-jython"
+    xmlns:s="http://example.org/script">
+  <s:echo>nested text</s:echo>
+</target>
+
+ +

Testing Scripts

+ +

+The easiest way to test scripts is to use the +AntUnit ant library. +This will run all targets in a script that begin with "test" (and their dependencies).


-

Copyright © 2000-2005 The Apache Software Foundation. All rights +

Copyright © 2000-2006 The Apache Software Foundation. All rights Reserved.

diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java b/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java index 172a1114c..4be2d71e4 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java @@ -309,7 +309,7 @@ public class ScriptDef extends DefBase { * * @param attributes collection of attributes * @param elements a list of nested element values. - * @param instance the script instance + * @param instance the script instance; can be null */ public void executeScript(Map attributes, Map elements, ScriptDefBase instance) { runner.addBean("attributes", attributes); diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDefBase.java b/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDefBase.java index 471e3540a..44805f0ba 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDefBase.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDefBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2004 The Apache Software Foundation + * Copyright 2000-2006 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. @@ -38,13 +38,15 @@ public class ScriptDefBase extends Task implements DynamicConfigurator { /** Attributes */ private Map attributes = new HashMap(); + + private String text; /** * Locate the script defining task and execute the script by passing * control to it */ public void execute() { - getScript().executeScript(attributes, nestedElementMap, this); + getScript().executeScript(attributes, nestedElementMap,this); } private ScriptDef getScript() { @@ -94,5 +96,35 @@ public class ScriptDefBase extends Task implements DynamicConfigurator { attributes.put(name, value); } + + /** + * Set the script text. + * + * @param text a component of the script text to be added. + * @since ant1.7 + */ + public void addText(String text) { + this.text=text; + } + + /** + * get the text of this element; may be null + * @return text or null for no nested text + * @since ant1.7 + */ + public String getText() { + return text; + } + + /** + * Utility method for nested scripts; throws a BuildException + * with the given message. + * @param message text to pass to the BuildException + * @throws BuildException always. + * @since ant1.7 + */ + public void fail(String message) { + throw new BuildException(message); + } }