Skip to content

Commit dd9d660

Browse files
committed
using new directive style
1 parent af24421 commit dd9d660

File tree

3 files changed

+91
-16
lines changed

3 files changed

+91
-16
lines changed

modules/build/src/main/scala/scala/build/preprocessing/DirectivesPreprocessor.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ case class DirectivesPreprocessor(
4545
def preprocess(extractedDirectives: ExtractedDirectives)
4646
: Either[BuildException, PreprocessedDirectives] = either {
4747
val ExtractedDirectives(directives, directivesPositions) = extractedDirectives
48-
println(s"DirectivesPreprocessor: $directives")
49-
5048
val (
5149
buildOptionsWithoutRequirements: PartiallyProcessedDirectives[BuildOptions],
5250
buildOptionsWithTargetRequirements: PartiallyProcessedDirectives[
@@ -129,6 +127,7 @@ case class DirectivesPreprocessor(
129127
else
130128
if handler.isExperimental && !shouldSuppressExperimentalFeatures then
131129
logger.experimentalWarning(scopedDirective.directive.toString, FeatureType.Directive)
130+
// println(scopedDirective.cwd)
132131
handler.handleValues(scopedDirective, logger)
133132

134133
val handlersMap = handlers

modules/directives/src/main/scala/scala/build/directives/DirectiveValueParser.scala

-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ object DirectiveValueParser {
138138
given DirectiveSingleValueParser[String] =
139139
(key, value, scopePath, path) =>
140140
value.asString.toRight {
141-
println("do we get here?")
142141
val pos = value.position(path)
143142
new MalformedDirectiveError(
144143
message =
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package scala.build.preprocessing.directives
22

3+
// Virtuslab Processor
4+
import com.virtuslab.using_directives.UsingDirectivesProcessor
5+
import com.virtuslab.using_directives.custom.model.{
6+
BooleanValue,
7+
EmptyValue,
8+
StringValue,
9+
UsingDirectives,
10+
Value
11+
}
12+
import com.virtuslab.using_directives.custom.utils.ast._
13+
import scala.jdk.CollectionConverters.*
14+
315
import scala.cli.commands.SpecificationLevel
416
import scala.build.directives.*
517
import scala.build.EitherCps.{either, value}
@@ -8,31 +20,96 @@ import scala.build.errors.{BuildException, CompositeBuildException}
820
import scala.build.options.{BuildOptions, SourceGeneratorOptions, GeneratorConfig}
921
import scala.build.options.GeneratorConfig
1022
import scala.build.{Positioned, options}
23+
import scala.build.directives.DirectiveValueParser.WithScopePath
1124

25+
@DirectiveGroupName("SourceGenerator")
26+
@DirectivePrefix("sourceGenerator.")
1227
@DirectiveUsage("//> using sourceGenerator", "`//> using sourceGenerator`")
1328
@DirectiveDescription("Generate code using Source Generator")
1429
@DirectiveLevel(SpecificationLevel.EXPERIMENTAL)
1530
final case class SourceGenerator(
16-
sourceGenerator: DirectiveValueParser.WithScopePath[List[Positioned[String]]] =
17-
DirectiveValueParser.WithScopePath.empty(Nil)
31+
testy: DirectiveValueParser.WithScopePath[List[Positioned[String]]] =
32+
DirectiveValueParser.WithScopePath.empty(Nil),
33+
scripts: DirectiveValueParser.WithScopePath[List[Positioned[String]]] =
34+
DirectiveValueParser.WithScopePath.empty(Nil),
35+
excludeScripts: Option[Boolean] = None,
36+
inputDirectory: DirectiveValueParser.WithScopePath[Option[Positioned[String]]] =
37+
DirectiveValueParser.WithScopePath.empty(None),
38+
glob: Option[Positioned[String]] = None,
39+
commandProcessor: DirectiveValueParser.WithScopePath[Option[Positioned[String]]] =
40+
DirectiveValueParser.WithScopePath.empty(None),
1841
) extends HasBuildOptions {
1942
def buildOptions: Either[BuildException, BuildOptions] =
20-
SourceGenerator.buildOptions(sourceGenerator)
43+
// println(s"ScopePath of Scripts: ${scripts.scopePath}")
44+
// println(s"Values of Scripts: ${scripts.value(0).value}")
45+
// println(s"Values of InputDir: ${inputDirectory.value}")
46+
SourceGenerator.buildOptions(testy,scripts)
2147
}
2248

2349
object SourceGenerator {
2450
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)
51+
def buildOptions(
52+
sourceGenerator: DirectiveValueParser.WithScopePath[List[Positioned[String]]],
53+
scripts: DirectiveValueParser.WithScopePath[List[Positioned[String]]]
54+
): Either[BuildException, BuildOptions] = {
55+
val proc = UsingDirectivesProcessor()
56+
val scriptConvert = scripts.value
57+
.map(script => os.Path(script.value))
58+
.map(os.read(_))
59+
.map(_.toCharArray())
60+
.map(proc.extract(_).asScala)
61+
.map(_.headOption)
62+
63+
// println(scriptConvert.size)
64+
65+
def modify(script: Option[UsingDirectives]) = {
66+
script.toSeq.flatMap { directives =>
67+
def toStrictValue(value: UsingValue): Seq[Value[_]] = value match {
68+
case uvs: UsingValues => uvs.values.asScala.toSeq.flatMap(toStrictValue)
69+
case el: EmptyLiteral => Seq(EmptyValue(el))
70+
case sl: StringLiteral => Seq(StringValue(sl.getValue(), sl))
71+
case bl: BooleanLiteral => Seq(BooleanValue(bl.getValue(), bl))
72+
}
73+
def toStrictDirective(ud: UsingDef) = StrictDirective(
74+
ud.getKey(),
75+
toStrictValue(ud.getValue()),
76+
ud.getPosition().getColumn()
3577
)
78+
79+
// println(directives.getAst())
80+
81+
directives.getAst match
82+
case uds: UsingDefs => uds.getUsingDefs.asScala.toSeq.map(toStrictDirective)
83+
case _ => Nil // There should be nothing else here other than UsingDefs
3684
}
85+
}
86+
87+
val strictDirectives = scriptConvert
88+
.map(modify(_))
89+
.map(_.take(3))
90+
91+
// println(scripts.scopePath.subPath)
92+
// strictDirectives.map(f => f.map(w => println(w)))
93+
94+
val directive = strictDirectives.collect {
95+
case Seq(inputDir, glob, processor) =>
96+
GeneratorConfig(inputDir.values.mkString, List(glob.values.mkString), processor.values.mkString.split(" ").toList,scripts.scopePath.subPath)
97+
}
98+
println(directive.size)
99+
100+
// val sourceGenValue = sourceGenerator.value
101+
// sourceGenValue
102+
// .map(config => GeneratorConfig.parse(config, sourceGenerator.scopePath.subPath))
103+
// .sequence
104+
// .left.map(CompositeBuildException(_))
105+
// .map { configs =>
106+
// BuildOptions(sourceGeneratorOptions =
107+
// SourceGeneratorOptions(generatorConfig = configs)
108+
// )
109+
// }
110+
111+
// directive.map { f => println(f)}
112+
113+
Right(BuildOptions(sourceGeneratorOptions = SourceGeneratorOptions(generatorConfig = directive)))
37114
}
38115
}

0 commit comments

Comments
 (0)