mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-09 06:55:54 +01:00
(refs #464)Twirl support for plugin
This commit is contained in:
@@ -70,12 +70,13 @@ object PluginSystem extends PluginService {
|
|||||||
|
|
||||||
// TODO Method name seems to not so good.
|
// TODO Method name seems to not so good.
|
||||||
def installPlugin(id: String)(implicit session: Session): Unit = {
|
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){
|
if(scalaFile.exists && scalaFile.isFile){
|
||||||
val properties = new java.util.Properties()
|
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)
|
properties.load(in)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +96,12 @@ object PluginSystem extends PluginService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Compile and eval Scala source code
|
// 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
|
// Migrate database
|
||||||
val plugin = getPlugin(pluginId)
|
val plugin = getPlugin(pluginId)
|
||||||
@@ -186,4 +192,3 @@ object PluginSystem extends PluginService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package plugin
|
package plugin
|
||||||
|
|
||||||
import scala.collection.mutable.ListBuffer
|
import scala.collection.mutable.ListBuffer
|
||||||
import scala.collection.mutable.{Map => MutableMap}
|
|
||||||
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
|
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
|
||||||
import app.Context
|
import app.Context
|
||||||
import plugin.PluginSystem._
|
import plugin.PluginSystem._
|
||||||
@@ -11,6 +10,7 @@ import service.RepositoryService.RepositoryInfo
|
|||||||
import scala.reflect.runtime.currentMirror
|
import scala.reflect.runtime.currentMirror
|
||||||
import scala.tools.reflect.ToolBox
|
import scala.tools.reflect.ToolBox
|
||||||
import play.twirl.compiler.TwirlCompiler
|
import play.twirl.compiler.TwirlCompiler
|
||||||
|
import scala.io.Codec
|
||||||
|
|
||||||
// TODO This is a sample implementation for Scala based plug-ins.
|
// TODO This is a sample implementation for Scala based plug-ins.
|
||||||
class ScalaPlugin(val id: String, val version: String,
|
class ScalaPlugin(val id: String, val version: String,
|
||||||
@@ -61,23 +61,17 @@ object ScalaPlugin extends App {
|
|||||||
toolbox.eval(tree)
|
toolbox.eval(tree)
|
||||||
}
|
}
|
||||||
|
|
||||||
def compileTemplate(source: String) = {
|
def compileTemplate(packageName: String, name: String, source: String): String = {
|
||||||
val result = TwirlCompiler.compileVirtual(source,
|
val result = TwirlCompiler.parseAndGenerateCodeNewParser(
|
||||||
new java.io.File("./sample.scala.html"),
|
Array(packageName, name),
|
||||||
new java.io.File("."),
|
source.getBytes("UTF-8"),
|
||||||
"twirl.api.HtmlFormat.Appendable",
|
Codec(scala.util.Properties.sourceEncoding),
|
||||||
"twirl.api.HtmlFormat")
|
"",
|
||||||
|
"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)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
|
|||||||
val result = action.function(request, response)
|
val result = action.function(request, response)
|
||||||
result match {
|
result match {
|
||||||
case x: String => renderGlobalHtml(request, response, context, x)
|
case x: String => renderGlobalHtml(request, response, context, x)
|
||||||
|
case x: Html => renderGlobalHtml(request, response, context, x.toString)
|
||||||
case x: AnyRef => renderJson(request, response, x)
|
case x: AnyRef => renderJson(request, response, x)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -73,6 +74,7 @@ class PluginActionInvokeFilter extends Filter with SystemSettingsService with Re
|
|||||||
}
|
}
|
||||||
result match {
|
result match {
|
||||||
case x: String => renderRepositoryHtml(request, response, context, repository, x)
|
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)
|
case x: AnyRef => renderJson(request, response, x)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user