This is an automated email from the ASF dual-hosted git repository.

mbenson pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ant.git


The following commit(s) were added to refs/heads/master by this push:
     new f1570a2  support default value for scriptdef attribute
f1570a2 is described below

commit f1570a246ee9c78f96e8ca3374e1f7fb0e185a98
Author: Matt Benson <mben...@apache.org>
AuthorDate: Thu Feb 24 20:45:08 2022 -0600

    support default value for scriptdef attribute
---
 manual/Tasks/scriptdef.html                        |  5 ++
 .../ant/taskdefs/optional/script/ScriptDef.java    | 70 +++++++++++++++++++++-
 .../taskdefs/optional/script/scriptdef-test.xml    | 25 +++++++-
 3 files changed, 97 insertions(+), 3 deletions(-)

diff --git a/manual/Tasks/scriptdef.html b/manual/Tasks/scriptdef.html
index 680f1a8..a741fba 100644
--- a/manual/Tasks/scriptdef.html
+++ b/manual/Tasks/scriptdef.html
@@ -147,6 +147,11 @@ the <a href="script.html"><code>&lt;script&gt;</code></a> 
task.</p>
     <td>the name of the attribute</td>
     <td>Yes</td>
   </tr>
+  <tr>
+    <td>default</td>
+    <td>the default value of the attribute</td>
+    <td>No</td>
+  </tr>
 </table>
 
 <h4>element</h4>
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 9f97b96..f1c5d4e 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
@@ -21,10 +21,12 @@ import java.io.File;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 import org.apache.tools.ant.AntTypeDefinition;
 import org.apache.tools.ant.BuildException;
@@ -104,14 +106,53 @@ public class ScriptDef extends DefBase {
         /** The attribute name */
         private String name;
 
+        /** The attribute's default value */
+        private String defaultValue;
+
         /**
-         * Sets the attribute name
+         * Sets the attribute name.
          *
          * @param name the attribute name
          */
         public void setName(String name) {
             this.name = name.toLowerCase(Locale.ENGLISH);
         }
+
+        /**
+         * Get the name of this {@link Attribute}.
+         * 
+         * @return {@link String}
+         */
+        String getName() {
+            return name;
+        }
+
+        /**
+         * Set the default value of this {@link Attribute}.
+         *
+         * @param defaultValue {@link String}
+         */
+        public void setDefault(String defaultValue) {
+            this.defaultValue = defaultValue;
+        }
+
+        /**
+         * Get the default value of this {@link Attribute}, {@code null} if
+         * unset.
+         * 
+         * @return {@link String}
+         */
+        String getDefault() {
+            return defaultValue;
+        }
+
+        /**
+         * Learn whether this {@link Attribute} has a default value set.
+         * @return {@code boolean}
+         */
+        boolean hasDefault() {
+            return defaultValue != null;
+        }
     }
 
     /**
@@ -340,7 +381,8 @@ public class ScriptDef extends DefBase {
     public void executeScript(Map<String, String> attributes,
         Map<String, List<Object>> elements, ScriptDefBase instance) {
         ScriptRunnerBase runner = helper.getScriptRunner();
-        runner.addBean("attributes", attributes);
+
+        runner.addBean("attributes", withDefault(attributes));
         runner.addBean("elements", elements);
         runner.addBean("project", getProject());
         if (instance != null) {
@@ -422,4 +464,28 @@ public class ScriptDef extends DefBase {
     public void add(ResourceCollection resource) {
         helper.add(resource);
     }
+
+    private Map<String, String> withDefault(Map<String, String> attributes) {
+        /*
+         * The following is checked in ScriptDefBase but some other caller to
+         * #executeScript() could pass in anything they like
+         */
+        final Set<String> unsupported =
+            attributeSet.stream().filter(a -> 
!this.isAttributeSupported(a)).map(s -> '@' + s)
+                .collect(Collectors.toSet());
+
+        if (!unsupported.isEmpty()) {
+            throw new BuildException("Found unsupported attributes " + 
unsupported);
+        }
+        if (this.attributes.isEmpty()) {
+            return attributes;
+        }
+        final Map<String, String> result =
+            
this.attributes.stream().filter(Attribute::hasDefault).collect(Collectors
+                .toMap(Attribute::getName, Attribute::getDefault, (l, r) -> r, 
LinkedHashMap::new));
+
+        result.putAll(attributes);
+
+        return result;
+    }
 }
diff --git a/src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml 
b/src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml
index e2a7718..3c76278 100644
--- a/src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml
+++ b/src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml
@@ -60,6 +60,7 @@
     <scriptdef language="javascript" name="scripttest" 
manager="${script.manager}">
       <!-- optional property attribute-->
       <attribute name="property" />
+      <attribute name="propertywithdefault" default="prop_dv" />
     </scriptdef>
   </presetdef>
 
@@ -69,7 +70,6 @@
     <au:assertPropertyEquals name="property" value="live" />
   </presetdef>
 
-
   <!--purely to test that everything works -->
   <target name="testInline" if="prereqs-ok">
     <js>self.log("Hello");</js>
@@ -132,4 +132,27 @@
       <quickfail />
     </au:expectfailure>
   </target>
+
+  <target name="testAttribute" if="prereqs-ok">
+    <js>
+      project.setNewProperty(attributes.get('property'), 'live');
+    </js>
+    <scripttest property="property" />
+    <assertPropSet />
+  </target>
+
+  <target name="testAttributeDefaultValue" if="prereqs-ok">
+    <js>
+      project.setNewProperty(attributes.get('propertywithdefault'), 'live');
+    </js>
+    <scripttest />
+    <assertPropSet name="prop_dv" />
+  </target>
+
+  <target name="testInvalidAttribute" if="prereqs-ok">
+    <js />
+    <au:expectfailure message="Found unsupported attributes">
+      <scripttest foo="bar" />
+    </au:expectfailure>
+  </target>
 </project>

Reply via email to