Skip to content

feat: Add command template to source generators #2646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions frontend/src/main/scala/bloop/engine/SourceGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.nio.file.FileSystems

import scala.collection.mutable
import scala.util.control.NoStackTrace
import scala.util.matching.Regex

import bloop.cli.CommonOptions
import bloop.config.Config
Expand All @@ -21,7 +22,8 @@ final case class SourceGenerator(
sourcesGlobs: List[SourcesGlobs],
outputDirectory: AbsolutePath,
unmangedInputs: List[AbsolutePath],
command: List[String]
command: List[String],
commandTemplate: Option[List[String]]
) {

/**
Expand Down Expand Up @@ -66,7 +68,14 @@ final case class SourceGenerator(
logger: Logger,
opts: CommonOptions
): Task[SourceGenerator.Run] = {
val cmd = (command :+ outputDirectory.syntax) ++ inputs.keys.map(_.syntax)
val cmd =
buildCommand(
outputDirectory.syntax,
inputs.keys.map(_.syntax).toSeq,
unmangedInputs.keys.map(_.syntax).toSeq,
logger
)

logger.debug { () =>
cmd.mkString(s"Running source generator:${System.lineSeparator()}$$ ", " ", "")
}
Expand All @@ -79,6 +88,28 @@ final case class SourceGenerator(
}
}

private def buildCommand(
outputDirectory: String,
inputs: Seq[String],
unmangedInputs: Seq[String],
logger: Logger
): Seq[String] =
commandTemplate match {
case None =>
(command :+ outputDirectory) ++ inputs
case Some(cmd) =>
val substs = Map[String, Seq[String]](
SourceGenerator.Arg.Output -> Seq(outputDirectory),
SourceGenerator.Arg.Inputs -> inputs,
SourceGenerator.Arg.UnmanagedInputs -> unmangedInputs
).withDefault { name =>
logger.warn(s"Couldn't find substitution for `$name`, consider escaping it with a $$.")
Seq.empty[String]
}

cmd.flatMap(SourceGenerator.Arg.substitute(substs)(_))
}

private def needsUpdate(previous: SourceGenerator.Run): Task[SourceGenerator.Changes] = {
previous match {
case SourceGenerator.NoRun =>
Expand Down Expand Up @@ -141,6 +172,33 @@ object SourceGenerator {
unamanagedInputs: Map[AbsolutePath, Int]
) extends Changes

private object Arg {
private val Single: Regex = """((?:\$)+)\{([a-zA-Z]+)\}""".r
private val Anywhere: Regex = Single.unanchored

// TODO: make these configurable in some way?
val Inputs = "inputs"
val Output = "output"
val UnmanagedInputs = "unmanaged"

def substitute(substs: Map[String, Seq[String]])(s: String): Seq[String] =
s match {
case Single("$", name) => substs(name)
case _ =>
Seq(Anywhere.replaceAllIn(s, m => Regex.quoteReplacement(replace(m, substs))))
}

private def replace(mtch: Regex.Match, substs: Map[String, Seq[String]]): String = {
val dollars = mtch.group(1).size
val name = mtch.group(2)
val value =
if (dollars % 2 == 0) s"{$name}"
else substs(name).mkString(" ")

s"${"$" * (dollars / 2)}$value"
}
}

def fromConfig(cwd: AbsolutePath, generator: Config.SourceGenerator): SourceGenerator = {
val sourcesGlobs = generator.sourcesGlobs.map {
case Config.SourcesGlobs(directory, depth, includes, excludes) =>
Expand All @@ -159,7 +217,9 @@ object SourceGenerator {
sourcesGlobs,
AbsolutePath(generator.outputDirectory),
generator.unmanagedInputs.map(AbsolutePath.apply),
generator.command
generator.command,
// TODO: change to `generator.commandTemplate` after PR to bloop-config is merged
None
)
}

Expand Down
1 change: 1 addition & 0 deletions frontend/src/test/scala/bloop/SourceGeneratorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import bloop.logging.RecordingLogger
import bloop.util.TestProject
import bloop.util.TestUtil

// TODO: add tests after PR to bloop-config is merged
object SourceGeneratorSpec extends bloop.testing.BaseSuite {

val generator = TestUtil.generator
Expand Down
1 change: 1 addition & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ object Dependencies {
val asmVersion = "9.7.1"
val ztExecVersion = "1.12"
val debugAdapterVersion = "4.2.4"
// TODO: update after PR to bloop-config is merged
val bloopConfigVersion = "2.3.2"
val semanticdbVersion = "4.9.9"
val millVersion = "0.12.7"
Expand Down
Loading