ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scot P. Floess" <sflo...@nc.rr.com>
Subject Creating a macrodef dynamically...within beanshell or any scripting language?
Date Thu, 08 Dec 2011 18:29:52 GMT
All,

I have what may seem like a strange question...  I was looking through the 
source for Ant Contrib's ForTask.java...  I see it uses MacroDef 
internally as well as MacroInstance to execute().

I've got a script def that has a child element entitled "sequential" of 
type org.apache.tools.ant.taskdefs.Sequential.

I was trying to mimick the ForTask within a Beanshell script.  Below is 
the code I was writing - but it definitely doesn't execute "sequential" 
whatsoever.  What started me on this was I liked how ForTask uses the same 
param expansion that MacroDef uses...and wanted to see what ForTask did to 
make that happen.  I can definitely get "sequential" to execute by itself 
within the for loop no problem...  Here is the script def - and thanks 
ahead of time!!!!!!!!

     <scriptdef name="bshForLoop" language="beanshell">
         <classpath>
             <fileset dir="lib" includes="*.jar"/>
         </classpath>

         <attribute name="start"/>
         <attribute name="end"/>
         <attribute name="step"/>
         <attribute name="param"/>

         <element name="sequential" 
classname="org.apache.tools.ant.taskdefs.Sequential"/>

         <![CDATA[
         try {
             import org.apache.tools.ant.Task;
             import org.apache.tools.ant.RuntimeConfigurable;
             import org.apache.tools.ant.UnknownElement;

             import org.apache.tools.ant.taskdefs.MacroDef;
             import org.apache.tools.ant.taskdefs.MacroInstance;

             int    start = Integer.parseInt(attributes.get("start"));
             int    end   = Integer.parseInt(attributes.get("end"));
             int    step  = Integer.parseInt(attributes.get("step"));
             String param = attributes.get("param");
             Task   body  = (Task) elements.get("sequential").get(0);

             MacroDef macroDef = new MacroDef();
             macroDef.setProject(project);

             MacroDef.Attribute attribute = new MacroDef.Attribute();
             attribute.setName(param);
             attribute.setDefault("" + start);
             macroDef.addConfiguredAttribute(attribute);

             MacroDef.NestedSequential sequential = 
macroDef.createSequential();

             UnknownElement unknownElement = macroDef.getNestedTask();
             unknownElement.setRealThing(body);
             unknownElement.setProject(project);

             sequential.addTask(unknownElement);

             MacroInstance instance = new MacroInstance();
             instance.setProject(project);
             instance.setMacroDef(macroDef);

             for (int index = start; index != end; index += step) {
                 instance.setDynamicAttribute(param, "" + index);

                 instance.execute();
             }
         }

         catch(Exception e) {
             e.printStackTrace();
         }
         ]]>
     </scriptdef

Scot P. Floess             RHCT  (Certificate Number 605010084735240)
Chief Architect FlossWare  http://sourceforge.net/projects/flossware
                            http://flossware.sourceforge.net
                            https://github.com/organizations/FlossWare

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message