Skip to content

Commit

Permalink
Let some workers implement AutoCloseable
Browse files Browse the repository at this point in the history
  • Loading branch information
lefou committed Mar 16, 2022
1 parent 4103b16 commit 9de08bb
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
6 changes: 5 additions & 1 deletion contrib/playlib/src/mill/playlib/RouteCompilerWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import mill.playlib.api.{RouteCompilerType, RouteCompilerWorkerApi}
import mill.scalalib.api.CompilationResult
import mill.{Agg, T}

private[playlib] class RouteCompilerWorker {
private[playlib] class RouteCompilerWorker extends AutoCloseable {

private var routeCompilerInstanceCache =
Option.empty[(Long, mill.playlib.api.RouteCompilerWorkerApi)]
Expand Down Expand Up @@ -63,4 +63,8 @@ private[playlib] class RouteCompilerWorker {
case err => Result.Failure(err)
}
}

override def close(): Unit = {
routeCompilerInstanceCache = None
}
}
6 changes: 5 additions & 1 deletion contrib/scalapblib/src/ScalaPBWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import mill.api.PathRef
import mill.T
import mill.define.{Discover, ExternalModule, Worker}

class ScalaPBWorker {
class ScalaPBWorker extends AutoCloseable {

private var scalaPBInstanceCache = Option.empty[(Long, ScalaPBWorkerApi)]

Expand Down Expand Up @@ -100,6 +100,10 @@ class ScalaPBWorker {

mill.api.Result.Success(PathRef(dest))
}

override def close(): Unit = {
scalaPBInstanceCache = None
}
}

trait ScalaPBWorkerApi {
Expand Down
6 changes: 5 additions & 1 deletion contrib/scoverage/src/ScoverageReportWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import mill.api.{ClassLoader, Ctx}
import mill.contrib.scoverage.api.ScoverageReportWorkerApi
import mill.define.{Discover, ExternalModule, Worker}

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

def bridge(classpath: Agg[os.Path])(implicit ctx: Ctx): ScoverageReportWorkerApi = {
Expand All @@ -31,6 +31,10 @@ class ScoverageReportWorker {
.newInstance()
.asInstanceOf[api.ScoverageReportWorkerApi]
}

override def close(): Unit = {
scoverageClCache = None
}
}

object ScoverageReportWorker extends ExternalModule {
Expand Down
5 changes: 4 additions & 1 deletion scalajslib/src/ScalaJSWorkerApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import mill.api.{Ctx, Result}
import mill.define.Discover
import mill.scalajslib.api._
import mill.{Agg, T}
class ScalaJSWorker {
class ScalaJSWorker extends AutoCloseable {
private var scalaInstanceCache = Option.empty[(Long, ScalaJSWorkerApi)]

private def bridge(toolsClasspath: Agg[os.Path])(implicit ctx: Ctx.Home) = {
Expand Down Expand Up @@ -68,6 +68,9 @@ class ScalaJSWorker {
bridge(toolsClasspath).getFramework(config, frameworkName, linkedFile, moduleKind)
}

override def close(): Unit = {
scalaInstanceCache = None
}
}

object ScalaJSWorkerApi extends mill.define.ExternalModule {
Expand Down
6 changes: 5 additions & 1 deletion scalalib/src/scalafmt/ScalafmtWorker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object ScalafmtWorkerModule extends ExternalModule {
lazy val millDiscover = Discover[this.type]
}

private[scalafmt] class ScalafmtWorker {
private[scalafmt] class ScalafmtWorker extends AutoCloseable {
private val reformatted: mutable.Map[os.Path, Int] = mutable.Map.empty
private var configSig: Int = 0

Expand Down Expand Up @@ -94,4 +94,8 @@ private[scalafmt] class ScalafmtWorker {
}
}

override def close(): Unit = {
reformatted.clear()
configSig = 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ZincWorkerImpl(
compilerJarNameGrep: (Agg[os.Path], String) => os.Path,
compilerCache: KeyedLockedCache[Compilers],
compileToJar: Boolean
) extends ZincWorkerApi {
) extends ZincWorkerApi with AutoCloseable {
private val ic = new sbt.internal.inc.IncrementalCompilerImpl()
lazy val javaOnlyCompilers = {
// Keep the classpath as written by the user
Expand Down Expand Up @@ -558,4 +558,8 @@ class ZincWorkerImpl(
reporter.foreach(_.finish())
}
}

override def close(): Unit = {
classloaderCache.clear()
}
}
7 changes: 6 additions & 1 deletion scalanativelib/src/ScalaNativeWorkerApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import mill.define.{Discover, Worker}
import mill.{Agg, T}
import mill.scalanativelib.api._

class ScalaNativeWorker {
class ScalaNativeWorker extends AutoCloseable {
private var scalaInstanceCache = Option.empty[(Long, ScalaNativeWorkerApi)]

def impl(toolsClasspath: Agg[os.Path])(implicit ctx: mill.api.Ctx.Home): ScalaNativeWorkerApi = {
Expand Down Expand Up @@ -37,6 +37,11 @@ class ScalaNativeWorker {
}
}
}

override def close(): Unit = {
// drop instance
scalaInstanceCache = None
}
}

object ScalaNativeWorkerApi extends mill.define.ExternalModule {
Expand Down

0 comments on commit 9de08bb

Please sign in to comment.