Skip to content

Commit

Permalink
Refactor and update
Browse files Browse the repository at this point in the history
  • Loading branch information
earldouglas committed Oct 10, 2024
1 parent 251c02d commit e7d7c1b
Show file tree
Hide file tree
Showing 19 changed files with 118 additions and 178 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions src/main/scala-2/com/earldouglas/sbt/war/Compat.scala
Original file line number Diff line number Diff line change
@@ -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)
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

39 changes: 37 additions & 2 deletions src/main/scala-3/com/earldouglas/sbt/war/Compat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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())

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

10 changes: 5 additions & 5 deletions src/main/scala/com/earldouglas/sbt/war/WarPackagePlugin.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.earldouglas.sbt.war

import sbt.Keys._
import sbt.Keys.{`package` => pkg}
import sbt._
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -60,7 +60,7 @@ object WarPackageRunnerPlugin extends AutoPlugin {
runner.getPath(),
"--port",
warPort.value.toString(),
WarPackageRunnerPluginCompat
Compat
.toFile(pkg)
.value
.getPath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
13 changes: 13 additions & 0 deletions src/test/scala-2/com/earldouglas/sbt/war/TestCompat.scala
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions src/test/scala-3/com/earldouglas/sbt/war/TestCompat.scala
Original file line number Diff line number Diff line change
@@ -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
13 changes: 8 additions & 5 deletions src/test/scala/com/earldouglas/sbt/war/HttpClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit e7d7c1b

Please sign in to comment.