sbt/plugin/src/main/scala-2.10/coursier/Tasks.scala

574 lines
17 KiB
Scala
Raw Normal View History

2015-12-30 01:34:34 +01:00
package coursier
2015-12-30 01:34:43 +01:00
import java.io.{ OutputStreamWriter, File }
import java.net.URL
import java.nio.file.Files
2015-12-30 01:34:41 +01:00
import java.util.concurrent.Executors
2015-12-30 01:34:39 +01:00
import coursier.core.Publication
2015-12-30 01:34:39 +01:00
import coursier.ivy.IvyRepository
2015-12-30 01:34:43 +01:00
import coursier.Keys._
import coursier.Structure._
2016-01-10 21:32:28 +01:00
import coursier.util.{ Config, Print }
import org.apache.ivy.core.module.id.ModuleRevisionId
2015-12-30 01:34:43 +01:00
import sbt.{ UpdateReport, Classpaths, Resolver, Def }
import sbt.Configurations.{ Compile, Test }
2015-12-30 01:34:34 +01:00
import sbt.Keys._
2015-12-30 01:34:43 +01:00
import scala.collection.mutable
import scala.collection.JavaConverters._
2015-12-30 01:34:43 +01:00
import scalaz.{ \/-, -\/ }
2015-12-30 01:34:41 +01:00
import scalaz.concurrent.{ Task, Strategy }
2015-12-30 01:34:39 +01:00
2015-12-30 01:34:34 +01:00
object Tasks {
def coursierResolversTask: Def.Initialize[sbt.Task[Seq[Resolver]]] =
(
externalResolvers,
sbtPlugin,
sbtResolver,
bootResolvers,
overrideBuildResolvers
).map { (extRes, isSbtPlugin, sbtRes, bootResOpt, overrideFlag) =>
bootResOpt.filter(_ => overrideFlag).getOrElse {
var resolvers = extRes
if (isSbtPlugin)
resolvers = Seq(
sbtRes,
Classpaths.sbtPluginReleases
) ++ resolvers
resolvers
}
}
2015-12-30 01:34:34 +01:00
def coursierFallbackDependenciesTask: Def.Initialize[sbt.Task[Seq[(Module, String, URL, Boolean)]]] =
(
sbt.Keys.state,
sbt.Keys.thisProjectRef
).flatMap { (state, projectRef) =>
val allDependenciesTask = allDependencies.in(projectRef).get(state)
for {
allDependencies <- allDependenciesTask
} yield {
FromSbt.fallbackDependencies(
allDependencies,
scalaVersion.in(projectRef).get(state),
scalaBinaryVersion.in(projectRef).get(state)
)
}
}
def coursierProjectTask: Def.Initialize[sbt.Task[Project]] =
2015-12-30 01:34:34 +01:00
(
sbt.Keys.state,
sbt.Keys.thisProjectRef
).flatMap { (state, projectRef) =>
// should projectID.configurations be used instead?
val configurations = ivyConfigurations.in(projectRef).get(state)
val allDependenciesTask = allDependencies.in(projectRef).get(state)
for {
allDependencies <- allDependenciesTask
} yield {
FromSbt.project(
2015-12-30 01:34:34 +01:00
projectID.in(projectRef).get(state),
allDependencies,
configurations.map { cfg => cfg.name -> cfg.extendsConfigs.map(_.name) }.toMap,
scalaVersion.in(projectRef).get(state),
scalaBinaryVersion.in(projectRef).get(state)
)
}
}
def coursierProjectsTask: Def.Initialize[sbt.Task[Seq[Project]]] =
2015-12-30 01:34:34 +01:00
sbt.Keys.state.flatMap { state =>
val projects = structure(state).allProjectRefs
coursierProject.forAllProjects(state, projects).map(_.values.toVector)
}
def coursierPublicationsTask: Def.Initialize[sbt.Task[Seq[(String, Publication)]]] =
(
sbt.Keys.state,
sbt.Keys.thisProjectRef,
sbt.Keys.projectID,
sbt.Keys.scalaVersion,
2016-02-20 15:53:09 +01:00
sbt.Keys.scalaBinaryVersion,
sbt.Keys.ivyConfigurations
).map { (state, projectRef, projId, sv, sbv, ivyConfs) =>
val packageTasks = Seq(packageBin, packageSrc, packageDoc)
val configs = Seq(Compile, Test)
val sbtArtifacts =
for {
pkgTask <- packageTasks
config <- configs
} yield {
val publish = publishArtifact.in(projectRef).in(pkgTask).in(config).getOrElse(state, false)
if (publish)
Option(artifact.in(projectRef).in(pkgTask).in(config).getOrElse(state, null))
.map(config.name -> _)
else
None
}
2016-02-20 15:53:09 +01:00
def artifactPublication(artifact: sbt.Artifact) = {
val name = FromSbt.sbtCrossVersionName(
artifact.name,
projId.crossVersion,
sv,
sbv
)
Publication(
name,
artifact.`type`,
artifact.extension,
artifact.classifier.getOrElse("")
)
}
val sbtArtifactsPublication = sbtArtifacts.collect {
case Some((config, artifact)) =>
2016-02-20 15:53:09 +01:00
config -> artifactPublication(artifact)
}
2016-02-20 15:53:09 +01:00
val stdArtifactsSet = sbtArtifacts.flatMap(_.map { case (_, a) => a }.toSeq).toSet
// Second-way of getting artifacts from SBT
// No obvious way of getting the corresponding publishArtifact value for the ones
// only here, it seems.
val extraSbtArtifacts = Option(artifacts.in(projectRef).getOrElse(state, null))
.toSeq
.flatten
.filterNot(stdArtifactsSet)
// Seems that SBT does that - if an artifact has no configs,
// it puts it in all of them. See for example what happens to
// the standalone JAR artifact of the coursier cli module.
def allConfigsIfEmpty(configs: Iterable[sbt.Configuration]): Iterable[sbt.Configuration] =
if (configs.isEmpty) ivyConfs else configs
val extraSbtArtifactsPublication = for {
artifact <- extraSbtArtifacts
config <- allConfigsIfEmpty(artifact.configurations) if config.isPublic
} yield config.name -> artifactPublication(artifact)
sbtArtifactsPublication ++ extraSbtArtifactsPublication
}
2015-12-30 01:34:43 +01:00
// FIXME More things should possibly be put here too (resolvers, etc.)
private case class CacheKey(
project: Project,
repositories: Seq[Repository],
2015-12-30 01:34:43 +01:00
resolution: Resolution,
withClassifiers: Boolean,
sbtClassifiers: Boolean
)
private val resolutionsCache = new mutable.HashMap[CacheKey, UpdateReport]
private def forcedScalaModules(scalaVersion: String): Map[Module, String] =
Map(
Module("org.scala-lang", "scala-library") -> scalaVersion,
Module("org.scala-lang", "scala-compiler") -> scalaVersion,
Module("org.scala-lang", "scala-reflect") -> scalaVersion,
Module("org.scala-lang", "scalap") -> scalaVersion
)
2015-12-30 01:34:39 +01:00
def updateTask(withClassifiers: Boolean, sbtClassifiers: Boolean = false) = Def.task {
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 }
}
// 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, fallbackDependencies) =
if (sbtClassifiers) {
val sv = scalaVersion.value
val sbv = scalaBinaryVersion.value
val proj = FromSbt.project(
2015-12-30 01:34:39 +01:00
cm.id,
cm.modules,
cm.configurations.map(cfg => cfg.name -> cfg.extendsConfigs.map(_.name)).toMap,
sv,
sbv
2015-12-30 01:34:39 +01:00
)
val fallbackDeps = FromSbt.fallbackDependencies(
cm.modules,
sv,
sbv
)
(proj, fallbackDeps)
} else {
val proj = coursierProject.value
val publications = coursierPublications.value
val fallbackDeps = coursierFallbackDependencies.value
(proj.copy(publications = publications), fallbackDeps)
2015-12-30 01:34:39 +01:00
}
val ivySbt0 = ivySbt.value
val ivyCacheManager = ivySbt0.withIvy(streams.value.log)(ivy =>
ivy.getResolutionCacheManager
)
val ivyModule = ModuleRevisionId.newInstance(
currentProject.module.organization,
currentProject.module.name,
currentProject.version,
currentProject.module.attributes.asJava
)
val cacheIvyFile = ivyCacheManager.getResolvedIvyFileInCache(ivyModule)
val cacheIvyPropertiesFile = ivyCacheManager.getResolvedIvyPropertiesInCache(ivyModule)
2015-12-30 01:34:39 +01:00
val projects = coursierProjects.value
val parallelDownloads = coursierParallelDownloads.value
val checksums = coursierChecksums.value
val artifactsChecksums = coursierArtifactsChecksums.value
2015-12-30 01:34:39 +01:00
val maxIterations = coursierMaxIterations.value
val cachePolicies = coursierCachePolicies.value
val cache = coursierCache.value
2015-12-30 01:34:39 +01:00
2016-04-05 16:24:39 +02:00
val log = streams.value.log
val sv = scalaVersion.value // is this always defined? (e.g. for Java only projects?)
val sbv = scalaBinaryVersion.value
val userForceVersions = dependencyOverrides.value.map(
FromSbt.moduleVersion(_, sv, sbv)
).toMap
var anyNonSupportedExclusionRule = false
val exclusions = excludeDependencies.value.flatMap {
rule =>
if (
rule.artifact != "*" ||
rule.configurations.nonEmpty ||
rule.crossVersion != sbt.CrossVersion.Disabled
) {
2016-04-05 16:24:39 +02:00
log.warn(s"Unsupported exclusion rule $rule")
anyNonSupportedExclusionRule = true
Nil
} else
Seq((rule.organization, rule.name))
}.toSet
if (anyNonSupportedExclusionRule)
2016-04-05 16:24:39 +02:00
log.warn("Only supported exclusion rule fields: organization, name")
val resolvers =
if (sbtClassifiers)
coursierSbtResolvers.value
else
coursierResolvers.value
2015-12-30 01:34:39 +01:00
val verbosityLevel = coursierVerbosity.value
2015-12-30 01:34:39 +01:00
val startRes = Resolution(
currentProject.dependencies.map {
case (_, dep) =>
dep.copy(exclusions = dep.exclusions ++ exclusions)
}.toSet,
2015-12-30 01:34:39 +01:00
filter = Some(dep => !dep.optional),
forceVersions = userForceVersions ++ forcedScalaModules(sv) ++ projects.map(_.moduleVersion)
2015-12-30 01:34:39 +01:00
)
// required for publish to be fine, later on
def writeIvyFiles() = {
val printer = new scala.xml.PrettyPrinter(80, 2)
val b = new StringBuilder
b ++= """<?xml version="1.0" encoding="UTF-8"?>"""
b += '\n'
b ++= printer.format(MakeIvyXml(currentProject))
cacheIvyFile.getParentFile.mkdirs()
Files.write(cacheIvyFile.toPath, b.result().getBytes("UTF-8"))
// Just writing an empty file here... Are these only used?
cacheIvyPropertiesFile.getParentFile.mkdirs()
Files.write(cacheIvyPropertiesFile.toPath, "".getBytes("UTF-8"))
}
if (verbosityLevel >= 2) {
2016-04-05 16:24:39 +02:00
log.info("InterProjectRepository")
for (p <- projects)
2016-04-05 16:24:39 +02:00
log.info(s" ${p.module}:${p.version}")
}
2015-12-30 01:34:39 +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,
withSignatures = false,
withArtifacts = false
)
2015-12-30 01:34:39 +01:00
val interProjectRepo = InterProjectRepository(projects)
2015-12-30 01:34:43 +01:00
val ivyProperties = Map(
"ivy.home" -> (new File(sys.props("user.home")).toURI.getPath + ".ivy2")
) ++ sys.props
2015-12-30 01:34:43 +01:00
val repositories = Seq(
globalPluginsRepo,
interProjectRepo
) ++ resolvers.flatMap(
2016-04-05 16:24:39 +02:00
FromSbt.repository(_, ivyProperties, log)
) ++ {
if (fallbackDependencies.isEmpty)
Nil
else {
val map = fallbackDependencies.map {
case (mod, ver, url, changing) =>
(mod, ver) -> ((url, changing))
}.toMap
Seq(
FallbackDependenciesRepository(map)
)
}
}
2015-12-30 01:34:39 +01:00
def report = {
2015-12-30 01:34:43 +01:00
val pool = Executors.newFixedThreadPool(parallelDownloads, Strategy.DefaultDaemonThreadFactory)
2015-12-30 01:34:41 +01:00
def createLogger() = new TermDisplay(new OutputStreamWriter(System.err))
2015-12-30 01:34:48 +01:00
val resLogger = createLogger()
2015-12-30 01:34:43 +01:00
2016-01-03 16:38:29 +01:00
val fetch = Fetch.from(
2015-12-30 01:34:43 +01:00
repositories,
Cache.fetch(cache, cachePolicies.head, checksums = checksums, logger = Some(resLogger), pool = pool),
cachePolicies.tail.map(p =>
Cache.fetch(cache, p, checksums = checksums, logger = Some(resLogger), pool = pool)
): _*
2015-12-30 01:34:43 +01:00
)
2015-12-30 01:34:39 +01:00
2015-12-30 01:34:45 +01:00
def depsRepr(deps: Seq[(String, Dependency)]) =
deps.map { case (config, dep) =>
s"${dep.module}:${dep.version}:$config->${dep.configuration}"
}.sorted.distinct
def depsRepr0(deps: Seq[Dependency]) =
deps.map { dep =>
s"${dep.module}:${dep.version}:${dep.configuration}"
}.sorted.distinct
if (verbosityLevel >= 1) {
2016-01-10 21:32:28 +01:00
val repoReprs = repositories.map {
case r: IvyRepository =>
s"ivy:${r.pattern}"
case r: InterProjectRepository =>
"inter-project"
case r: MavenRepository =>
r.root
case r =>
// should not happen
r.toString
2015-12-30 01:34:45 +01:00
}
2016-01-10 21:32:28 +01:00
2016-04-05 16:24:39 +02:00
log.info(
"Repositories:\n" +
repoReprs.map(" " + _).mkString("\n")
)
2015-12-30 01:34:45 +01:00
}
2015-12-30 01:34:43 +01:00
if (verbosityLevel >= 0)
2016-04-05 16:24:39 +02:00
log.info(s"Resolving ${currentProject.module.organization}:${currentProject.module.name}:${currentProject.version}")
if (verbosityLevel >= 1)
2015-12-30 01:34:45 +01:00
for (depRepr <- depsRepr(currentProject.dependencies))
2016-04-05 16:24:39 +02:00
log.info(s" $depRepr")
2015-12-30 01:34:43 +01:00
2015-12-30 01:34:48 +01:00
resLogger.init()
2015-12-30 01:34:43 +01:00
val res = startRes
.process
.run(fetch, maxIterations)
.attemptRun
2016-04-05 16:24:39 +02:00
.leftMap(ex => throw new Exception("Exception during resolution", ex))
2015-12-30 01:34:43 +01:00
.merge
2015-12-30 01:34:48 +01:00
resLogger.stop()
2015-12-30 01:34:45 +01:00
2015-12-30 01:34:43 +01:00
if (!res.isDone)
2016-04-05 16:24:39 +02:00
throw new Exception("Maximum number of iteration of dependency resolution reached")
2015-12-30 01:34:43 +01:00
2016-01-10 21:32:28 +01:00
if (res.conflicts.nonEmpty) {
val projCache = res.projectCache.mapValues { case (_, p) => p }
2016-04-05 16:24:39 +02:00
log.error(
s"${res.conflicts.size} conflict(s):\n" +
" " + Print.dependenciesUnknownConfigs(res.conflicts.toVector, projCache)
)
throw new Exception("Conflict(s) in dependency resolution")
2015-12-30 01:34:43 +01:00
}
2015-12-30 01:34:39 +01:00
2016-01-10 21:32:28 +01:00
if (res.errors.nonEmpty) {
2016-04-05 16:24:39 +02:00
log.error(
s"\n${res.errors.size} error(s):\n" +
res.errors.map {
case (dep, errs) =>
s" ${dep.module}:${dep.version}:\n" +
errs
.map(" " + _.replace("\n", " \n"))
.mkString("\n")
}.mkString("\n")
)
2016-01-10 21:32:28 +01:00
throw new Exception(s"Encountered ${res.errors.length} error(s) in dependency resolution")
2015-12-30 01:34:43 +01:00
}
2015-12-30 01:34:39 +01:00
2016-01-10 21:32:28 +01:00
val depsByConfig = grouped(currentProject.dependencies)
2015-12-30 01:34:39 +01:00
2016-01-10 21:32:28 +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))
}
configs0.map {
case (config, _) =>
config -> allExtends(config)
2015-12-30 01:34:43 +01:00
}
2016-01-10 21:32:28 +01:00
}
if (verbosityLevel >= 0)
2016-04-05 16:24:39 +02:00
log.info("Resolution done")
if (verbosityLevel >= 1) {
2016-01-10 21:32:28 +01:00
val finalDeps = Config.dependenciesWithConfig(
res,
depsByConfig.map { case (k, l) => k -> l.toSet },
configs
)
val projCache = res.projectCache.mapValues { case (_, p) => p }
val repr = Print.dependenciesUnknownConfigs(finalDeps.toVector, projCache)
2016-04-05 16:24:39 +02:00
log.info(repr.split('\n').map(" "+_).mkString("\n"))
2015-12-30 01:34:39 +01:00
}
2015-12-30 01:34:43 +01:00
val classifiers =
if (withClassifiers)
Some {
if (sbtClassifiers)
cm.classifiers
else
transitiveClassifiers.value
}
else
None
val allArtifacts =
classifiers match {
case None => res.artifacts
case Some(cl) => res.classifiersArtifacts(cl)
2015-12-30 01:34:39 +01:00
}
2015-12-30 01:34:48 +01:00
val artifactsLogger = createLogger()
2015-12-30 01:34:43 +01:00
val artifactFileOrErrorTasks = allArtifacts.toVector.map { a =>
def f(p: CachePolicy) =
Cache.file(
a,
cache,
p,
checksums = artifactsChecksums,
logger = Some(artifactsLogger),
pool = pool
)
cachePolicies.tail
.foldLeft(f(cachePolicies.head))(_ orElse f(_))
.run
.map((a, _))
2015-12-30 01:34:39 +01:00
}
if (verbosityLevel >= 0)
2016-04-05 16:24:39 +02:00
log.info("Fetching artifacts")
2015-12-30 01:34:39 +01:00
2015-12-30 01:34:48 +01:00
artifactsLogger.init()
2015-12-30 01:34:43 +01:00
val artifactFilesOrErrors = Task.gatherUnordered(artifactFileOrErrorTasks).attemptRun match {
case -\/(ex) =>
2016-04-05 16:24:39 +02:00
throw new Exception("Error while downloading / verifying artifacts", ex)
2015-12-30 01:34:43 +01:00
case \/-(l) =>
l.toMap
}
2015-12-30 01:34:39 +01:00
2015-12-30 01:34:48 +01:00
artifactsLogger.stop()
if (verbosityLevel >= 0)
2016-04-05 16:24:39 +02:00
log.info("Fetching artifacts: done")
2015-12-30 01:34:43 +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) =>
2016-04-05 16:24:39 +02:00
log.error(s"${artifact.url}: $err")
2015-12-30 01:34:43 +01:00
None
}
2015-12-30 01:34:39 +01:00
}
writeIvyFiles()
2015-12-30 01:34:43 +01:00
ToSbt.updateReport(
depsByConfig,
res,
configs,
classifiers,
artifactFileOpt
)
2015-12-30 01:34:39 +01:00
}
2015-12-30 01:34:43 +01:00
resolutionsCache.getOrElseUpdate(
CacheKey(
currentProject,
repositories,
startRes.copy(filter = None),
withClassifiers,
sbtClassifiers
),
2015-12-30 01:34:43 +01:00
report
2015-12-30 01:34:39 +01:00
)
}
}
2015-12-30 01:34:34 +01:00
}