Merge pull request #5435 from dwijnand/tweak-Def.displayRelative

Tweak Def.displayRelative
This commit is contained in:
eugene yokota 2020-02-28 09:57:04 -05:00 committed by GitHub
commit 9c384dca15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 6 deletions

View File

@ -10,6 +10,7 @@ package sbt
import java.io.File
import java.net.URI
import scala.annotation.tailrec
import sbt.KeyRanks.{ DTask, Invisible }
import sbt.Scope.{ GlobalScope, ThisScope }
import sbt.internal.util.Types.const
@ -132,13 +133,19 @@ object Def extends Init[Scope] with TaskMacroExtra {
project: Reference,
trailingSlash: Boolean
): String = {
val trailing = if (trailingSlash) " /" else ""
project match {
case BuildRef(current.build) => "ThisBuild" + trailing
case `current` => ""
case ProjectRef(current.build, x) => x + trailing
case _ => Reference.display(project) + trailing
import Reference.{ display => displayRef }
@tailrec def loop(ref: Reference): String = ref match {
case ProjectRef(b, p) => if (b == current.build) loop(LocalProject(p)) else displayRef(ref)
case BuildRef(b) => if (b == current.build) loop(ThisBuild) else displayRef(ref)
case RootProject(b) => if (b == current.build) loop(LocalRootProject) else displayRef(ref)
case LocalProject(p) => if (p == current.project) "" else p
case ThisBuild => "ThisBuild"
case LocalRootProject => "<root>"
case ThisProject => "<this>"
}
val str = loop(project)
if (trailingSlash && !str.isEmpty) s"$str /"
else str
}
@deprecated("Use variant without multi", "1.1.1")

View File

@ -47,4 +47,28 @@ class ScopeDisplaySpec extends FlatSpec {
)
assert(string == "blah")
}
behavior of "Def.displayRelative2"
val b1 = project.build
val b2 = sbt.io.IO.toURI(file("other"))
val p1 = project
val p2 = ProjectRef(b1, "baz")
val p3 = ProjectRef(b2, "qux")
private def disp(r: Reference) = Def.displayRelative2(current = p1, r)
it should "ProjectRef curr proj" in assert(disp(p1) == "")
it should "ProjectRef same build" in assert(disp(p2) == "baz /")
it should "ProjectRef diff build" in assert(disp(p3) == """ProjectRef(uri("other"), "qux") /""")
it should "BuildRef same build" in assert(disp(BuildRef(b1)) == "ThisBuild /")
it should "BuildRef diff build" in assert(disp(BuildRef(b2)) == "{other} /")
it should "RootProject same build" in assert(disp(RootProject(b1)) == "<root> /")
it should "RootProject diff build" in assert(disp(RootProject(b2)) == "{other }<root> /")
it should "LocalProject curr proj" in assert(disp(LocalProject(p1.project)) == "")
it should "LocalProject diff proj" in assert(disp(LocalProject(p2.project)) == "baz /")
it should "ThisBuild" in assert(disp(ThisBuild) == "ThisBuild /")
it should "LocalRootProject" in assert(disp(LocalRootProject) == "<root> /")
it should "ThisProject" in assert(disp(ThisProject) == "<this> /")
}