From e7d7c1b656aac5c72e71cbf08d1f8354bb96198b Mon Sep 17 00:00:00 2001 From: James Earl Douglas Date: Thu, 10 Oct 2024 07:37:29 -0700 Subject: [PATCH] Refactor and update --- build.sbt | 2 +- .../com/earldouglas/sbt/war/Compat.scala | 22 +++++++++++ .../sbt/war/WarPackagePluginCompat.scala | 17 -------- .../war/WarPackageRunnerPluginCompat.scala | 19 --------- .../war/WebappComponentsPluginCompat.scala | 12 ------ .../war/WebappPackageRunnerPluginCompat.scala | 16 -------- .../com/earldouglas/sbt/war/Compat.scala | 39 ++++++++++++++++++- .../sbt/war/WarPackagePluginCompat.scala | 26 ------------- .../war/WarPackageRunnerPluginCompat.scala | 28 ------------- .../war/WebappComponentsPluginCompat.scala | 18 --------- .../war/WebappPackageRunnerPluginCompat.scala | 23 ----------- .../sbt/war/WarPackagePlugin.scala | 10 ++--- .../sbt/war/WarPackageRunnerPlugin.scala | 4 +- .../sbt/war/WebappComponentsPlugin.scala | 6 +-- .../war/WebappComponentsRunnerPlugin.scala | 2 +- .../com/earldouglas/sbt/war/TestCompat.scala | 13 +++++++ .../com/earldouglas/sbt/war/TestCompat.scala | 20 ++++++++++ .../com/earldouglas/sbt/war/HttpClient.scala | 13 ++++--- .../sbt/war/WebappComponentsTest.scala | 6 +++ 19 files changed, 118 insertions(+), 178 deletions(-) delete mode 100644 src/main/scala-2/com/earldouglas/sbt/war/WarPackagePluginCompat.scala delete mode 100644 src/main/scala-2/com/earldouglas/sbt/war/WarPackageRunnerPluginCompat.scala delete mode 100644 src/main/scala-2/com/earldouglas/sbt/war/WebappComponentsPluginCompat.scala delete mode 100644 src/main/scala-2/com/earldouglas/sbt/war/WebappPackageRunnerPluginCompat.scala delete mode 100644 src/main/scala-3/com/earldouglas/sbt/war/WarPackagePluginCompat.scala delete mode 100644 src/main/scala-3/com/earldouglas/sbt/war/WarPackageRunnerPluginCompat.scala delete mode 100644 src/main/scala-3/com/earldouglas/sbt/war/WebappComponentsPluginCompat.scala delete mode 100644 src/main/scala-3/com/earldouglas/sbt/war/WebappPackageRunnerPluginCompat.scala create mode 100644 src/test/scala-2/com/earldouglas/sbt/war/TestCompat.scala create mode 100644 src/test/scala-3/com/earldouglas/sbt/war/TestCompat.scala diff --git a/build.sbt b/build.sbt index 7c81a4c5..a0a3fd81 100644 --- a/build.sbt +++ b/build.sbt @@ -5,7 +5,7 @@ organization := "com.earldouglas" sbtPlugin := true scalacOptions ++= Seq("-feature", "-deprecation") scalaVersion := "2.12.20" -crossScalaVersions += "3.3.4" +crossScalaVersions += "3.5.1" pluginCrossBuild / sbtVersion := { scalaBinaryVersion.value match { case "2.12" => (pluginCrossBuild / sbtVersion).value diff --git a/src/main/scala-2/com/earldouglas/sbt/war/Compat.scala b/src/main/scala-2/com/earldouglas/sbt/war/Compat.scala index 254b5900..c51ad1d8 100644 --- a/src/main/scala-2/com/earldouglas/sbt/war/Compat.scala +++ b/src/main/scala-2/com/earldouglas/sbt/war/Compat.scala @@ -1,13 +1,35 @@ package com.earldouglas.sbt.war +import sbt.Def.Initialize import sbt.Keys._ import sbt.Keys.{`package` => pkg} import sbt._ +import java.io.{File => JavaFile} + object Compat { + val Compile_pkg_artifact = Compile / pkg / artifact val Compile_sourceDirectory = Compile / sourceDirectory val Compile_target = Compile / target val Global_onLoad = Global / onLoad val pkg_artifact = pkg / artifact + + val webappContents: Initialize[Task[Map[String, JavaFile]]] = + WebappComponentsPlugin.webappContents + + def managedJars(config: Configuration): Initialize[Task[Seq[File]]] = + Def.task { + Classpaths + .managedJars(config, classpathTypes.value, update.value) + .map(_.data) + .toSeq + } + + def toFile(file: TaskKey[File]): Initialize[Task[File]] = + file + + val classpathFiles: Initialize[Task[Seq[File]]] = + (Runtime / fullClasspath) + .map(_.files) } diff --git a/src/main/scala-2/com/earldouglas/sbt/war/WarPackagePluginCompat.scala b/src/main/scala-2/com/earldouglas/sbt/war/WarPackagePluginCompat.scala deleted file mode 100644 index 609235ed..00000000 --- a/src/main/scala-2/com/earldouglas/sbt/war/WarPackagePluginCompat.scala +++ /dev/null @@ -1,17 +0,0 @@ -package com.earldouglas.sbt.war - -import sbt.Def.Initialize -import sbt._ - -object WarPackagePluginCompat { - - // Flip webappContents around from (dst -> src) to (src -> dst) - lazy val packageContents - : Initialize[Task[Seq[(java.io.File, String)]]] = - WebappComponentsPlugin.webappContents - .map { - _.map { - _.swap - }.toSeq - } -} diff --git a/src/main/scala-2/com/earldouglas/sbt/war/WarPackageRunnerPluginCompat.scala b/src/main/scala-2/com/earldouglas/sbt/war/WarPackageRunnerPluginCompat.scala deleted file mode 100644 index 844f6fc0..00000000 --- a/src/main/scala-2/com/earldouglas/sbt/war/WarPackageRunnerPluginCompat.scala +++ /dev/null @@ -1,19 +0,0 @@ -package com.earldouglas.sbt.war - -import sbt.Def.Initialize -import sbt.Keys._ -import sbt._ - -object WarPackageRunnerPluginCompat { - - def runnerJars(config: Configuration): Initialize[Task[Seq[File]]] = - Def.task { - Classpaths - .managedJars(config, classpathTypes.value, update.value) - .map(_.data) - .toSeq - } - - def toFile(file: TaskKey[File]): Initialize[Task[File]] = - file -} diff --git a/src/main/scala-2/com/earldouglas/sbt/war/WebappComponentsPluginCompat.scala b/src/main/scala-2/com/earldouglas/sbt/war/WebappComponentsPluginCompat.scala deleted file mode 100644 index 2f87011a..00000000 --- a/src/main/scala-2/com/earldouglas/sbt/war/WebappComponentsPluginCompat.scala +++ /dev/null @@ -1,12 +0,0 @@ -package com.earldouglas.sbt.war - -import sbt.Def.Initialize -import sbt.Keys._ -import sbt._ - -object WebappComponentsPluginCompat { - - val classpathFiles: Initialize[Task[Seq[File]]] = - (Runtime / fullClasspath) - .map(_.files) -} diff --git a/src/main/scala-2/com/earldouglas/sbt/war/WebappPackageRunnerPluginCompat.scala b/src/main/scala-2/com/earldouglas/sbt/war/WebappPackageRunnerPluginCompat.scala deleted file mode 100644 index 9d058096..00000000 --- a/src/main/scala-2/com/earldouglas/sbt/war/WebappPackageRunnerPluginCompat.scala +++ /dev/null @@ -1,16 +0,0 @@ -package com.earldouglas.sbt.war - -import sbt.Def.Initialize -import sbt.Keys._ -import sbt._ - -object WebappPackageRunnerPluginCompat { - - def runnerJars(config: Configuration): Initialize[Task[Seq[File]]] = - Def.task { - Classpaths - .managedJars(config, classpathTypes.value, update.value) - .map(_.data) - .toSeq - } -} diff --git a/src/main/scala-3/com/earldouglas/sbt/war/Compat.scala b/src/main/scala-3/com/earldouglas/sbt/war/Compat.scala index a2f326f9..75bcc729 100644 --- a/src/main/scala-3/com/earldouglas/sbt/war/Compat.scala +++ b/src/main/scala-3/com/earldouglas/sbt/war/Compat.scala @@ -4,13 +4,48 @@ import sbt.Keys._ import sbt.Keys.{`package` => pkg} import sbt._ import sbt.given +import sbt.Def.Initialize +import sbt.Keys._ +import sbt.Keys.{`package` => pkg} +import sbt._ + +import java.io.{File => JavaFile} import scala.language.implicitConversions -object Compat { +object Compat: + val Compile_pkg_artifact = Compile / pkg / artifact val Compile_sourceDirectory = Compile / sourceDirectory val Compile_target = Compile / target val Global_onLoad = Global / onLoad val pkg_artifact = pkg / artifact -} + + val webappContents: Initialize[Task[Map[String, HashedVirtualFileRef]]] = + Def.task: + val conv: FileConverter = fileConverter.value + WebappComponentsPlugin.webappContents.value + .map: + case (dst, src) => dst -> conv.toVirtualFile(src.toPath()) + + def managedJars(config: Configuration): Initialize[Task[Seq[File]]] = + Def.task: + Classpaths + .managedJars(config, classpathTypes.value, update.value, fileConverter.value) + .map(_.data) + .map(fileConverter.value.toPath(_)) + .map(_.toFile()) + .toList + + def toFile(file: TaskKey[HashedVirtualFileRef]): Initialize[Task[File]] = + Def.task: + fileConverter.value + .toPath(file.value) + .toFile() + + val classpathFiles: Initialize[Task[Seq[File]]] = + Def.task: + (Runtime / fullClasspath).value + .map(_.data) + .map(fileConverter.value.toPath(_)) + .map(_.toFile()) diff --git a/src/main/scala-3/com/earldouglas/sbt/war/WarPackagePluginCompat.scala b/src/main/scala-3/com/earldouglas/sbt/war/WarPackagePluginCompat.scala deleted file mode 100644 index 4954a509..00000000 --- a/src/main/scala-3/com/earldouglas/sbt/war/WarPackagePluginCompat.scala +++ /dev/null @@ -1,26 +0,0 @@ -package com.earldouglas.sbt.war - -import sbt.Def.Initialize -import sbt.Keys.artifact -import sbt.Keys.fileConverter -import sbt.Keys.moduleName -import sbt.Keys.{`package` => pkg} -import sbt.* -import sbt.given -import scala.language.implicitConversions -import java.nio.file.{ Path => NioPath } -import xsbti.{ FileConverter, HashedVirtualFileRef, VirtualFile } -import sbt.librarymanagement.Configuration -import sbt.SlashSyntax.RichConfiguration - -object WarPackagePluginCompat: - - // Flip webappContents around from (dst -> src) to (src -> dst) - def packageContents: Initialize[Task[Seq[(HashedVirtualFileRef, String)]]] = - Def.task: - val conv: FileConverter = fileConverter.value - WebappComponentsPlugin.webappContents.value - .map: - case (dst, src) => (conv.toVirtualFile(src.toPath()), dst) - .toSeq - diff --git a/src/main/scala-3/com/earldouglas/sbt/war/WarPackageRunnerPluginCompat.scala b/src/main/scala-3/com/earldouglas/sbt/war/WarPackageRunnerPluginCompat.scala deleted file mode 100644 index ec2a7ef4..00000000 --- a/src/main/scala-3/com/earldouglas/sbt/war/WarPackageRunnerPluginCompat.scala +++ /dev/null @@ -1,28 +0,0 @@ -package com.earldouglas.sbt.war - -import sbt.Def.Initialize -import sbt.Keys._ -import sbt.* -import sbt.given -import scala.language.implicitConversions -import sbt.internal.util.Attributed -import sbt.librarymanagement.Configuration -import sbt.SlashSyntax.RichConfiguration -import xsbti.HashedVirtualFileRef - -object WarPackageRunnerPluginCompat: - - def runnerJars(config: Configuration): Initialize[Task[Seq[File]]] = - Def.task: - Classpaths - .managedJars(config, classpathTypes.value, update.value, fileConverter.value) - .map(_.data) - .map(fileConverter.value.toPath(_)) - .map(_.toFile()) - .toList - - def toFile(file: TaskKey[HashedVirtualFileRef]): Initialize[Task[File]] = - Def.task: - fileConverter.value - .toPath(file.value) - .toFile() diff --git a/src/main/scala-3/com/earldouglas/sbt/war/WebappComponentsPluginCompat.scala b/src/main/scala-3/com/earldouglas/sbt/war/WebappComponentsPluginCompat.scala deleted file mode 100644 index 1b01801c..00000000 --- a/src/main/scala-3/com/earldouglas/sbt/war/WebappComponentsPluginCompat.scala +++ /dev/null @@ -1,18 +0,0 @@ -package com.earldouglas.sbt.war - -import sbt.Def.Initialize -import sbt.Keys._ -import sbt.* -import sbt.given -import scala.language.implicitConversions -import sbt.librarymanagement.Configuration -import sbt.SlashSyntax.RichConfiguration - -object WebappComponentsPluginCompat: - - lazy val classpathFiles: Initialize[Task[Seq[File]]] = - Def.task: - (Runtime / fullClasspath).value - .map(_.data) - .map(fileConverter.value.toPath(_)) - .map(_.toFile()) diff --git a/src/main/scala-3/com/earldouglas/sbt/war/WebappPackageRunnerPluginCompat.scala b/src/main/scala-3/com/earldouglas/sbt/war/WebappPackageRunnerPluginCompat.scala deleted file mode 100644 index f67cac7d..00000000 --- a/src/main/scala-3/com/earldouglas/sbt/war/WebappPackageRunnerPluginCompat.scala +++ /dev/null @@ -1,23 +0,0 @@ -package com.earldouglas.sbt.war - -import sbt.Def.Initialize -import sbt.Keys.* -import sbt.* -import sbt.given -import scala.language.implicitConversions -import xsbti.HashedVirtualFileRef - -object WebappPackageRunnerPluginCompat: - - def runnerJars(config: Configuration): Initialize[Task[Seq[File]]] = - Def.task: - Classpaths - .managedJars(config, classpathTypes.value, update.value, fileConverter.value) - .map(_.data) - .map(fileConverter.value.toPath(_)) - .map(_.toFile()) - .toList - - def toFilePath(file: HashedVirtualFileRef): Initialize[Task[String]] = - Def.task: - fileConverter.value.toPath(file).toString() diff --git a/src/main/scala/com/earldouglas/sbt/war/WarPackagePlugin.scala b/src/main/scala/com/earldouglas/sbt/war/WarPackagePlugin.scala index 48f3ce7b..cb16a3cf 100644 --- a/src/main/scala/com/earldouglas/sbt/war/WarPackagePlugin.scala +++ b/src/main/scala/com/earldouglas/sbt/war/WarPackagePlugin.scala @@ -1,5 +1,4 @@ package com.earldouglas.sbt.war - import sbt.Keys._ import sbt.Keys.{`package` => pkg} import sbt._ @@ -18,11 +17,12 @@ object WarPackagePlugin extends AutoPlugin { override lazy val projectSettings: Seq[Setting[_]] = { + // Flip webappContents around from (dst -> src) to (src -> dst) + val packageContents = + Compat.webappContents.map(_.map(_.swap).toSeq) + val packageTaskSettings: Seq[Setting[_]] = - Defaults.packageTaskSettings( - pkg, - WarPackagePluginCompat.packageContents - ) + Defaults.packageTaskSettings(pkg, packageContents) val packageArtifactSetting: Setting[_] = Compat.pkg_artifact := Artifact(moduleName.value, "war", "war") diff --git a/src/main/scala/com/earldouglas/sbt/war/WarPackageRunnerPlugin.scala b/src/main/scala/com/earldouglas/sbt/war/WarPackageRunnerPlugin.scala index 61d4e8cc..159d7c8c 100644 --- a/src/main/scala/com/earldouglas/sbt/war/WarPackageRunnerPlugin.scala +++ b/src/main/scala/com/earldouglas/sbt/war/WarPackageRunnerPlugin.scala @@ -47,7 +47,7 @@ object WarPackageRunnerPlugin extends AutoPlugin { stopContainerInstance() val runnerJars: Seq[File] = - WarPackageRunnerPluginCompat.runnerJars(War).value + Compat.managedJars(War).value runnerJars match { case runner :: Nil => @@ -60,7 +60,7 @@ object WarPackageRunnerPlugin extends AutoPlugin { runner.getPath(), "--port", warPort.value.toString(), - WarPackageRunnerPluginCompat + Compat .toFile(pkg) .value .getPath() diff --git a/src/main/scala/com/earldouglas/sbt/war/WebappComponentsPlugin.scala b/src/main/scala/com/earldouglas/sbt/war/WebappComponentsPlugin.scala index 456c3b8d..217b9c2f 100644 --- a/src/main/scala/com/earldouglas/sbt/war/WebappComponentsPlugin.scala +++ b/src/main/scala/com/earldouglas/sbt/war/WebappComponentsPlugin.scala @@ -51,16 +51,16 @@ object WebappComponentsPlugin extends AutoPlugin { override val projectSettings: Seq[Setting[_]] = { val webappResourcesTask: Initialize[Task[Map[String, File]]] = - (Compat.Compile_sourceDirectory) + Compat.Compile_sourceDirectory .map(_ / "webapp") .map(WebappComponents.getResources(_)) val webappClassesTask: Initialize[Task[Map[String, File]]] = - WebappComponentsPluginCompat.classpathFiles + Compat.classpathFiles .map(WebappComponents.getClasses(_)) val webappLibTask: Initialize[Task[Map[String, File]]] = - WebappComponentsPluginCompat.classpathFiles + Compat.classpathFiles .map(WebappComponents.getLib(_)) Seq( diff --git a/src/main/scala/com/earldouglas/sbt/war/WebappComponentsRunnerPlugin.scala b/src/main/scala/com/earldouglas/sbt/war/WebappComponentsRunnerPlugin.scala index 4cbba8a9..51d95eb0 100644 --- a/src/main/scala/com/earldouglas/sbt/war/WebappComponentsRunnerPlugin.scala +++ b/src/main/scala/com/earldouglas/sbt/war/WebappComponentsRunnerPlugin.scala @@ -81,7 +81,7 @@ object WebappComponentsRunnerPlugin extends AutoPlugin { stopContainerInstance() val runnerJars: Seq[File] = - WebappPackageRunnerPluginCompat.runnerJars(Webapp).value + Compat.managedJars(Webapp).value streams.value.log.info("[sbt-war] Starting server") val process: ScalaProcess = diff --git a/src/test/scala-2/com/earldouglas/sbt/war/TestCompat.scala b/src/test/scala-2/com/earldouglas/sbt/war/TestCompat.scala new file mode 100644 index 00000000..5dc478f7 --- /dev/null +++ b/src/test/scala-2/com/earldouglas/sbt/war/TestCompat.scala @@ -0,0 +1,13 @@ +package com.earldouglas.sbt.war +import java.util.{List => JavaList} +import java.util.{Map => JavaMap} +import scala.collection.JavaConverters._ + +object TestCompat { + + def asScala[A, B](javaMap: JavaMap[A, B]): Map[A, B] = + javaMap.asScala.toMap + + def asScala[A](javaList: JavaList[A]): List[A] = + javaList.asScala.toList +} diff --git a/src/test/scala-3/com/earldouglas/sbt/war/TestCompat.scala b/src/test/scala-3/com/earldouglas/sbt/war/TestCompat.scala new file mode 100644 index 00000000..16e7dd02 --- /dev/null +++ b/src/test/scala-3/com/earldouglas/sbt/war/TestCompat.scala @@ -0,0 +1,20 @@ +package com.earldouglas.sbt.war + +import java.net.HttpURLConnection +import java.net.URI +import scala.jdk.CollectionConverters._ +import scala.io.Source +import java.util.{List => JavaList} +import java.util.{Map => JavaMap} + +object TestCompat: + + def asScala[A, B](javaMap: JavaMap[A, B]): Map[A, B] = + javaMap + .asScala + .toMap + + def asScala[A](javaList: JavaList[A]): List[A] = + javaList + .asScala + .toList diff --git a/src/test/scala/com/earldouglas/sbt/war/HttpClient.scala b/src/test/scala/com/earldouglas/sbt/war/HttpClient.scala index df16941a..adb607d7 100644 --- a/src/test/scala/com/earldouglas/sbt/war/HttpClient.scala +++ b/src/test/scala/com/earldouglas/sbt/war/HttpClient.scala @@ -2,7 +2,6 @@ package com.earldouglas.sbt.war import java.net.HttpURLConnection import java.net.URI -import scala.collection.JavaConverters._ import scala.io.Source object HttpClient { @@ -42,11 +41,15 @@ object HttpClient { val response = Response( status = c.getResponseCode(), - headers = c - .getHeaderFields() - .asScala + headers = TestCompat + .asScala( + c + .getHeaderFields() + ) .filter({ case (k, _) => k != null }) - .map({ case (k, v) => (k, v.asScala.mkString(",")) }) + .map({ case (k, v) => + (k, TestCompat.asScala(v).mkString(",")) + }) .toMap - "Date" - "Content-Length" - "Server", body = Source.fromInputStream { if (c.getResponseCode() < 400) { diff --git a/src/test/scala/com/earldouglas/sbt/war/WebappComponentsTest.scala b/src/test/scala/com/earldouglas/sbt/war/WebappComponentsTest.scala index b7fcdd32..27c51ba2 100644 --- a/src/test/scala/com/earldouglas/sbt/war/WebappComponentsTest.scala +++ b/src/test/scala/com/earldouglas/sbt/war/WebappComponentsTest.scala @@ -7,6 +7,8 @@ import java.io.File class WebappComponentsTest extends AnyFunSuite with Matchers { + println("asdf") + val fakeproject: File = new File("src/test/fakeproject/src/main/webapp") @@ -19,6 +21,8 @@ class WebappComponentsTest extends AnyFunSuite with Matchers { test("getResources") { + println("getResources start") + val expected: Map[String, File] = Map( "bar.html" -> "src/test/fakeproject/src/main/webapp/bar.html", @@ -30,6 +34,8 @@ class WebappComponentsTest extends AnyFunSuite with Matchers { WebappComponents.getResources(fakeproject) obtained shouldBe expected + + println("getResources end") } test("getClasses") {