Alex Hildyard wrote:
Hi,
Is it possible to repeat an arbitrary set of tasks with a user-defined exit condition? If not, could I propose this as a new task?
The "foreach" task lets you iterate over a certain set of prescribed structures, but it would be nice to have something which simply provides an uncomplicated
looping construct. For example:

Using foreach as an example, it would be a pretty simple task to create a <repeat> task... I'm not sure this should be part of core NAnt though, since it tends to undermine NAnt's position as a declarative language.


<property name="var1" value="0"/>
<repeat test="${var1 &lt; 10}">
<echo message="${var1}"/>
<property name="var1" value="${convert::to-int(var1)+1}"/>
</repeat>
Currently I implement this with a "call" task, which doesn't really have the right semantics, and means I have to initialise var1 from inside a different
template, which tends to break the encapsulation.

<target name="repeat"> <property name="var1" value="0" overwrite="false" /> <echo message="${var1}" /> <property name="var1" value="${convert::to-int(var1) + 1)" /> <call target="repeat" if="${convert::to-int(var1) < 10}" /> </target>

I'm not sure why you suggest that call doesn't have the right semantics... you're simply re-rolling your iterative approach back into a tail-recursive approach. The above should do exactly the same as your repeat function, doesn't require pre-initialisation (the first time) and is almost as concise.

If you use this often in your build, though, you would probably benefit from creating the repeat task - the recursive approach will create excessively verbose logs, especially when using an XML logger; also, any loop variables will need to be reset (externally) before the second and subsequent calls.


The other alternative is to wrap any loops in the <script> task. Use could use Project.Execute to perform any required tasks... the example above again:


<target name="repeat-body">
  <echo message="${var1}" />
</target>
<target name="repeat">
  <script language="C#">
    <code><![CDATA[
      public static void ScriptMain(Project project) {
        for (int i = 0; i < 10; i++) {
          Project.Properties["var1"] = i;
          Project.Execute("repeat-body");
        }
      }
    ]]></code>
  </script>
</target>



Regards,

-- Troy


------------------------------------------------------- This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools! Get your free copy of BEA WebLogic Workshop 8.1 today. http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click _______________________________________________ nant-developers mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/nant-developers

Reply via email to