I thought I was one of those people :)

Anyway, the foreach was really just a test case for the TaskContainer
concept. Well, it wasn't really a test; it is useful on its own. The
ghenghis build has a script section that loops through all the sample
subdirs and builds each sample [1]. It is a lot of code, and it seemed
to me that it would be much simpler, and easier to understand if it
could be re-written with a loop task. In fact I think this was Brad's
question in his post
(http://www.mail-archive.com/nant-developers@lists.sourceforge.net/msg00
205.html) that started the idea.

The general syntax was pretty straight forward. So I started playing
with the idea of a loop. It turned out to be really simple. And I think
a coded task version is much better than writing this in a script block
every time you want to do it. Besides the whole maintenance and testing
issues that arise from putting code in a place that is edited a lot.

Right now <foreach/> can iterate over Files, Folders, Lines, or a
delimitated string.
(http://www.mail-archive.com/nant-developers@lists.sourceforge.net/msg00
341.html)

The general syntax is a little clumsy, but I can't think of a better
way. 

<foreach itemtype="Folders" source="c:\" property="folder">
        <anyTasks/>
</>

--The itemtype attribute species on of the Enum values:
        public enum ItemTypes {
            None,
            Files,
            Folders,
            DelimString,
            Lines
        }

--The source species what to work with. It is dependant on the itemtype.

--The property attribute specifies the name of the attribute to use.
This attribute is used for the duration of the task, and the value is
returned to its original.

I don't like the itemtype/source separation, but I wanted to keep these
all in attributes so it is clear that everything else is a task and get
executed.

I'm not sure exactly what other uses there are for this looping task
than what has already been proposed. The sky is the limit :)

Oh... it also works great for counting...
<foreach itemtype="DelimString" source="1,2,3" property="number">
        <echo message="I can count! I now have ${number}."/>
</>

(I'm just kidding here, counting is over-rated.)

[1]
   <target name="samples" description="Builds the sample applications"
depends="debug">
      <script language="C#">
         <code><![CDATA[
         public static void ScriptMain(Project project) {
            // Get the samples directory
            string sourcePath = Path.Combine(project.BaseDirectory,
"samples");

            // Get all the subdirectories of the samples directory
            string[] dirs = Directory.GetDirectories(sourcePath);
            char sep = Path.DirectorySeparatorChar;

            // Iterate
            foreach(string dir in dirs) {
               int idx = dir.LastIndexOf(sep);
               if(idx >= 0) {
                  // Launch a second NAnt to build the sample
                  System.Diagnostics.Process process = new
System.Diagnostics.Process();
                  process.StartInfo.FileName = "nant";
                  process.StartInfo.Arguments = "-buildfile:" +
project.BaseDirectory + "\\Genghis.build2 -D:sample=" +
dir.Substring(idx+1) + " mksample";
                  process.StartInfo.UseShellExecute = false;
                  process.StartInfo.WorkingDirectory =
project.BaseDirectory;
                  process.Start();
                  process.WaitForExit();
                  if(process.ExitCode != 0)
                     throw new BuildException();
               }
            }
         }
      ]]></code>
      </script>
   </target>
> -----Original Message-----
> From: [EMAIL PROTECTED]
[mailto:nant-developers-
> [EMAIL PROTECTED]] On Behalf Of Gerry Shaw
> Sent: Monday, June 24, 2002 10:25 PM
> To: [EMAIL PROTECTED]
> Subject: RE: [nant-dev] Loop Task
> 
> Can you help the slower people in the group by explaining a bit more
> about the foreach task.  I can see how it could print out all the
> properties
> 
> * What do you plan on doing with it?
> * What is the syntax for iterating over the containers?
> * What containers can you iterate over?  properites, folders, files,
> tasks, targets??
> 
> I'm probally missing something obvious but it seems a bit weird.  On
the
> chance that you needed to do something like this why not just drop
down
> into C# using the <script> task?  It would seem to have a lot cleaner
> syntax IMO.



-------------------------------------------------------
This sf.net email is sponsored by: Jabber Inc.
Don't miss the IM event of the season | Special offer for OSDN members! 
JabConf 2002, Aug. 20-22, Keystone, CO http://www.jabberconf.com/osdn
_______________________________________________
Nant-developers mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/nant-developers

Reply via email to