Skip to content

Commit

Permalink
fix: move resource generation out from onLoad
Browse files Browse the repository at this point in the history
  • Loading branch information
dos65 committed Apr 1, 2022
1 parent 1fc97fa commit 687516d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
8 changes: 7 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ lazy val frontend: Project = project
ownProductDirectories ++ dependencyClasspath
}
),
Test / resources := {
val main = (Test / resources).value
val dir = (ThisBuild / baseDirectory).value
val log = streams.value
BuildDefaults.exportProjectsInTestResources(dir, log.log, enableCache = true)
main
},
(Test / unmanagedResources / includeFilter) := {
new FileFilter {
def accept(file: File): Boolean = {
Expand Down Expand Up @@ -942,7 +949,6 @@ val bloop = project
releaseEarly := { () },
(publish / skip) := true,
crossSbtVersions := Seq(Sbt1Version, Sbt013Version),
commands += BuildDefaults.exportProjectsInTestResourcesCmd,
buildIntegrationsBase := (ThisBuild / Keys.baseDirectory).value / "build-integrations",
twitterDodo := buildIntegrationsBase.value./("build-twitter"),
publishLocalAllModules := {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ object App {
println("Allowed application was compiled successfully v3.0")
}
}

44 changes: 20 additions & 24 deletions project/BuildPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import ch.epfl.scala.sbt.release.ReleaseEarlyPlugin.{autoImport => ReleaseEarlyK
import sbt.internal.BuildLoader
import sbt.librarymanagement.MavenRepository
import build.BloopShadingPlugin.{autoImport => BloopShadingKeys}
import sbt.util.Logger

object BuildPlugin extends AutoPlugin {
import sbt.plugins.JvmPlugin
Expand Down Expand Up @@ -322,7 +323,7 @@ object BuildImplementation {
BuildKeys.schemaVersion := "4.2-refresh-3",
(Test / Keys.testOptions) += sbt.Tests.Argument("-oD"),
Keys.onLoadMessage := Header.intro,
Keys.onLoad := BuildDefaults.bloopOnLoad.value,
//Keys.onLoad := BuildDefaults.bloopOnLoad.value,
(Test / Keys.publishArtifact) := false
)

Expand Down Expand Up @@ -426,27 +427,20 @@ object BuildImplementation {
}
}

/**
* This onLoad hook will clone any repository required for the build tool integration tests.
* In this case, we clone kafka so that the gradle plugin unit tests can access to its directory.
*/
val bloopOnLoad: Def.Initialize[State => State] = Def.setting {
Keys.onLoad.value.andThen { state =>
if (sys.env.isDefinedAt("SKIP_TEST_RESOURCES_GENERATION")) state
else exportProjectsInTestResources(state, enableCache = true)
}
}

def exportProjectsInTestResources(state: State, enableCache: Boolean): State = {
def exportProjectsInTestResources(
baseDir: File,
log: Logger,
enableCache: Boolean
): Seq[File] = {
import java.util.Locale
val isWindows: Boolean =
System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows")

// Generate bloop configuration files for projects we use in our test suite upfront
val resourcesDir = state.baseDir / "frontend" / "src" / "test" / "resources"
val pluginSourceDir = state.baseDir / "integrations" / "sbt-bloop" / "src" / "main"
val resourcesDir = baseDir / "frontend" / "src" / "test" / "resources"
val pluginSourceDir = baseDir / "integrations" / "sbt-bloop" / "src" / "main"
val projectDirs = resourcesDir.listFiles().filter(_.isDirectory)
projectDirs.foreach { projectDir =>
val out = projectDirs.flatMap { projectDir =>
val targetDir = projectDir / "target"
val cacheDirectory = targetDir / "generation-cache-dir"
if (sys.env.isDefinedAt("FORCE_TEST_RESOURCES_GENERATION"))
Expand All @@ -471,7 +465,7 @@ object BuildImplementation {
import scala.sys.process.Process

val generate = { (changedFiles: Set[File]) =>
state.log.info(s"Generating bloop configuration files for ${projectDir}")
log.info(s"Generating bloop configuration files for ${projectDir}")
val cmd = {
val isGithubAction = sys.env.get("GITHUB_WORKFLOW").nonEmpty
if (isWindows && isGithubAction) "sh" :: "-c" :: "sbt bloopInstall" :: Nil
Expand All @@ -483,7 +477,7 @@ object BuildImplementation {
throw new sbt.MessageOnlyException(
s"Failed to generate bloop config for ${projectDir}."
)
state.log.success(s"Generated bloop configuration files for ${projectDir}")
log.success(s"Generated bloop configuration files for ${projectDir}")
changedFiles
}

Expand All @@ -494,14 +488,16 @@ object BuildImplementation {

cached(projectsFiles ++ pluginFiles)
} else generate(Set.empty)
}

state
}.map(f => f.toString -> f)
.toMap
.values
.toList
println("~~~~~~~~~~~~")
println(out.mkString("\n"))
println("~~~~~~~~~~~~")
out
}

val exportProjectsInTestResourcesCmd: sbt.Command =
sbt.Command.command("exportProjectsInTestResources")(exportProjectsInTestResources(_, false))

def getStagingDirectory(state: State): File = {
// Use the default staging directory, we don't care if the user changed it.
val globalBase = sbt.BuildPaths.getGlobalBase(state)
Expand Down

0 comments on commit 687516d

Please sign in to comment.