@@ -21,6 +21,9 @@ import scala.build.options.{BuildOptions, SourceGeneratorOptions, GeneratorConfi
21
21
import scala .build .options .GeneratorConfig
22
22
import scala .build .{Positioned , options }
23
23
import scala .build .directives .DirectiveValueParser .WithScopePath
24
+ import scala .util .matching .Regex
25
+ import java .nio .file .Paths
26
+ import scala .build .options .InternalOptions
24
27
25
28
@ DirectiveGroupName (" SourceGenerator" )
26
29
@ DirectivePrefix (" sourceGenerator." )
@@ -35,19 +38,17 @@ final case class SourceGenerator(
35
38
excludeScripts : Option [Boolean ] = None ,
36
39
inputDirectory : DirectiveValueParser .WithScopePath [Option [Positioned [String ]]] =
37
40
DirectiveValueParser .WithScopePath .empty(None ),
38
- glob : Option [Positioned [String ]] = None ,
41
+ glob : Option [Positioned [String ]] = None
39
42
) extends HasBuildOptions {
40
43
def buildOptions : Either [BuildException , BuildOptions ] =
41
- // println(s"ScopePath of Scripts: ${scripts.scopePath}")
42
- // println(s"Values of Scripts: ${scripts.value(0).value}")
43
- // println(s"Values of InputDir: ${inputDirectory.value}")
44
- SourceGenerator .buildOptions(scripts)
44
+ SourceGenerator .buildOptions(scripts, excludeScripts)
45
45
}
46
46
47
47
object SourceGenerator {
48
48
val handler : DirectiveHandler [SourceGenerator ] = DirectiveHandler .derive
49
49
def buildOptions (
50
- scripts : DirectiveValueParser .WithScopePath [List [Positioned [String ]]]
50
+ scripts : DirectiveValueParser .WithScopePath [List [Positioned [String ]]],
51
+ excludeScripts : Option [Boolean ]
51
52
): Either [BuildException , BuildOptions ] = {
52
53
val proc = UsingDirectivesProcessor ()
53
54
val scriptConvert = scripts.value
@@ -57,9 +58,12 @@ object SourceGenerator {
57
58
.map(proc.extract(_).asScala)
58
59
.map(_.headOption)
59
60
60
- // println(scriptConvert.size)
61
+ val scriptsValue = scripts.value
62
+ .map(script =>
63
+ os.Path (script.value)
64
+ )
61
65
62
- def modify (script : Option [UsingDirectives ]) = {
66
+ def modify (script : Option [UsingDirectives ]) =
63
67
script.toSeq.flatMap { directives =>
64
68
def toStrictValue (value : UsingValue ): Seq [Value [_]] = value match {
65
69
case uvs : UsingValues => uvs.values.asScala.toSeq.flatMap(toStrictValue)
@@ -79,6 +83,20 @@ object SourceGenerator {
79
83
case uds : UsingDefs => uds.getUsingDefs.asScala.toSeq.map(toStrictDirective)
80
84
case _ => Nil // There should be nothing else here other than UsingDefs
81
85
}
86
+
87
+ def replaceSpecialSyntax (directiveValue : String , path : os.Path ): String = {
88
+ val pattern = """ (((?:\$)+)(\{\.\}))""" .r
89
+ pattern.replaceAllIn(
90
+ directiveValue,
91
+ (m : Regex .Match ) => {
92
+ val dollarSigns = m.group(2 )
93
+ val dollars = " \\ $" * (dollarSigns.length / 2 )
94
+ if (dollarSigns.length % 2 == 0 )
95
+ s " $dollars${m.group(3 )}"
96
+ else
97
+ s " $dollars${path / os.up}"
98
+ }
99
+ )
82
100
}
83
101
84
102
val componentKeyword = Seq (" inputDirectory" , " glob" )
@@ -90,31 +108,26 @@ object SourceGenerator {
90
108
)
91
109
)
92
110
93
- // generatorComponents.map(f => f.map(g => println(g.values)))
111
+ val pathModifier = scriptsValue.iterator
94
112
val directive = generatorComponents.collect {
95
113
case Seq (inputDir, glob) =>
114
+ val relPath = pathModifier.next()
96
115
GeneratorConfig (
97
- inputDir.values.mkString,
116
+ replaceSpecialSyntax( inputDir.values.mkString, relPath) ,
98
117
List (glob.values.mkString),
99
118
scripts.value(0 ).value,
100
119
scripts.scopePath.subPath
101
120
)
102
121
}
103
122
104
- // val sourceGenValue = sourceGenerator.value
105
- // sourceGenValue
106
- // .map(config => GeneratorConfig.parse(config, sourceGenerator.scopePath.subPath))
107
- // .sequence
108
- // .left.map(CompositeBuildException(_))
109
- // .map { configs =>
110
- // BuildOptions(sourceGeneratorOptions =
111
- // SourceGeneratorOptions(generatorConfig = configs)
112
- // )
113
- // }
114
- // directive.map { f => println(f)}
123
+ val excludedGeneratorPath = excludeScripts.match {
124
+ case Some (true ) => scripts.value
125
+ case _ => List .empty[Positioned [String ]]
126
+ }
115
127
116
- Right (BuildOptions (sourceGeneratorOptions =
117
- SourceGeneratorOptions (generatorConfig = directive)
128
+ Right (BuildOptions (
129
+ sourceGeneratorOptions = SourceGeneratorOptions (generatorConfig = directive),
130
+ internal = InternalOptions (exclude = excludedGeneratorPath)
118
131
))
119
132
}
120
133
}
0 commit comments