Skip to content

Commit

Permalink
chore: move resource generation out from onLoad (#1708)
Browse files Browse the repository at this point in the history
  • Loading branch information
dos65 authored Apr 5, 2022
1 parent 5c28695 commit ebf0cf4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 67 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
118 changes: 52 additions & 66 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,6 @@ object BuildImplementation {
BuildKeys.schemaVersion := "4.2-refresh-3",
(Test / Keys.testOptions) += sbt.Tests.Argument("-oD"),
Keys.onLoadMessage := Header.intro,
Keys.onLoad := BuildDefaults.bloopOnLoad.value,
(Test / Keys.publishArtifact) := false
)

Expand Down Expand Up @@ -426,82 +426,68 @@ 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): Unit = {
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 targetDir = projectDir / "target"
val cacheDirectory = targetDir / "generation-cache-dir"
if (sys.env.isDefinedAt("FORCE_TEST_RESOURCES_GENERATION"))
IO.delete(cacheDirectory)
java.nio.file.Files.createDirectories(cacheDirectory.toPath)

val projectsFiles = sbt.io.Path
.allSubpaths(projectDir)
.map(_._1)
.filter { f =>
val filename = f.toString
filename.endsWith(".sbt") || filename.endsWith(".scala")
}
.toSet

val pluginFiles = sbt.io.Path
.allSubpaths(pluginSourceDir)
.map(_._1)
.filter(f => f.toString.endsWith(".scala"))
.toSet

import scala.sys.process.Process

val generate = { (changedFiles: Set[File]) =>
state.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
else if (isWindows) "cmd.exe" :: "/C" :: "sbt.bat" :: "bloopInstall" :: Nil
else "sbt" :: "bloopInstall" :: Nil
}
val exitGenerate = Process(cmd, projectDir).!
if (exitGenerate != 0)
throw new sbt.MessageOnlyException(
s"Failed to generate bloop config for ${projectDir}."
)
state.log.success(s"Generated bloop configuration files for ${projectDir}")
changedFiles
}

if (enableCache) {
val cached = FileFunction.cached(cacheDirectory, sbt.util.FileInfo.hash) { changedFiles =>
generate(changedFiles)
projectDirs
.flatMap { projectDir =>
val targetDir = projectDir / "target"
val cacheDirectory = targetDir / "generation-cache-dir"
if (sys.env.isDefinedAt("FORCE_TEST_RESOURCES_GENERATION"))
IO.delete(cacheDirectory)
java.nio.file.Files.createDirectories(cacheDirectory.toPath)

val projectsFiles = sbt.io.Path
.allSubpaths(projectDir)
.map(_._1)
.filter { f =>
val filename = f.toString
filename.endsWith(".sbt") || filename.endsWith(".scala")
}
.toSet

val pluginFiles = sbt.io.Path
.allSubpaths(pluginSourceDir)
.map(_._1)
.filter(f => f.toString.endsWith(".scala"))
.toSet

import scala.sys.process.Process

val generate = { (changedFiles: Set[File]) =>
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
else if (isWindows) "cmd.exe" :: "/C" :: "sbt.bat" :: "bloopInstall" :: Nil
else "sbt" :: "bloopInstall" :: Nil
}
val exitGenerate = Process(cmd, projectDir).!
if (exitGenerate != 0)
throw new sbt.MessageOnlyException(
s"Failed to generate bloop config for resource project: ${projectDir}."
)
log.success(s"Generated bloop configuration files for ${projectDir}")
changedFiles
}

cached(projectsFiles ++ pluginFiles)
} else generate(Set.empty)
}
if (enableCache) {
val cached = FileFunction.cached(cacheDirectory, sbt.util.FileInfo.hash) {
changedFiles =>
generate(changedFiles)
}

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

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 ebf0cf4

Please sign in to comment.