Implementation of a "while" task

Durand Van Arnem <[email protected]>
Newsgroups gmane.comp.windows.dotnet.nant.user
Message-ID <[email protected]>
Hello,

 

I was in need of a NAnt "while" task, and searching didn't turn up any solutions.  Here is my implementation.  The IF task was helpful for figuring out how to implement this.

 

using System;

using System.Globalization;

using System.Xml;

using NAnt.Core;

using NAnt.Core.Attributes;

using NAnt.Core.Util;

 

[TaskName("while")]

public class WhileTask : TaskContainer

{

    private string _Test;

 

    #region ctor

 

    public WhileTask()

        : base()

    {

    }

 

    #endregion

 

    [TaskAttribute("test")]

    [BooleanValidator()]

    public string Test

    {

        get

        {

            return _Test;

        }

        set

        {

            _Test = StringUtils.ConvertEmptyToNull(value);

        }

    }

 

    protected virtual bool ConditionsTrue

    {

        get

        {

            if (null != Test)

            {

                Log(Level.Verbose, "Evaluating expression '{0}'", Test);

                string expanded = Project.ExpandProperties(Test, Location);

                Log(Level.Verbose, "Result: {0}", expanded);

                return Convert.ToBoolean(expanded, CultureInfo.InvariantCulture);

            }

            return false;

        }

    }

 

    protected override void InitializeTask(XmlNode taskNode)

    {

        // Need to get the unexpanded expression from the task so it can be evaluated each iteration.

        XmlNode test = taskNode.SelectSingleNode("@test");

        if (null == test)

        {

            throw new BuildException("'test' is a required attribute of the while task.");

        }

        Test = test.Value;

    }

 

    protected override void ExecuteTask()

    {

        while (ConditionsTrue)

        {

            base.ExecuteTask();

        }

    }

}

 

Durand

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev

_______________________________________________
NAnt-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nant-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.