tapestry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject [1/2] git commit: Add support for Less compilation
Date Thu, 23 May 2013 01:16:38 GMT
Updated Branches:
  refs/heads/master d111e421b -> 74dcf0bd0


Add support for Less compilation


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/09a94cd5
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/09a94cd5
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/09a94cd5

Branch: refs/heads/master
Commit: 09a94cd5926e061212b09f78576772dae68e1ce9
Parents: d111e42
Author: Howard M. Lewis Ship <hlship@apache.org>
Authored: Wed May 22 17:34:26 2013 -0700
Committer: Howard M. Lewis Ship <hlship@apache.org>
Committed: Wed May 22 17:34:26 2013 -0700

----------------------------------------------------------------------
 54_RELEASE_NOTES.txt                               |    5 +-
 .../wro4j/CoffeeScriptResourceCompiler.java        |   71 ---------------
 .../wro4j/ResourceProcessorSourceImpl.java         |    7 ++-
 .../internal/wro4j/ResourceTransformerFactory.java |   43 +++++++++
 .../wro4j/ResourceTransformerFactoryImpl.java      |   70 ++++++++++++++
 .../tapestry5/wro4j/modules/WRO4JModule.java       |   25 ++++--
 .../src/test/groovy/t5/wro4j/pages/Index.groovy    |    2 +-
 .../src/test/resources/META-INF/assets/index.less  |    5 +
 8 files changed, 146 insertions(+), 82 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/09a94cd5/54_RELEASE_NOTES.txt
----------------------------------------------------------------------
diff --git a/54_RELEASE_NOTES.txt b/54_RELEASE_NOTES.txt
index 0ce07d9..884ed40 100644
--- a/54_RELEASE_NOTES.txt
+++ b/54_RELEASE_NOTES.txt
@@ -57,8 +57,8 @@ asset URLs are distinct.
 Tapestry 5.4 now re-writes CSS files, expanding any `url()` references in them into fully
qualify URLs; this
 is to allow for the checksum embedded into each URL, which breaks relative references.
 
-Tapestry 5.4 introduces a new module, tapestry-wro4j, that provides support for compiling
CoffeeScript and JavaScript,
-and for minimizing CSS and JavaScript. All processing takes place at runtime.
+Tapestry 5.4 introduces a new module, tapestry-wro4j, that provides support for compiling
CoffeeScript into JavaScript,
+Less into CSS, and for minimizing CSS and JavaScript. All processing takes place at runtime.
 
 ## ControlGroup Mixin
 
