Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created November 29, 2022 06:53
Show Gist options
  • Save xuwei-k/bbe794441c99984f9d1a6cdb0dbe41bd to your computer and use it in GitHub Desktop.
Save xuwei-k/bbe794441c99984f9d1a6cdb0dbe41bd to your computer and use it in GitHub Desktop.
commands += {
def getProjects(s: State): Seq[String] = {
val extracted = Project.extract(s)
val currentBuildUri = extracted.currentRef.build
val buildStructure = extracted.structure
val buildUnitsMap = buildStructure.units
val currentBuildUnit = buildUnitsMap(currentBuildUri)
val projectsMap = currentBuildUnit.defined
projectsMap.values.map(_.id).toVector
}
import sbt.internal.util.complete.DefaultParsers._
import sbt.internal.util.complete.Parser
def distinctParser(exs: Set[String]): Parser[Seq[String]] = {
val base = token(Space) ~> token(NotSpace examples exs)
base.flatMap { ex =>
val (_, notMatching) = exs.partition(GlobFilter(ex).accept)
distinctParser(notMatching).map { result => ex +: result }
} ?? Nil
}
val suggestionTasks = Set("compile", "Test/compile", "test", "scalafmtAll", "clean")
Command.arb { s =>
token(literal("reverseTaskAll")) ~> Space.string ~> NotSpace.examples(suggestionTasks) ~ distinctParser(
getProjects(s).toSet
)
} {
case (s, (taskName: String, projectRegexList: Seq[String])) =>
val projects = getProjects(s).filter { id =>
projectRegexList.exists(r => id.matches(r) || id.startsWith(r))
}
if (projects.nonEmpty) {
val currentBuildURI = Project.extract(s).currentRef.build
def allDependencies(id: String): Seq[String] = {
buildDependencies.value
.classpathTransitiveRefs(ProjectRef(currentBuildURI, id))
.map(_.project)
}
val reverseMap: Map[String, Seq[String]] = getProjects(s)
.map(x => x -> allDependencies(x))
.flatMap { case (k, v) => v.map(_ -> k) }
.groupBy(_._1)
.map {
case (k, v) => k -> v.map(_._2)
}
val all = projects.flatMap(reverseMap).sorted
val result = all.map(_ + "/" + taskName).mkString("all ", " ", "")
s.log.info(result)
result :: s
} else {
s.log.error(s"no match ${projectRegexList.mkString(" ")}")
s
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment