From 63c1ba3d8e3d835a598d1d35d4a3ee1fc6031b1f Mon Sep 17 00:00:00 2001 From: Lorenzo Gabriele Date: Fri, 17 Nov 2023 18:02:20 +0100 Subject: [PATCH] Migrate Java worker API to Scala --- .../lolgab/mill/mima/worker/api/Artifact.java | 11 ---- .../mill/mima/worker/api/CheckDirection.java | 5 -- .../mill/mima/worker/api/MimaWorkerApi.java | 19 ------ .../mill/mima/worker/api/MimaWorkerApi.scala | 32 +++++++++ .../mill/mima/worker/api/ProblemFilter.java | 11 ---- .../mill/mima/worker/MimaWorkerImpl.scala | 56 ++++++++-------- .../github/lolgab/mill/mima/MimaBase.scala | 66 ++++++++----------- .../lolgab/mill/mima/worker/MimaWorker.scala | 2 +- 8 files changed, 88 insertions(+), 114 deletions(-) delete mode 100644 mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/Artifact.java delete mode 100644 mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/CheckDirection.java delete mode 100644 mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/MimaWorkerApi.java create mode 100644 mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/MimaWorkerApi.scala delete mode 100644 mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/ProblemFilter.java diff --git a/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/Artifact.java b/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/Artifact.java deleted file mode 100644 index ac81f62..0000000 --- a/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/Artifact.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.github.lolgab.mill.mima.worker.api; - -public class Artifact { - public String prettyDep; - public java.io.File file; - - public Artifact(String prettyDep, java.io.File file) { - this.prettyDep = prettyDep; - this.file = file; - } -} diff --git a/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/CheckDirection.java b/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/CheckDirection.java deleted file mode 100644 index 33a6f4d..0000000 --- a/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/CheckDirection.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.lolgab.mill.mima.worker.api; - -public enum CheckDirection { - Backward, Forward, Both -} diff --git a/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/MimaWorkerApi.java b/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/MimaWorkerApi.java deleted file mode 100644 index 11bbd97..0000000 --- a/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/MimaWorkerApi.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.github.lolgab.mill.mima.worker.api; - -public interface MimaWorkerApi { - String reportBinaryIssues( - String scalaBinaryVersionOrNull, - java.util.function.Consumer logDebug, - java.util.function.Consumer logError, - java.util.function.Consumer logPrintln, - CheckDirection checkDirection, - java.io.File[] runClasspath, - Artifact[] previous, - java.io.File current, - ProblemFilter[] binaryFilters, - java.util.Map backwardFilters, - java.util.Map forwardFilters, - String[] excludeAnnos, - String publishVersion - ); -} diff --git a/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/MimaWorkerApi.scala b/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/MimaWorkerApi.scala new file mode 100644 index 0000000..3d644a3 --- /dev/null +++ b/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/MimaWorkerApi.scala @@ -0,0 +1,32 @@ +package com.github.lolgab.mill.mima.worker.api; + +import java.io.File + +trait MimaWorkerApi { + def reportBinaryIssues( + scalaBinaryVersion: Option[String], + logDebug: String => Unit, + logError: String => Unit, + logPrintln: String => Unit, + checkDirection: CheckDirection, + runClasspath: Seq[File], + artifacts: Seq[Artifact], + current: File, + binaryFilters: Seq[ProblemFilter], + backwardFilters: Map[String, Seq[ProblemFilter]], + forwardFilters: Map[String, Seq[ProblemFilter]], + excludeAnnos: Seq[String], + publishVersion: String + ): Option[String] +} + +case class Artifact(prettyDep: String, file: File) + +sealed trait CheckDirection +object CheckDirection { + case object Backward extends CheckDirection + case object Forward extends CheckDirection + case object Both extends CheckDirection +} + +case class ProblemFilter(name: String, problem: String) diff --git a/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/ProblemFilter.java b/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/ProblemFilter.java deleted file mode 100644 index 145efde..0000000 --- a/mill-mima-worker-api/src/com/github/lolgab/mill/mima/worker/api/ProblemFilter.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.github.lolgab.mill.mima.worker.api; - -public final class ProblemFilter { - public final String name; - public final String problem; - - public ProblemFilter(String n, String p) { - name = n; - problem = p; - } -} diff --git a/mill-mima-worker-impl/src/com/github/lolgab/mill/mima/worker/MimaWorkerImpl.scala b/mill-mima-worker-impl/src/com/github/lolgab/mill/mima/worker/MimaWorkerImpl.scala index bc75728..c4adb95 100644 --- a/mill-mima-worker-impl/src/com/github/lolgab/mill/mima/worker/MimaWorkerImpl.scala +++ b/mill-mima-worker-impl/src/com/github/lolgab/mill/mima/worker/MimaWorkerImpl.scala @@ -7,36 +7,34 @@ import com.typesafe.tools.mima.core.ProblemFilters import com.typesafe.tools.mima.core.{ProblemFilter => MimaProblemFilter} import com.typesafe.tools.mima.lib.MiMaLib -import scala.jdk.CollectionConverters._ - class MimaWorkerImpl extends MimaWorkerApi { def reportBinaryIssues( - scalaBinaryVersionOrNull: String, - logDebug: java.util.function.Consumer[String], - logError: java.util.function.Consumer[String], - logPrintln: java.util.function.Consumer[String], + scalaBinaryVersion: Option[String], + logDebug: String => Unit, + logError: String => Unit, + logPrintln: String => Unit, checkDirection: CheckDirection, - runClasspath: Array[java.io.File], - previous: Array[Artifact], + runClasspath: Seq[java.io.File], + previous: Seq[Artifact], current: java.io.File, - binaryFilters: Array[ProblemFilter], - backwardFilters: java.util.Map[String, Array[ProblemFilter]], - forwardFilters: java.util.Map[String, Array[ProblemFilter]], - excludeAnnos: Array[String], + binaryFilters: Seq[ProblemFilter], + backwardFilters: Map[String, Seq[ProblemFilter]], + forwardFilters: Map[String, Seq[ProblemFilter]], + excludeAnnos: Seq[String], publishVersion: String - ): String = { - sanityCheckScalaBinaryVersion(scalaBinaryVersionOrNull) + ): Option[String] = { + sanityCheckScalaBinaryVersion(scalaBinaryVersion) val mimaLib = new MiMaLib(runClasspath.toSeq) def isReported( - versionedFilters: java.util.Map[String, Array[ProblemFilter]] + versionedFilters: Map[String, Seq[ProblemFilter]] )(problem: Problem) = { - val filters = binaryFilters.map(problemFilterToMima).toSeq - val mimaVersionedFilters = versionedFilters.asScala.map { case (k, v) => - k -> v.map(problemFilterToMima).toSeq - }.toMap + val filters = binaryFilters.map(problemFilterToMima) + val mimaVersionedFilters = versionedFilters.map { case (k, v) => + k -> v.map(problemFilterToMima) + } MyProblemReporting.isReported( publishVersion, filters, @@ -44,7 +42,7 @@ class MimaWorkerImpl extends MimaWorkerApi { )(problem) } - logPrintln.accept( + logPrintln( s"Scanning binary compatibility in ${current} ..." ) val (problemsCount, filteredCount) = @@ -64,7 +62,7 @@ class MimaWorkerImpl extends MimaWorkerApi { val forwErrors = forward.filter(isReported(forwardFilters)) val count = backErrors.size + forwErrors.size val filteredCount = backward.size + forward.size - count - val doLog = if (count == 0) logDebug.accept(_) else logError.accept(_) + val doLog = if (count == 0) logDebug(_) else logError(_) doLog(s"Found ${count} issue when checking against ${prev.prettyDep}") backErrors.foreach(problem => doLog(prettyProblem("current")(problem))) forwErrors.foreach(problem => doLog(prettyProblem("other")(problem))) @@ -74,10 +72,12 @@ class MimaWorkerImpl extends MimaWorkerApi { if (problemsCount > 0) { val filteredNote = if (filteredCount > 0) s" (filtered $filteredCount)" else "" - s"Failed binary compatibility check! Found $problemsCount potential problems$filteredNote" + Some( + s"Failed binary compatibility check! Found $problemsCount potential problems$filteredNote" + ) } else { - logPrintln.accept("Binary compatibility check passed") - null + logPrintln("Binary compatibility check passed") + None } } @@ -90,11 +90,11 @@ class MimaWorkerImpl extends MimaWorkerApi { } private def sanityCheckScalaBinaryVersion( - scalaBinaryVersionOrNull: String + scalaBinaryVersion: Option[String] ) = { - scalaBinaryVersionOrNull match { - case "3" | "2.13" | "2.12" | "2.11" | null => // ok - case other => + scalaBinaryVersion match { + case Some("3" | "2.13" | "2.12" | "2.11") | None => // ok + case Some(other) => throw new IllegalArgumentException( s"MiMa supports Scala 2.11, 2.12, 2.13 and 3, not $other" ) diff --git a/mill-mima/src/com/github/lolgab/mill/mima/MimaBase.scala b/mill-mima/src/com/github/lolgab/mill/mima/MimaBase.scala index 7f74e1f..9790af6 100644 --- a/mill-mima/src/com/github/lolgab/mill/mima/MimaBase.scala +++ b/mill-mima/src/com/github/lolgab/mill/mima/MimaBase.scala @@ -9,7 +9,6 @@ import mill.define.Target import mill.define.Task import mill.scalalib._ -import scala.jdk.CollectionConverters._ import scala.util.chaining._ private[mima] trait MimaBase @@ -114,7 +113,7 @@ private[mima] trait MimaBase def mimaCurrentArtifact: T[PathRef] = T { jar() } def mimaReportBinaryIssues(): Command[Unit] = { - val scalaBinVersionOrNullTask: Task[Option[String]] = this match { + val scalaBinVersionTask: Task[Option[String]] = this match { case m: ScalaModule => T.task { Some(scalaBinaryVersion(m.scalaVersion())) } case _ => T.task { None } @@ -124,10 +123,7 @@ private[mima] trait MimaBase s"${dep.dep.module.orgName}:${dep.dep.version}" } val log = T.ctx().log - val logDebug: java.util.function.Consumer[String] = log.debug(_) - val logError: java.util.function.Consumer[String] = log.error(_) - val logPrintln: java.util.function.Consumer[String] = - log.outputStream.println(_) + val runClasspathIO = runClasspath().view.map(_.path).filter(os.exists).map(_.toIO).toArray val current = mimaCurrentArtifact().path.pipe { @@ -135,10 +131,10 @@ private[mima] trait MimaBase case _ => (T.dest / "emptyClasses").tap(os.makeDir) }.toIO - val previous = resolvedMimaPreviousArtifacts().iterator.map { + val previous = resolvedMimaPreviousArtifacts().map { case (dep, artifact) => - new worker.api.Artifact(prettyDep(dep), artifact.path.toIO) - }.toArray + worker.api.Artifact(prettyDep(dep), artifact.path.toIO) + }.toSeq val checkDirection = mimaCheckDirection() match { case CheckDirection.Forward => worker.api.CheckDirection.Forward @@ -147,7 +143,7 @@ private[mima] trait MimaBase } def toWorkerApi(p: ProblemFilter) = - new worker.api.ProblemFilter(p.name, p.problem) + worker.api.ProblemFilter(name = p.name, problem = p.problem) val incompatibleSignatureProblemFilters = if (mimaReportSignatureProblems()) Seq.empty @@ -155,37 +151,29 @@ private[mima] trait MimaBase val binaryFilters = (mimaBinaryIssueFilters() ++ incompatibleSignatureProblemFilters) .map(toWorkerApi) - .toArray val backwardFilters = - mimaBackwardIssueFilters().view - .mapValues(_.map(toWorkerApi).toArray) - .toMap - .asJava + mimaBackwardIssueFilters().view.mapValues(_.map(toWorkerApi)).toMap val forwardFilters = - mimaForwardIssueFilters().view - .mapValues(_.map(toWorkerApi).toArray) - .toMap - .asJava - - val errorOrNull: String = - mimaWorker().reportBinaryIssues( - scalaBinVersionOrNullTask().orNull, - logDebug, - logError, - logPrintln, - checkDirection, - runClasspathIO, - previous, - current, - binaryFilters, - backwardFilters, - forwardFilters, - mimaExcludeAnnotations().toArray, - publishVersion() - ) - - if (errorOrNull == null) Result.Success(()) - else Result.Failure(errorOrNull) + mimaForwardIssueFilters().view.mapValues(_.map(toWorkerApi)).toMap + + mimaWorker().reportBinaryIssues( + scalaBinVersionTask(), + log.debug(_), + log.error(_), + log.outputStream.println(_), + checkDirection, + runClasspathIO, + previous, + current, + binaryFilters, + backwardFilters, + forwardFilters, + mimaExcludeAnnotations(), + publishVersion() + ) match { + case Some(error) => Result.Failure(error) + case None => Result.Success(()) + } } } diff --git a/mill-mima/src/com/github/lolgab/mill/mima/worker/MimaWorker.scala b/mill-mima/src/com/github/lolgab/mill/mima/worker/MimaWorker.scala index b29e85c..82504dc 100644 --- a/mill-mima/src/com/github/lolgab/mill/mima/worker/MimaWorker.scala +++ b/mill-mima/src/com/github/lolgab/mill/mima/worker/MimaWorker.scala @@ -19,7 +19,7 @@ class MimaWorker { case _ => val cl = mill.api.ClassLoader.create( mimaWorkerClasspath.map(_.path.toIO.toURI.toURL).iterator.to(Seq), - parent = null, + parent = getClass.getClassLoader, sharedLoader = getClass.getClassLoader, sharedPrefixes = Seq("com.github.lolgab.mill.mima.worker.api.") )