@@ -91,6 +91,7 @@ for example).
 
 By default, tapestry-wro4j enables:
 - compilation of CoffeeScript to JavaScript (using Java's Rhino JavaScript engine)
+- compilation of Less to CSS
 - minification of CSS using Andy Robert's csscompressor
 - minification of JavaScript using the Google Closure compiler (in simple optimizations mode)
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/09a94cd5/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/CoffeeScriptResourceCompiler.java
----------------------------------------------------------------------
diff --git a/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/CoffeeScriptResourceCompiler.java
b/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/CoffeeScriptResourceCompiler.java
deleted file mode 100644
index 432dfe5..0000000
--- a/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/CoffeeScriptResourceCompiler.java
+++ /dev/null
@@ -1,71 +0,0 @@
-// Copyright 2013 The Apache Software Foundation
-//
-// Licensed 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.tapestry5.internal.wro4j;
-
-import org.apache.tapestry5.ioc.Resource;
-import org.apache.tapestry5.services.assets.ResourceDependencies;
-import org.apache.tapestry5.services.assets.ResourceTransformer;
-import org.apache.tapestry5.wro4j.services.ResourceProcessor;
-import org.apache.tapestry5.wro4j.services.ResourceProcessorSource;
-import org.slf4j.Logger;
-import ro.isdc.wro.extensions.processor.js.RhinoCoffeeScriptProcessor;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-/**
- * Compiles CoffeeScript to JavaScript, using {@link RhinoCoffeeScriptProcessor}. Because
what is most commonly written
- * are AMD Modules, which have (effectively) an implicit hygienic function wrapper, we compile
as with "--bare".
- *
- * @since 5.4
- */
-public class CoffeeScriptResourceCompiler implements ResourceTransformer
-{
-    private final Logger logger;
-
-    private static final double NANOS_TO_MILLIS = 1.0d / 1000000.0d;
-
-    private final ResourceProcessor compiler;
-
-    public CoffeeScriptResourceCompiler(Logger logger, ResourceProcessorSource processorSource)
-    {
-        this.logger = logger;
-
-        // Could set up some special kind of injection for this, but overkill for the couple
of places it is used.
-        compiler = processorSource.getProcessor("CoffeeScriptCompiler");
-    }
-
-    public String getTransformedContentType()
-    {
-        return "text/javascript";
-    }
-
-    public InputStream transform(final Resource source, ResourceDependencies dependencies)
throws IOException
-    {
-        final long startTime = System.nanoTime();
-
-        InputStream result = compiler.process(String.format("Compiling %s from CoffeeScript
to JavaScript", source),
-                source.toURL().toString(),
-                source.openStream(), "text/javascript");
-
-        final long elapsedTime = System.nanoTime() - startTime;
-
-        logger.info(String.format("Compiled %s to JavaScript in %.2f ms",
-                source,
-                ((double) elapsedTime) * NANOS_TO_MILLIS));
-
-        return result;
-    }
-}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/09a94cd5/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceProcessorSourceImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceProcessorSourceImpl.java
b/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceProcessorSourceImpl.java
index 266832c..cb94760 100644
--- a/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceProcessorSourceImpl.java
+++ b/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceProcessorSourceImpl.java
@@ -79,7 +79,12 @@ public class ResourceProcessorSourceImpl implements ResourceProcessorSource
 
                 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(5000);
 
-                lazyCreator.createObject().process(resource, new InputStreamReader(input),
new OutputStreamWriter(outputStream));
+                OutputStreamWriter writer = new OutputStreamWriter(outputStream);
+
+                lazyCreator.createObject().process(resource, new InputStreamReader(input),
writer);
+
+                // close the writer to flush content into the outputStream
+                writer.close();
 
                 return new BytestreamCache(outputStream).openStream();
             }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/09a94cd5/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactory.java
----------------------------------------------------------------------
diff --git a/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactory.java
b/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactory.java
new file mode 100644
index 0000000..4016ebe
--- /dev/null
+++ b/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactory.java
@@ -0,0 +1,43 @@
+// Copyright 2013 The Apache Software Foundation
+//
+// Licensed 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.tapestry5.internal.wro4j;
+
+import org.apache.tapestry5.services.assets.ResourceTransformer;
+
+/**
+ * Creates ResourceTransformer around a named {@link org.apache.tapestry5.wro4j.services.ResourceProcessor}.
+ *
+ * @since 5.4
+ * @see org.apache.tapestry5.services.assets.StreamableResourceSource
+ */
+public interface ResourceTransformerFactory
+{
+
+    /**
+     * Constructs a compiler around a named processor.
+     *
+     * @param contentType
+     *         transformed content type, e.g., "text/javascript"
+     * @param processorName
+     *         name of processor to do work
+     * @param sourceName
+     *         for debugging: source name, e.g., "CoffeeScript"
+     * @param targetName
+     *         for debugging: target name, e.g., "JavaScript"
+     * @return transformer
+     * @see org.apache.tapestry5.wro4j.services.ResourceProcessorSource
+     */
+    ResourceTransformer createCompiler(String contentType, String processorName, String sourceName,
String targetName);
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/09a94cd5/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactoryImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactoryImpl.java
b/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactoryImpl.java
new file mode 100644
index 0000000..5893bd1
--- /dev/null
+++ b/tapestry-wro4j/src/main/java/org/apache/tapestry5/internal/wro4j/ResourceTransformerFactoryImpl.java
@@ -0,0 +1,70 @@
+// Copyright 2013 The Apache Software Foundation
+//
+// Licensed 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.tapestry5.internal.wro4j;
+
+import org.apache.tapestry5.ioc.Resource;
+import org.apache.tapestry5.services.assets.ResourceDependencies;
+import org.apache.tapestry5.services.assets.ResourceTransformer;
+import org.apache.tapestry5.wro4j.services.ResourceProcessor;
+import org.apache.tapestry5.wro4j.services.ResourceProcessorSource;
+import org.slf4j.Logger;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class ResourceTransformerFactoryImpl implements ResourceTransformerFactory
+{
+    private static final double NANOS_TO_MILLIS = 1.0d / 1000000.0d;
+
+    private final Logger logger;
+
+    private final ResourceProcessorSource source;
+
+    public ResourceTransformerFactoryImpl(Logger logger, ResourceProcessorSource source)
+    {
+        this.logger = logger;
+        this.source = source;
+    }
+
+    public ResourceTransformer createCompiler(final String contentType, String processorName,
final String sourceName, final String targetName)
+    {
+        final ResourceProcessor compiler = source.getProcessor(processorName);
+
+        return new ResourceTransformer()
+        {
+            public String getTransformedContentType()
+            {
+                return contentType;
+            }
+
+            public InputStream transform(Resource source, ResourceDependencies dependencies)
throws IOException
+            {
+                final long startTime = System.nanoTime();
+
+                InputStream result = compiler.process(String.format("Compiling %s from %s
to %s", source, sourceName, targetName),
+                        source.toURL().toString(),
+                        source.openStream(), contentType);
+
+                final long elapsedTime = System.nanoTime() - startTime;
+
+                logger.info(String.format("Compiled %s to %s in %.2f ms",
+                        source, targetName,
+                        ((double) elapsedTime) * NANOS_TO_MILLIS));
+
+                return result;
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/09a94cd5/tapestry-wro4j/src/main/java/org/apache/tapestry5/wro4j/modules/WRO4JModule.java
----------------------------------------------------------------------
diff --git a/tapestry-wro4j/src/main/java/org/apache/tapestry5/wro4j/modules/WRO4JModule.java
b/tapestry-wro4j/src/main/java/org/apache/tapestry5/wro4j/modules/WRO4JModule.java
index 108858b..7874d73 100644
--- a/tapestry-wro4j/src/main/java/org/apache/tapestry5/wro4j/modules/WRO4JModule.java
+++ b/tapestry-wro4j/src/main/java/org/apache/tapestry5/wro4j/modules/WRO4JModule.java
@@ -14,10 +14,7 @@
 
 package org.apache.tapestry5.wro4j.modules;
 
-import org.apache.tapestry5.internal.wro4j.CSSMinimizer;
-import org.apache.tapestry5.internal.wro4j.CoffeeScriptResourceCompiler;
-import org.apache.tapestry5.internal.wro4j.JavaScriptMinimizer;
-import org.apache.tapestry5.internal.wro4j.ResourceProcessorSourceImpl;
+import org.apache.tapestry5.internal.wro4j.*;
 import org.apache.tapestry5.ioc.MappedConfiguration;
 import org.apache.tapestry5.ioc.ObjectCreator;
 import org.apache.tapestry5.ioc.ObjectLocator;
@@ -28,6 +25,7 @@ import org.apache.tapestry5.services.assets.ResourceMinimizer;
 import org.apache.tapestry5.services.assets.ResourceTransformer;
 import org.apache.tapestry5.services.assets.StreamableResourceSource;
 import org.apache.tapestry5.wro4j.services.ResourceProcessorSource;
+import ro.isdc.wro.extensions.processor.css.Less4jProcessor;
 import ro.isdc.wro.extensions.processor.js.GoogleClosureCompressorProcessor;
 import ro.isdc.wro.extensions.processor.js.RhinoCoffeeScriptProcessor;
 import ro.isdc.wro.extensions.processor.support.coffeescript.CoffeeScript;
@@ -43,6 +41,7 @@ public class WRO4JModule
     public static void bind(ServiceBinder binder)
     {
         binder.bind(ResourceProcessorSource.class, ResourceProcessorSourceImpl.class);
+        binder.bind(ResourceTransformerFactory.class, ResourceTransformerFactoryImpl.class);
     }
 
     /**
@@ -53,6 +52,7 @@ public class WRO4JModule
      * <dt>JavaScriptMinimizer</dt>
      * <dd>{@link GoogleClosureCompressorProcessor} configured for simple optimizations.
Advanced optimizations assume that all code is loaded
      * in a single bundle, not a given for Tapestry.</dd>
+     * <dt>LessCompiler</dt> <dd>Compiles Less source files into CSS.</dd>
      * </dl>
      *
      * @param configuration
@@ -94,13 +94,24 @@ public class WRO4JModule
                 return new GoogleClosureCompressorProcessor();
             }
         });
+
+        configuration.add("LessCompiler", new ObjectCreator()
+        {
+            public Object createObject()
+            {
+                return new Less4jProcessor();
+            }
+        });
     }
 
     @Contribute(StreamableResourceSource.class)
-    public static void provideCoffeeScriptCompilation
-            (MappedConfiguration<String, ResourceTransformer> configuration)
+    public static void provideCompilations
+            (MappedConfiguration<String, ResourceTransformer> configuration, ResourceTransformerFactory
factory)
     {
-        configuration.addInstance("coffee", CoffeeScriptResourceCompiler.class);
+        configuration.add("coffee",
+                factory.createCompiler("text/javascript", "CoffeeScriptCompiler", "CoffeeScript",
"JavaScript"));
+
+        configuration.add("less", factory.createCompiler("text/css", "LessCompiler", "Less",
"CSS"));
     }
 
     @Contribute(ResourceMinimizer.class)

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/09a94cd5/tapestry-wro4j/src/test/groovy/t5/wro4j/pages/Index.groovy
----------------------------------------------------------------------
diff --git a/tapestry-wro4j/src/test/groovy/t5/wro4j/pages/Index.groovy b/tapestry-wro4j/src/test/groovy/t5/wro4j/pages/Index.groovy
index 690577e..78b58da 100644
--- a/tapestry-wro4j/src/test/groovy/t5/wro4j/pages/Index.groovy
+++ b/tapestry-wro4j/src/test/groovy/t5/wro4j/pages/Index.groovy
@@ -2,7 +2,7 @@ package t5.wro4j.pages
 
 import org.apache.tapestry5.annotations.Import
 
-@Import(stack = "core", module = "index")
+@Import(stack = "core", module = "index", stylesheet = "index.less")
 class Index {
 
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/09a94cd5/tapestry-wro4j/src/test/resources/META-INF/assets/index.less
----------------------------------------------------------------------
diff --git a/tapestry-wro4j/src/test/resources/META-INF/assets/index.less b/tapestry-wro4j/src/test/resources/META-INF/assets/index.less
new file mode 100644
index 0000000..738be7b
--- /dev/null
+++ b/tapestry-wro4j/src/test/resources/META-INF/assets/index.less
@@ -0,0 +1,5 @@
+container {
+  alert {
+    font-size: x-large;
+  }
+}
\ No newline at end of file


Mime
View raw message