11package 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+
315import scala .cli .commands .SpecificationLevel
416import scala .build .directives .*
517import scala .build .EitherCps .{either , value }
@@ -8,31 +20,96 @@ import scala.build.errors.{BuildException, CompositeBuildException}
820import scala .build .options .{BuildOptions , SourceGeneratorOptions , GeneratorConfig }
921import scala .build .options .GeneratorConfig
1022import 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 )
1530final 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
2349object 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