Skip to content

Commit

Permalink
scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Dec 13, 2019
1 parent 8f8af7d commit 540e632
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 115 deletions.
6 changes: 6 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version = "2.3.2"
align = more
maxColumn = 110
spaces.inImportCurlyBraces = true
rewrite.rules = [SortImports, RedundantParens, SortModifiers]
rewrite.redundantBraces.stringInterpolation = true
12 changes: 4 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import com.typesafe.sbt.packager.Keys.scriptClasspath
import com.typesafe.sbt.SbtScalariform.autoImport.scalariformFormat

import BuildSettings._
import Dependencies._
Expand All @@ -25,7 +24,10 @@ PlayKeys.playDefaultPort := 9663
PlayKeys.externalizeResources := false
// shorter prod classpath
scriptClasspath := Seq("*")
// offline := true
// don't make an assets jar
resourceDirectory in Assets := (sourceDirectory in Compile).value / "assets"

// format: off
libraryDependencies ++= Seq(
macwire.macros, macwire.util, play.json, jodaForms, ws,
scalaz, chess, compression, scalalib, hasher,
Expand All @@ -35,12 +37,6 @@ libraryDependencies ++= Seq(
scrimage, scaffeine, lettuce, epoll
) ++ silencer.bundle

resourceDirectory in Assets := (sourceDirectory in Compile).value / "assets"

scalariformPreferences := scalariformPrefs(scalariformPreferences.value)
excludeFilter in scalariformFormat := "*Routes*"
routesGenerator := LilaRoutesGenerator

lazy val modules = Seq(
common, db, rating, user, security, hub, socket,
message, notifyModule, i18n, game, bookmark, search,
Expand Down
36 changes: 19 additions & 17 deletions project/BuildSettings.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import com.typesafe.sbt.SbtScalariform.autoImport.scalariformPreferences
import play.sbt.PlayImport._
import sbt._, Keys._
import scalariform.formatter.preferences._

object BuildSettings {

Expand All @@ -19,29 +17,31 @@ object BuildSettings {
// disable publishing the main API jar
publishArtifact in (Compile, packageDoc) := false,
// disable publishing the main sources jar
publishArtifact in (Compile, packageSrc) := false,
scalariformPreferences := scalariformPrefs(scalariformPreferences.value)
publishArtifact in (Compile, packageSrc) := false
)

def scalariformPrefs(prefs: IFormattingPreferences) = prefs
.setPreference(DanglingCloseParenthesis, Force)
.setPreference(DoubleIndentConstructorArguments, true)

def defaultLibs: Seq[ModuleID] = Seq(
play.api, scalaz, chess, scalalib, jodaTime, ws,
macwire.macros, macwire.util, autoconfig, specs2
play.api,
scalaz,
chess,
scalalib,
jodaTime,
ws,
macwire.macros,
macwire.util,
autoconfig,
specs2
)

def module(
name: String,
deps: Seq[sbt.ClasspathDep[sbt.ProjectReference]],
libs: Seq[ModuleID]
name: String,
deps: Seq[sbt.ClasspathDep[sbt.ProjectReference]],
libs: Seq[ModuleID]
) =
Project(
name,
file("modules/" + name)
)
.dependsOn(deps: _*)
).dependsOn(deps: _*)
.settings(
libraryDependencies ++= defaultLibs ++ libs ++ silencer.bundle,
buildSettings,
Expand All @@ -59,8 +59,10 @@ object BuildSettings {
"-Ywarn-macros:after",
"-Ywarn-unused:_",
// "-Xfatal-warnings",
"-Xmaxerrs", "12",
"-Xmaxwarns", "12"
"-Xmaxerrs",
"12",
"-Xmaxwarns",
"12"
)

val srcMain = Seq(
Expand Down
62 changes: 31 additions & 31 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@ object Dependencies {

object Resolvers {

val sonatype = Resolver.sonatypeRepo("releases")
val sonatype = Resolver.sonatypeRepo("releases")
val lilaMaven = "lila-maven" at "https://raw.githubusercontent.com/ornicar/lila-maven/master"

val commons = Seq(lilaMaven, sonatype)
}

val scalaz = "org.scalaz" %% "scalaz-core" % "7.2.29"
val scalalib = "com.github.ornicar" %% "scalalib" % "6.7"
val hasher = "com.roundeights" %% "hasher" % "1.2.1"
val jodaTime = "joda-time" % "joda-time" % "2.10.5"
val chess = "org.lichess" %% "scalachess" % "9.0.27"
val compression = "org.lichess" %% "compression" % "1.5"
val maxmind = "com.sanoma.cda" %% "maxmind-geoip2-scala" % "1.3.1-THIB"
val prismic = "io.prismic" %% "scala-kit" % "1.2.13-THIB213"
val scrimage = "com.sksamuel.scrimage" %% "scrimage-core" % "2.1.8-SNAPSHOT"
val scaffeine = "com.github.blemale" %% "scaffeine" % "3.1.0" % "compile"
val googleOAuth = "com.google.auth" % "google-auth-library-oauth2-http" % "0.18.0"
val scalaUri = "io.lemonlabs" %% "scala-uri" % "1.5.1"
val scalatags = "com.lihaoyi" %% "scalatags" % "0.7.0"
val lettuce = "io.lettuce" % "lettuce-core" % "5.2.1.RELEASE"
val epoll = "io.netty" % "netty-transport-native-epoll" % "4.1.43.Final" classifier "linux-x86_64"
val markdown = "com.vladsch.flexmark" % "flexmark-all" % "0.50.44"
val autoconfig = "io.methvin.play" %% "autoconfig-macros" % "0.3.2-SNAPSHOT" % "provided"
val scalaz = "org.scalaz" %% "scalaz-core" % "7.2.29"
val scalalib = "com.github.ornicar" %% "scalalib" % "6.7"
val hasher = "com.roundeights" %% "hasher" % "1.2.1"
val jodaTime = "joda-time" % "joda-time" % "2.10.5"
val chess = "org.lichess" %% "scalachess" % "9.0.27"
val compression = "org.lichess" %% "compression" % "1.5"
val maxmind = "com.sanoma.cda" %% "maxmind-geoip2-scala" % "1.3.1-THIB"
val prismic = "io.prismic" %% "scala-kit" % "1.2.13-THIB213"
val scrimage = "com.sksamuel.scrimage" %% "scrimage-core" % "2.1.8-SNAPSHOT"
val scaffeine = "com.github.blemale" %% "scaffeine" % "3.1.0" % "compile"
val googleOAuth = "com.google.auth" % "google-auth-library-oauth2-http" % "0.18.0"
val scalaUri = "io.lemonlabs" %% "scala-uri" % "1.5.1"
val scalatags = "com.lihaoyi" %% "scalatags" % "0.7.0"
val lettuce = "io.lettuce" % "lettuce-core" % "5.2.1.RELEASE"
val epoll = "io.netty" % "netty-transport-native-epoll" % "4.1.43.Final" classifier "linux-x86_64"
val markdown = "com.vladsch.flexmark" % "flexmark-all" % "0.50.44"
val autoconfig = "io.methvin.play" %% "autoconfig-macros" % "0.3.2-SNAPSHOT" % "provided"

object macwire {
val version = "2.3.3"
val macros = "com.softwaremill.macwire" %% "macros" % version % "provided"
val util = "com.softwaremill.macwire" %% "util" % version % "provided"
val macros = "com.softwaremill.macwire" %% "macros" % version % "provided"
val util = "com.softwaremill.macwire" %% "util" % version % "provided"
}

object reactivemongo {
val version = "0.19.3"
val driver = "org.reactivemongo" %% "reactivemongo" % "0.20.0-SNAPSHOT"
val bson = "org.reactivemongo" %% "reactivemongo-bson-api" % "0.20.0-SNAPSHOT"
val stream = "org.reactivemongo" %% "reactivemongo-akkastream" % version
val driver = "org.reactivemongo" %% "reactivemongo" % "0.20.0-SNAPSHOT"
val bson = "org.reactivemongo" %% "reactivemongo-bson-api" % "0.20.0-SNAPSHOT"
val stream = "org.reactivemongo" %% "reactivemongo-akkastream" % version
// val native = "org.reactivemongo" % "reactivemongo-shaded-native" % s"$version-linux-x86-64" classifier "linux-x86_64"
val native = "org.reactivemongo" % "reactivemongo-shaded-native" % s"$version-linux-x86-64"
// #TODO remove compat
Expand All @@ -49,19 +49,19 @@ object Dependencies {

object play {
val version = "2.8.0"
val api = "com.typesafe.play" %% "play" % version
val json = "com.typesafe.play" %% "play-json" % version
val api = "com.typesafe.play" %% "play" % version
val json = "com.typesafe.play" %% "play-json" % version
}
object kamon {
val core = "io.kamon" %% "kamon-core" % "2.0.2"
val influxdb = "io.kamon" %% "kamon-influxdb" % "2.0.1-LILA"
val metrics = "io.kamon" %% "kamon-system-metrics" % "2.0.0"
val core = "io.kamon" %% "kamon-core" % "2.0.2"
val influxdb = "io.kamon" %% "kamon-influxdb" % "2.0.1-LILA"
val metrics = "io.kamon" %% "kamon-system-metrics" % "2.0.0"
}

object silencer {
val version = "1.4.4"
val plugin = "com.github.ghik" % "silencer-plugin" % version cross CrossVersion.full
val lib = "com.github.ghik" % "silencer-lib" % version % Provided cross CrossVersion.full
val bundle = Seq(compilerPlugin(plugin), lib)
val plugin = "com.github.ghik" % "silencer-plugin" % version cross CrossVersion.full
val lib = "com.github.ghik" % "silencer-lib" % version % Provided cross CrossVersion.full
val bundle = Seq(compilerPlugin(plugin), lib)
}
}
47 changes: 27 additions & 20 deletions project/MessageCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ object MessageCompiler {

private def doFile(db: String, sourceFile: File, destDir: File, compileTo: File): Seq[File] = {
destDir.mkdirs()
val registry = ("en-GB" -> sourceFile) :: destDir.list.toList.map { f =>
f.takeWhile('.' !=) -> (destDir / f)
}.sortBy(_._1)
val registry = ("en-GB" -> sourceFile) :: destDir.list.toList
.map { f =>
f.takeWhile('.' !=) -> (destDir / f)
}
.sortBy(_._1)
compileTo.mkdirs()
var translatedLocales = Set.empty[String]
val res = for {
entry <- registry
compilable <- {
val (locale, file) = entry
val compileToFile = compileTo / s"$locale.scala"
val compileToFile = compileTo / s"$locale.scala"
if (!isFileEmpty(file)) {
translatedLocales = translatedLocales + locale
if (file.lastModified > compileToFile.lastModified) {
printToFile(compileToFile)(render(db, locale, file))
}
Some(compileToFile)
}
else None
} else None
}
} yield compilable
writeRegistry(db, compileTo, translatedLocales) :: res
Expand Down Expand Up @@ -77,25 +78,28 @@ private[i18n] object Registry {
}

private def render(db: String, locale: String, file: File): String = {
val xml = try {
XML.loadFile(file)
}
catch {
case e: Exception => println(file); throw e;
}
val xml =
try {
XML.loadFile(file)
} catch {
case e: Exception => println(file); throw e;
}
def quote(msg: String) = s"""""\"$msg""\""""
val content = xml.child.collect {
case e if e.label == "string" =>
val safe = escape(e.text)
val translation = escapeHtmlOption(safe) match {
case None => s"""new Simple(\"\"\"$safe\"\"\")"""
case None => s"""new Simple(\"\"\"$safe\"\"\")"""
case Some(escaped) => s"""new Escaped(\"\"\"$safe\"\"\",\"\"\"$escaped\"\"\")"""
}
s"""m.put(${toKey(e)},$translation)"""
case e if e.label == "plurals" =>
val items: Map[String, String] = e.child.filter(_.label == "item").map { i =>
ucfirst(i.\("@quantity").toString) -> s"""\"\"\"${escape(i.text)}\"\"\""""
}.toMap
val items: Map[String, String] = e.child
.filter(_.label == "item")
.map { i =>
ucfirst(i.\("@quantity").toString) -> s"""\"\"\"${escape(i.text)}\"\"\""""
}
.toMap
s"""m.put(${toKey(e)},new Plurals(${pluralMap(items)}))"""
}
s"""package lila.i18n
Expand All @@ -116,15 +120,14 @@ ${content mkString "\n"}
}

private def pluralMap(items: Map[String, String]): String =
if (items.size > 4) s"""Map(${items.map { case (k, v) => s"$k->$v" } mkString ","})"""
if (items.size > 4) s"""Map(${items.map { case (k, v) => s"$k->$v" } mkString ","})"""
else s"""new Map.Map${items.size}(${items.map { case (k, v) => s"$k,$v" } mkString ","})"""


private val badChars = """[<>&"'\r\n]""".r.pattern
private def escapeHtmlOption(s: String): Option[String] =
if (badChars.matcher(s).find) Some {
val sb = new java.lang.StringBuilder(s.size + 10) // wet finger style
var i = 0
var i = 0
while (i < s.length) {
s.charAt(i) match {
case '<' => sb append "&lt;"
Expand All @@ -143,6 +146,10 @@ ${content mkString "\n"}

private def printToFile(f: File)(content: String): Unit = {
val p = new java.io.PrintWriter(f, "UTF-8")
try { content.foreach(p.print) } finally { p.close() }
try {
content.foreach(p.print)
} finally {
p.close()
}
}
}
Loading

0 comments on commit 540e632

Please sign in to comment.