2016-03-29 00:29:32 -06:00
|
|
|
import _root_.java.io.File
|
2016-03-29 03:24:29 -06:00
|
|
|
import _root_.java.nio.file.{ Files, StandardCopyOption }
|
2016-03-29 00:29:32 -06:00
|
|
|
import sbt._, Keys._
|
|
|
|
|
|
|
|
object MessageCompiler {
|
|
|
|
|
|
|
|
def apply(src: File, dst: File): Seq[File] = {
|
2016-03-29 03:24:29 -06:00
|
|
|
val startsAt = System.currentTimeMillis()
|
2016-03-29 00:29:32 -06:00
|
|
|
val sourceFiles = Option(src.list) getOrElse Array() filter (_ startsWith "messages")
|
2016-03-29 03:24:29 -06:00
|
|
|
val registry = sourceFiles.toList.map { f =>
|
|
|
|
f.split('.') match {
|
|
|
|
case Array("messages", lang) => lang -> f
|
|
|
|
case Array("messages") => "default" -> f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst.mkdirs()
|
|
|
|
val registryFile = writeRegistry(dst, registry)
|
|
|
|
val res = for (entry <- registry) yield {
|
|
|
|
val (lang, file) = entry
|
2016-03-29 00:29:32 -06:00
|
|
|
val srcFile = src / file
|
2016-03-29 03:24:29 -06:00
|
|
|
val dstFile = dst / s"$lang.scala"
|
2016-03-31 01:00:31 -06:00
|
|
|
if (srcFile.lastModified > dstFile.lastModified) {
|
|
|
|
val pairs = readLines(srcFile) map makePair
|
|
|
|
printToFile(dstFile) {
|
|
|
|
render(lang, pairs)
|
|
|
|
}
|
2016-03-29 03:24:29 -06:00
|
|
|
}
|
2016-03-29 00:29:32 -06:00
|
|
|
dstFile
|
|
|
|
}
|
2016-03-29 03:24:29 -06:00
|
|
|
println(s"MessageCompiler took ${System.currentTimeMillis() - startsAt}ms")
|
|
|
|
registryFile :: res
|
|
|
|
}
|
|
|
|
|
|
|
|
private def writeRegistry(dst: File, registry: List[(String, String)]) = {
|
|
|
|
val file = dst / "Registry.scala"
|
|
|
|
printToFile(file) {
|
|
|
|
val content = registry.map {
|
|
|
|
case (lang, _) => s""""$lang"->$lang.load"""
|
|
|
|
} mkString ",\n"
|
|
|
|
s"""package lila.i18n
|
|
|
|
package db
|
|
|
|
|
2017-02-14 08:19:01 -07:00
|
|
|
// format: OFF
|
2016-03-29 03:24:29 -06:00
|
|
|
object Registry {
|
|
|
|
|
|
|
|
def load = Map[String, Map[String, String]]($content)
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
file
|
|
|
|
}
|
|
|
|
|
|
|
|
private def render(lang: String, pairs: List[(String, String)]) = {
|
|
|
|
def quote(msg: String) = s"""""\"$msg""\""""
|
|
|
|
val content = pairs.map {
|
|
|
|
case (key, message) => s""""$key"->${quote(message)}"""
|
|
|
|
} mkString ",\n"
|
|
|
|
s"""package lila.i18n
|
|
|
|
package db
|
|
|
|
|
2017-02-14 08:19:01 -07:00
|
|
|
// format: OFF
|
2016-03-29 03:24:29 -06:00
|
|
|
private object $lang {
|
|
|
|
|
|
|
|
def load = Map[String, String]($content)
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
|
|
|
|
private def makePair(str: String): (String, String) = {
|
|
|
|
val p = str.splitAt(str indexOf "=")
|
|
|
|
p._1 -> p._2.drop(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
private def readLines(f: File) =
|
2017-03-21 08:33:42 -06:00
|
|
|
scala.io.Source.fromFile(f)("UTF-8").getLines.toList
|
2016-03-29 03:24:29 -06:00
|
|
|
|
|
|
|
private def printToFile(f: File)(content: String): Unit = {
|
2017-03-21 08:33:42 -06:00
|
|
|
val p = new java.io.PrintWriter(f, "UTF-8")
|
2016-03-29 03:24:29 -06:00
|
|
|
try { content.foreach(p.print) } finally { p.close() }
|
2016-03-29 00:29:32 -06:00
|
|
|
}
|
|
|
|
}
|