2
0
mirror of https://github.com/rehlds/reapi.git synced 2025-01-21 11:08:04 +03:00
reapi/buildSrc/src/main/groovy/gradlecpp/VelocityUtils.groovy
2017-01-05 16:27:59 +07:00

36 lines
1012 B
Groovy

package gradlecpp
import org.apache.velocity.Template
import org.apache.velocity.VelocityContext
import org.apache.velocity.app.Velocity
import org.joda.time.format.DateTimeFormat
import versioning.ReapiVersionInfo
class VelocityUtils {
static {
Properties p = new Properties();
p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
p.setProperty("class.resource.loader.path", "");
p.setProperty("input.encoding", "UTF-8");
p.setProperty("output.encoding", "UTF-8");
Velocity.init(p);
}
static String renderTemplate(File tplFile, Map<String, ? extends Object> ctx) {
Template tpl = Velocity.getTemplate(tplFile.absolutePath)
if (!tpl) {
throw new RuntimeException("Failed to load velocity template ${tplFile.absolutePath}: not found")
}
def velocityContext = new VelocityContext(ctx)
def sw = new StringWriter()
tpl.merge(velocityContext, sw)
return sw.toString()
}
}