Author: kevj
Date: Sat Jun 6 11:46:05 2009
New Revision: 782238
URL: http://svn.apache.org/viewvc?rev=782238&view=rev
Log:
-refactor, convenience classes and initial documentation
Added:
ant/sandbox/antlibs/compress/trunk/docs/yui.compressor.html (with props)
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICssCompressor.java
(with props)
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUIJsCompressor.java
(with props)
Modified:
ant/sandbox/antlibs/compress/trunk/README
ant/sandbox/antlibs/compress/trunk/src/etc/testcases/compress.xml
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICompressorTask.java
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/antlib.xml
Modified: ant/sandbox/antlibs/compress/trunk/README
URL:
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/README?rev=782238&r1=782237&r2=782238&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/README (original)
+++ ant/sandbox/antlibs/compress/trunk/README Sat Jun 6 11:46:05 2009
@@ -8,7 +8,11 @@
<project name="x" basedir="." default="y"
xmlns:compress="antlib:org.apache.ant.js.compressor">
# Compressing many javascript files
-<compress:yui.compressor verbose="true" outputPath="${outputPath}" type="js">
+<compress:yui.compressor
+ verbose="true"
+ outputPath="${outputPath}"
+ linebreak="80"
+ type="js">
<fileset dir="${testscript.dir}">
<include name="*.js"/>
</fileset>
Added: ant/sandbox/antlibs/compress/trunk/docs/yui.compressor.html
URL:
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/docs/yui.compressor.html?rev=782238&view=auto
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/docs/yui.compressor.html (added)
+++ ant/sandbox/antlibs/compress/trunk/docs/yui.compressor.html Sat Jun 6
11:46:05 2009
@@ -0,0 +1,92 @@
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<html>
+
+<head>
+<meta http-equiv="Content-Language" content="en-us">
+<title>Yahoo! UI Compressor Task</title>
+<link rel="stylesheet" type="text/css" href="../stylesheets/antmanual.css">
+</head>
+
+<body>
+
+<h2><a name="yui.compressor">Compressor</a></h2>
+<h3>Description</h3>
+<p>Compresses or 'minifies' JavaScript or CSS files using the
+<a href="http://developer.yahoo.com/yui/compressor/" target="_top">Yahoo! UI
Compressor library</a></p>
+<p><b>Note:</b> This task depends on external libraries not included in the
Ant distribution.</p>
+<h3>Installation</h3>
+<p>Download the <a href="http://developer.yahoo.com/yui/compressor/"
target="_top">Yahoo! UI Compressor distribution</a>
+and copy the yuicompressor-n.n.n.jar to $ANT_HOME/lib; ~/.ant/lib or the ant
$CLASSPATH.
+Also take a copy of this antlib and place in the same location.</p>
+<p>Utilise the antlib by referencing it:
+<pre>
+<project name="compress-test" basedir="."
+ default="all" xmlns:compress="antlib:org.apache.ant.js.compressor">
+</pre>
+</p>
+
+<h3>Parameters</h3>
+<table border="1" cellpadding="2" cellspacing="0">
+ <tr>
+ <td valign="top"><b>Attribute</b></td>
+ <td valign="top"><b>Description</b></td>
+ <td align="center" valign="top"><b>Required</b></td>
+ </tr>
+ <tr>
+ <td>type</td>
+ <td>The type of files to compress valid options are <code>js</code> or
<code>css</code></td>
+ <td>YES</td>
+ </tr>
+ <tr>
+ <td>charset</td>
+ <td>The character set encoding of the input file</td>
+ <td>NO [defaults to UTF-8 if not specified]</td>
+ </tr>
+ <tr>
+ <td>linebreak</td>
+ <td>The column to break at in the output file</td>
+ <td>NO [defaults to never breaking lines]</td>
+ </tr>
+ <tr>
+ <td>verbose</td>
+ <td>Sets the output of the task to display all warnings while
minifying</td>
+ <td>NO [defaults to no]</td>
+ </tr>
+</table>
+
+<h3>Parameters specified as nested elements</h3>
+<h4>fileset</h4>
+ <p><a href="../CoreTypes/fileset.html">FileSet</a>s are used to select
+sets of files to copy.</p>
+
+<h3>Examples</h3>
+<pre>
+<compress:yui.compressor
+ verbose="true"
+ outputPath="${outputPath}"
+ linebreak="80"
+ type="js">
+
+ <fileset dir="${testscript.dir}">
+ <include name="*.js"/>
+ </fileset>
+</compress:yui.compressor>
+</pre>
+<p>Minifies all javascript files found in the fileset. In the output file
lines are broken after 80 chars.</p>
+</body>
+</html>
\ No newline at end of file
Propchange: ant/sandbox/antlibs/compress/trunk/docs/yui.compressor.html
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: ant/sandbox/antlibs/compress/trunk/src/etc/testcases/compress.xml
URL:
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/etc/testcases/compress.xml?rev=782238&r1=782237&r2=782238&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/etc/testcases/compress.xml (original)
+++ ant/sandbox/antlibs/compress/trunk/src/etc/testcases/compress.xml Sat Jun
6 11:46:05 2009
@@ -19,7 +19,6 @@
<project name="compress-test" basedir="."
default="all" xmlns:compress="antlib:org.apache.ant.js.compressor">
- <property name="output" value="test.js"/>
<property name="outputPath" value="${basedir}/min"/>
<property name="testscript.dir" value="${basedir}/testscripts"/>
@@ -28,14 +27,11 @@
</target>
<target name="all" depends="init">
-
- <compress:yui.compressor verbose="true" output="${output}"
mergefiles="true" type="js">
- <fileset dir="${testscript.dir}">
- <include name="*.js"/>
- </fileset>
- </compress:yui.compressor>
-
- <compress:yui.compressor verbose="true" outputPath="${outputPath}"
type="js">
+ <compress:yui.compressor
+ verbose="true"
+ outputPath="${outputPath}"
+ linebreak="80"
+ type="js">
<fileset dir="${testscript.dir}">
<include name="*.js"/>
</fileset>
@@ -46,10 +42,23 @@
<include name="*.css"/>
</fileset>
</compress:yui.compressor>
+
+ <!-- specialised tasks -->
+ <compress:yui.js verbose="false" outputPath="${outputPath}"
linebreak="80">
+ <fileset dir="${testscript.dir}">
+ <include name="*.js"/>
+ </fileset>
+ </compress:yui.js>
+
+ <compress:yui.css verbose="false" outputPath="${outputPath}"
linebreak="80">
+ <fileset dir="${testscript.dir}">
+ <include name="*.css"/>
+ </fileset>
+ </compress:yui.css>
</target>
<target name="cleanup">
<delete file="${output}"/>
<delete dir="${outputPath}"/>
</target>
-</project>
+</project>
\ No newline at end of file
Modified:
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICompressorTask.java
URL:
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICompressorTask.java?rev=782238&r1=782237&r2=782238&view=diff
==============================================================================
---
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICompressorTask.java
(original)
+++
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICompressorTask.java
Sat Jun 6 11:46:05 2009
@@ -1,19 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
package org.apache.ant.js.compressor;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
+import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
-import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.resources.FileResource;
@@ -22,8 +41,12 @@
import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
-import com.yahoo.platform.yui.compressor.YUICompressor;
+/**
+ * Base task for wrapping the Yahoo! UI compressor library
+ * Requires yui compressor jar in $ANT_HOME/lib; ~/.ant/lib or on CLASSPATH
+ * @author [email protected]
+ */
public class YUICompressorTask extends Task {
private boolean verbose;
@@ -33,62 +56,89 @@
private boolean preserveSemi;
private boolean disableOptimization;
private String charset;
- private String output;
private String outputPath;
- private boolean mergeFiles;
private List inputResources = new ArrayList();
-
+
public void execute() {
try {
- if(mergeFiles) {
- //TODO
- //refactor to remove the commandline building
- Commandline cmd = buildArgs();
- YUICompressor.main(cmd.getArguments());
- } else {
- for(Iterator i = inputResources.iterator();
i.hasNext();) {
- FileSet fs = (FileSet)i.next();
- for(Iterator j = fs.iterator();
j.hasNext();) {
- FileResource f =
(FileResource)j.next();
- if(verbose) {
- log("Minifying:
"+f.getFile().getAbsolutePath());
- }
- //Ensure charset is set
- if(null == charset ||
charset.trim() == "") {
- charset = "UTF-8";
- }
- InputStreamReader in = new
InputStreamReader(new FileInputStream(f.getFile().getAbsolutePath()), charset);
- Writer out;
- if(null != outputPath &&
outputPath.trim() != "") {
- out = new
OutputStreamWriter(new FileOutputStream(outputPath + File.separator +
f.getFile().getName()));
- } else {
- out = new
OutputStreamWriter(new FileOutputStream(f.getFile().getAbsolutePath()));
- }
-
- if(null != type && type.trim()
!= "" && "js".equals(type)) {
- JavaScriptCompressor c
= getJavaScriptCompressor(in);
- c.compress(out,
- (null
== getLineBreak() || getLineBreak() == "" ? -1 :
Integer.parseInt(getLineBreak())),
-
isNomunge(),
-
isVerbose(),
-
isPreserveSemi(),
-
isDisableOptimization()
- );
- } else if(null != type && type
!= "" && "css".equals(type)) {
- CssCompressor c =
getCssCompressor(in);
- c.compress(out, (null
== getLineBreak() || getLineBreak() == "" ? -1 :
Integer.parseInt(getLineBreak())));
- }
- in.close();
- out.close();
- }
-
- }
- }
+ validate();
+ for(Iterator i = inputResources.iterator();
i.hasNext();) {
+ FileSet fs = (FileSet)i.next();
+ for(Iterator j = fs.iterator(); j.hasNext();) {
+ FileResource f = (FileResource)j.next();
+ minify(f);
+ }
+ }
} catch (Exception e) {
- log("Error occurred processing file "+ e.getMessage());
+ log("Error occurred processing file ");
+ e.printStackTrace();
+ }
+ }
+
+ private void validate() throws BuildException {
+ if(null == type || "".equals(type)) {
+ throw new BuildException("Type property must be
specified <js|css>");
+ }
+ if(null == charset || "".equals(charset)) {
+ setCharset("UTF-8");
+ log("Charset not set; using [UTF-8]");
+ }
+ if(null == lineBreak || "".equals(lineBreak)) {
+ setLineBreak("-1");
+ log("Linebreak position not set; using [-1]");
+ } else if (lineBreak.matches("\\D+")) {
+ throw new BuildException("Linebreak must be an
integer");
}
}
+ /**
+ * @param f the FileResource to compress
+ * @throws UnsupportedEncodingException
+ * @throws FileNotFoundException
+ * @throws IOException
+ */
+ private void minify(FileResource f) throws UnsupportedEncodingException,
+ FileNotFoundException, IOException {
+
+ Reader in = new InputStreamReader(new
FileInputStream(f.getFile().getAbsolutePath()), charset);
+ Writer out = getWriter(f);
+ if(verbose) {
+ log("Minifying: "+f.getFile().getAbsolutePath());
+ }
+
+ if("js".equals(getType())) {
+ JavaScriptCompressor c = getJavaScriptCompressor(in);
+ //in case we are overwriting the input file, ensure
that it's closed first
+ in.close();
+ in = null;
+ c.compress(out,
+ Integer.parseInt(getLineBreak()),
+ isNomunge(),
+ isVerbose(),
+ isPreserveSemi(),
+ isDisableOptimization()
+ );
+ out.close();
+ } else if("css".equals(getType())) {
+ CssCompressor c = getCssCompressor(in);
+ //in case we are overwriting the input file, ensure
that it's closed first
+ in.close();
+ in = null;
+ c.compress(out, Integer.parseInt(getLineBreak()));
+ out.close();
+ }
+ }
+
+ private Writer getWriter(FileResource f) throws IOException {
+ Writer out;
+ out = new OutputStreamWriter(
+ null != getOutputPath() &&
getOutputPath().trim() != "" ?
+ new
FileOutputStream(getOutputPath() + File.separator + f.getFile().getName()) :
+ f.getOutputStream()
+ );
+ return out;
+ }
+
protected JavaScriptCompressor getJavaScriptCompressor(Reader in)
throws IOException {
JavaScriptCompressor compressor = new JavaScriptCompressor(in,
new ErrorReporter() {
@@ -125,51 +175,6 @@
return new CssCompressor(in);
}
- protected Commandline buildArgs() {
- Commandline cmd = new Commandline();
- if(verbose) {
- cmd.createArgument().setValue("-v");
- }
- if(nomunge) {
- cmd.createArgument().setValue("--nomunge");
- }
- if(preserveSemi) {
- cmd.createArgument().setValue("--preserve-semi");
- }
- if(disableOptimization) {
-
cmd.createArgument().setValue("--disable-optimizations");
- }
- if(null != lineBreak && lineBreak.trim() != "") {
- cmd.createArgument().setValue("--line-break");
- cmd.createArgument().setValue(lineBreak);
- }
- if(null != type && type.trim() != "") {
- cmd.createArgument().setValue("--type");
- cmd.createArgument().setValue(type);
- }
- if(null != charset && charset.trim() != "") {
- cmd.createArgument().setValue("--charset");
- cmd.createArgument().setValue(charset);
- }
- if(null != output && output.trim() != "" && mergeFiles) {
- cmd.createArgument().setValue("-o");
- cmd.createArgument().setValue(output);
- }
-
- if(mergeFiles) {
- for(Iterator i = inputResources.iterator();
i.hasNext();) {
- FileSet fs = (FileSet)i.next();
- for(Iterator j = fs.iterator(); j.hasNext();) {
- FileResource f = (FileResource)j.next();
-
cmd.createArgument().setValue(f.getFile().getAbsolutePath());
- }
-
- }
- }
- log("args: "+cmd.describeCommand());
- return cmd;
- }
-
public boolean isVerbose() {
return verbose;
}
@@ -226,14 +231,6 @@
this.charset = charset;
}
- public String getOutput() {
- return output;
- }
-
- public void setOutput(String output) {
- this.output = output;
- }
-
public String getOutputPath() {
return outputPath;
}
@@ -249,12 +246,4 @@
public void add(ResourceCollection res) {
inputResources.add(res);
}
-
- public boolean isMergeFiles() {
- return mergeFiles;
- }
-
- public void setMergeFiles(boolean m) {
- mergeFiles = m;
- }
-}
+}
\ No newline at end of file
Added:
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICssCompressor.java
URL:
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICssCompressor.java?rev=782238&view=auto
==============================================================================
---
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICssCompressor.java
(added)
+++
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICssCompressor.java
Sat Jun 6 11:46:05 2009
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.ant.js.compressor;
+
+/**
+ * Task for compressing CSS files using the Yahoo! UI compressor
+ * Requires yui compressor jar in $ANT_HOME/lib; ~/.ant/lib or on CLASSPATH
+ * @author [email protected]
+ */
+public class YUICssCompressor extends YUICompressorTask {
+
+ public void execute() {
+ super.setType("css");
+ super.execute();
+ }
+}
\ No newline at end of file
Propchange:
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUICssCompressor.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUIJsCompressor.java
URL:
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUIJsCompressor.java?rev=782238&view=auto
==============================================================================
---
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUIJsCompressor.java
(added)
+++
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUIJsCompressor.java
Sat Jun 6 11:46:05 2009
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.ant.js.compressor;
+
+/**
+ * Task for compressing JavaScript files using the Yahoo! UI compressor
+ * Requires yui compressor jar in $ANT_HOME/lib; ~/.ant/lib or on CLASSPATH
+ * @author [email protected]
+ */
+public class YUIJsCompressor extends YUICompressorTask {
+
+ public void execute() {
+ super.setType("js");
+ super.execute();
+ }
+}
\ No newline at end of file
Propchange:
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/YUIJsCompressor.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/antlib.xml
URL:
http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/antlib.xml?rev=782238&r1=782237&r2=782238&view=diff
==============================================================================
---
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/antlib.xml
(original)
+++
ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/js/compressor/antlib.xml
Sat Jun 6 11:46:05 2009
@@ -1,6 +1,33 @@
+<?xml version="1.0"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
<antlib>
<taskdef
name="yui.compressor"
classname="org.apache.ant.js.compressor.YUICompressorTask"
/>
+
+ <taskdef
+ name="yui.js"
+ classname="org.apache.ant.js.compressor.YUIJsCompressor"
+ />
+
+ <taskdef
+ name="yui.css"
+ classname="org.apache.ant.js.compressor.YUICssCompressor"
+ />
</antlib>
\ No newline at end of file