Skip to content

Commit

Permalink
NIT: Refactor DirectivePreprocessor, contain common arguments in clas…
Browse files Browse the repository at this point in the history
…s' scope
  • Loading branch information
MaciejG604 committed Aug 31, 2023
1 parent dda7948 commit d1436d4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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
)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ 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,
allowRestrictedFeatures,
suppressWarningOptions,
maybeRecoverOnError
)
.preprocess(content)
}
Seq(PreprocessedSource.OnDisk(
path = j.path,
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d1436d4

Please sign in to comment.