Jan,

now it even looks aesthetic :-)

I've integrated jslint which checks the syntax (see
http://www.crockford.com) using another macrodef, and if all files
pass runs them through jsmin.  Well, I'm aware that this may get OT,
but I must say I learnt an awful lot.  Thanks again, Jan.

Hmm, just one small problem I couldn't solve yet.  Apply will bail out
if it tries to output a file to a directory that doesn't exist.

I attach the build.xml file (we'll see if it gets through ... Jakarta
mailing list guidelines don't say anything about attachments).  Thanks
again!

--
cheers,
Jakob.
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: build.xml 831 2005-11-25 16:13:50Z dncjfix $ -->
<project name="jsmin" basedir="." default="usage">
	<description>
		This buildfile takes the javascript files in the input directory,
	    checks their syntax using jslint, and compresses them to output
		directory.
		
		Note that any subdirectories in the input directory must be present
	    in the output directory or the jsmin won't be able to run.
		
		Note further that a recent version of Ant is required.  I'm using
		1.6.5.
		
		References:
		- jslint and jsmin: http://www.crockford.com/
		- http://article.gmane.org/gmane.comp.jakarta.ant.user/34812
	</description>
    
	<!-- ================================= 
          properties
         ================================= -->
	<!-- where to find the jsmin.exe -->
	<property name="jsmin.exe" location="./jsmin.exe"/>
	<!-- where to find cscript exe necessary for jslint -->
	<property name="cscript.exe" location="C:\WINDOWS\System32\cscript.exe"/>
	<!-- where to find the jslint.js Javascript file -->
	<property name="jslint.js" location="./jslint.js"/>
	<!-- can be overridden by -Ddir.in=.... -->
	<property name="dir.in" value="in"/>
	<!-- can be overridden by -Ddir.out=.... -->
	<property name="dir.out" value="out"/>
	<!-- comment with which to prepend the output file -->
	<property name="comment" value="(c) 2005 whatever"/>

    <!-- ================================= 
          macrodef: jsmin
         ================================= -->
	<!-- equivalent commandline:
		C:/> jsmin < big.js > small.js "a comment" -->
	<macrodef name="jsmin">
		<!-- Where to find the development version of JavaScript files -->
	    <attribute name="srcDir"/>
	    <!-- Where to store the 'compressed' files -->
	    <attribute name="destDir"/>
	    <sequential>
	    	<!-- Iterate over the JS-files and dont pass the filename
	    	     as argument -->
	        <apply executable="jsmin" addsourcefile="false">
	        	<!-- Collect the JS-files -->
	            <fileset dir="@{srcDir}">
	            	<include name="*.js"/>
	            	<include name="**/*.js"/>
            	</fileset>
	            <redirector>
	            	<!-- redirect STDIN; fileset collects relative to its 
	            	     dir, but we need relative to basedir -->
	                <inputmapper type="glob" from="*" to="@{srcDir}/*"/>
	                <!-- redirect STDOUT to file in dest-dir -->
	                <outputmapper id="out" 
	                	type="glob" from="*.js" to="@{destDir}/*.js"/>
				</redirector>
	        	<arg value="${comment}"/>
			</apply>
		</sequential>
	</macrodef>
	
    <!-- ================================= 
          macrodef: jslint
         ================================= -->
	<!-- equivalent commandline:
		C:/> cscript //Nologo jslint.js < big.js -->
	<macrodef name="jslint">
		<attribute name="srcDir"/>
		<sequential>
			<apply executable="${cscript.exe}" failonerror="true">
				<fileset dir="@{srcDir}">
					<include name="*.js"/>
					<include name="**/*.js"/>
				</fileset>
				<arg value="//Nologo"/>
				<arg file="${jslint.js}"/>
				<redirector>
					<inputmapper type="glob" from="*" to="@{srcDir}/*"/>
				</redirector>
			</apply>
		</sequential>
	</macrodef>
	
    <!-- ================================= 
          target: jsmin              
         ================================= -->
    <target name="jsmin" depends="jslint" 
    	description="take all js files and run them through jsmin">
        <jsmin srcDir="${dir.in}" destDir="${dir.out}"/>
    </target>
    
    <!-- ================================= 
          target: jslint
         ================================= -->
	<target name="jslint" depends="init"
		description="check syntax of js files in dir.in">
		<jslint srcDir="${dir.in}"/>
	</target>

	<!-- ================================= 
          target: usage
         ================================= -->
    <target name="usage" description="How to use this build file">
        <echo>
Usage: ant jsmin  to minimize a given directory, calls jslint first
        	      the minimized files are saved in the given destination
                  directory
                  you can specify the directories using -Din.dir=.....
                  and -Dout.dir=.....
        	
       ant jslint only run jslint over files in the directory specified
                  by -Din.dir=.....
        </echo>    	
    </target>
	
	<!-- ================================= 
          target: init
         ================================= -->
    <target name="init">
	    <tstamp/>
    </target>
</project>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to