sbt/plugin/src/main/scala/coursier/CoursierPlugin.scala

291 lines
8.8 KiB
Scala
Raw Normal View History

2015-12-30 01:34:34 +01:00
package coursier
import java.io.{ File, OutputStreamWriter }
import coursier.cli.TermDisplay
2015-12-30 01:34:38 +01:00
import coursier.ivy.IvyRepository
2015-12-30 01:34:34 +01:00
import sbt.{ MavenRepository => _, _ }
import sbt.Keys._
import scalaz.{ -\/, \/- }
import scalaz.concurrent.Task
object CoursierPlugin extends AutoPlugin {
private def grouped[K, V](map: Seq[(K, V)]): Map[K, Seq[V]] =
map.groupBy { case (k, _) => k }.map {
case (k, l) =>
k -> l.map { case (_, v) => v }
}
2015-12-30 01:34:34 +01:00
override def trigger = allRequirements
override def requires = sbt.plugins.IvyPlugin
private def errPrintln(s: String): Unit = scala.Console.err.println(s)
object autoImport {
val coursierParallelDownloads = Keys.coursierParallelDownloads
val coursierMaxIterations = Keys.coursierMaxIterations
val coursierChecksums = Keys.coursierChecksums
val coursierCachePolicy = Keys.coursierCachePolicy
2015-12-30 01:34:35 +01:00
val coursierVerbosity = Keys.coursierVerbosity
2015-12-30 01:34:34 +01:00
val coursierResolvers = Keys.coursierResolvers
val coursierCache = Keys.coursierCache
val coursierProject = Keys.coursierProject
val coursierProjects = Keys.coursierProjects
val coursierSbtClassifiersModule = Keys.coursierSbtClassifiersModule
2015-12-30 01:34:34 +01:00
}
import autoImport._
private val ivyProperties = Map(
"ivy.home" -> s"${sys.props("user.home")}/.ivy2"
) ++ sys.props
private def createLogger() = Some {
2015-12-30 01:34:35 +01:00
new TermDisplay(
new OutputStreamWriter(System.err),
fallbackMode = sys.env.get("COURSIER_NO_TERM").nonEmpty
)
2015-12-30 01:34:34 +01:00
}
private def updateTask(withClassifiers: Boolean, sbtClassifiers: Boolean = false) = Def.task {
2015-12-30 01:34:37 +01:00
2015-12-30 01:34:34 +01:00
// let's update only one module at once, for a better output
// Downloads are already parallel, no need to parallelize further anyway
synchronized {
lazy val cm = coursierSbtClassifiersModule.value
val currentProject =
if (sbtClassifiers) {
FromSbt.project(
cm.id,
cm.modules,
cm.configurations.map(cfg => cfg.name -> cfg.extendsConfigs.map(_.name)).toMap,
scalaVersion.value,
scalaBinaryVersion.value
)
} else {
val (p, _) = coursierProject.value
p
}
2015-12-30 01:34:34 +01:00
val projects = coursierProjects.value
val parallelDownloads = coursierParallelDownloads.value
val checksums = coursierChecksums.value
val maxIterations = coursierMaxIterations.value
val cachePolicy = coursierCachePolicy.value
val cacheDir = coursierCache.value
val resolvers = coursierResolvers.value
2015-12-30 01:34:35 +01:00
val verbosity = coursierVerbosity.value
2015-12-30 01:34:34 +01:00
val startRes = Resolution(
currentProject.dependencies.map { case (_, dep) => dep }.toSet,
filter = Some(dep => !dep.optional),
2015-12-30 01:34:38 +01:00
forceVersions = projects.map { case (proj, _) => proj.moduleVersion }.toMap
2015-12-30 01:34:34 +01:00
)
2015-12-30 01:34:37 +01:00
if (verbosity >= 1) {
println("InterProjectRepository")
2015-12-30 01:34:38 +01:00
for ((p, _) <- projects)
2015-12-30 01:34:37 +01:00
println(s" ${p.module}:${p.version}")
}
2015-12-30 01:34:38 +01:00
val globalPluginsRepo = IvyRepository(
new File(sys.props("user.home") + "/.sbt/0.13/plugins/target/resolution-cache/").toURI.toString +
"[organization]/[module](/scala_[scalaVersion])(/sbt_[sbtVersion])/[revision]/resolved.xml.[ext]",
withChecksums = false,
2015-12-30 01:34:39 +01:00
withSignatures = false,
withArtifacts = false
2015-12-30 01:34:38 +01:00
)
val interProjectRepo = InterProjectRepository(projects)
val repositories = Seq(globalPluginsRepo, interProjectRepo) ++ resolvers.flatMap(FromSbt.repository(_, ivyProperties))
2015-12-30 01:34:34 +01:00
val files = Files(
Seq("http://" -> new File(cacheDir, "http"), "https://" -> new File(cacheDir, "https")),
() => ???,
concurrentDownloadCount = parallelDownloads
)
val logger = createLogger()
logger.foreach(_.init())
val fetch = coursier.Fetch(
repositories,
files.fetch(checksums = checksums, logger = logger)(cachePolicy = CachePolicy.LocalOnly),
files.fetch(checksums = checksums, logger = logger)(cachePolicy = cachePolicy)
)
def depsRepr = currentProject.dependencies.map { case (config, dep) =>
s"${dep.module}:${dep.version}:$config->${dep.configuration}"
}.sorted
2015-12-30 01:34:35 +01:00
if (verbosity >= 0)
errPrintln(s"Resolving ${currentProject.module.organization}:${currentProject.module.name}:${currentProject.version}")
if (verbosity >= 1)
for (depRepr <- depsRepr)
errPrintln(s" $depRepr")
2015-12-30 01:34:34 +01:00
val res = startRes
.process
.run(fetch, maxIterations)
.attemptRun
.leftMap(ex => throw new Exception(s"Exception during resolution", ex))
.merge
if (!res.isDone)
throw new Exception(s"Maximum number of iteration reached!")
2015-12-30 01:34:35 +01:00
if (verbosity >= 0)
errPrintln("Resolution done")
2015-12-30 01:34:34 +01:00
def repr(dep: Dependency) = {
// dep.version can be an interval, whereas the one from project can't
val version = res
.projectCache
.get(dep.moduleVersion)
.map(_._2.version)
.getOrElse(dep.version)
val extra =
if (version == dep.version) ""
else s" ($version for ${dep.version})"
(
Seq(
dep.module.organization,
dep.module.name,
dep.attributes.`type`
) ++
Some(dep.attributes.classifier)
.filter(_.nonEmpty)
.toSeq ++
Seq(
version
)
).mkString(":") + extra
}
if (res.conflicts.nonEmpty) {
// Needs test
println(s"${res.conflicts.size} conflict(s):\n ${res.conflicts.toList.map(repr).sorted.mkString(" \n")}")
}
val errors = res.errors
2015-12-30 01:34:34 +01:00
if (errors.nonEmpty) {
println(s"\n${errors.size} error(s):")
for ((dep, errs) <- errors) {
println(s" ${dep.module}:${dep.version}:\n${errs.map(" " + _.replace("\n", " \n")).mkString("\n")}")
}
}
2015-12-30 01:34:37 +01:00
val classifiers =
if (withClassifiers)
Some {
if (sbtClassifiers)
cm.classifiers
else
transitiveClassifiers.value
}
2015-12-30 01:34:37 +01:00
else
None
val allArtifacts =
classifiers match {
case None => res.artifacts
case Some(cl) => res.classifiersArtifacts(cl)
}
val artifactFileOrErrorTasks = allArtifacts.toVector.map { a =>
files.file(a, checksums = checksums, logger = logger)(cachePolicy = cachePolicy).run.map((a, _))
}
2015-12-30 01:34:34 +01:00
2015-12-30 01:34:35 +01:00
if (verbosity >= 0)
errPrintln(s"Fetching artifacts")
val artifactFilesOrErrors = Task.gatherUnordered(artifactFileOrErrorTasks).attemptRun match {
2015-12-30 01:34:34 +01:00
case -\/(ex) =>
throw new Exception(s"Error while downloading / verifying artifacts", ex)
case \/-(l) =>
l.toMap
2015-12-30 01:34:34 +01:00
}
2015-12-30 01:34:35 +01:00
if (verbosity >= 0)
errPrintln(s"Fetching artifacts: done")
2015-12-30 01:34:34 +01:00
val configs = {
val configs0 = ivyConfigurations.value.map { config =>
config.name -> config.extendsConfigs.map(_.name)
}.toMap
def allExtends(c: String) = {
// possibly bad complexity
def helper(current: Set[String]): Set[String] = {
val newSet = current ++ current.flatMap(configs0.getOrElse(_, Nil))
if ((newSet -- current).nonEmpty)
helper(newSet)
else
newSet
}
helper(Set(c))
2015-12-30 01:34:34 +01:00
}
configs0.map {
case (config, _) =>
config -> allExtends(config)
}
2015-12-30 01:34:34 +01:00
}
def artifactFileOpt(artifact: Artifact) = {
val fileOrError = artifactFilesOrErrors.getOrElse(artifact, -\/("Not downloaded"))
fileOrError match {
case \/-(file) =>
if (file.toString.contains("file:/"))
throw new Exception(s"Wrong path: $file")
Some(file)
case -\/(err) =>
errPrintln(s"${artifact.url}: $err")
None
2015-12-30 01:34:34 +01:00
}
}
2015-12-30 01:34:34 +01:00
val depsByConfig = grouped(currentProject.dependencies)
2015-12-30 01:34:37 +01:00
ToSbt.updateReport(
depsByConfig,
res,
configs,
classifiers,
artifactFileOpt
2015-12-30 01:34:34 +01:00
)
}
}
override lazy val projectSettings = Seq(
coursierParallelDownloads := 6,
coursierMaxIterations := 50,
2015-12-30 01:34:39 +01:00
coursierChecksums := Seq(Some("SHA-1"), None),
2015-12-30 01:34:34 +01:00
coursierCachePolicy := CachePolicy.FetchMissing,
2015-12-30 01:34:38 +01:00
coursierVerbosity := 1,
2015-12-30 01:34:34 +01:00
coursierResolvers <<= Tasks.coursierResolversTask,
coursierCache := new File(sys.props("user.home") + "/.coursier/sbt"),
2015-12-30 01:34:37 +01:00
update <<= updateTask(withClassifiers = false),
updateClassifiers <<= updateTask(withClassifiers = true),
updateSbtClassifiers in Defaults.TaskGlobal <<= updateTask(withClassifiers = true, sbtClassifiers = true),
2015-12-30 01:34:34 +01:00
coursierProject <<= Tasks.coursierProjectTask,
coursierProjects <<= Tasks.coursierProjectsTask,
coursierSbtClassifiersModule <<= classifiersModule in updateSbtClassifiers
2015-12-30 01:34:34 +01:00
)
}