Skip to content

Commit

Permalink
Cherry-pick source compatible Scala 3 changes (#3721)
Browse files Browse the repository at this point in the history
Pulls in some changes from #3369
to try and reduce the size of that diff and simplify future backports
  • Loading branch information
lihaoyi authored Oct 12, 2024
1 parent 7221788 commit 7da0265
Show file tree
Hide file tree
Showing 80 changed files with 477 additions and 253 deletions.
2 changes: 1 addition & 1 deletion bsp/worker/src/mill/bsp/worker/MillBuildServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ private class MillBuildServer(
State
) => V): CompletableFuture[V] = {
val prefix = hint.split(" ").head
completable(hint) { state: State =>
completable(hint) { (state: State) =>
val ids = state.filterNonSynthetic(targetIds(state).asJava).asScala
val tasksSeq = ids.flatMap { id =>
val (m, ev) = state.bspModulesById(id)
Expand Down
2 changes: 1 addition & 1 deletion bsp/worker/src/mill/bsp/worker/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private object Utils {
originId: String,
bspIdsByModule: Map[BspModule, BuildTargetIdentifier],
client: BuildClient
): Int => Option[CompileProblemReporter] = { moduleHashCode: Int =>
): Int => Option[CompileProblemReporter] = { (moduleHashCode: Int) =>
bspIdsByModule.find(_._1.hashCode == moduleHashCode).map {
case (module: JavaModule, targetId) =>
val buildTarget = module.bspBuildTarget
Expand Down
1 change: 1 addition & 0 deletions build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ object Deps {
// When updating, run "Publish Bridges" Github Actions for the new version
// and then add to it `bridgeScalaVersions`
val scalaVersion = "2.13.14"
val scala2Version = "2.13.14"
// The Scala 2.12.x version to use for some workers
val workerScalaVersion212 = "2.12.19"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ object BuildInfo {
|package ${buildInfoPackageName}
|
|object $buildInfoObjectName {
| private[this] val buildInfoProperties: java.util.Properties = new java.util.Properties()
| private val buildInfoProperties: java.util.Properties = new java.util.Properties()
|
| {
| val buildInfoInputStream = getClass
Expand Down
1 change: 1 addition & 0 deletions contrib/playlib/src/mill/playlib/RouterModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ trait RouterModule extends ScalaModule with Version {
repositoriesTask(),
artifactSuffix = playMinorVersion() match {
case "2.6" => "_2.12"
case "2.7" | "2.8" => "_2.13"
case _ => "_2.13"
}
)
Expand Down
4 changes: 2 additions & 2 deletions contrib/playlib/src/mill/playlib/Version.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ private[playlib] trait Version extends Module {
playVersion().split('.').take(2).mkString(".")
}

private[playlib] def playOrganization: T[String] = Task.Anon {
private[playlib] def playOrganization: Task[String] = Task.Anon {
if (playVersion().startsWith("2.")) "com.typesafe.play" else "org.playframework"
}

private[playlib] def component(id: String) = Task.Anon {
private[playlib] def component(id: String): Task[Dep] = Task.Anon {
ivy"${playOrganization()}::$id::${playVersion()}"
}
}
3 changes: 2 additions & 1 deletion contrib/playlib/test/src/mill/playlib/PlayModuleTests.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mill
package playlib

import mill.scalalib.api.ZincWorkerUtil
import mill.testkit.{TestBaseModule, UnitTester}
import utest.{TestSuite, Tests, assert, _}

Expand Down Expand Up @@ -102,7 +103,7 @@ object PlayModuleTests extends TestSuite with PlayTestSuite {
os.RelPath("controllers/routes$javascript.class"),
os.RelPath("controllers/javascript/ReverseHomeController.class"),
os.RelPath("controllers/javascript/ReverseAssets.class"),
if (scalaVersion.startsWith("3.")) os.RelPath("router/Routes$$anon$1.class")
if (ZincWorkerUtil.isScala3(scalaVersion)) os.RelPath("router/Routes$$anon$1.class")
else os.RelPath("router/Routes$$anonfun$routes$1.class"),
os.RelPath("router/Routes.class"),
os.RelPath("router/RoutesPrefix$.class"),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package mill.contrib.scoverage.api;

import java.nio.file.Path;
import java.nio.file.Files;
import java.io.IOException;
import java.io.Serializable;

public interface ScoverageReportWorkerApi2 {

interface Logger {
void info(String msg);
void error(String msg);
void debug(String msg);
}

interface Ctx {
Logger log();
Path dest();
}

public static abstract class ReportType implements Serializable {
private String name;

/*private[api]*/
ReportType(String name) {}

public static final ReportType Console = new ConsoleModule();
public static final FileReportType Html = new HtmlModule();
public static final FileReportType Xml = new XmlModule();
public static final FileReportType XmlCobertura = new XmlCoberturaModule();

/* private[api]*/
static final class ConsoleModule extends ReportType implements Serializable {
/* private[api]*/
ConsoleModule() {
super("Console");
}
};

/* private[api]*/
static final class HtmlModule extends FileReportType implements Serializable {
/* private[api]*/
HtmlModule() {
super("Html", "htmlReport");
}
};

/* private[api]*/
static final class XmlModule extends FileReportType implements Serializable {
/* private[api]*/
XmlModule() {
super("Xml", "xmlReport");
}
}

/* private[api]*/
static final class XmlCoberturaModule extends FileReportType implements Serializable {
/* private[api]*/
XmlCoberturaModule() {
super("XmlCobertura", "xmlCoberturaReport");
}
}

@Override
public String toString() {
return name;
}
}

public static abstract class FileReportType extends ReportType implements Serializable {
private final String folderName;

/*private[api]*/
FileReportType(String name, String folderName) {
super(name);
this.folderName = folderName;
}

public String folderName() {
return folderName;
}
}

void report(ReportType reportType, Path[] sources, Path[] dataDirs, Path sourceRoot, Ctx ctx);

static void makeAllDirs(Path path) throws IOException {
// Replicate behavior of `os.makeDir.all(path)`
if (Files.isDirectory(path) && Files.isSymbolicLink(path)) {
// do nothing
} else {
Files.createDirectories(path);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mill.contrib.scoverage
import coursier.Repository
import mill._
import mill.api.{Loose, PathRef, Result}
import mill.contrib.scoverage.api.ScoverageReportWorkerApi.ReportType
import mill.contrib.scoverage.api.ScoverageReportWorkerApi2.ReportType
import mill.main.BuildInfo
import mill.scalalib.api.ZincWorkerUtil
import mill.scalalib.{Dep, DepSyntax, JavaModule, ScalaModule}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mill.contrib.scoverage

import mill.contrib.scoverage.api.ScoverageReportWorkerApi.ReportType
import mill.contrib.scoverage.api.ScoverageReportWorkerApi2.ReportType
import mill.define.{Command, Module, Task}
import mill.eval.Evaluator
import mill.resolve.{Resolve, SelectMode}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package mill.contrib.scoverage

import mill.{Agg, Task}
import mill.api.{ClassLoader, Ctx, PathRef}
import mill.contrib.scoverage.api.ScoverageReportWorkerApi
import mill.contrib.scoverage.api.ScoverageReportWorkerApi2
import mill.define.{Discover, ExternalModule, Worker}

import ScoverageReportWorker.ScoverageReportWorkerApiBridge
import ScoverageReportWorkerApi2.ReportType
import ScoverageReportWorkerApi2.{Logger => ApiLogger}
import ScoverageReportWorkerApi2.{Ctx => ApiCtx}

class ScoverageReportWorker extends AutoCloseable {
private[this] var scoverageClCache = Option.empty[(Long, ClassLoader)]
private var scoverageClCache = Option.empty[(Long, ClassLoader)]

def bridge(classpath: Agg[PathRef])(implicit ctx: Ctx): ScoverageReportWorkerApi = {
def bridge(classpath: Agg[PathRef])(implicit ctx: Ctx): ScoverageReportWorkerApiBridge = {

val classloaderSig = classpath.hashCode
val cl = scoverageClCache match {
Expand All @@ -24,11 +29,43 @@ class ScoverageReportWorker extends AutoCloseable {
cl
}

cl
.loadClass("mill.contrib.scoverage.worker.ScoverageReportWorkerImpl")
.getDeclaredConstructor()
.newInstance()
.asInstanceOf[api.ScoverageReportWorkerApi]
val worker =
cl
.loadClass("mill.contrib.scoverage.worker.ScoverageReportWorkerImpl")
.getDeclaredConstructor()
.newInstance()
.asInstanceOf[api.ScoverageReportWorkerApi2]

def ctx0(implicit ctx: Ctx): ApiCtx = {
val logger = new ApiLogger {
def info(msg: String): Unit = ctx.log.info(msg)
def error(msg: String): Unit = ctx.log.error(msg)
def debug(msg: String): Unit = ctx.log.debug(msg)
}
new ApiCtx {
def log() = logger
def dest() = ctx.dest.toNIO
}
}

new ScoverageReportWorkerApiBridge {
override def report(
reportType: ReportType,
sources: Seq[os.Path],
dataDirs: Seq[os.Path],
sourceRoot: os.Path
)(implicit
ctx: Ctx
): Unit = {
worker.report(
reportType,
sources.map(_.toNIO).toArray,
dataDirs.map(_.toNIO).toArray,
sourceRoot.toNIO,
ctx0
)
}
}
}

override def close(): Unit = {
Expand All @@ -37,6 +74,18 @@ class ScoverageReportWorker extends AutoCloseable {
}

object ScoverageReportWorker extends ExternalModule {
import ScoverageReportWorkerApi2.ReportType

trait ScoverageReportWorkerApiBridge {
def report(
reportType: ReportType,
sources: Seq[os.Path],
dataDirs: Seq[os.Path],
sourceRoot: os.Path
)(implicit
ctx: Ctx
): Unit
}

def scoverageReportWorker: Worker[ScoverageReportWorker] =
Task.Worker { new ScoverageReportWorker() }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
package mill.contrib.scoverage.worker

import mill.contrib.scoverage.api.ScoverageReportWorkerApi
import _root_.scoverage.reporter.{
CoberturaXmlWriter,
CoverageAggregator,
ScoverageHtmlWriter,
ScoverageXmlWriter
}
import mill.api.Ctx
import mill.contrib.scoverage.api.ScoverageReportWorkerApi.ReportType

import mill.contrib.scoverage.api.ScoverageReportWorkerApi2
import ScoverageReportWorkerApi2.ReportType
import ScoverageReportWorkerApi2.Ctx

import java.nio.file.Path

/**
* Scoverage Worker for Scoverage 2.x
*/
class ScoverageReportWorkerImpl extends ScoverageReportWorkerApi {
class ScoverageReportWorkerImpl extends ScoverageReportWorkerApi2 {

override def report(
reportType: ReportType,
sources: Seq[os.Path],
dataDirs: Seq[os.Path],
sourceRoot: os.Path
)(implicit ctx: Ctx): Unit =
sources: Array[Path],
dataDirs: Array[Path],
sourceRoot: Path,
ctx: Ctx
): Unit =
try {
ctx.log.info(s"Processing coverage data for ${dataDirs.size} data locations")
CoverageAggregator.aggregate(dataDirs.map(_.toIO), sourceRoot.toIO) match {
CoverageAggregator.aggregate(dataDirs.map(_.toFile).toIndexedSeq, sourceRoot.toFile) match {
case Some(coverage) =>
val sourceFolders = sources.map(_.toIO)
val sourceFolders = sources.map(_.toFile).toIndexedSeq
val folder = ctx.dest
os.makeDir.all(folder)
ScoverageReportWorkerApi2.makeAllDirs(folder)
reportType match {
case ReportType.Html =>
new ScoverageHtmlWriter(sourceFolders, folder.toIO, None)
new ScoverageHtmlWriter(sourceFolders, folder.toFile, None)
.write(coverage)
case ReportType.Xml =>
new ScoverageXmlWriter(sourceFolders, folder.toIO, false, None)
new ScoverageXmlWriter(sourceFolders, folder.toFile, false, None)
.write(coverage)
case ReportType.XmlCobertura =>
new CoberturaXmlWriter(sourceFolders, folder.toIO, None)
new CoberturaXmlWriter(sourceFolders, folder.toFile, None)
.write(coverage)
case ReportType.Console =>
ctx.log.info(s"Statement coverage.: ${coverage.statementCoverageFormatted}%")
Expand Down
Loading

0 comments on commit 7da0265

Please sign in to comment.