Skip to content

Instantly share code, notes, and snippets.

@aulizko
Created April 21, 2009 13:00
Show Gist options
  • Save aulizko/99127 to your computer and use it in GitHub Desktop.
Save aulizko/99127 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<project name="production-build" default="build" basedir=".">
<!-- место, куда будем складывать свежескачанные yui-compressor и jslint4java -->
<property name="tools.location" value="tools/"/>
<!-- какую версию yui compressor'а и откуда качать, а также, какое имя будет у получившегося jar-файла -->
<property name="yuicompressor-version" value="2.4.2"/>
<property name="yuicompressor-zip" value="yuicompressor-${yuicompressor-version}.zip"/>
<property name="yuicompressor-unzip-dir" value="yuicompressor-${yuicompressor-version}"/>
<property name="yuicompressor-location" value="http://www.julienlecomte.net/yuicompressor/"/>
<property name="yuicompressor-jar" value="yuicompressor-${yuicompressor-version}.jar"/>
<!-- какую версию jslint4java и откуда будем качать, а также, какое имя будет у получившегося jar-файла -->
<property name="jslint-version" value="1.2.1"/>
<property name="jslint-location" value="http://jslint4java.googlecode.com/files/"/>
<property name="jslint-zip" value="jslint4java-${jslint-version}.zip"/>
<property name="jslint-jar" value="jslint4java-${jslint-version}+rhino.jar"/>
<property name="jslint-unzip-dir" value="jslint4java-${jslint-version}"/>
<!-- откуда мы будем брать js-файлы, css-файлы, html-темплейт -->
<property environment="env"/>
<property name="js.src" value="js/"/>
<property name="css.src" value="css/"/>
<property name="template.name" value="index.html"/>
<property name="template" value="${template.name}"/>
<!-- и куда мы будем их все складывать -->
<property name="output.dir" value="build/"/>
<property name="js.out" value="${output.dir}/js/"/>
<property name="css.out" value="${output.dir}/css/"/>
<property name="template.out" value="${output.dir}/${template.name}"/>
<!-- порядок конкатенации js-файлов. Указанные файлы будут расположены в начале общего js-файла в указанном порядке -->
<!-- все оставшиеся файлы будут присоединены в конец файла -->
<property name="js-required-file-order" value="jquery-1.2.6.js, some_object.js"/>
<!-- эта задача всегда выполнится первой -->
<target name="init">
<tstamp>
<!-- запомним в качестве переменной текущее время в формате mm-hh-MM-dd-yyyy -->
<format property="build-time" pattern="mm-hh-MM-dd-yyyy"/>
</tstamp>
<!-- создаем директорию, содержащую yui compressor и jslint4java -->
<mkdir dir="${tools.location}"/>
</target>
<target name="prepare-tools" depends="init">
<!-- скачаем и распакуем jslint и yui compressor -->
<antcall target="prepare-yuicompressor"/>
<antcall target="prepare-jslint"/>
</target>
<!-- скачаем и подготовим к работе jslint -->
<target name="prepare-jslint" depends="check-if-jslint-exists" unless="jslint.exist">
<get src="${jslint-location}${jslint-zip}" dest="${tools.location}${jslint-zip}" verbose="true"/>
<unzip src="${tools.location}${jslint-zip}" dest="${tools.location}" />
<copy file="${tools.location}${jslint-unzip-dir}/${jslint-jar}" todir="${tools.location}"/>
<delete dir="${tools.location}${jslint-unzip-dir}"/>
<delete file="${tools.location}${jslint-zip}"/>
</target>
<!-- удостоверимся, что jslint скачан и готов к работе - эта задача выполняется непосредственно перед проверкой js-файлов -->
<target name="check-if-jslint-exists">
<condition property="jslint.exist">
<and>
<available file="${tools.location}${jslint-jar}"/>
</and>
</condition>
</target>
<!-- скачаем и подготовим к работе jslint -->
<target name="prepare-yuicompressor" depends="check-if-yuicompressor-exists" unless="yuicompressor.exist">
<get src="${yuicompressor-location}${yuicompressor-zip}" dest="${tools.location}${yuicompressor-zip}" verbose="true"/>
<unzip src="${tools.location}${yuicompressor-zip}" dest="${tools.location}" />
<copy file="${tools.location}${yuicompressor-unzip-dir}/build/${yuicompressor-jar}" todir="${tools.location}"/>
<delete dir="${tools.location}${yuicompressor-unzip-dir}"/>
<delete file="${tools.location}${yuicompressor-zip}"/>
</target>
<!-- удостоверимся, что jslint скачан и готов к работе - эта задача выполняется непосредственно перед сжатием js/css-файлов -->
<target name="check-if-yuicompressor-exists">
<condition property="yuicompressor.exist">
<and>
<available file="${tools.location}${yuicompressor-jar}"/>
</and>
</condition>
</target>
<!-- валидируем javascript -->
<target name="validate-javascript" depends="prepare-tools">
<apply executable="java" parallel="false" failonerror="false">
<fileset dir="${js.src}">
<include name="**/*.js"/>
<!-- файлы библиотек тестировать не нужно, их и другие люди уже оттестировали -->
<exclude name="**/jquery-1.2.6.js"/>
</fileset>
<arg value="-jar" />
<arg file="${tools.location}${jslint-jar}" />
<arg value="--bitwise" />
<arg value="--browser" />
<arg value="--undef" />
<arg value="--widget" />
<srcfile />
</apply>
</target>
<!-- сжимаем js/css-файлы -->
<target name="compress" depends="prepare-tools, concatenate-files">
<apply executable="java" parallel="false" failonerror="true" dest="${js.out}" verbose="true" force="true">
<fileset dir="${js.out}" includes="*.js"/>
<arg line="-jar"/>
<arg path="${tools.location}${yuicompressor-jar}"/>
<arg line="--line-break 8000"/>
<arg line="-o"/>
<targetfile/>
<srcfile/>
<mapper type="glob" from="*.js" to="*.js"/>
</apply>
<apply executable="java" parallel="false" failonerror="true" dest="${css.out}" verbose="true" force="true">
<fileset dir="${css.out}" includes="*.css" excludes="ie_*.css"/>
<arg line="-jar"/>
<arg path="${tools.location}${yuicompressor-jar}"/>
<arg line="--line-break 0"/>
<srcfile/>
<arg line="-o"/>
<mapper type="glob" from="*.css" to="*.css"/>
<targetfile/>
</apply>
</target>
<!-- удаляем все старые css и js файлы в темплейте, а затем вставляем ссылки на наши сжатые файлы -->
<target name="update-tags" depends="prepare-tools">
<copy file="${template}" tofile="${template.out}" overwrite="true"/>
<replaceregexp file="${template.out}" match="&lt;script\s+type=&quot;text/javascript&quot;\s+src=&quot;[A-Za-z0-9._\-/]*&quot;&gt;&lt;/script&gt;" flags="igm" replace=""/>
<replaceregexp file="${template.out}" match="(&lt;script*|&lt;/body&gt;)" flags="im" replace="&lt;script type=&quot;text/javascript&quot; src=&quot;js/main-${build-time}.js&quot;&gt;&lt;/script&gt;${line.separator}\1"/>
<replaceregexp file="${template.out}" match="&lt;link[^&gt;]*href=&quot;css/[^i&gt;]{1}[^e&gt;]{1}[^_&gt;]{1}[^&gt;]*/&gt;" flags="igm" replace=""/>
<replaceregexp file="${template.out}" match="&lt;/head&gt;" flags="im" replace="&lt;link rel=&quot;stylesheet&quot; href=&quot;css/main-${build-time}.css&quot; type=&quot;text/css&quot; /&gt;&lt;/head&gt;"/>
</target>
<!-- конкатенация файлов -->
<target name="concatenate-files" depends="update-tags">
<concat destfile="${js.out}/main-${build-time}.js" fixlastline="true">
<filelist dir="${js.src}"
files="${js-required-file-order}"/>
<fileset dir="${js.src}"
includes="**/*.js"
excludes="${js-required-file-order}"/>
</concat>
<copy todir="${css.out}">
<fileset dir="${css.src}" includes="**/ie_*.css"/>
</copy>
<concat destfile="${css.out}/main-${build-time}.css" fixlastline="true">
<fileset dir="${css.src}"
includes="**/*.css"
excludes="**/ie_*.css"/>
</concat>
</target>
<target name="build">
<mkdir dir="${output.dir}"/>
<antcall target="validate-javascript"/>
<antcall target="compress"/>
</target>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment