Skip to content

Commit

Permalink
Changed formatter to Scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
magnolia-k committed Jan 1, 2025
1 parent fdcd6ea commit d726cd4
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = 3.8.3
runner.dialect=scala212source3
12 changes: 4 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import scalariform.formatter.preferences._

lazy val root = (project in file(".")).settings(
organization := "org.scalatra.sbt",
name := "sbt-scalatra",
Expand All @@ -14,7 +12,8 @@ lazy val root = (project in file(".")).settings(
)
},
publishTo := {
if (version.value.trim.endsWith("SNAPSHOT")) Some(Opts.resolver.sonatypeSnapshots)
if (version.value.trim.endsWith("SNAPSHOT"))
Some(Opts.resolver.sonatypeSnapshots)
else Some(Opts.resolver.sonatypeStaging)
},
publishMavenStyle := true,
Expand Down Expand Up @@ -45,12 +44,9 @@ lazy val root = (project in file(".")).settings(
</developers>
)

scalariformPreferences := scalariformPreferences.value
.setPreference(DanglingCloseParenthesis, Force)

enablePlugins(ScriptedPlugin)

scriptedLaunchOpts ++= Seq(
"scalatra_version" -> "2.8.4",
"scala_version" -> "2.13.15",
).map{ case (k, v) => s"-D$k=$v" }
"scala_version" -> "2.13.15"
).map { case (k, v) => s"-D$k=$v" }
8 changes: 5 additions & 3 deletions project/plugin.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.3")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.3.1")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.3.1")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")

scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature")

// scripted for plugin testing
libraryDependencies += { "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value }
libraryDependencies += {
"org.scala-sbt" %% "scripted-plugin" % sbtVersion.value
}
5 changes: 3 additions & 2 deletions scripted.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
scriptedLaunchOpts := { scriptedLaunchOpts.value ++
Seq("-Xmx1024M", "-Dplugin.version=" + version.value)
scriptedLaunchOpts := {
scriptedLaunchOpts.value ++
Seq("-Xmx1024M", "-Dplugin.version=" + version.value)
}
scriptedBufferLog := false
91 changes: 75 additions & 16 deletions src/main/scala/org/scalatra/sbt/DistPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import com.earldouglas.xwp.WebappPlugin.autoImport.webappPrepare
object DistPlugin extends AutoPlugin {

object DistKeys {
val dist = taskKey[File]("Build a distribution, assemble the files, create a launcher and make an archive.")
val stage = taskKey[Seq[File]]("Build a distribution, assemble the files and create a launcher.")
val dist = taskKey[File](
"Build a distribution, assemble the files, create a launcher and make an archive."
)
val stage = taskKey[Seq[File]](
"Build a distribution, assemble the files and create a launcher."
)
val assembleJarsAndClasses = taskKey[Seq[File]]("Assemble jars and classes")
val memSetting = settingKey[String]("The maximium and initial heap size.")
@deprecated("there is no effect. will be removed", "")
val permGenSetting = settingKey[String]("The PermGen size.")
val envExports = settingKey[Seq[String]]("The exports which will be expored in the launcher script.")
val envExports = settingKey[Seq[String]](
"The exports which will be expored in the launcher script."
)
val runScriptName = settingKey[String]("The name of the service runscript.")
}

Expand All @@ -33,8 +39,12 @@ object DistPlugin extends AutoPlugin {

Def.task {
IO.delete(tgt.value)
val (libs, dirs) = cp.value.files partition Compat.ClasspathUtilities.isArchive
val jars = libs.descendantsExcept(GlobFilter("*"), excl.value) pair Path.flat(tgt.value / "lib")
val (libs, dirs) =
cp.value.files partition Compat.ClasspathUtilities.isArchive
val jars =
libs.descendantsExcept(GlobFilter("*"), excl.value) pair Path.flat(
tgt.value / "lib"
)
val classesAndResources = dirs flatMap { dir =>
val files = dir.descendantsExcept(GlobFilter("*"), excl.value)
files pair Path.rebase(dir, tgt.value / "lib")
Expand All @@ -44,27 +54,59 @@ object DistPlugin extends AutoPlugin {
}
}

private def createLauncherScriptTask(base: File, name: String, libFiles: Seq[File], mainClass: Option[String], javaOptions: Seq[String], envExports: Seq[String], logger: Logger): File = {
private def createLauncherScriptTask(
base: File,
name: String,
libFiles: Seq[File],
mainClass: Option[String],
javaOptions: Seq[String],
envExports: Seq[String],
logger: Logger
): File = {
val f = base / "bin" / name
if (!f.getParentFile.exists()) f.getParentFile.mkdirs()
IO.write(f, createScriptString(base, name, libFiles, mainClass, javaOptions, envExports))
IO.write(
f,
createScriptString(
base,
name,
libFiles,
mainClass,
javaOptions,
envExports
)
)
Compat.executeProccess("chmod +x %s".format(f.getAbsolutePath), logger)
f
}

private def createScriptString(base: File, name: String, libFiles: Seq[File], mainClass: Option[String], javaOptions: Seq[String], envExports: Seq[String]): String = {
private def createScriptString(
base: File,
name: String,
libFiles: Seq[File],
mainClass: Option[String],
javaOptions: Seq[String],
envExports: Seq[String]
): String = {
"""#!/bin/env bash
|
|export CLASSPATH="lib:%s"
|export JAVA_OPTS="%s"
|%s
|
|java $JAVA_OPTS -cp $CLASSPATH %s
|""".stripMargin.format(classPathString(base, libFiles), javaOptions.mkString(" "), envExports.map(e => "export %s".format(e)).mkString("\n"), mainClass.getOrElse(""))
|""".stripMargin.format(
classPathString(base, libFiles),
javaOptions.mkString(" "),
envExports.map(e => "export %s".format(e)).mkString("\n"),
mainClass.getOrElse("")
)
}

private def classPathString(base: File, libFiles: Seq[File]) = {
(libFiles filter Compat.ClasspathUtilities.isArchive map (_.relativeTo(base))).flatten mkString java.io.File.pathSeparator
(libFiles filter Compat.ClasspathUtilities.isArchive map (_.relativeTo(
base
))).flatten mkString java.io.File.pathSeparator
}

private def stageTask: Initialize[Task[Seq[File]]] = {
Expand All @@ -79,13 +121,26 @@ object DistPlugin extends AutoPlugin {
val s = streams

Def.task {
val launch = createLauncherScriptTask(tgt.value, nm.value, libFiles.value, mc.value, jo.value, ee.value, s.value.log)
val launch = createLauncherScriptTask(
tgt.value,
nm.value,
libFiles.value,
mc.value,
jo.value,
ee.value,
s.value.log
)
val logsDir = tgt.value / "logs"
if (!logsDir.exists()) logsDir.mkdirs()

s.value.log.info("Adding " + webRes.value + " to dist in " + tgt.value + "/webapp")
val resourceFilesFinder = webRes.value.descendantsExcept(GlobFilter("*"), excl.value)
val resourceFiles = IO.copy(resourceFilesFinder pair Path.rebase(webRes.value, tgt.value / "webapp"))
s.value.log.info(
"Adding " + webRes.value + " to dist in " + tgt.value + "/webapp"
)
val resourceFilesFinder =
webRes.value.descendantsExcept(GlobFilter("*"), excl.value)
val resourceFiles = IO.copy(
resourceFilesFinder pair Path.rebase(webRes.value, tgt.value / "webapp")
)

libFiles.value ++ Seq(launch, logsDir) ++ resourceFiles
}
Expand Down Expand Up @@ -113,12 +168,16 @@ object DistPlugin extends AutoPlugin {
}

private object PatternFileFilter {
def apply(expression: String): PatternFileFilter = new PatternFileFilter(Pattern.compile(expression))
def apply(expression: String): PatternFileFilter = new PatternFileFilter(
Pattern.compile(expression)
)
}

val distSettings = Seq(
Dist / excludeFilter := {
HiddenFileFilter || PatternFileFilter(".*/WEB-INF/classes") || PatternFileFilter(".*/WEB-INF/lib")
HiddenFileFilter || PatternFileFilter(
".*/WEB-INF/classes"
) || PatternFileFilter(".*/WEB-INF/lib")
// could use (webappDest in webapp).value.getCanonicalPath instead of .*, but webappDest is a task and SBT settings cant depend on tasks
},
Dist / target := (Compile / target)(_ / "dist").value,
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/org/scalatra/sbt/PluginKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import sbt._
import Keys._

object PluginKeys {
val browse = taskKey[Unit]("Open a web browser to localhost on container:port")
val browse =
taskKey[Unit]("Open a web browser to localhost on container:port")
}
9 changes: 6 additions & 3 deletions src/main/scala/org/scalatra/sbt/ScalatraPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Keys._
import java.net.URI
import com.earldouglas.xwp.ContainerPlugin.autoImport.containerPort
import com.earldouglas.xwp.JettyPlugin
import com.earldouglas.xwp.JettyPlugin.{ projectSettings => jettySettings }
import com.earldouglas.xwp.JettyPlugin.{projectSettings => jettySettings}
import com.earldouglas.xwp.JettyPlugin.autoImport.Jetty

object ScalatraPlugin extends AutoPlugin {
Expand All @@ -30,14 +30,17 @@ object ScalatraPlugin extends AutoPlugin {
java.awt.Desktop.getDesktop.browse(url)
} catch {
case _: Throwable => {
log.info(f"Could not open browser, sorry. Open manually to ${url.toASCIIString}")
log.info(
f"Could not open browser, sorry. Open manually to ${url.toASCIIString}"
)
}
}
}

val scalatraSettings: Seq[Def.Setting[_]] = jettySettings ++ Seq(browseTask)

val scalatraWithDist: Seq[Def.Setting[_]] = scalatraSettings ++ DistPlugin.distSettings
val scalatraWithDist: Seq[Def.Setting[_]] =
scalatraSettings ++ DistPlugin.distSettings

override lazy val projectSettings = scalatraSettings
}
7 changes: 5 additions & 2 deletions src/sbt-test/DistPlugin/dist/project/plugin.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
sys.props.get("plugin.version") match {
case Some(x) => addSbtPlugin("org.scalatra.sbt" % "sbt-scalatra" % x)
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
case _ =>
sys.error(
"""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin
)
}
18 changes: 11 additions & 7 deletions src/sbt-test/ScalatraPlugin/browse/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ Jetty / containerPort := 8090

lazy val check = taskKey[Unit]("check if / is available")


check := {

import org.http4s.implicits._
import cats.effect.unsafe.implicits.global

org.http4s.blaze.client.BlazeClientBuilder[cats.effect.IO].resource.use {
client =>
org.http4s.blaze.client
.BlazeClientBuilder[cats.effect.IO]
.resource
.use { client =>
val uri = uri"http://localhost:8090/"

client.expect[String](uri).map(
res => if (res != "hey") sys.error("unexpected output: " + res) else ()
)
}.unsafeRunSync()
client
.expect[String](uri)
.map(res =>
if (res != "hey") sys.error("unexpected output: " + res) else ()
)
}
.unsafeRunSync()

}
7 changes: 5 additions & 2 deletions src/sbt-test/ScalatraPlugin/browse/project/plugin.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ libraryDependencies += "org.http4s" %% "http4s-blaze-client" % "0.23.16"

sys.props.get("plugin.version") match {
case Some(x) => addSbtPlugin("org.scalatra.sbt" % "sbt-scalatra" % x)
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
case _ =>
sys.error(
"""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin
)
}
18 changes: 11 additions & 7 deletions src/sbt-test/ScalatraPlugin/jetty-start/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ Jetty / containerPort := 8090

lazy val check = taskKey[Unit]("check if / is available")


check := {

import org.http4s.implicits._
import cats.effect.unsafe.implicits.global

org.http4s.blaze.client.BlazeClientBuilder[cats.effect.IO].resource.use {
client =>
org.http4s.blaze.client
.BlazeClientBuilder[cats.effect.IO]
.resource
.use { client =>
val uri = uri"http://localhost:8090/"

client.expect[String](uri).map(
res => if (res != "hey") sys.error("unexpected output: " + res) else ()
)
}.unsafeRunSync()
client
.expect[String](uri)
.map(res =>
if (res != "hey") sys.error("unexpected output: " + res) else ()
)
}
.unsafeRunSync()

}
7 changes: 5 additions & 2 deletions src/sbt-test/ScalatraPlugin/jetty-start/project/plugin.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ libraryDependencies += "org.http4s" %% "http4s-blaze-client" % "0.23.16"

sys.props.get("plugin.version") match {
case Some(x) => addSbtPlugin("org.scalatra.sbt" % "sbt-scalatra" % x)
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
case _ =>
sys.error(
"""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin
)
}

0 comments on commit d726cd4

Please sign in to comment.