Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twirllib: new twirlScalaVersion target to support for newer versions #1959

Merged
merged 7 commits into from
Jul 25, 2022
Merged
29 changes: 27 additions & 2 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import $file.ci.shared
import $file.ci.upload
import $ivy.`org.scalaj::scalaj-http:2.4.2`
import $ivy.`de.tototec::de.tobiasroeser.mill.vcs.version::0.1.4`
import $ivy.`com.github.lolgab::mill-mima::0.0.10`
import $ivy.`com.github.lolgab::mill-mima::0.0.11`
import $ivy.`net.sourceforge.htmlcleaner:htmlcleaner:2.25`
import com.github.lolgab.mill.mima
import com.github.lolgab.mill.mima.{
CheckDirection,
DirectMissingMethodProblem,
IncompatibleMethTypeProblem,
IncompatibleSignatureProblem,
ProblemFilter
ProblemFilter,
ReversedMissingMethodProblem
}
import coursier.maven.MavenRepository
import de.tobiasroeser.mill.vcs.version.VcsVersion
Expand All @@ -21,6 +23,7 @@ import mill.scalalib._
import mill.scalalib.publish._
import mill.modules.Jvm
import mill.define.SelectMode
import upickle.default.{ReadWriter, macroRW}

object Settings {
val pomOrg = "com.lihaoyi"
Expand Down Expand Up @@ -186,6 +189,16 @@ trait MillMimaConfig extends mima.Mima {
"mill.api.internal",
"mill.api.experimental"
)

implicit val checkDirectionBackwardUpickleRW: ReadWriter[CheckDirection.Backward.type] = macroRW
implicit val checkDirectionBothUpickleRW: ReadWriter[CheckDirection.Both.type] = macroRW
implicit val checkDirectionForwardUpickleRW: ReadWriter[CheckDirection.Forward.type] = macroRW
implicit val checkDirectionUpickleRW: ReadWriter[CheckDirection] = ReadWriter.merge(
checkDirectionBackwardUpickleRW,
checkDirectionBothUpickleRW,
checkDirectionForwardUpickleRW
)
override def mimaCheckDirection: Target[CheckDirection] = T { CheckDirection.Backward }
override def mimaBinaryIssueFilters: Target[Seq[ProblemFilter]] = T {
issueFilterByModule.getOrElse(this, Seq())
}
Expand Down Expand Up @@ -225,6 +238,18 @@ trait MillMimaConfig extends mima.Mima {
ProblemFilter.exclude[DirectMissingMethodProblem](
"mill.contrib.scoverage.ScoverageReport#workerModule.bspCompileClasspath"
)
),
// we added a new target and a submodule after 0.10.5
contrib.twirllib -> Seq(
ProblemFilter.exclude[ReversedMissingMethodProblem](
"mill.twirllib.TwirlModule.twirlScalaVersion"
),
ProblemFilter.exclude[ReversedMissingMethodProblem](
"mill.twirllib.TwirlModule.twirlCoursierResolver"
),
ProblemFilter.exclude[ReversedMissingMethodProblem](
"mill.twirllib.TwirlModule.mill$twirllib$TwirlModule$_setter_$twirlCoursierResolver_="
)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ object PlayModuleTests extends TestSuite with PlayTestSuite {
override def playVersion = crossPlayVersion
override def scalaVersion = crossScalaVersion
override def twirlVersion = "1.5.1"
override def twirlScalaVersion = sys.props.getOrElse("MILL_SCALA_2_13_VERSION", ???)
object test extends PlayTests
override def ivyDeps = T { super.ivyDeps() ++ Agg(ws()) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ object PlaySingleApiModuleTests extends TestSuite with PlayTestSuite {

object playsingleapi extends TestUtil.BaseModule with PlayApiModule with SingleModule {
override def playVersion = T { "2.7.0" }
def twirlVersion = T { "1.4.0" }
override def scalaVersion = T { "2.12.8" }
object test extends PlayTests
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ object PlaySingleModuleTests extends TestSuite with PlayTestSuite {
object playsingle extends TestUtil.BaseModule with PlayModule with SingleModule {
override def playVersion = T { "2.8.9" }
override def twirlVersion = T { "1.5.1" }
override def scalaVersion = T { "2.13.6" }
override def twirlScalaVersion: T[String] =
T { sys.props.getOrElse("MILL_SCALA_2_13_VERSION", ???) }
override def scalaVersion = T { sys.props.getOrElse("MILL_SCALA_2_13_VERSION", ???) }
object test extends PlayTests
}

Expand Down
10 changes: 7 additions & 3 deletions contrib/playlib/test/src/mill/playlib/PlayTestSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import mill.util.{TestEvaluator, TestUtil}
import utest.framework.TestPath

trait PlayTestSuite {

val testScala212 = sys.props.getOrElse("MILL_SCALA_2_12_VERSION", ???)
val testScala213 = sys.props.getOrElse("MILL_SCALA_2_13_VERSION", ???)

val matrix = Seq(
("2.12.13", "2.6.15"),
("2.12.13", "2.7.9"),
("2.13.6", "2.8.8")
(testScala212, "2.6.15"),
(testScala212, "2.7.9"),
(testScala213, "2.8.8")
)

def resourcePath: os.Path
Expand Down
54 changes: 39 additions & 15 deletions contrib/twirllib/src/TwirlModule.scala
Original file line number Diff line number Diff line change
@@ -1,38 +1,62 @@
package mill
package twirllib

import coursier.MavenRepository
import mill.define.Sources
import coursier.{Dependency, Repository}
import mill.define.{Sources, Task}
import mill.api.PathRef
import mill.scalalib.Lib.resolveDependencies
import mill.scalalib._
import mill.api.Loose

import scala.io.Codec
import scala.util.Properties

trait TwirlModule extends mill.Module {
trait TwirlModule extends mill.Module { twirlModule =>

def twirlVersion: T[String]

/**
* The Scala version matching the twirl version.
* @since Mill after 0.10.5
*/
def twirlScalaVersion: T[String]

def twirlSources: Sources = T.sources {
millSourcePath / "views"
}

def twirlClasspath: T[Loose.Agg[PathRef]] = T {
resolveDependencies(
Seq(
coursier.LocalRepositories.ivy2Local,
MavenRepository("https://repo1.maven.org/maven2")
),
Lib.depToDependency(_, "2.12.5"),
Seq(
ivy"com.typesafe.play::twirl-compiler:${twirlVersion()}",
ivy"org.scala-lang.modules::scala-parser-combinators:1.1.0"
)
/**
* @since Mill after 0.10.5
*/
def twirlIvyDeps: T[Agg[Dep]] = T {
Agg(
ivy"com.typesafe.play::twirl-compiler:${twirlVersion()}",
ivy"org.scala-lang.modules::scala-parser-combinators:1.1.2"
)
}

/** Class instead of an object, to allow re-configuration.
* @since Mill after 0.10.5
*/
class TwirlResolver()(implicit ctx0: mill.define.Ctx) extends mill.Module()(ctx0)
with CoursierModule {
override def resolveCoursierDependency: Task[Dep => Dependency] = T.task { d: Dep =>
Lib.depToDependency(d, twirlScalaVersion())
}

override def repositoriesTask: Task[Seq[Repository]] = twirlModule match {
case m: CoursierModule => m.repositoriesTask
case _ => super.repositoriesTask
}
}
/**
* @since Mill after 0.10.5
*/
val twirlCoursierResolver = new TwirlResolver()

def twirlClasspath: T[Loose.Agg[PathRef]] = T {
twirlCoursierResolver.resolveDeps(twirlIvyDeps)
}

def twirlImports: T[Seq[String]] = T {
TwirlWorkerApi.twirlWorker.defaultImports(twirlClasspath().map(_.path))
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/twirllib/src/TwirlWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TwirlWorker {
private var twirlInstanceCache = Option.empty[(Long, (TwirlWorkerApi, Class[_]))]

private def twirlCompilerAndClass(twirlClasspath: Agg[os.Path]): (TwirlWorkerApi, Class[_]) = {
val classloaderSig = twirlClasspath.map(p => p.toString().hashCode + os.mtime(p)).sum
val classloaderSig = twirlClasspath.map(p => p.toString().hashCode + os.mtime(p)).iterator.sum
twirlInstanceCache match {
case Some((sig, instance)) if sig == classloaderSig => instance
case _ =>
Expand Down
24 changes: 18 additions & 6 deletions contrib/twirllib/test/src/HelloWorldTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import utest.{TestSuite, Tests, assert, _}

import scala.io.Codec

object HelloWorldTests extends TestSuite {
trait HelloWorldTests extends TestSuite {
val testTwirlVersion: String
val testTwirlScalaVersion: String

trait HelloBase extends TestUtil.BaseModule {
override def millSourcePath: os.Path =
TestUtil.getSrcPathBase() / millOuterCtx.enclosing.split('.')
}

trait HelloWorldModule extends mill.twirllib.TwirlModule {

def twirlVersion = "1.3.15"

def twirlVersion = testTwirlVersion
val twirlScalaVersion = testTwirlScalaVersion
}

object HelloWorld extends HelloBase {
Expand Down Expand Up @@ -86,13 +87,15 @@ object HelloWorldTests extends TestSuite {
eval.apply(HelloWorld.core.twirlVersion)

assert(
result == "1.3.15",
result == testTwirlVersion,
evalCount > 0
)
}
}
"compileTwirl" - workspaceTest(HelloWorld, "hello-world") { eval =>
val Right((result, evalCount)) = eval.apply(HelloWorld.core.compileTwirl)
val res = eval.apply(HelloWorld.core.compileTwirl)
assert(res.isRight)
val Right((result, evalCount)) = res

val outputFiles = os.walk(result.classes.path).filter(_.last.endsWith(".scala"))
val expectedClassfiles = compileClassfiles.map(
Expand Down Expand Up @@ -160,3 +163,12 @@ object HelloWorldTests extends TestSuite {
}
}
}

object HelloWorldTests2_12 extends HelloWorldTests {
override val testTwirlVersion = "1.3.15"
override val testTwirlScalaVersion = sys.props.getOrElse("MILL_SCALA_2_12_VERSION", ???)
}
object HelloWorldTests2_13 extends HelloWorldTests {
override val testTwirlVersion = "1.5.1"
override val testTwirlScalaVersion = sys.props.getOrElse("MILL_SCALA_2_13_VERSION", ???)
}
13 changes: 9 additions & 4 deletions docs/antora/modules/ROOT/pages/Plugin_Twirl.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import mill.scalalib._
import $ivy.`com.lihaoyi::mill-contrib-twirllib:`, mill.twirllib._

object app extends ScalaModule with TwirlModule {
def twirlVersion = "1.3.15"
def twirlVersion = "1.5.1"
def generatedSources = T{ Seq(compileTwirl().classes) }
}
----
Expand All @@ -59,7 +59,11 @@ object app extends ScalaModule with TwirlModule {

=== +def twirlVersion: T[String]+

Mandatory - the version of the twirl compiler to use, like "1.3.15".
Mandatory - the version of the Twirl compiler to use.

=== +def twirlScalaVersion: T[String]+

Mandatory - the Scala version matching the `twirlVersion`. Older Twirl versions (before 1.4.2) only supported Scala 2.12.

=== +def twirlImports: T[Seq[String]]+

Expand Down Expand Up @@ -87,7 +91,8 @@ import mill.scalalib._
import $ivy.`com.lihaoyi::mill-contrib-twirllib:`, mill.twirllib._

object app extends ScalaModule with TwirlModule {
def twirlVersion = "1.3.15"
def twirlVersion = "1.5.1"
def twirlScalaVersion = "2.13.8"
override def twirlImports = super.twirlImports() ++ Seq("my.additional.stuff._", "my.other.stuff._")
def generatedSources = T{ Seq(compileTwirl().classes) }
}
Expand Down Expand Up @@ -129,7 +134,7 @@ import mill.scalalib._
import $ivy.`com.lihaoyi::mill-contrib-twirllib:`, mill.twirllib._

object app extends ScalaModule with TwirlModule {
def twirlVersion = "1.3.15"
def twirlVersion = "1.5.1"
override def twirlFormats = super.twirlFormats() + Map("svg" -> "play.twirl.api.HtmlFormat")
def generatedSources = T{ Seq(compileTwirl().classes) }
}
Expand Down
2 changes: 2 additions & 0 deletions readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ corresponding version of Mill.

_Changes since {prev-version}:_

* `mill.twirllib.TwirlModule` - new mandatory target `twirlScalaVersion` to configure the Scala version used by Twirl compiler

_For details refer to
{link-milestone}/{milestone}?closed=1[milestone {milestone-name}]
and the {link-compare}/{prev-version}\...{version}[list of commits]._
Expand Down