Creating a custom WHILE loop in NANT

I tried my best to create a custom while loop, but ended in vain. Has anyone been successful in creating a custom while loop in NANT?

+3
source share
7 answers

You can create a custom task:

  <target name="sample">
    <property name="foo.value" value="0"/>
    <while property="foo.value" equals="0">
      <do>
        <echo message="${foo.value}"/>
        <property name="foo.value" value="${int::parse(foo.value) + 1}"/>
      </do>
    </while>
  </target>

  <script language="C#" prefix="directory">
    <code>
      <![CDATA[
[TaskName("while")]
public class WhileTask : TaskContainer
{
    private TaskContainer _doStuff;
    private string _propertyName;
    private string _equals;
    private string _notEquals;

    [BuildElement("do")]
    public TaskContainer StuffToDo
    {
        get
        {
            return this._doStuff;
        }
        set
        {
            this._doStuff = value;
        }
    }

    [TaskAttribute("property")]
    public string PropertyName
    {
        get
        {
            return this._propertyName;
        }
        set
        {
            this._propertyName = value;
        }
    }

    [TaskAttribute("equals")]
    public string Equals
    {
        get
        {
            return this._equals;
        }
        set
        {
            this._equals = value;
        }
    }

    [TaskAttribute("notequals")]
    public string NotEquals
    {
        get
        {
            return this._notEquals;
        }
        set
        {
            this._notEquals = value;
        }
    }

    protected override void ExecuteTask()
    {
        while (this.IsTrue())
        {
            this._doStuff.Execute();
        }
    }

    private bool IsTrue()
    {
      if (!string.IsNullOrEmpty(this.Equals))
      {
          return this.Properties[this.PropertyName] == this.Equals;
      }
      return this.Properties[this.PropertyName] != this.NotEquals;
    }
}
    ]]>
    </code>
  </script>
+6
source

