From 281d6b4f13be0a04454872137898dbc897206766 Mon Sep 17 00:00:00 2001 From: Rizky Maulana <71255973+Perklone@users.noreply.github.com> Date: Wed, 31 Jul 2024 19:57:33 +0700 Subject: [PATCH] using new directive style --- .../DirectivesPreprocessor.scala | 3 +- .../directives/DirectiveValueParser.scala | 1 - .../directives/SourceGenerator.scala | 103 +++++++++++++++--- 3 files changed, 91 insertions(+), 16 deletions(-) diff --git a/modules/build/src/main/scala/scala/build/preprocessing/DirectivesPreprocessor.scala b/modules/build/src/main/scala/scala/build/preprocessing/DirectivesPreprocessor.scala index 3fda875977..0494bfd989 100644 --- a/modules/build/src/main/scala/scala/build/preprocessing/DirectivesPreprocessor.scala +++ b/modules/build/src/main/scala/scala/build/preprocessing/DirectivesPreprocessor.scala @@ -45,8 +45,6 @@ case class DirectivesPreprocessor( def preprocess(extractedDirectives: ExtractedDirectives) : Either[BuildException, PreprocessedDirectives] = either { val ExtractedDirectives(directives, directivesPositions) = extractedDirectives - println(s"DirectivesPreprocessor: $directives") - val ( buildOptionsWithoutRequirements: PartiallyProcessedDirectives[BuildOptions], buildOptionsWithTargetRequirements: PartiallyProcessedDirectives[ @@ -129,6 +127,7 @@ case class DirectivesPreprocessor( else if handler.isExperimental && !shouldSuppressExperimentalFeatures then logger.experimentalWarning(scopedDirective.directive.toString, FeatureType.Directive) + // println(scopedDirective.cwd) handler.handleValues(scopedDirective, logger) val handlersMap = handlers diff --git a/modules/directives/src/main/scala/scala/build/directives/DirectiveValueParser.scala b/modules/directives/src/main/scala/scala/build/directives/DirectiveValueParser.scala index c25465fcff..866653ecaa 100644 --- a/modules/directives/src/main/scala/scala/build/directives/DirectiveValueParser.scala +++ b/modules/directives/src/main/scala/scala/build/directives/DirectiveValueParser.scala @@ -138,7 +138,6 @@ object DirectiveValueParser { given DirectiveSingleValueParser[String] = (key, value, scopePath, path) => value.asString.toRight { - println("do we get here?") val pos = value.position(path) new MalformedDirectiveError( message = diff --git a/modules/directives/src/main/scala/scala/build/preprocessing/directives/SourceGenerator.scala b/modules/directives/src/main/scala/scala/build/preprocessing/directives/SourceGenerator.scala index a2d60ee183..92efcc1cd4 100644 --- a/modules/directives/src/main/scala/scala/build/preprocessing/directives/SourceGenerator.scala +++ b/modules/directives/src/main/scala/scala/build/preprocessing/directives/SourceGenerator.scala @@ -1,5 +1,17 @@ package scala.build.preprocessing.directives +// Virtuslab Processor +import com.virtuslab.using_directives.UsingDirectivesProcessor +import com.virtuslab.using_directives.custom.model.{ + BooleanValue, + EmptyValue, + StringValue, + UsingDirectives, + Value +} +import com.virtuslab.using_directives.custom.utils.ast._ +import scala.jdk.CollectionConverters.* + import scala.cli.commands.SpecificationLevel import scala.build.directives.* import scala.build.EitherCps.{either, value} @@ -8,31 +20,96 @@ import scala.build.errors.{BuildException, CompositeBuildException} import scala.build.options.{BuildOptions, SourceGeneratorOptions, GeneratorConfig} import scala.build.options.GeneratorConfig import scala.build.{Positioned, options} +import scala.build.directives.DirectiveValueParser.WithScopePath +@DirectiveGroupName("SourceGenerator") +@DirectivePrefix("sourceGenerator.") @DirectiveUsage("//> using sourceGenerator", "`//> using sourceGenerator`") @DirectiveDescription("Generate code using Source Generator") @DirectiveLevel(SpecificationLevel.EXPERIMENTAL) final case class SourceGenerator( - sourceGenerator: DirectiveValueParser.WithScopePath[List[Positioned[String]]] = - DirectiveValueParser.WithScopePath.empty(Nil) + testy: DirectiveValueParser.WithScopePath[List[Positioned[String]]] = + DirectiveValueParser.WithScopePath.empty(Nil), + scripts: DirectiveValueParser.WithScopePath[List[Positioned[String]]] = + DirectiveValueParser.WithScopePath.empty(Nil), + excludeScripts: Option[Boolean] = None, + inputDirectory: DirectiveValueParser.WithScopePath[Option[Positioned[String]]] = + DirectiveValueParser.WithScopePath.empty(None), + glob: Option[Positioned[String]] = None, + commandProcessor: DirectiveValueParser.WithScopePath[Option[Positioned[String]]] = + DirectiveValueParser.WithScopePath.empty(None), ) extends HasBuildOptions { def buildOptions: Either[BuildException, BuildOptions] = - SourceGenerator.buildOptions(sourceGenerator) + // println(s"ScopePath of Scripts: ${scripts.scopePath}") + // println(s"Values of Scripts: ${scripts.value(0).value}") + // println(s"Values of InputDir: ${inputDirectory.value}") + SourceGenerator.buildOptions(testy,scripts) } object SourceGenerator { val handler: DirectiveHandler[SourceGenerator] = DirectiveHandler.derive - def buildOptions(sourceGenerator: DirectiveValueParser.WithScopePath[List[Positioned[String]]]) - : Either[BuildException, BuildOptions] = { - val sourceGenValue = sourceGenerator.value - sourceGenValue - .map(config => GeneratorConfig.parse(config, sourceGenerator.scopePath.subPath)) - .sequence - .left.map(CompositeBuildException(_)) - .map { configs => - BuildOptions(sourceGeneratorOptions = - SourceGeneratorOptions(generatorConfig = configs) + def buildOptions( + sourceGenerator: DirectiveValueParser.WithScopePath[List[Positioned[String]]], + scripts: DirectiveValueParser.WithScopePath[List[Positioned[String]]] + ): Either[BuildException, BuildOptions] = { + val proc = UsingDirectivesProcessor() + val scriptConvert = scripts.value + .map(script => os.Path(script.value)) + .map(os.read(_)) + .map(_.toCharArray()) + .map(proc.extract(_).asScala) + .map(_.headOption) + + // println(scriptConvert.size) + + def modify(script: Option[UsingDirectives]) = { + script.toSeq.flatMap { directives => + def toStrictValue(value: UsingValue): Seq[Value[_]] = value match { + case uvs: UsingValues => uvs.values.asScala.toSeq.flatMap(toStrictValue) + case el: EmptyLiteral => Seq(EmptyValue(el)) + case sl: StringLiteral => Seq(StringValue(sl.getValue(), sl)) + case bl: BooleanLiteral => Seq(BooleanValue(bl.getValue(), bl)) + } + def toStrictDirective(ud: UsingDef) = StrictDirective( + ud.getKey(), + toStrictValue(ud.getValue()), + ud.getPosition().getColumn() ) + + // println(directives.getAst()) + + directives.getAst match + case uds: UsingDefs => uds.getUsingDefs.asScala.toSeq.map(toStrictDirective) + case _ => Nil // There should be nothing else here other than UsingDefs } + } + + val strictDirectives = scriptConvert + .map(modify(_)) + .map(_.take(3)) + + // println(scripts.scopePath.subPath) + // strictDirectives.map(f => f.map(w => println(w))) + + val directive = strictDirectives.collect { + case Seq(inputDir, glob, processor) => + GeneratorConfig(inputDir.values.mkString, List(glob.values.mkString), processor.values.mkString.split(" ").toList,scripts.scopePath.subPath) + } + println(directive.size) + + // val sourceGenValue = sourceGenerator.value + // sourceGenValue + // .map(config => GeneratorConfig.parse(config, sourceGenerator.scopePath.subPath)) + // .sequence + // .left.map(CompositeBuildException(_)) + // .map { configs => + // BuildOptions(sourceGeneratorOptions = + // SourceGeneratorOptions(generatorConfig = configs) + // ) + // } + + // directive.map { f => println(f)} + + Right(BuildOptions(sourceGeneratorOptions = SourceGeneratorOptions(generatorConfig = directive))) } }