Search code examples
nant

Using NAnt filterchain to instantiate multiple template files


I have a set of template files like

<!-- @TEMPLATE@ -->
<!-- @DONOTEDIT@ -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SuperDuper @VARIANT@</title>
</head>
<body>
...
  @PRODUCTCODENAME@
...
</body>

which I want to deploy using NAnt so that the embedded keywords are instantiated, as in

<target name="deployTemplates">
  <property name="_buildstamp" value="${DestDir}/templates_buildstamp"/>
  <uptodate property="_uptodate" verbose="${Verbose}">
    <sourcefiles basedir="${TemplateDir}">
      <include name="**"/>
    </sourcefiles>
    <targetfiles>
      <include name="${_buildstamp}"/>
    </targetfiles>
  </uptodate>
  <if test="${not _uptodate}">
    <copy todir="${CourseDestDir}" verbose="${Verbose}" overwrite="True">
      <!-- Have to specify the fileset again to maintain directory structure. -->
      <fileset basedir="${TemplateDir}">
        <include name="**"/>
      </fileset>
      <filterchain>
        <replacetokens>
          <token key="DONOTEDIT" value="Do not edit, will be overwritten."/>
          <token key="PRODUCTCODENAME" value="${Product}"/>
          <token key="VARIANT" value="${Variant}"/>
        </replacetokens>
      </filterchain>
    </copy>
    <touch file="${_buildstamp}"/>
  </if>
</target>

but how do I embed the name of the individual template files in each deployed file, that is, get @TEMPLATE@ replaced by the location of the template file so that others will know where to make permanent changes?


Solution

  • Looking into NAnt source code and <filterchain> documentation - you will have to implement a custom Filter, which makes the current file name available for replacements. The existing filters do not expose this information.