-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
101 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
main/src/de/tobiasroeser/mill/kotlin/KotlinWorkerManager.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package de.tobiasroeser.mill.kotlin | ||
|
||
import mill.api.{Ctx, PathRef} | ||
|
||
trait KotlinWorkerManager { | ||
def get(toolsClasspath: Seq[PathRef])(implicit ctx: Ctx): KotlinWorker | ||
} |
64 changes: 64 additions & 0 deletions
64
main/src/de/tobiasroeser/mill/kotlin/KotlinWorkerManagerImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package de.tobiasroeser.mill.kotlin | ||
|
||
import mill.PathRef | ||
import mill.api.{Ctx, PathRef} | ||
|
||
import java.io.PrintStream | ||
import java.net.{URL, URLClassLoader} | ||
|
||
class KotlinWorkerManagerImpl(ctx: Ctx) extends KotlinWorkerManager with AutoCloseable { | ||
|
||
private[this] var workerCache: Map[Seq[PathRef], (KotlinWorker, Int)] = Map.empty | ||
|
||
override def get(toolsClasspath: Seq[PathRef])(implicit ctx: Ctx): KotlinWorker = { | ||
val toolsCp = toolsClasspath.distinct | ||
val (worker, count) = workerCache.get(toolsCp) match { | ||
case Some((w, count)) => | ||
ctx.log.debug(s"Reusing existing AspectjWorker for classpath: ${toolsCp}") | ||
w -> count | ||
case None => | ||
ctx.log.debug(s"Creating Classloader with classpath: [${toolsCp}]") | ||
val classLoader = new URLClassLoader( | ||
toolsCp.map(_.path.toNIO.toUri().toURL()).toArray[URL], | ||
getClass().getClassLoader() | ||
) | ||
|
||
val className = | ||
classOf[KotlinWorker].getPackage().getName() + ".impl." + classOf[KotlinWorker].getSimpleName() + "Impl" | ||
ctx.log.debug(s"Creating ${className} from classpath: ${toolsCp}") | ||
val impl = classLoader.loadClass(className) | ||
val worker = impl.getConstructor().newInstance().asInstanceOf[KotlinWorker] | ||
if (worker.getClass().getClassLoader() != classLoader) { | ||
ctx.log.error( | ||
"""Worker not loaded from worker classloader. | ||
|You should not add the mill-kotlin-worker JAR to the mill build classpath""".stripMargin | ||
) | ||
} | ||
if (worker.getClass().getClassLoader() == classOf[KotlinWorker].getClassLoader()) { | ||
ctx.log.error("Worker classloader used to load interface and implementation") | ||
} | ||
worker -> 0 | ||
} | ||
workerCache += toolsCp -> (worker -> (1 + count)) | ||
ctx.log.debug(stats()) | ||
worker | ||
} | ||
|
||
def stats(): String = { | ||
s"""Cache statistics of ${this.toString()}: | ||
|${ | ||
workerCache.map { case (cp, (worker, count)) => | ||
s"""- worker: ${worker.toString()} | ||
| used: ${count} | ||
|""".stripMargin | ||
}.mkString | ||
}""".stripMargin | ||
} | ||
|
||
override def close(): Unit = { | ||
ctx.log.debug(stats()) | ||
|
||
// We drop cached worker instances | ||
workerCache = Map.empty | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
main/src/de/tobiasroeser/mill/kotlin/KotlinWorkerModule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package de.tobiasroeser.mill.kotlin | ||
|
||
import mill.T | ||
import mill.define.{Discover, ExternalModule, Module, Worker} | ||
|
||
trait KotlinWorkerModule extends Module { | ||
def kotlinWorkerManager: Worker[KotlinWorkerManager] = T.worker { | ||
new KotlinWorkerManagerImpl(T.ctx()) | ||
} | ||
} | ||
|
||
object KotlinWorkerModule extends ExternalModule with KotlinWorkerModule { | ||
override def millDiscover: Discover[this.type] = Discover[this.type] | ||
} |