From ff0367c62bce1dc1f80c2ccab9c3bbf3899b79e2 Mon Sep 17 00:00:00 2001 From: Athou Date: Wed, 1 May 2013 07:13:57 +0200 Subject: [PATCH] concat templates and use build timestamp as cache invalidator --- pom.xml | 66 +++++++++++++++++++++++++++++- src/main/script/HTMLConcat.groovy | 28 +++++++++++++ src/main/script/template-loader.js | 35 ++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/main/script/HTMLConcat.groovy create mode 100644 src/main/script/template-loader.js diff --git a/pom.xml b/pom.xml index a3a00cde..ae99ef29 100644 --- a/pom.xml +++ b/pom.xml @@ -413,7 +413,7 @@ - + mysql @@ -424,9 +424,73 @@ prod + ${maven.build.timestamp} true false + + + + org.codehaus.gmaven + gmaven-plugin + 1.4 + + + commons-io + commons-io + 2.4 + + + + + generate-sources + + execute + + + + ${basedir}/src/main/webapp/templates + templates/ + ${basedir}/target/generated-sources/angularjs/all-templates.html + + + ${basedir}/src/main/script + + + def source = project.properties['source']; + def prefix = + project.properties['prefix']; + def dest = + project.properties['destination']; + new + HTMLConcat().concat(source, prefix, dest); + + + + + + + maven-war-plugin + 2.3 + + + + target/generated-sources/angularjs/ + templates + + + src/main/script/ + + *.js + + js + true + + + + + + diff --git a/src/main/script/HTMLConcat.groovy b/src/main/script/HTMLConcat.groovy new file mode 100644 index 00000000..da32dca2 --- /dev/null +++ b/src/main/script/HTMLConcat.groovy @@ -0,0 +1,28 @@ +import java.io.File; +import java.io.IOException; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.SystemUtils; + +public class HTMLConcat { + public void concat(String source, String prefix, String destination) + throws IOException { + StringBuilder sb = new StringBuilder(); + sb.append("
"); + for (File file : new File(source).listFiles()) { + if (file.isFile()) { + sb.append(SystemUtils.LINE_SEPARATOR); + sb.append(String.format( + ""); + sb.append(SystemUtils.LINE_SEPARATOR); + } + } + sb.append("
"); + FileUtils.writeStringToFile(new File(destination), sb.toString()); + } +} diff --git a/src/main/script/template-loader.js b/src/main/script/template-loader.js new file mode 100644 index 00000000..3ded2693 --- /dev/null +++ b/src/main/script/template-loader.js @@ -0,0 +1,35 @@ +var app = angular.module('commafeed.services'); + +app.factory('$templateCache', ['$cacheFactory', '$http', '$injector', function($cacheFactory, $http, $injector) { + var cache = $cacheFactory('templates'); + var allTplPromise; + + return { + get : function(url) { + var fromCache = cache.get(url); + + if (fromCache) { + return fromCache; + } + + if (!allTplPromise) { + allTplPromise = $http.get('templates/all-templates.html?${timestamp}').then( + function(response) { + $injector.get('$compile')(response.data); + return response; + }); + } + + return allTplPromise.then(function(response) { + return { + status : response.status, + data : cache.get(url) + }; + }); + }, + + put : function(key, value) { + cache.put(key, value); + } + }; +}]); \ No newline at end of file