Use SBT logging to print things

This commit is contained in:
Alexandre Archambault 2016-04-05 16:24:39 +02:00
parent 105236393d
commit d899303d2e
2 changed files with 46 additions and 30 deletions

View File

@ -133,7 +133,11 @@ object FromSbt {
) )
} }
def repository(resolver: Resolver, ivyProperties: Map[String, String]): Option[Repository] = def repository(
resolver: Resolver,
ivyProperties: Map[String, String],
log: sbt.Logger
): Option[Repository] =
resolver match { resolver match {
case sbt.MavenRepository(_, root) => case sbt.MavenRepository(_, root) =>
try { try {
@ -142,10 +146,10 @@ object FromSbt {
Some(MavenRepository(root0, sbtAttrStub = true)) Some(MavenRepository(root0, sbtAttrStub = true))
} catch { } catch {
case e: MalformedURLException => case e: MalformedURLException =>
Console.err.println( log.warn(
"Warning: error parsing Maven repository base " + "Error parsing Maven repository base " +
root + root +
Option(e.getMessage).map(" ("+_+")").mkString + Option(e.getMessage).map(" (" + _ + ")").mkString +
", ignoring it" ", ignoring it"
) )
@ -177,7 +181,7 @@ object FromSbt {
)) ))
case other => case other =>
Console.err.println(s"Warning: unrecognized repository ${other.name}, ignoring it") log.warn(s"Unrecognized repository ${other.name}, ignoring it")
None None
} }

View File

