0

I've got foo.js, and an ant build process that results in foo.min.js.

foo.js has a header comment that includes:

* $Id: foo.js 12345 2011-10-04 14:35:23Z itoltz $

Where 12345 is the revision of the file when committed to SVN.

I'd like to copy foo.min.js to foo.min.12345.js

Asmor
  • 5,043
  • 6
  • 32
  • 42

1 Answers1

2

You can extract the revision number into a property using loadfile and regex. Then you can copy the file using the property.

<project default="rename">

  <target name="rename" depends="get-rev">
    <copy file="foo.min.js" toFile="foo.min.${revision.number}.js"/>
  </target>

  <target name="get-rev">
    <loadfile srcFile="foo.js" property="revision.number">
      <filterchain>
        <linecontainsregexp>
          <regexp pattern="\* \$Id: foo.js"/>
        </linecontainsregexp>
        <tokenfilter>
          <replaceregex pattern="\* \$Id: foo.js (\d+).*" replace="\1"/>
        </tokenfilter>
        <striplinebreaks/>
      </filterchain>
    </loadfile>
    <echo message="revision.number: ${revision.number}"/>
  </target>

</project>

Output:

$ ls
build.xml  foo.js  foo.min.js
$
$ ant
Buildfile: C:\tmp\build.xml

get-rev:
     [echo] revision.number: 12345

rename:
     [copy] Copying 1 file to C:\tmp

BUILD SUCCESSFUL
Total time: 0 seconds
$
$ ls
build.xml  foo.js  foo.min.12345.js  foo.min.js
ewan.chalmers
  • 16,145
  • 43
  • 60
  • Actually, there is one tiny problem I can't figure out. It's somehow capturing a space! E.g. the file is named "foo.min. 12345.js". I modified the echo to , and the output was: [echo] revision.number: - 15354-. Any idea? I can't even see how it could be capturing a space, since it's specifically capturing \d+... – Asmor Oct 07 '11 at 16:12
  • I fixed it by adding to the end of the filter chain, but I'm still curious if anyone knows why this is happening. – Asmor Oct 07 '11 at 16:15