Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Coursier plugins via customizing FileCache used (with classloaders) #1760

Merged
merged 4 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions main/src/mill/modules/Jvm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ object Jvm {
sources: Boolean = false,
mapDependencies: Option[Dependency => Dependency] = None,
customizer: Option[coursier.core.Resolution => coursier.core.Resolution] = None,
cacheCustomizer: Option[coursier.cache.FileCache[Task] => coursier.cache.FileCache[Task]] = None,
ctx: Option[mill.api.Ctx.Log] = None
): Result[Agg[PathRef]] = {

Expand All @@ -543,6 +544,7 @@ object Jvm {
force,
mapDependencies,
customizer,
cacheCustomizer,
ctx
)
val errs = resolution.errors
Expand All @@ -561,12 +563,15 @@ object Jvm {
Result.Failure(msg)
} else {

val cache0 = coursier.cache.FileCache[Task].noCredentials
val cache = cacheCustomizer.getOrElse(identity[coursier.cache.FileCache[Task]](_)).apply(cache0)

def load(artifacts: Seq[coursier.util.Artifact]) = {

import scala.concurrent.ExecutionContext.Implicits.global
val loadedArtifacts = Gather[Task].gather(
for (a <- artifacts)
yield coursier.cache.Cache.default.file(a).run.map(a.optional -> _)
yield cache.file(a).run.map(a.optional -> _)
).unsafeRun

val errors = loadedArtifacts.collect {
Expand Down Expand Up @@ -611,6 +616,7 @@ object Jvm {
force: IterableOnce[coursier.Dependency],
mapDependencies: Option[Dependency => Dependency] = None,
customizer: Option[coursier.core.Resolution => coursier.core.Resolution] = None,
cacheCustomizer: Option[coursier.cache.FileCache[Task] => coursier.cache.FileCache[Task]] = None,
ctx: Option[mill.api.Ctx.Log] = None
): (Seq[Dependency], Resolution) = {

Expand All @@ -631,13 +637,14 @@ object Jvm {
val start = customizer.getOrElse(identity[Resolution](_)).apply(start0)

val resolutionLogger = ctx.map(c => new TickerResolutionLogger(c))
val cache = resolutionLogger match {
val cache0 = resolutionLogger match {
case None => coursier.cache.FileCache[Task].withCachePolicies(cachePolicies)
case Some(l) =>
coursier.cache.FileCache[Task]
.withCachePolicies(cachePolicies)
.withLogger(l)
}
val cache = cacheCustomizer.getOrElse(identity[coursier.cache.FileCache[Task]](_)).apply(cache0)

val fetches = cache.fetchs

Expand Down Expand Up @@ -781,6 +788,7 @@ object Jvm {
sources = sources,
mapDependencies = mapDependencies,
customizer = None,
cacheCustomizer = None,
ctx = ctx
)

Expand All @@ -801,6 +809,7 @@ object Jvm {
force = force,
mapDependencies = mapDependencies,
customizer = None,
cacheCustomizer = None,
ctx = ctx
)

Expand Down
20 changes: 19 additions & 1 deletion scalalib/src/CoursierModule.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package mill.scalalib

import scala.annotation.nowarn
import coursier.cache.FileCache

import scala.annotation.nowarn
import coursier.{Dependency, Repository, Resolve}
import coursier.core.Resolution
import mill.{Agg, T}
Expand Down Expand Up @@ -35,6 +36,7 @@ trait CoursierModule extends mill.Module {
sources = sources,
mapDependencies = Some(mapDependencies()),
customizer = resolutionCustomizer(),
cacheCustomizer = cacheCustomizer(),
ctx = Some(implicitly[mill.api.Ctx.Log])
)
}
Expand Down Expand Up @@ -71,6 +73,22 @@ trait CoursierModule extends mill.Module {
*/
def resolutionCustomizer: Task[Option[Resolution => Resolution]] = T.task { None }

/**
* Customize the coursier file cache.
*
* This is rarely needed to be changed, but sometimes e.g you want to load a coursier plugin.
* Doing so requires adding to coursier's classpath. To do this you could use the following:
* {{{
* override def cacheCustomizer = T.task {
* Some( (fc: coursier.cache.FileCache[Task]) =>
* fc.withClassLoaders(Seq(classOf[coursier.cache.protocol.S3Handler].getClassLoader))
* )
* }
* }}}
* @return
*/
def cacheCustomizer: Task[Option[FileCache[coursier.util.Task] => FileCache[coursier.util.Task]]] = T.task { None }

/**
* The repositories used to resolved dependencies with [[resolveDeps()]].
*/
Expand Down
3 changes: 2 additions & 1 deletion scalalib/src/JavaModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,8 @@ trait JavaModule
resolveCoursierDependency().apply(_),
additionalDeps() ++ transitiveIvyDeps(),
Some(mapDependencies()),
customizer = resolutionCustomizer()
customizer = resolutionCustomizer(),
cacheCustomizer = cacheCustomizer(),
)

println(
Expand Down
7 changes: 7 additions & 0 deletions scalalib/src/Lib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.lang.annotation.Annotation
import java.lang.reflect.Modifier
import java.util.zip.ZipInputStream

import coursier.util.Task
import coursier.{Dependency, Fetch, Repository, Resolution}
import mill.api.{Ctx, Loose, PathRef, Result}
import sbt.testing._
Expand All @@ -29,6 +30,7 @@ object Lib {
deps: IterableOnce[Dep],
mapDependencies: Option[Dependency => Dependency] = None,
customizer: Option[coursier.core.Resolution => coursier.core.Resolution] = None,
cacheCustomizer: Option[coursier.cache.FileCache[Task] => coursier.cache.FileCache[Task]] = None,
ctx: Option[Ctx.Log] = None
): (Seq[Dependency], Resolution) = {
val depSeq = deps.iterator.toSeq
Expand All @@ -38,6 +40,7 @@ object Lib {
force = depSeq.filter(_.force).map(depToDependency),
mapDependencies = mapDependencies,
customizer = customizer,
cacheCustomizer = cacheCustomizer,
ctx = ctx
)
}
Expand All @@ -56,6 +59,7 @@ object Lib {
sources: Boolean = false,
mapDependencies: Option[Dependency => Dependency] = None,
customizer: Option[coursier.core.Resolution => coursier.core.Resolution] = None,
cacheCustomizer: Option[coursier.cache.FileCache[Task] => coursier.cache.FileCache[Task]] = None,
ctx: Option[Ctx.Log] = None
): Result[Agg[PathRef]] = {
val depSeq = deps.iterator.toSeq
Expand All @@ -66,6 +70,7 @@ object Lib {
sources = sources,
mapDependencies = mapDependencies,
customizer = customizer,
cacheCustomizer = cacheCustomizer,
ctx = ctx
)
}
Expand Down Expand Up @@ -137,6 +142,7 @@ object Lib {
deps = deps,
mapDependencies = mapDependencies,
customizer = None,
cacheCustomizer = None,
ctx = ctx
)

Expand All @@ -159,6 +165,7 @@ object Lib {
sources = sources,
mapDependencies = mapDependencies,
customizer = None,
cacheCustomizer = None,
ctx = ctx
)

Expand Down
2 changes: 2 additions & 0 deletions scalalib/src/dependency/versions/VersionsFinder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private[dependency] object VersionsFinder {
val repos = javaModule.repositoriesTask()
val mapDeps = javaModule.mapDependencies()
val custom = javaModule.resolutionCustomizer()
val cacheCustom = javaModule.cacheCustomizer()

val (dependencies, _) =
Lib.resolveDependenciesMetadata(
Expand All @@ -47,6 +48,7 @@ private[dependency] object VersionsFinder {
deps = deps ++ compileIvyDeps ++ runIvyDeps,
mapDependencies = Some(mapDeps),
customizer = custom,
cacheCustomizer = cacheCustom,
ctx = Some(T.log)
)

Expand Down