concat templates and use build timestamp as cache invalidator

This commit is contained in:
Athou
2013-05-01 07:13:57 +02:00
parent 10cbfd6171
commit ff0367c62b
3 changed files with 128 additions and 1 deletions

66
pom.xml
View File

@@ -413,7 +413,7 @@
</plugins>
</build>
</profile>
<profile>
<id>mysql</id>
<properties>
@@ -424,9 +424,73 @@
<profile>
<id>prod</id>
<properties>
<timestamp>${maven.build.timestamp}</timestamp>
<production>true</production>
<jpa.show_sql>false</jpa.show_sql>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.4</version>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<properties>
<source>${basedir}/src/main/webapp/templates</source>
<prefix>templates/</prefix>
<destination>${basedir}/target/generated-sources/angularjs/all-templates.html</destination>
</properties>
<scriptpath>
<element>${basedir}/src/main/script</element>
</scriptpath>
<source>
def source = project.properties['source'];
def prefix =
project.properties['prefix'];
def dest =
project.properties['destination'];
new
HTMLConcat().concat(source, prefix, dest);
</source>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<directory>target/generated-sources/angularjs/</directory>
<targetPath>templates</targetPath>
</resource>
<resource>
<directory>src/main/script/</directory>
<includes>
<include>*.js</include>
</includes>
<targetPath>js</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@@ -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("<div>");
for (File file : new File(source).listFiles()) {
if (file.isFile()) {
sb.append(SystemUtils.LINE_SEPARATOR);
sb.append(String.format(
"<script type=\"text/ng-template\" id=\"%s%s\">",
prefix, file.getName()));
sb.append(SystemUtils.LINE_SEPARATOR);
sb.append(FileUtils.readFileToString(file));
sb.append(SystemUtils.LINE_SEPARATOR);
sb.append("</script>");
sb.append(SystemUtils.LINE_SEPARATOR);
}
}
sb.append("</div>");
FileUtils.writeStringToFile(new File(destination), sb.toString());
}
}

View File

@@ -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);
}
};
}]);