looking at the list of currently available tasks for NAnt, it looks like and is no longer supported ( http://nant.sourceforge.net/release/latest/help/tasks/ )

So, I think the simplest and most efficient way to execute a custom while loop is recursion.

So, for example, something like this:

<property name="count" value="120" />

<target name="wait">        
     <if test="${int::parse(count) > 0}" >
        <property name="count" value="${int::parse(count) - 1}" />

        <call target="wait"/>   
    </if>   
</target>

Hello,

Marek

+5
source

, while, NAnt.

<?xml version="1.0"?>
<project name="whiletask" xmlns="http://tempuri.org/nant-donotuse.xsd">

  <script language="C#" prefix="loop">
    <code>
      <![CDATA[

    /// <summary>
    /// A while loop task. Will continuelly execute the task while the <c>test</c> is <c>empty</c> 
    /// or evalutes to <c>true</c>.
    /// </summary>
    [TaskName("while")]
    public class WhileTask : TaskContainer
    {
        private string _test;
        private TaskContainer _childTasks;

        /// <summary>
        /// The expression to test each iteration. If empty, then always evalutes to true (i.e. infinite loop.)
        /// </summary>
        [TaskAttribute("test", ExpandProperties = false)]
        public string Test
        {
            get { return _test; }
            set { _test = NAnt.Core.Util.StringUtils.ConvertEmptyToNull(value); }
        }

        /// <summary>
        /// Superficial to ensure the XML schema is rendered correctly for this task. It will get executed
        /// if tasks exist within it.
        /// </summary>
        [BuildElement("do")]
        public TaskContainer ChildTasks
        {
            get { return _childTasks; }
            set { _childTasks = value; }
        }

        /// <summary>
        /// Executes the while loop while the <c>test</c> evalutes to true or <c>test</c> is empty.
        /// </summary>
        protected override void ExecuteTask()
        {
            while (this.Test == null
                || bool.Parse(Project.ExpandProperties(this.Test, this.Location)))
            {
                if (this._childTasks != null)
                {
                    this._childTasks.Execute();
                }
                else
                {
                    base.ExecuteTask();
                }
            }
        }
    }

          ]]>
    </code>
  </script>

  <property name="i" value="0" />
  <while test="${int::parse(i) &lt;= 10}">
    <echo message="${i}" />
    <property name="i" value="${int::parse(i)+1}" />
  </while>

</project>
+3

. - Cao, true, , , , , , , . . . , , , ( "if" break/continue - - , , failonerror trycatch.

Nant script, 10:

<property name="greaterthanzero" value="${int::parse(count) > 0}" dynamic="true"/>

<property name="count" value="10" />
<while propertytrue="greaterthanzero" >
  <echo>CountDown = ${count}</echo>
  <property name="count" value="${int::parse(count) - 1}" />
</while>

<property name="count" value="10" />
<while>
  <if test="${int::parse(count) > 0}" >
    <echo>CountDown = ${count}</echo>
    <property name="count" value="${int::parse(count) - 1}" />
    <continue/>
  </if>
  <break/>
</while>

, :

<property name="count" value="0" />
<property name="lockfileexists" value="${file::exists(lockfile)}" dynamic="true"/>
<while propertytrue="lockfileexists" >
  <sleep seconds="1" />
  <property name="count" value="${int::parse(count) + 1}" />
  <if test="${count == '15'}" >
    <echo>Timed out after 15 seconds</echo>
    <break/>
  </if>
</while>

:

  <script language="C#" prefix="loops">
<code>
  <![CDATA[

    public class LoopBreakException : Exception {}
    public class LoopContinueException : Exception {}

    [TaskName("break")]
    public class BreakTask : Task
    {
        protected override void ExecuteTask()
        {
            throw new LoopBreakException();
        }
    }

    [TaskName("continue")]
    public class ContinueTask : Task
    {
        protected override void ExecuteTask()
        {
            throw new LoopContinueException();
        }
    }

    [TaskName("while")]
    public class WhileTask : TaskContainer
    {
        [TaskAttribute("propertytrue")]
        public string PropertyName { get; set; }

        protected bool CheckCondition()
        {
            if (!string.IsNullOrEmpty(PropertyName)) 
            {
                try 
                {
                    return bool.Parse(Properties[PropertyName]);
                }
                catch (Exception ex) 
                {
                    throw new BuildException(string.Format("While Property '{0}' not found", PropertyName), Location);
                }
            }
            //for infinite loops
            return true;
        }

        protected override void ExecuteTask()
        {
            while (CheckCondition())
            {
                try
                {
                  ExecuteChildTasks();
                }
                catch (LoopContinueException)
                {
                  continue;
                }
                catch (LoopBreakException)
                {
                  break;
                }
            }
        }
    }
]]>
</code>

+1

NAnt .

- 2 :

  • ( pdb) NAnt bin. Visual Studio, . . "". Debug Program Start Application NAnt (, C:\Program Files\NAnt\bin\NAnt.exe). / , NAnt . "" .

  • System.Diagnostics.Debbugger.Break(); , . ( pdb) NAnt bin. NAnt script, .

.

foreach?

0

. , , NANT.

. while foreach foreach foreach. , , .

0

WHILE nant, script, failonerror="false" foreach.

    <property name="n" value="10000" /><!-- this would be inefficient if "n" is very large -->
    <property name="i" value="0" />
    <foreach item="String" in="${string::pad-right(' ', int::parse(n), ',')}" delim="," property="val" failonerror="false" >
        <if test="${int::parse(i) &gt; 3}"><!-- put our exit condition here -->
            <fail message="condition met, exit loop early" />
        </if>
        <echo message="i: ${i}" />
        <property name="i" value="${int::parse(i) + 1}" />
    </foreach>

The result of executing the WHILE loop above is as follows. Note that due to the failonerror="false"call fail, the script does not complete:

     [echo] i: 0
     [echo] i: 1
     [echo] i: 2
     [echo] i: 3
  [foreach] myscript.nant(24,18):
  [foreach] condition met, exit loop early

  BUILD SUCCEEDED - 1 non-fatal error(s), 0 warning(s)

I based the WHILE loop above on how I create the FOR loop, which is a slightly simplified version of the above code:

        <property name="n" value="5" />
        <property name="i" value="0" />
        <foreach item="String" in="${string::pad-right(' ', int::parse(n), ',')}" delim="," property="val" >
            <echo message="i: ${i}" />
            <property name="i" value="${int::parse(i) + 1}" /> <!-- increment "i" -->
        </foreach>

The exit from the FOR loop is as follows:

     [echo] i: 0
     [echo] i: 1
     [echo] i: 2
     [echo] i: 3
     [echo] i: 4

 BUILD SUCCEEDED
0
source

Source: https://habr.com/ru/post/1709682/


All Articles