Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created July 30, 2021 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuwei-k/804204886346ffd5ba4b51343dc2fd5a to your computer and use it in GitHub Desktop.
Save xuwei-k/804204886346ffd5ba4b51343dc2fd5a 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("taskAll")) ~> Space.string ~> NotSpace.examples(suggestionTasks) ~ distinctParser(getProjects(s).toSet)
} { case (s, (taskName: String, projectRegexList: Seq[String])) =>
println((taskName, projectRegexList))
val projects = getProjects(s).filter{ id =>
projectRegexList.exists(r => id.matches(r) || id.startsWith(r))
}
if (projects.nonEmpty) {
val result = projects.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