Permalink
Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
273 lines (193 sloc) 9.3 KB
<?xml version="1.0" encoding="UTF-8"?>
<project default="build" basedir=".">
<property name="module.build.dir" value="${basedir}/build" />
<tstamp>
<format property="now" pattern="yyyyMMddHHmm" />
</tstamp>
<macrodef name="exec-node">
<attribute name="module" description="The name of the NodeJS module to execute" />
<attribute name="failonerror" default="true" description="Fail if the exit code is not 0" />
<attribute name="dir" default="${basedir}" description="" />
<element name="args" implicit="yes" description="Argument to pass to the exec task" />
<sequential>
<exec executable="cmd.exe" failonerror="@{failonerror}" osfamily="winnt" dir="@{dir}">
<arg line="/c @{module}" />
<args/>
<!-- Windows cmd output workaround: http://stackoverflow.com/a/10359327/227349 -->
<!-- Forces node's stderror and stdout to a temporary file -->
<arg line=" &gt; _tempfile.out 2&lt;&amp;1" />
<!-- If command exits with an error, then output the temporary file -->
<!-- to stdout delete the temporary file and finally exit with error level 1 -->
<!-- so that the apply task can catch the error if @failonerror="true" -->
<arg line=" || (type _tempfile.out &amp; del _tempfile.out &amp; exit /b 1)" />
<!-- Otherwise, just type the temporary file and delete it-->
<arg line=" &amp; type _tempfile.out &amp; del _tempfile.out &amp;" />
</exec>
<exec executable="@{module}" failonerror="@{failonerror}" osfamily="unix" dir="@{dir}">
<args/>
</exec>
</sequential>
</macrodef>
<macrodef name="exec-node-out">
<attribute name="module" description="The name of the NodeJS module to execute" />
<attribute name="failonerror" default="true" description="Fail if the exit code is not 0" />
<attribute name="dir" default="${basedir}" description="" />
<attribute name="outputproperty" description="" />
<element name="args" implicit="yes" description="Argument to pass to the exec task" />
<sequential>
<exec executable="cmd.exe" failonerror="@{failonerror}" osfamily="winnt" dir="@{dir}" outputproperty="@{outputproperty}">
<arg line="/c @{module}" />
<args/>
<!-- Windows cmd output workaround: http://stackoverflow.com/a/10359327/227349 -->
<!-- Forces node's stderror and stdout to a temporary file -->
<arg line=" &gt; _tempfile.out 2&lt;&amp;1" />
<!-- If command exits with an error, then output the temporary file -->
<!-- to stdout delete the temporary file and finally exit with error level 1 -->
<!-- so that the apply task can catch the error if @failonerror="true" -->
<arg line=" || (type _tempfile.out &amp; del _tempfile.out &amp; exit /b 1)" />
<!-- Otherwise, just type the temporary file and delete it-->
<arg line=" &amp; type _tempfile.out &amp; del _tempfile.out &amp;" />
</exec>
<exec executable="@{module}" failonerror="@{failonerror}" osfamily="unix" dir="@{dir}" outputproperty="@{outputproperty}">
<args/>
</exec>
</sequential>
</macrodef>
<target name="build" depends="clean, init, npm-install-dev, test" >
<echo message="Building ${package.name}" />
<antcall target="clean" />
<antcall target="npm-install" />
<antcall target="stage" />
<antcall target="generate-build-number" />
<antcall target="package" />
</target>
<target name="ci" depends="init, npm-install-dev, jshint, test" >
<echo message="Running Continuous Integration tests on ${package.name}" />
</target>
<target name="clean" depends="init">
<echo message="Cleaning ${package.name}" />
<!-- Remove build directories -->
<delete dir="${module.build.dir}" />
<delete dir="${basedir}/coverage" />
<delete dir="${basedir}/node_modules" />
</target>
<target name="dev-package" depends="init, npm-install-dev, stage, generate-build-number, package">
<echo message="Development package of ${package.name}" />
</target>
<target name="generate-build-number" depends="init">
<echo message="Generating build number" />
<property name="build.number" value="${now}" />
<echo message="${build.number}" file="${staging.dir}/build-number" />
<copy file="${staging.dir}/package.json" tofile="${staging.dir}/package.json.tmp" />
<delete file="${staging.dir}/package.json" />
<move file="${staging.dir}/package.json.tmp" tofile="${staging.dir}/package.json" />
</target>
<target name="init" depends="package-json">
<echo message="Initializing build for ${package.name}" />
<property name="staging.dir" value="${module.build.dir}/staging" />
<property name="dist.dir" value="${module.build.dir}/dist" />
<mkdir dir="${module.build.dir}" />
<mkdir dir="${staging.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<target name="jshint" depends="init, npm-install-dev">
<echo message="JSHinting ${package.name}" />
<exec-node module="node">
<arg value="node_modules/.bin/jshint" />
<arg value="./" />
</exec-node>
</target>
<target name="npm-install" depends="init">
<echo message="Resolving public dependencies for ${package.name}" />
<exec-node module="npm">
<arg value="install" />
<arg value="--production" />
</exec-node>
</target>
<target name="npm-install-dev" depends="init">
<echo message="Resolving dev dependencies for ${package.name}" />
<exec-node module="npm">
<arg value="install" />
</exec-node>
</target>
<target name="package" depends="init, stage">
<echo message="Packaging ${package.name} to a zip" />
<exec-node-out module="npm" dir="${dist.dir}" outputproperty="pack.out">
<arg value="pack" />
<arg value="${staging.dir}" />
</exec-node-out>
<copy file="${dist.dir}/${pack.out}" tofile="${dist.dir}/${package.name}-latest.tgz" />
</target>
<target name="package-json">
<echo message="Reading package.json" />
<script language="javascript">
importClass(java.io.File);
importClass(java.io.FileReader);
importClass(java.io.BufferedReader);
var file = new File(project.getProperty("basedir") + "/" + "package.json");
fr = new FileReader(file);
br = new BufferedReader(fr);
var data = "";
var line;
while ((line = br.readLine()) != null) {
data += line;
}
var properties = eval("(" + data + ")");
for (var i in properties) {
if (typeof properties[i] === 'string') {
project.setProperty("package." + i, properties[i]);
} else if (typeof properties[i] === 'object') {
for (var x in properties[i]) {
if (typeof properties[i][x] === 'string') {
project.setProperty("package." + i + "." + x, properties[i][x]);
}
}
}
}
</script>
</target>
<target name="publish" depends="clean, init">
<echo message="Publishing a new version of ${package.name}" />
<input message="What do you want to deploy? [major|minor|patch|build|_version_]" addproperty="npm.deploy" />
<!-- Create the tag -->
<exec-node module="npm" dir="${basedir}">
<arg value="version" />
<arg value="${npm.deploy}" />
</exec-node>
<antcall target="build" />
<!-- Publish to NPM -->
<exec-node module="npm">
<arg value="publish" />
<arg value="${dist.dir}/${package.name}-latest.tgz" />
</exec-node>
<!-- Push the tag -->
<exec-node module="git">
<arg value="push" />
<arg value="--tags" />
</exec-node>
<!-- Push the change -->
<exec-node module="git">
<arg value="push" />
</exec-node>
</target>
<target name="stage" depends="init">
<echo message="Staging ${package.name}" />
<echo message="${module.build.dir}" />
<copy todir="${staging.dir}" includeEmptyDirs="true">
<fileset dir=".">
<exclude name="build.xml" />
<exclude name="**/*build*/**" />
<exclude name="**/*coverage*/**" />
<exclude name="**/*nbproject*/**" />
<exclude name="**/*test*/**" />
<exclude name="**/.*/**" />
</fileset>
</copy>
</target>
<target name="test" depends="init, npm-install-dev">
<echo message="Running unit tests for ${package.name} with Mocha" />
<exec-node module="npm">
<arg value="test" />
</exec-node>
</target>
</project>