(refs #464)Twirl support for plugin

This commit is contained in:
Naoki Takezoe
2014-08-16 02:53:43 +09:00
parent 8b8bd0289b
commit 975dfb17e1
3 changed files with 24 additions and 23 deletions

View File

@@ -70,12 +70,13 @@ object PluginSystem extends PluginService {
// TODO Method name seems to not so good.
def installPlugin(id: String)(implicit session: Session): Unit = {
val pluginDir = new java.io.File(PluginHome)
val pluginHome = new java.io.File(PluginHome)
val pluginDir = new java.io.File(pluginHome, id)
val scalaFile = new java.io.File(pluginDir, id + "/plugin.scala")
val scalaFile = new java.io.File(pluginDir, "plugin.scala")
if(scalaFile.exists && scalaFile.isFile){
val properties = new java.util.Properties()
using(new java.io.FileInputStream(new java.io.File(pluginDir, s"${id}/plugin.properties"))){ in =>
using(new java.io.FileInputStream(new java.io.File(pluginDir, "plugin.properties"))){ in =>
properties.load(in)
}
@@ -95,7 +96,12 @@ object PluginSystem extends PluginService {
try {
// Compile and eval Scala source code
ScalaPlugin.eval(source)
ScalaPlugin.eval(pluginDir.listFiles.filter(_.getName.endsWith(".scala.html")).map { file =>
ScalaPlugin.compileTemplate(
id.replaceAll("-", ""),
file.getName.replaceAll("\\.scala\\.html$", ""),
IOUtils.toString(new FileInputStream(file)))
}.mkString("\n") + source)
// Migrate database
val plugin = getPlugin(pluginId)
@@ -186,4 +192,3 @@ object PluginSystem extends PluginService {
}
}

View File

@@ -1,7 +1,6 @@
package plugin
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.{Map => MutableMap}
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
import app.Context
import plugin.PluginSystem._
@@ -11,6 +10,7 @@ import service.RepositoryService.RepositoryInfo
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox
import play.twirl.compiler.TwirlCompiler
import scala.io.Codec
// TODO This is a sample implementation for Scala based plug-ins.
class ScalaPlugin(val id: String, val version: String,
@@ -61,23 +61,17 @@ object ScalaPlugin extends App {
toolbox.eval(tree)
}
def compileTemplate(source: String) = {
val result = TwirlCompiler.compileVirtual(source,
new java.io.File("./sample.scala.html"),
new java.io.File("."),
"twirl.api.HtmlFormat.Appendable",
"twirl.api.HtmlFormat")
def compileTemplate(packageName: String, name: String, source: String): String = {
val result = TwirlCompiler.parseAndGenerateCodeNewParser(
Array(packageName, name),
source.getBytes("UTF-8"),
Codec(scala.util.Properties.sourceEncoding),
"",
"play.twirl.api.HtmlFormat.Appendable",
"play.twirl.api.HtmlFormat",
"",
false)
println(result.content)
result.replaceFirst("package .*", "")
}
compileTemplate(
"""@(value: String)
| <html>@value</html>
| """.stripMargin)
compileTemplate(
"""@(value: String)
| <b>@value</b>
| """.stripMargin)
}

View File

@@ -41,6 +41,7 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
val result = action.function(request, response)
result match {
case x: String => renderGlobalHtml(request, response, context, x)
case x: Html => renderGlobalHtml(request, response, context, x.toString)
case x: AnyRef => renderJson(request, response, x)
}
} else {
@@ -73,6 +74,7 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
}
result match {
case x: String => renderRepositoryHtml(request, response, context, repository, x)
case x: Html => renderGlobalHtml(request, response, context, x.toString)
case x: AnyRef => renderJson(request, response, x)
}
} else {