我有一个SBT任务,该任务从 base 文件在play 2.6项目中生成有效个翻译文件(conf / messages {,de,fr,..})。 (conf / variants / base / messages {en,de,fr,...})和 variant 文件(conf / variants / [variant] / message {en,de,fr,... }。它通过将基础文件与变体文件合并(可能会覆盖基础文件转换键)来生成有效文件。该系统可以正常工作,并且可以使用我们翻译的不同变体(针对不同客户的版本)。
我现在如何将该任务集成到包装(dist或Universal:packageZipTarball)和运行任务中?可以挂起play run任务,但不能使用sbt taks,而只能使用常规的scala类。我也找不到合适的任务来扩展编译或打包任务。最好的方法是什么?此外,配置有效变体的最佳方法是什么?我目前为此使用settingKey [String]。
package sbttranslationvariants
import sbt._
import Keys._
object TranslationVariantsPlugin extends AutoPlugin {
override val requires = plugins.JvmPlugin
override val trigger = allRequirements
// by defining autoImport, the settings are automatically imported into user's `*.sbt`
object autoImport {
val variant = settingKey[String]("The variant of the kussbus-backend build")
val languages = settingKey[Seq[String]]("The languages used")
val variantsFile = settingKey[File]("The directory where variants are stored")
val baseMessagesFiles = settingKey[Map[String, File]]("The files that implement the base messages")
val variantMessagesFiles = settingKey[Map[String, File]]("The file that implement variant message overrides")
val baseMessages = taskKey[Map[String, Map[String, String]]]("The base messages per language")
val variantMessages = taskKey[Map[String, Map[String, String]]]("The variant messages per language")
val targetMessages = taskKey[Map[String, Map[String, String]]]("The messages merged")
val writeTargetMessagesFiles = taskKey[Map[String, File]]("Write the merged messages to disk")
val deleteTargetMessagesFiles = taskKey[Map[String, File]]("Delete the generated messages")
}
import autoImport._
private def mkVariantFile(variantDir: File, variant: String, lang: String): File =
variantDir / variant / Seq("messages", lang).mkString(".")
private def mkTargetFile(baseDir: File, lang: String): File =
baseDir / "conf" / (lang match { case "en" => "messages"
case _ => Seq("messages", lang).mkString(".") })
override def projectSettings = Seq(
variant := "kussbus",
languages := Seq("en", "de"),
variantsFile := baseDirectory.value / "conf" / "variants",
baseMessagesFiles := languages.value.map(l => (l -> mkVariantFile(variantsFile.value, "base", l))).toMap,
variantMessagesFiles := languages.value.map(l => (l -> mkVariantFile(variantsFile.value, variant.value, l))).toMap,
baseMessages := {
baseMessagesFiles.value.map({ case (k, v) => k -> util.loadMessages(v) })
},
variantMessages := {
variantMessagesFiles.value.map({ case (k, v) => k -> util.loadMessages(v) })
},
targetMessages := {
baseMessages.value.map({
case (lang, baseMessagesMap) =>
lang -> util.mergeOverwrite(baseMessagesMap, variantMessages.value.get(lang).get)
})
},
writeTargetMessagesFiles := {
targetMessages.value.map({
case (lang, messages) => {
val tf = mkTargetFile(baseDirectory.value, lang)
val messagesRendered = messages.toSeq.sortBy(_._1).foldLeft("") { case (s, (k, v)) => s + s"$k = $v\n" }
IO.write(tf, messagesRendered)
(lang -> tf)
}
})
},
deleteTargetMessagesFiles := {
targetMessages.value.map({
case (lang, messages) => {
val tf = mkTargetFile(baseDirectory.value, lang)
IO.delete(tf)
(lang -> tf)
}
})
}
)
}
object util {
def merge[A, B](a: Map[A, B], b: Map[A, B])(mergef: (B, Option[B]) => B): Map[A, B] = {
a.foldLeft(b) { case (z, (k, v)) => z + (k -> mergef(v, z.get(k))) }
}
def mergeOverwrite[A, B](a: Map[A, B], b: Map[A, B]) = {
merge(a, b) ((v1, v2) => v1)
}
import play.api.i18n.Messages
def loadMessages(file: File) = {
Messages.parse(Messages.UrlMessageSource(file.toURI.toURL), "messages").right.get
}
}