Skip to content

Commit

Permalink
Migrate Java worker API to Scala (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
lolgab authored May 30, 2024
1 parent 283e145 commit 95c8b15
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 114 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,42 @@ 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,
mimaVersionedFilters
)(problem)
}

logPrintln.accept(
logPrintln(
s"Scanning binary compatibility in ${current} ..."
)
val (problemsCount, filteredCount) =
Expand All @@ -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)))
Expand All @@ -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
}
}

Expand All @@ -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"
)
Expand Down
66 changes: 27 additions & 39 deletions mill-mima/src/com/github/lolgab/mill/mima/MimaBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand All @@ -124,21 +123,18 @@ 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 {
case p if os.exists(p) => p
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
Expand All @@ -147,45 +143,37 @@ 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
else Seq(ProblemFilter.exclude[IncompatibleSignatureProblem]("*"))
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(())
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
)
Expand Down

0 comments on commit 95c8b15

Please sign in to comment.