From d1436d4e8e255317bedb57e96fe882dd0f82f110 Mon Sep 17 00:00:00 2001 From: Maciej Gajek Date: Mon, 21 Aug 2023 16:49:24 +0200 Subject: [PATCH] NIT: Refactor DirectivePreprocessor, contain common arguments in class' scope --- .../DirectivesPreprocessor.scala | 80 +++++-------------- .../preprocessing/JavaPreprocessor.scala | 9 ++- .../preprocessing/ScalaPreprocessor.scala | 5 +- 3 files changed, 29 insertions(+), 65 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 1b48e4732e..d94bc5a666 100644 --- a/modules/build/src/main/scala/scala/build/preprocessing/DirectivesPreprocessor.scala +++ b/modules/build/src/main/scala/scala/build/preprocessing/DirectivesPreprocessor.scala @@ -27,56 +27,24 @@ import scala.build.preprocessing.directives.DirectivesPreprocessingUtils.* import scala.build.preprocessing.directives.PartiallyProcessedDirectives.* import scala.build.preprocessing.directives.* -object DirectivesPreprocessor { - def preprocess( - content: String, - path: Either[String, os.Path], - cwd: ScopePath, - logger: Logger, - allowRestrictedFeatures: Boolean, - suppressWarningOptions: SuppressWarningOptions, - maybeRecoverOnError: BuildException => Option[BuildException] - )(using ScalaCliInvokeData): Either[BuildException, PreprocessedDirectives] = either { - val directives = value { - ExtractedDirectives.from(content.toCharArray, path, logger, maybeRecoverOnError) - } - value { - preprocess( - directives, - path, - cwd, - logger, - allowRestrictedFeatures, - suppressWarningOptions, - maybeRecoverOnError - ) - } - } +case class DirectivesPreprocessor( + path: Either[String, os.Path], + cwd: ScopePath, + logger: Logger, + allowRestrictedFeatures: Boolean, + suppressWarningOptions: SuppressWarningOptions, + maybeRecoverOnError: BuildException => Option[BuildException] +)( + using ScalaCliInvokeData +) { + def preprocess(content: String): Either[BuildException, PreprocessedDirectives] = for { + directives <- ExtractedDirectives.from(content.toCharArray, path, logger, maybeRecoverOnError) + res <- preprocess(directives) + } yield res - def preprocess( - extractedDirectives: ExtractedDirectives, - path: Either[String, os.Path], - cwd: ScopePath, - logger: Logger, - allowRestrictedFeatures: Boolean, - suppressWarningOptions: SuppressWarningOptions, - maybeRecoverOnError: BuildException => Option[BuildException] - )(using ScalaCliInvokeData): Either[BuildException, PreprocessedDirectives] = either { + def preprocess(extractedDirectives: ExtractedDirectives) + : Either[BuildException, PreprocessedDirectives] = either { val ExtractedDirectives(directives, directivesPositions) = extractedDirectives - def preprocessWithDirectiveHandlers[T: ConfigMonoid]( - remainingDirectives: Seq[StrictDirective], - directiveHandlers: Seq[DirectiveHandler[T]] - ): Either[BuildException, PartiallyProcessedDirectives[T]] = - applyDirectiveHandlers( - remainingDirectives, - directiveHandlers, - path, - cwd, - logger, - allowRestrictedFeatures, - suppressWarningOptions, - maybeRecoverOnError - ) val ( buildOptionsWithoutRequirements: PartiallyProcessedDirectives[BuildOptions], @@ -88,16 +56,16 @@ object DirectivesPreprocessor { ) = value { for { regularUsingDirectives: PartiallyProcessedDirectives[BuildOptions] <- - preprocessWithDirectiveHandlers(directives, usingDirectiveHandlers) + applyDirectiveHandlers(directives, usingDirectiveHandlers) usingDirectivesWithRequirements: PartiallyProcessedDirectives[ List[WithBuildRequirements[BuildOptions]] ] <- - preprocessWithDirectiveHandlers( + applyDirectiveHandlers( regularUsingDirectives.unused, usingDirectiveWithReqsHandlers ) targetDirectives: PartiallyProcessedDirectives[BuildRequirements] <- - preprocessWithDirectiveHandlers( + applyDirectiveHandlers( usingDirectivesWithRequirements.unused, requireDirectiveHandlers ) @@ -142,14 +110,8 @@ object DirectivesPreprocessor { private def applyDirectiveHandlers[T: ConfigMonoid]( directives: Seq[StrictDirective], - handlers: Seq[DirectiveHandler[T]], - path: Either[String, os.Path], - cwd: ScopePath, - logger: Logger, - allowRestrictedFeatures: Boolean, - suppressWarningOptions: SuppressWarningOptions, - maybeRecoverOnError: BuildException => Option[BuildException] = e => Some(e) - )(using ScalaCliInvokeData): Either[BuildException, PartiallyProcessedDirectives[T]] = { + handlers: Seq[DirectiveHandler[T]] + ): Either[BuildException, PartiallyProcessedDirectives[T]] = { val configMonoidInstance = implicitly[ConfigMonoid[T]] val shouldSuppressExperimentalFeatures = suppressWarningOptions.suppressExperimentalFeatureWarning.getOrElse(false) diff --git a/modules/build/src/main/scala/scala/build/preprocessing/JavaPreprocessor.scala b/modules/build/src/main/scala/scala/build/preprocessing/JavaPreprocessor.scala index 876819c15a..8dbdcf7d8d 100644 --- a/modules/build/src/main/scala/scala/build/preprocessing/JavaPreprocessor.scala +++ b/modules/build/src/main/scala/scala/build/preprocessing/JavaPreprocessor.scala @@ -47,8 +47,7 @@ final case class JavaPreprocessor( val content: String = value(PreprocessingUtil.maybeRead(j.path)) val scopePath = ScopePath.fromPath(j.path) val preprocessedDirectives: PreprocessedDirectives = value { - DirectivesPreprocessor.preprocess( - content, + DirectivesPreprocessor( Right(j.path), scopePath, logger, @@ -56,6 +55,7 @@ final case class JavaPreprocessor( suppressWarningOptions, maybeRecoverOnError ) + .preprocess(content) } Seq(PreprocessedSource.OnDisk( path = j.path, @@ -89,14 +89,15 @@ final case class JavaPreprocessor( else v.subPath val content = new String(v.content, StandardCharsets.UTF_8) val preprocessedDirectives: PreprocessedDirectives = value { - DirectivesPreprocessor.preprocess( - content, + DirectivesPreprocessor( Left(relPath.toString), v.scopePath, logger, allowRestrictedFeatures, suppressWarningOptions, maybeRecoverOnError + ).preprocess( + content ) } val s = PreprocessedSource.InMemory( diff --git a/modules/build/src/main/scala/scala/build/preprocessing/ScalaPreprocessor.scala b/modules/build/src/main/scala/scala/build/preprocessing/ScalaPreprocessor.scala index ea4c01e042..a46e12c212 100644 --- a/modules/build/src/main/scala/scala/build/preprocessing/ScalaPreprocessor.scala +++ b/modules/build/src/main/scala/scala/build/preprocessing/ScalaPreprocessor.scala @@ -221,14 +221,15 @@ case object ScalaPreprocessor extends Preprocessor { )(using ScalaCliInvokeData): Either[BuildException, Option[ProcessingOutput]] = either { val (content0, isSheBang) = SheBang.ignoreSheBangLines(content) val preprocessedDirectives: PreprocessedDirectives = - value(DirectivesPreprocessor.preprocess( - extractedDirectives, + value(DirectivesPreprocessor( path, scopeRoot, logger, allowRestrictedFeatures, suppressWarningOptions, maybeRecoverOnError + ).preprocess( + extractedDirectives )) if (preprocessedDirectives.isEmpty) None