Skip to content

Commit

Permalink
Merge pull request #2310 from MaciejG604/build-info-with-compute-version
Browse files Browse the repository at this point in the history
Build info with compute version
  • Loading branch information
lwronski authored Aug 8, 2023
2 parents 626c920 + 37d25da commit 2443d24
Show file tree
Hide file tree
Showing 41 changed files with 419 additions and 210 deletions.
4 changes: 2 additions & 2 deletions modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ object Build {

val scopedSources = value(crossSources.scopedSources(baseOptions))

val mainSources = scopedSources.sources(Scope.Main, baseOptions)
val mainSources = value(scopedSources.sources(Scope.Main, baseOptions, allInputs.workspace))
val mainOptions = mainSources.buildOptions

val testSources = scopedSources.sources(Scope.Test, baseOptions)
val testSources = value(scopedSources.sources(Scope.Test, baseOptions, allInputs.workspace))
val testOptions = testSources.buildOptions

val inputs0 = updateInputs(
Expand Down
58 changes: 35 additions & 23 deletions modules/build/src/main/scala/scala/build/ScopedSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package scala.build

import java.nio.charset.StandardCharsets

import scala.build.EitherCps.{either, value}
import scala.build.errors.BuildException
import scala.build.info.{BuildInfo, ScopedBuildInfo}
import scala.build.options.{BuildOptions, HasScope, Scope}
import scala.build.preprocessing.ScriptPreprocessor
Expand Down Expand Up @@ -50,7 +52,11 @@ final case class ScopedSources(
* @return
* [[Sources]] instance that belong to specified scope
*/
def sources(scope: Scope, baseOptions: BuildOptions): Sources =
def sources(
scope: Scope,
baseOptions: BuildOptions,
workspace: os.Path
): Either[BuildException, Sources] = either {
val combinedOptions = combinedBuildOptions(scope, baseOptions)

val codeWrapper = ScriptPreprocessor.getScriptWrapper(combinedOptions)
Expand All @@ -62,12 +68,16 @@ final case class ScopedSources(
val needsBuildInfo = combinedOptions.sourceGeneratorOptions.useBuildInfo.getOrElse(false)

val maybeBuildInfoSource = if (needsBuildInfo && scope == Scope.Main)
Seq(Sources.InMemory(
Left("build-info"),
os.rel / "BuildInfo.scala",
buildInfo(combinedOptions).generateContents().getBytes(StandardCharsets.UTF_8),
None
))
Seq(
Sources.InMemory(
Left("build-info"),
os.rel / "BuildInfo.scala",
value(buildInfo(combinedOptions, workspace)).generateContents().getBytes(
StandardCharsets.UTF_8
),
None
)
)
else Nil

Sources(
Expand All @@ -77,6 +87,7 @@ final case class ScopedSources(
resourceDirs.flatMap(_.valueFor(scope).toSeq),
combinedOptions
)
}

/** Combine build options that had no requirements (console and using directives) or their
* requirements have been resolved (e.g. target using directives) with build options that require
Expand All @@ -94,24 +105,25 @@ final case class ScopedSources(
buildOptionsFor(scope)
.foldRight(baseOptions)(_ orElse _)

def buildInfo(baseOptions: BuildOptions): BuildInfo = {
def getScopedBuildInfo(scope: Scope): ScopedBuildInfo =
val combinedOptions = combinedBuildOptions(scope, baseOptions)
val sourcePaths = paths.flatMap(_.valueFor(scope).toSeq).map(_._1.toString)
val inMemoryPaths =
(inMemory.flatMap(_.valueFor(scope).toSeq).flatMap(_.originalPath.toOption) ++
unwrappedScripts.flatMap(_.valueFor(scope).toSeq).flatMap(_.originalPath.toOption))
.map(_._2.toString)
def buildInfo(baseOptions: BuildOptions, workspace: os.Path): Either[BuildException, BuildInfo] =
either {
def getScopedBuildInfo(scope: Scope): ScopedBuildInfo =
val combinedOptions = combinedBuildOptions(scope, baseOptions)
val sourcePaths = paths.flatMap(_.valueFor(scope).toSeq).map(_._1.toString)
val inMemoryPaths =
(inMemory.flatMap(_.valueFor(scope).toSeq).flatMap(_.originalPath.toOption) ++
unwrappedScripts.flatMap(_.valueFor(scope).toSeq).flatMap(_.originalPath.toOption))
.map(_._2.toString)

ScopedBuildInfo(combinedOptions, sourcePaths ++ inMemoryPaths)
ScopedBuildInfo(combinedOptions, sourcePaths ++ inMemoryPaths)

val baseBuildInfo = BuildInfo(combinedBuildOptions(Scope.Main, baseOptions))
val baseBuildInfo = value(BuildInfo(combinedBuildOptions(Scope.Main, baseOptions), workspace))

val mainBuildInfo = getScopedBuildInfo(Scope.Main)
val testBuildInfo = getScopedBuildInfo(Scope.Test)
val mainBuildInfo = getScopedBuildInfo(Scope.Main)
val testBuildInfo = getScopedBuildInfo(Scope.Test)

baseBuildInfo
.withScope(Scope.Main.name, mainBuildInfo)
.withScope(Scope.Test.name, testBuildInfo)
}
baseBuildInfo
.withScope(Scope.Main.name, mainBuildInfo)
.withScope(Scope.Test.name, testBuildInfo)
}
}
13 changes: 9 additions & 4 deletions modules/build/src/main/scala/scala/build/bsp/BspImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,15 @@ final class BspImpl(
if (verbosity >= 3)
pprint.err.log(scopedSources)

val sourcesMain =
scopedSources.sources(Scope.Main, sharedOptions)
val sourcesTest =
scopedSources.sources(Scope.Test, sharedOptions)
val sourcesMain = value {
scopedSources.sources(Scope.Main, sharedOptions, allInputs.workspace)
.left.map((_, Scope.Main))
}

val sourcesTest = value {
scopedSources.sources(Scope.Test, sharedOptions, allInputs.workspace)
.left.map((_, Scope.Test))
}

if (verbosity >= 3)
pprint.err.log(sourcesMain)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object DirectivesPreprocessingUtils {
val usingDirectiveHandlers: Seq[DirectiveHandler[BuildOptions]] =
Seq[DirectiveHandler[_ <: HasBuildOptions]](
directives.BuildInfo.handler,
directives.ComputeVersion.handler,
directives.Exclude.handler,
directives.JavaHome.handler,
directives.Jvm.handler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.eed3si9n.expecty.Expecty.expect
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.Constants

import scala.build.options.ComputeVersion
import scala.build.tests.{TestInputs, TestUtil}

class ComputeVersionTests extends munit.FunSuite {
Expand Down
24 changes: 16 additions & 8 deletions modules/build/src/test/scala/scala/build/tests/ExcludeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ExcludeTests extends munit.FunSuite {
os.rel / "project.scala" ->
s"""//> using exclude "Main.scala" """
)
testInputs.withInputs { (_, inputs) =>
testInputs.withInputs { (root, inputs) =>
val (crossSources, _) =
CrossSources.forInputs(
inputs,
Expand All @@ -96,7 +96,9 @@ class ExcludeTests extends munit.FunSuite {
)(using ScalaCliInvokeData.dummy).orThrow
val scopedSources = crossSources.scopedSources(BuildOptions())
.orThrow
val sources = scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions()))
val sources =
scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions()), root)
.orThrow

expect(sources.paths.nonEmpty)
expect(sources.paths.length == 2)
Expand All @@ -113,7 +115,7 @@ class ExcludeTests extends munit.FunSuite {
os.rel / "project.scala" ->
s"""//> using exclude "$${.}${File.separator}Main.scala" """
)
testInputs.withInputs { (_, inputs) =>
testInputs.withInputs { (root, inputs) =>
val (crossSources, _) =
CrossSources.forInputs(
inputs,
Expand All @@ -123,7 +125,9 @@ class ExcludeTests extends munit.FunSuite {
)(using ScalaCliInvokeData.dummy).orThrow
val scopedSources = crossSources.scopedSources(BuildOptions())
.orThrow
val sources = scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions()))
val sources =
scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions()), root)
.orThrow

expect(sources.paths.nonEmpty)
expect(sources.paths.length == 2)
Expand All @@ -140,7 +144,7 @@ class ExcludeTests extends munit.FunSuite {
os.rel / "project.scala" ->
"""//> using exclude "src/*.scala" """
)
testInputs.withInputs { (_, inputs) =>
testInputs.withInputs { (root, inputs) =>
val (crossSources, _) =
CrossSources.forInputs(
inputs,
Expand All @@ -150,7 +154,9 @@ class ExcludeTests extends munit.FunSuite {
)(using ScalaCliInvokeData.dummy).orThrow
val scopedSources = crossSources.scopedSources(BuildOptions())
.orThrow
val sources = scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions()))
val sources =
scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions()), root)
.orThrow

expect(sources.paths.nonEmpty)
expect(sources.paths.length == 2)
Expand All @@ -167,7 +173,7 @@ class ExcludeTests extends munit.FunSuite {
os.rel / "project.scala" ->
"""//> using exclude "src/*.scala" """
)
testInputs.withInputs { (_, inputs) =>
testInputs.withInputs { (root, inputs) =>
val (crossSources, _) =
CrossSources.forInputs(
inputs,
Expand All @@ -177,7 +183,9 @@ class ExcludeTests extends munit.FunSuite {
)(using ScalaCliInvokeData.dummy).orThrow
val scopedSources = crossSources.scopedSources(BuildOptions())
.orThrow
val sources = scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions()))
val sources =
scopedSources.sources(Scope.Main, crossSources.sharedOptions(BuildOptions()), root)
.orThrow

expect(sources.paths.nonEmpty)
expect(sources.paths.length == 2)
Expand Down
Loading

0 comments on commit 2443d24

Please sign in to comment.