views:

24

answers:

1

I have a set of targets that each do essentially the same thing except each contains a specific patternset on which to perform its tasks. I want to collapse these targets into a single "reusable" target that instead takes a set of files "as a parameter".

For example, this

<target name="echo1">
  <foreach item="File" property="fn">
    <in>
      <items>
        <include name="*.config"/>
      </items>
    </in>
    <do>
      <echo message="${fn}" />
    </do>
  </foreach>
</target>

<target name="echo2">
  <foreach item="File" property="fn">
    <in>
      <items>
        <include name="*.xml"/>
      </items>
    </in>
    <do>
      <echo message="${fn}" />
    </do>
  </foreach>
</target>

<target name="use">
  <call target="echo1"/>
  <call target="echo2"/>
</target>

would be replaced by

<patternset id="configs">
   <include name="*.config"/>
</patternset>

<patternset id="xmls">
   <include name="*.xml"/>
</patternset>

<target name="echo">
  <foreach item="File" property="fn">
    <in>
      <items>
        <patternset refid="${sourcefiles}"/>
      </items>
    </in>
    <do>
      <echo message="${fn}" />
    </do>
  </foreach>
</target>

<target name="use">
  <property name="sourcefiles" value="configs"/>
  <call target="echo"/>
  <property name="sourcefiles" value="xmls"/>
  <call target="echo"/>
</target>

However, it turns out that refid is not expanded as answered in a nant-dev email posting because patternsets and filesets differ from properties. In this non-working code, when echo is called, its patternset element references a patternset literally named ${sourcefiles} instead of the one named test.

How would one write a re-usable NAnt target that operates on a varying set of files? Is there a way to do this in NAnt as-is without resorting to writing custom tasks?

A: 

I'm not sure I completely understood what You're trying to achieve, but shouldn't attribute dynamic of task property do the job?

<target name="filesettest">
  <property name="sourcefiles" value="test" dynamic="true" />
  <!-- ... -->
</target>
The Chairman
That was actually my second attempt, but that controls when the property gets its value. In NAnt code, reference types used for tasks aren't subject to property expansion so no luck there.
Kit