Skip to content

Commit 01c284a

Browse files
committed
supports multi-line directives for Source Generator
1 parent c05f7cf commit 01c284a

File tree

5 files changed

+41
-74
lines changed

5 files changed

+41
-74
lines changed

modules/build/src/main/scala/scala/build/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ object Build {
10121012
scope = scope,
10131013
javaHomeOpt = Option(options.javaHomeLocation().value),
10141014
javacOptions = javacOptions,
1015-
generateSource = hardcodedSource
1015+
generateSource = Option(sourceGeneratorConfig)
10161016
)
10171017
project
10181018
}

modules/build/src/main/scala/scala/build/Project.scala

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final case class Project(
2929
javaHomeOpt: Option[os.Path],
3030
scope: Scope,
3131
javacOptions: List[String],
32-
generateSource: Option[GeneratorConfig]
32+
generateSource: Option[Seq[GeneratorConfig]]
3333
) {
3434

3535
import Project._
@@ -52,51 +52,25 @@ final case class Project(
5252
)
5353
}
5454

55-
val sourceGen0: Option[List[BloopConfig.SourceGenerator]] =
56-
generateSource.map(config =>
57-
println(s"Input Directory => ${config.inputDir}")
58-
println(s"Globber => ${config.glob}")
59-
println(s"Command File Path => ${config.commandFilePath}")
60-
// val command0 = s"${os.pwd}/testing-a/${config.commandFilePath}"
61-
val command0 = config.commandFilePath
62-
val sourceGlobs0 = BloopConfig.SourcesGlobs(
63-
// (os.pwd / "testing-a" / config.inputDir).toNIO,
64-
Paths.get(config.inputDir),
65-
None,
66-
config.glob,
67-
Nil
68-
)
69-
70-
val sourceGen = BloopConfig.SourceGenerator(
71-
List(sourceGlobs0),
72-
(os.pwd / "testing-a" / "source-generator-output").toNIO,
73-
// Paths.get(config.inputDir),
74-
List("python3", command0)
75-
// Nil
76-
)
55+
val sourceGenerator: Option[List[BloopConfig.SourceGenerator]] =
56+
generateSource.map(configs =>
57+
configs.map { config =>
58+
val command0 = config.commandFilePath
59+
val sourceGlobs0 = BloopConfig.SourcesGlobs(
60+
Paths.get(config.inputDir),
61+
None,
62+
config.glob,
63+
Nil
64+
)
7765

78-
List(sourceGen)
66+
BloopConfig.SourceGenerator(
67+
List(sourceGlobs0),
68+
(config.outputPath / "source-generator-output").toNIO,
69+
List("python3", command0)
70+
)
71+
}.toList
7972
)
8073

81-
// val sourceGen: BloopConfig.SourceGenerator = {
82-
// val command = s"${os.pwd}/${generateSource.commandFilePath}"
83-
// val sourceGlobs = BloopConfig.SourcesGlobs(
84-
// (os.pwd / generateSource.inputDir).toNIO,
85-
// None,
86-
// List(generateSource.glob),
87-
// Nil
88-
// )
89-
90-
// BloopConfig.SourceGenerator(
91-
// List(sourceGlobs),
92-
// (os.pwd / "testing-a" / "source-generator-output").toNIO,
93-
// List("python3", command)
94-
// // Nil
95-
// )
96-
// }
97-
98-
99-
10074
baseBloopProject(
10175
projectName,
10276
directory.toNIO,
@@ -113,7 +87,7 @@ final case class Project(
11387
`scala` = scalaConfigOpt,
11488
java = Some(BloopConfig.Java(javacOptions)),
11589
resolution = resolution,
116-
sourceGenerators = sourceGen0
90+
sourceGenerators = sourceGenerator
11791
)
11892
}
11993

modules/directives/src/main/scala/scala/build/preprocessing/directives/SourceGenerator.scala

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ import scala.build.{Positioned, options}
1313
@DirectiveDescription("Generate code using Source Generator")
1414
@DirectiveLevel(SpecificationLevel.EXPERIMENTAL)
1515
final case class SourceGenerator(
16-
sourceGenerator: DirectiveValueParser.WithScopePath[Option[Positioned[String]]] = DirectiveValueParser.WithScopePath.empty(None)
16+
sourceGenerator: DirectiveValueParser.WithScopePath[List[Positioned[String]]] =
17+
DirectiveValueParser.WithScopePath.empty(Nil)
1718
) extends HasBuildOptions {
18-
def buildOptions: Either[BuildException, BuildOptions] = either {
19-
val sourceGen = sourceGenerator.value
20-
21-
val maybeGenerateSource = sourceGen
22-
.map(GeneratorConfig.parse)
23-
.sequence
24-
25-
val generateSource = maybeGenerateSource match {
26-
case Left(buildException) => throw buildException
27-
case Right(config) => config
28-
}
29-
30-
BuildOptions(sourceGeneratorOptions =
31-
SourceGeneratorOptions(generatorConfig = generateSource)
32-
)
33-
}
19+
def buildOptions: Either[BuildException, BuildOptions] =
20+
SourceGenerator.buildOptions(sourceGenerator)
3421
}
3522

3623
object SourceGenerator {
3724
val handler: DirectiveHandler[SourceGenerator] = DirectiveHandler.derive
25+
def buildOptions(sourceGenerator: DirectiveValueParser.WithScopePath[List[Positioned[String]]])
26+
: Either[BuildException, BuildOptions] = {
27+
val sourceGenValue = sourceGenerator.value
28+
sourceGenValue
29+
.map(config => GeneratorConfig.parse(config, sourceGenerator.scopePath.subPath))
30+
.sequence
31+
.left.map(CompositeBuildException(_))
32+
.map { configs =>
33+
BuildOptions(sourceGeneratorOptions =
34+
SourceGeneratorOptions(generatorConfig = configs)
35+
)
36+
}
37+
}
3838
}

modules/options/src/main/scala/scala/build/options/SourceGenerator.scala renamed to modules/options/src/main/scala/scala/build/options/GeneratorConfig.scala

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import scala.build.errors.{BuildException, MalformedInputError}
66
final case class GeneratorConfig(
77
inputDir: String,
88
glob: List[String],
9-
commandFilePath: String
9+
commandFilePath: String,
10+
outputPath: os.SubPath
1011
)
1112

1213
object GeneratorConfig {
1314

14-
def parse(input: Positioned[String]): Either[BuildException, GeneratorConfig] =
15+
def parse(input: Positioned[String], output: os.SubPath): Either[BuildException, GeneratorConfig] =
1516
input.value.split("\\|", 3) match {
1617
case Array(inputDir, glob, commandFilePath) =>
17-
Right(GeneratorConfig(inputDir, List(glob), commandFilePath))
18+
Right(GeneratorConfig(inputDir, List(glob), commandFilePath, output))
1819
case _ =>
1920
Left(
2021
new MalformedInputError(
@@ -25,12 +26,4 @@ object GeneratorConfig {
2526
)
2627
)
2728
}
28-
29-
// def formatPath(
30-
// inputDir: String,
31-
// glob: String,
32-
// commandFilePath: String,
33-
// ): GeneratorConfig = {
34-
35-
// }
3629
}

modules/options/src/main/scala/scala/build/options/SourceGeneratorOptions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ final case class SourceGeneratorOptions(
44
useBuildInfo: Option[Boolean] = None,
55
projectVersion: Option[String] = None,
66
computeVersion: Option[ComputeVersion] = None,
7-
generatorConfig: Option[GeneratorConfig] = None
7+
generatorConfig: Seq[GeneratorConfig] = Nil,
88
)
99

1010
object SourceGeneratorOptions {

0 commit comments

Comments
 (0)