@ -187,9 +187,6 @@ object Tasks {
def updateTask(withClassifiers: Boolean, sbtClassifiers: Boolean = false) = Def.task { def updateTask(withClassifiers: Boolean, sbtClassifiers: Boolean = false) = Def.task {
// SBT logging should be better than that most of the time...
def errPrintln(s: String): Unit = scala.Console.err.println(s)
def grouped[K, V](map: Seq[(K, V)]): Map[K, Seq[V]] = def grouped[K, V](map: Seq[(K, V)]): Map[K, Seq[V]] =
map.groupBy { case (k, _) => k }.map { map.groupBy { case (k, _) => k }.map {
case (k, l) => case (k, l) =>
@ -252,6 +249,8 @@ object Tasks {
val cachePolicies = coursierCachePolicies.value val cachePolicies = coursierCachePolicies.value
val cache = coursierCache.value val cache = coursierCache.value
val log = streams.value.log
val sv = scalaVersion.value // is this always defined? (e.g. for Java only projects?) val sv = scalaVersion.value // is this always defined? (e.g. for Java only projects?)
val sbv = scalaBinaryVersion.value val sbv = scalaBinaryVersion.value
@ -267,7 +266,7 @@ object Tasks {
rule.configurations.nonEmpty || rule.configurations.nonEmpty ||
rule.crossVersion != sbt.CrossVersion.Disabled rule.crossVersion != sbt.CrossVersion.Disabled
) { ) {
Console.err.println(s"Warning: unsupported exclusion rule $rule") log.warn(s"Unsupported exclusion rule $rule")
anyNonSupportedExclusionRule = true anyNonSupportedExclusionRule = true
Nil Nil
} else } else
@ -275,7 +274,7 @@ object Tasks {
}.toSet }.toSet
if (anyNonSupportedExclusionRule) if (anyNonSupportedExclusionRule)
Console.err.println(s"Only supported exclusion rule fields: organization, name") log.warn("Only supported exclusion rule fields: organization, name")
val resolvers = val resolvers =
if (sbtClassifiers) if (sbtClassifiers)
@ -312,9 +311,9 @@ object Tasks {
} }
if (verbosityLevel >= 2) { if (verbosityLevel >= 2) {
println("InterProjectRepository") log.info("InterProjectRepository")
for (p <- projects) for (p <- projects)
println(s" ${p.module}:${p.version}") log.info(s" ${p.module}:${p.version}")
} }
val globalPluginsRepo = IvyRepository( val globalPluginsRepo = IvyRepository(
@ -335,7 +334,7 @@ object Tasks {
globalPluginsRepo, globalPluginsRepo,
interProjectRepo interProjectRepo
) ++ resolvers.flatMap( ) ++ resolvers.flatMap(
FromSbt.repository(_, ivyProperties) FromSbt.repository(_, ivyProperties, log)
) ++ { ) ++ {
if (fallbackDependencies.isEmpty) if (fallbackDependencies.isEmpty)
Nil Nil
@ -389,14 +388,17 @@ object Tasks {
r.toString r.toString
} }
errPrintln(s"Repositories:\n${repoReprs.map(" "+_).mkString("\n")}") log.info(
"Repositories:\n" +
repoReprs.map(" " + _).mkString("\n")
)
} }
if (verbosityLevel >= 0) if (verbosityLevel >= 0)
errPrintln(s"Resolving ${currentProject.module.organization}:${currentProject.module.name}:${currentProject.version}") log.info(s"Resolving ${currentProject.module.organization}:${currentProject.module.name}:${currentProject.version}")
if (verbosityLevel >= 1) if (verbosityLevel >= 1)
for (depRepr <- depsRepr(currentProject.dependencies)) for (depRepr <- depsRepr(currentProject.dependencies))
errPrintln(s" $depRepr") log.info(s" $depRepr")
resLogger.init() resLogger.init()
@ -404,26 +406,36 @@ object Tasks {
.process .process
.run(fetch, maxIterations) .run(fetch, maxIterations)
.attemptRun .attemptRun
.leftMap(ex => throw new Exception(s"Exception during resolution", ex)) .leftMap(ex => throw new Exception("Exception during resolution", ex))
.merge .merge
resLogger.stop() resLogger.stop()
if (!res.isDone) if (!res.isDone)
throw new Exception(s"Maximum number of iteration of dependency resolution reached") throw new Exception("Maximum number of iteration of dependency resolution reached")
if (res.conflicts.nonEmpty) { if (res.conflicts.nonEmpty) {
val projCache = res.projectCache.mapValues { case (_, p) => p } val projCache = res.projectCache.mapValues { case (_, p) => p }
println(s"${res.conflicts.size} conflict(s):\n ${Print.dependenciesUnknownConfigs(res.conflicts.toVector, projCache)}") log.error(
throw new Exception(s"Conflict(s) in dependency resolution") s"${res.conflicts.size} conflict(s):\n" +
" " + Print.dependenciesUnknownConfigs(res.conflicts.toVector, projCache)
)
throw new Exception("Conflict(s) in dependency resolution")
} }
if (res.errors.nonEmpty) { if (res.errors.nonEmpty) {
println(s"\n${res.errors.size} error(s):") log.error(
for ((dep, errs) <- res.errors) { s"\n${res.errors.size} error(s):\n" +
println(s" ${dep.module}:${dep.version}:\n${errs.map(" " + _.replace("\n", " \n")).mkString("\n")}") res.errors.map {
} case (dep, errs) =>
s" ${dep.module}:${dep.version}:\n" +
errs
.map(" " + _.replace("\n", " \n"))
.mkString("\n")
}.mkString("\n")
)
throw new Exception(s"Encountered ${res.errors.length} error(s) in dependency resolution") throw new Exception(s"Encountered ${res.errors.length} error(s) in dependency resolution")
} }
@ -454,7 +466,7 @@ object Tasks {
} }
if (verbosityLevel >= 0) if (verbosityLevel >= 0)
errPrintln("Resolution done") log.info("Resolution done")
if (verbosityLevel >= 1) { if (verbosityLevel >= 1) {
val finalDeps = Config.dependenciesWithConfig( val finalDeps = Config.dependenciesWithConfig(
res, res,
@ -464,7 +476,7 @@ object Tasks {
val projCache = res.projectCache.mapValues { case (_, p) => p } val projCache = res.projectCache.mapValues { case (_, p) => p }
val repr = Print.dependenciesUnknownConfigs(finalDeps.toVector, projCache) val repr = Print.dependenciesUnknownConfigs(finalDeps.toVector, projCache)
println(repr.split('\n').map(" "+_).mkString("\n")) log.info(repr.split('\n').map(" "+_).mkString("\n"))
} }
val classifiers = val classifiers =
@ -504,13 +516,13 @@ object Tasks {
} }
if (verbosityLevel >= 0) if (verbosityLevel >= 0)
errPrintln(s"Fetching artifacts") log.info("Fetching artifacts")
artifactsLogger.init() artifactsLogger.init()
val artifactFilesOrErrors = Task.gatherUnordered(artifactFileOrErrorTasks).attemptRun match { val artifactFilesOrErrors = Task.gatherUnordered(artifactFileOrErrorTasks).attemptRun match {
case -\/(ex) => case -\/(ex) =>
throw new Exception(s"Error while downloading / verifying artifacts", ex) throw new Exception("Error while downloading / verifying artifacts", ex)
case \/-(l) => case \/-(l) =>
l.toMap l.toMap
} }
@ -518,7 +530,7 @@ object Tasks {
artifactsLogger.stop() artifactsLogger.stop()
if (verbosityLevel >= 0) if (verbosityLevel >= 0)
errPrintln(s"Fetching artifacts: done") log.info("Fetching artifacts: done")
def artifactFileOpt(artifact: Artifact) = { def artifactFileOpt(artifact: Artifact) = {
val fileOrError = artifactFilesOrErrors.getOrElse(artifact, -\/("Not downloaded")) val fileOrError = artifactFilesOrErrors.getOrElse(artifact, -\/("Not downloaded"))
@ -529,7 +541,7 @@ object Tasks {
throw new Exception(s"Wrong path: $file") throw new Exception(s"Wrong path: $file")
Some(file) Some(file)
case -\/(err) => case -\/(err) =>
errPrintln(s"${artifact.url}: $err") log.error(s"${artifact.url}: $err")
None None
} }
} }