Skip to content

Commit

Permalink
Merge pull request #2046 from Gedochao/test-scope-deps-from-main
Browse files Browse the repository at this point in the history
Support declaring some test directives from the main scope
  • Loading branch information
Gedochao authored Apr 25, 2023
2 parents df627a7 + c484b03 commit 2c57665
Show file tree
Hide file tree
Showing 27 changed files with 743 additions and 224 deletions.
32 changes: 17 additions & 15 deletions modules/build/src/main/scala/scala/build/CrossSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,19 @@ object CrossSources {
.left.map(CompositeBuildException(_))
.map(_.flatten)

val preprocessedInputFromArgs = value(preprocessSources(inputs.flattened()))
val preprocessedInputFromArgs: Seq[PreprocessedSource] =
value(preprocessSources(inputs.flattened()))

val sourcesFromDirectives =
preprocessedInputFromArgs
.flatMap(_.options)
.flatMap(_.internal.extraSourceFiles)
.distinct
val inputsElemFromDirectives =
val inputsElemFromDirectives: Seq[SingleFile] =
value(resolveInputsFromSources(sourcesFromDirectives, inputs.enableMarkdown))
val preprocessedSourcesFromDirectives = value(preprocessSources(inputsElemFromDirectives))
val allInputs = inputs.add(inputsElemFromDirectives)
val preprocessedSourcesFromDirectives: Seq[PreprocessedSource] =
value(preprocessSources(inputsElemFromDirectives))
val allInputs = inputs.add(inputsElemFromDirectives)

val preprocessedSources =
(preprocessedInputFromArgs ++ preprocessedSourcesFromDirectives).distinct
Expand All @@ -190,19 +192,19 @@ object CrossSources {
else fromDirectives
}

val buildOptions = for {
s <- preprocessedSources
opt <- s.options.toSeq
if opt != BuildOptions()
val buildOptions: Seq[WithBuildRequirements[BuildOptions]] = (for {
preprocessedSource <- preprocessedSources
opts <- preprocessedSource.options.toSeq
if opts != BuildOptions() || preprocessedSource.optionsWithTargetRequirements.nonEmpty
} yield {
val baseReqs0 = baseReqs(s.scopePath)
WithBuildRequirements(
s.requirements.fold(baseReqs0)(_ orElse baseReqs0),
opt
val baseReqs0 = baseReqs(preprocessedSource.scopePath)
preprocessedSource.optionsWithTargetRequirements :+ WithBuildRequirements(
preprocessedSource.requirements.fold(baseReqs0)(_ orElse baseReqs0),
opts
)
}
}).flatten

val defaultMainClassOpt = for {
val defaultMainClassOpt: Option[String] = for {
mainClassPath <- allInputs.defaultMainClassElement
.map(s => ScopePath.fromPath(s.path).subPath)
processedMainClass <- preprocessedSources.find(_.scopePath.subPath == mainClassPath)
Expand Down Expand Up @@ -230,7 +232,7 @@ object CrossSources {
) -> m.directivesPositions
}

val resourceDirs = allInputs.elements.collect {
val resourceDirs: Seq[WithBuildRequirements[os.Path]] = allInputs.elements.collect {
case r: ResourceDirectory =>
WithBuildRequirements(BuildRequirements(), r.path)
} ++ preprocessedSources.flatMap(_.options).flatMap(_.classPathOptions.resourcesDir).map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import scala.build.EitherCps.{either, value}
import scala.build.Logger
import scala.build.errors.BuildException
import scala.build.input.{Inputs, SingleElement, VirtualData}
import scala.build.options.{BuildRequirements, SuppressWarningOptions}
import scala.build.options.{
BuildOptions,
BuildRequirements,
SuppressWarningOptions,
WithBuildRequirements
}
import scala.build.preprocessing.DirectivesProcessor.DirectivesProcessorOutput
import scala.build.preprocessing.PreprocessingUtil.optionsAndPositionsFromDirectives

case object DataPreprocessor extends Preprocessor {
Expand All @@ -21,7 +27,11 @@ case object DataPreprocessor extends Preprocessor {
case file: VirtualData =>
val res = either {
val content = new String(file.content, StandardCharsets.UTF_8)
val (updatedOptions, directivesPositions) = value {
val (
updatedOptions: BuildOptions,
optsWithReqs: List[WithBuildRequirements[BuildOptions]],
directivesPositions: Option[DirectivesPositions]
) = value {
optionsAndPositionsFromDirectives(
content,
file.scopePath,
Expand All @@ -35,15 +45,16 @@ case object DataPreprocessor extends Preprocessor {

val inMemory = Seq(
PreprocessedSource.InMemory(
Left(file.source),
file.subPath,
content,
0,
Some(updatedOptions.global),
Some(BuildRequirements()),
Nil,
None,
file.scopePath,
originalPath = Left(file.source),
relPath = file.subPath,
code = content,
ignoreLen = 0,
options = Some(updatedOptions),
optionsWithTargetRequirements = optsWithReqs,
requirements = Some(BuildRequirements()),
scopedRequirements = Nil,
mainClassOpt = None,
scopePath = file.scopePath,
directivesPositions
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import scala.build.options.{
ClassPathOptions,
SuppressWarningOptions
}
import scala.build.preprocessing.PreprocessingUtil.optionsAndPositionsFromDirectives

case object JarPreprocessor extends Preprocessor {
def preprocess(
Expand All @@ -30,12 +29,13 @@ case object JarPreprocessor extends Preprocessor {
)
)
Seq(PreprocessedSource.OnDisk(
jar.path,
Some(buildOptions),
Some(BuildRequirements()),
Nil,
None,
None
path = jar.path,
options = Some(buildOptions),
optionsWithTargetRequirements = List.empty,
requirements = Some(BuildRequirements()),
scopedRequirements = Nil,
mainClassOpt = None,
directivesPositions = None
))
})
case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import scala.build.Logger
import scala.build.errors.BuildException
import scala.build.input.{Inputs, JavaFile, SingleElement, VirtualJavaFile}
import scala.build.internal.JavaParserProxyMaker
import scala.build.options.{BuildRequirements, SuppressWarningOptions}
import scala.build.options.{
BuildOptions,
BuildRequirements,
SuppressWarningOptions,
WithBuildRequirements
}
import scala.build.preprocessing.DirectivesProcessor.DirectivesProcessorOutput
import scala.build.preprocessing.ExtractedDirectives.from
import scala.build.preprocessing.PreprocessingUtil.optionsAndPositionsFromDirectives
import scala.build.preprocessing.ScalaPreprocessor.*
Expand Down Expand Up @@ -44,7 +50,11 @@ final case class JavaPreprocessor(
case j: JavaFile => Some(either {
val content = value(PreprocessingUtil.maybeRead(j.path))
val scopePath = ScopePath.fromPath(j.path)
val (updatedOptions, directivesPositions) = value {
val (
updatedOptions: BuildOptions,
optsWithReqs: List[WithBuildRequirements[BuildOptions]],
directivesPositions: Option[DirectivesPositions]
) = value {
optionsAndPositionsFromDirectives(
content,
scopePath,
Expand All @@ -56,12 +66,13 @@ final case class JavaPreprocessor(
)
}
Seq(PreprocessedSource.OnDisk(
j.path,
Some(updatedOptions.global),
Some(BuildRequirements()),
Nil,
None,
directivesPositions
path = j.path,
options = Some(updatedOptions),
optionsWithTargetRequirements = optsWithReqs,
requirements = Some(BuildRequirements()),
scopedRequirements = Nil,
mainClassOpt = None,
directivesPositions = directivesPositions
))
})
case v: VirtualJavaFile =>
Expand All @@ -85,7 +96,11 @@ final case class JavaPreprocessor(
}
else v.subPath
val content = new String(v.content, StandardCharsets.UTF_8)
val (updatedOptions, directivesPositions) = value {
val (
updatedOptions: BuildOptions,
optsWithReqs: List[WithBuildRequirements[BuildOptions]],
directivesPositions: Option[DirectivesPositions]
) = value {
optionsAndPositionsFromDirectives(
content,
v.scopePath,
Expand All @@ -101,7 +116,8 @@ final case class JavaPreprocessor(
relPath = relPath,
code = content,
ignoreLen = 0,
options = Some(updatedOptions.global),
options = Some(updatedOptions),
optionsWithTargetRequirements = optsWithReqs,
requirements = Some(BuildRequirements()),
scopedRequirements = Nil,
mainClassOpt = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ case object MarkdownPreprocessor extends Preprocessor {
allowRestrictedFeatures = allowRestrictedFeatures,
suppressWarningOptions = suppressWarningOptions
)
}.getOrElse(ProcessingOutput(BuildRequirements(), Nil, BuildOptions(), None, None))
}.getOrElse(ProcessingOutput.empty)
val processedCode = processingOutput.updatedContent.getOrElse(wrappedMarkdown.code)
PreprocessedSource.InMemory(
originalPath = reportingPath.map(subPath -> _),
relPath = os.rel / (subPath / os.up) / s"${subPath.last}$generatedSourceNameSuffix",
processedCode,
ignoreLen = 0,
options = Some(processingOutput.opts),
optionsWithTargetRequirements = processingOutput.optsWithReqs,
requirements = Some(processingOutput.globalReqs),
processingOutput.scopedReqs,
mainClassOpt = None,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package scala.build.preprocessing

import scala.build.options.{BuildOptions, BuildRequirements}
import scala.build.options.{BuildOptions, BuildRequirements, WithBuildRequirements}

sealed abstract class PreprocessedSource extends Product with Serializable {
def options: Option[BuildOptions]
def optionsWithTargetRequirements: List[WithBuildRequirements[BuildOptions]]
def requirements: Option[BuildRequirements]
def mainClassOpt: Option[String]

Expand All @@ -17,6 +18,7 @@ object PreprocessedSource {
final case class OnDisk(
path: os.Path,
options: Option[BuildOptions],
optionsWithTargetRequirements: List[WithBuildRequirements[BuildOptions]],
requirements: Option[BuildRequirements],
scopedRequirements: Seq[Scoped[BuildRequirements]],
mainClassOpt: Option[String],
Expand All @@ -31,6 +33,7 @@ object PreprocessedSource {
code: String,
ignoreLen: Int,
options: Option[BuildOptions],
optionsWithTargetRequirements: List[WithBuildRequirements[BuildOptions]],
requirements: Option[BuildRequirements],
scopedRequirements: Seq[Scoped[BuildRequirements]],
mainClassOpt: Option[String],
Expand All @@ -42,6 +45,7 @@ object PreprocessedSource {
}
final case class NoSourceCode(
options: Option[BuildOptions],
optionsWithTargetRequirements: List[WithBuildRequirements[BuildOptions]],
requirements: Option[BuildRequirements],
scopedRequirements: Seq[Scoped[BuildRequirements]],
path: os.Path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.nio.charset.StandardCharsets
import scala.build.EitherCps.{either, value}
import scala.build.Logger
import scala.build.errors.{BuildException, FileNotFoundException}
import scala.build.options.{BuildOptions, SuppressWarningOptions}
import scala.build.options.{BuildOptions, SuppressWarningOptions, WithBuildRequirements}
import scala.build.preprocessing.DirectivesProcessor.DirectivesProcessorOutput
import scala.build.preprocessing.ExtractedDirectives.from
import scala.build.preprocessing.ScalaPreprocessor.*
Expand All @@ -30,7 +30,11 @@ object PreprocessingUtil {
suppressWarningOptions: SuppressWarningOptions
): Either[
BuildException,
(DirectivesProcessorOutput[BuildOptions], Option[DirectivesPositions])
(
BuildOptions,
List[WithBuildRequirements[BuildOptions]],
Option[DirectivesPositions]
)
] = either {
val ExtractedDirectives(directives0, directivesPositions) =
value(from(
Expand All @@ -40,7 +44,7 @@ object PreprocessingUtil {
scopePath,
maybeRecoverOnError
))
val updatedOptions = value(DirectivesProcessor.process(
val updatedOptions: DirectivesProcessorOutput[BuildOptions] = value(DirectivesProcessor.process(
directives0,
usingDirectiveHandlers,
path,
Expand All @@ -49,6 +53,26 @@ object PreprocessingUtil {
allowRestrictedFeatures,
suppressWarningOptions
))
(updatedOptions, directivesPositions)

val directives1 = updatedOptions.unused
val optionsWithReqs: DirectivesProcessorOutput[List[WithBuildRequirements[BuildOptions]]] =
value(DirectivesProcessor.process(
directives1,
usingDirectiveWithReqsHandlers,
path,
scopePath,
logger,
allowRestrictedFeatures,
suppressWarningOptions
))

val (optionsWithActualRequirements, optionsWithEmptyRequirements) =
optionsWithReqs.global.partition(_.requirements.nonEmpty)
val summedOptionsWithNoRequirements =
optionsWithEmptyRequirements
.map(_.value)
.foldLeft(updatedOptions.global)((acc, bo) => acc.orElse(bo))

(summedOptionsWithNoRequirements, optionsWithActualRequirements, directivesPositions)
}
}
Loading

0 comments on commit 2c57665

Please sign in to comment.