One of the projects I'm working on is conditional publishing of DITA content using ant. Basically what I want to do is, given a list of files, build the output that is affected when those files change.
We manage our files using Perforce, so the workflow is something like this:
- Check out files
- Edit/modify content
- Run conditional build
- Review output
- Check-in files
When you check files out of Perforce, they get put into a changelist, so, to get my file list, I need to ask Perforce what files are in the changelist. Perforce has a nice way to do that. The command is,
p4 opened -c <changelist_number>
The problem is the output includes a bunch of information I don't want.
I get lines like this:
//doc/main/core/build/build.xml#164 - edit change 1075456 (text) by sanderson@docbuild
what I want is
//doc/main/core/build/build.xml
or, even better,
files-in-changelist=/home/sanderson/doc/main/core/build/build.xml
That format is the format ant expects for a property files. Property files are nice, because you can load them up and use that property in ant.
What to do, what to do? In my case, I turn to ant. If I have a build issue, someone else has probably run into it, so, I look there first.
Sure enough, ant has a task called filterchain for exactly these kinds of uses. Here's what I wound up with:
<target name="getFilesFromPerforceChangelist" if="changelist">
<property name="ant.regexp.regexpimpl" value="org.apache.tools.ant.util.regexp.JakartaOroRegexp"/>
<exec executable="p4" output="changelist.txt" append="false" failonerror="true">
<arg value="opened"/>
<arg value="-c"/>
<arg value="${changelist}"/>
</exec>
<copy file="changelist.txt" tofile="changelist.properties">
<filterchain>
<replaceregex pattern="#.*" replace=","/>
<replaceregex pattern=".*build" replace="${basedir}"/>
<striplinebreaks/>
<prefixlines prefix="files-in-changelist="/>
</filterchain>
</copy>
</target>
Just another reason to love ant.
