mirror of https://github.com/sbt/sbt.git
Merge pull request #4956 from eatkins/custom-show
Allow custom show for a scope
This commit is contained in:
commit
740936e156
|
|
@ -54,12 +54,14 @@ object Def extends Init[Scope] with TaskMacroExtra {
|
||||||
keyNameColor: Option[String] = None,
|
keyNameColor: Option[String] = None,
|
||||||
): Show[ScopedKey[_]] =
|
): Show[ScopedKey[_]] =
|
||||||
Show[ScopedKey[_]](
|
Show[ScopedKey[_]](
|
||||||
key =>
|
key => {
|
||||||
Scope.display(
|
val color: String => String = withColor(_, keyNameColor)
|
||||||
key.scope,
|
key.scope.extra.toOption
|
||||||
withColor(key.key.label, keyNameColor),
|
.flatMap(_.get(Scope.customShowString).map(color))
|
||||||
ref => displayRelative2(current, ref)
|
.getOrElse {
|
||||||
)
|
Scope.display(key.scope, color(key.key.label), ref => displayRelative2(current, ref))
|
||||||
|
}
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
private[sbt] def showShortKey(
|
private[sbt] def showShortKey(
|
||||||
|
|
|
||||||
|
|
@ -178,6 +178,22 @@ object Scope {
|
||||||
): String =
|
): String =
|
||||||
displayMasked(scope, sep, showProject, mask, false)
|
displayMasked(scope, sep, showProject, mask, false)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows the user to override the result of `Scope.display` or `Scope.displayMasked` for a
|
||||||
|
* particular scope. This can be used to enhance super shell and/or error reporting for tasks
|
||||||
|
* that use mangled names. For example, one might have:
|
||||||
|
* {{{
|
||||||
|
* val mangledKey = TaskKey[Unit]("foo_slash_bar")
|
||||||
|
* val attributeMap = AttributeMap.empty.put(Scope.customShowString("foo/bar"))
|
||||||
|
* val sanitizedKey = mangledKey.copy(scope = mangledKey.copy(extra = Select(attributeMap)))
|
||||||
|
* sanitizedKey := { ... }
|
||||||
|
* }}}
|
||||||
|
*
|
||||||
|
* Now whenever the `foo_slash_bar` task specified by sanitizedKey is evaluated, it will display
|
||||||
|
* "foo/bar" in super shell progress and in the error message if an error is thrown.
|
||||||
|
*/
|
||||||
|
val customShowString = AttributeKey[String]("scope-custom-show-string")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* unified slash style introduced in sbt 1.1.0.
|
* unified slash style introduced in sbt 1.1.0.
|
||||||
* By default, sbt will no longer display the Zero-config,
|
* By default, sbt will no longer display the Zero-config,
|
||||||
|
|
@ -193,20 +209,22 @@ object Scope {
|
||||||
showZeroConfig: Boolean
|
showZeroConfig: Boolean
|
||||||
): String = {
|
): String = {
|
||||||
import scope.{ project, config, task, extra }
|
import scope.{ project, config, task, extra }
|
||||||
val zeroConfig = if (showZeroConfig) "Zero /" else ""
|
extra.toOption.flatMap(_.get(customShowString)).getOrElse {
|
||||||
val configPrefix = config.foldStrict(display, zeroConfig, "./")
|
val zeroConfig = if (showZeroConfig) "Zero /" else ""
|
||||||
val taskPrefix = task.foldStrict(_.label + " /", "", "./")
|
val configPrefix = config.foldStrict(display, zeroConfig, "./")
|
||||||
val extras = extra.foldStrict(_.entries.map(_.toString).toList, Nil, Nil)
|
val taskPrefix = task.foldStrict(_.label + " /", "", "./")
|
||||||
val postfix = if (extras.isEmpty) "" else extras.mkString("(", ", ", ")")
|
val extras = extra.foldStrict(_.entries.map(_.toString).toList, Nil, Nil)
|
||||||
if (scope == GlobalScope) "Global / " + sep + postfix
|
val postfix = if (extras.isEmpty) "" else extras.mkString("(", ", ", ")")
|
||||||
else
|
if (scope == GlobalScope) "Global / " + sep + postfix
|
||||||
mask.concatShow(
|
else
|
||||||
appendSpace(projectPrefix(project, showProject)),
|
mask.concatShow(
|
||||||
appendSpace(configPrefix),
|
appendSpace(projectPrefix(project, showProject)),
|
||||||
appendSpace(taskPrefix),
|
appendSpace(configPrefix),
|
||||||
sep,
|
appendSpace(taskPrefix),
|
||||||
postfix
|
sep,
|
||||||
)
|
postfix
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private[sbt] def appendSpace(s: String): String =
|
private[sbt] def appendSpace(s: String): String =
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
/*
|
||||||
|
* sbt
|
||||||
|
* Copyright 2011 - 2018, Lightbend, Inc.
|
||||||
|
* Copyright 2008 - 2010, Mark Harrah
|
||||||
|
* Licensed under Apache License 2.0 (see LICENSE)
|
||||||
|
*/
|
||||||
|
|
||||||
|
package sbt
|
||||||
|
|
||||||
|
import org.scalatest.FlatSpec
|
||||||
|
import sbt.internal.util.{ AttributeKey, AttributeMap }
|
||||||
|
import sbt.io.syntax.file
|
||||||
|
|
||||||
|
class ScopeDisplaySpec extends FlatSpec {
|
||||||
|
val project = ProjectRef(file("foo/bar"), "bar")
|
||||||
|
val mangledName = "bar_slash_blah_blah_blah"
|
||||||
|
val scopedKey = Def.ScopedKey(Scope.Global in project, AttributeKey[Task[String]](mangledName))
|
||||||
|
val am = AttributeMap.empty.put(Scope.customShowString, "blah")
|
||||||
|
val sanitizedKey = scopedKey.copy(scope = scopedKey.scope.copy(extra = Select(am)))
|
||||||
|
"Def.displayRelative2" should "display mangled name" in {
|
||||||
|
assert(Def.showRelativeKey2(project, None).show(scopedKey) == mangledName)
|
||||||
|
}
|
||||||
|
it should "display sanitized name with extra setting" in {
|
||||||
|
assert(Def.showRelativeKey2(project, None).show(sanitizedKey) == "blah")
|
||||||
|
}
|
||||||
|
"Scope.display" should "display the full scope" in {
|
||||||
|
val full = Scope.display(
|
||||||
|
scopedKey.scope,
|
||||||
|
"/",
|
||||||
|
(ref: Reference) =>
|
||||||
|
ref match {
|
||||||
|
case ProjectRef(_, n) => n
|
||||||
|
case _ => ???
|
||||||
|
}
|
||||||
|
)
|
||||||
|
assert(full == "bar /")
|
||||||
|
}
|
||||||
|
it should "display the sanitized scope" in {
|
||||||
|
val string = Scope.display(
|
||||||
|
sanitizedKey.scope,
|
||||||
|
"/",
|
||||||
|
(ref: Reference) =>
|
||||||
|
ref match {
|
||||||
|
case ProjectRef(_, n) => n
|
||||||
|
case _ => ???
|
||||||
|
}
|
||||||
|
)
|
||||||
|
assert(string == "blah")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue