Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stabilize test project resources generation #1715

Merged
merged 1 commit into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,9 @@ lazy val frontend: Project = project
val main = (Test / resources).value
val dir = (ThisBuild / baseDirectory).value
val log = streams.value
BuildDefaults.exportProjectsInTestResources(dir, log.log, enableCache = true)
main
val additionalResources =
BuildDefaults.exportProjectsInTestResources(dir, log.log, enableCache = true)
main ++ additionalResources
},
(Test / unmanagedResources / includeFilter) := {
new FileFilter {
Expand Down
106 changes: 56 additions & 50 deletions project/BuildPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,11 @@ object BuildImplementation {
}
}

def exportProjectsInTestResources(baseDir: File, log: Logger, enableCache: Boolean): Unit = {
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")
Expand All @@ -435,57 +439,59 @@ object BuildImplementation {
val resourcesDir = baseDir / "frontend" / "src" / "test" / "resources"
val pluginSourceDir = baseDir / "integrations" / "sbt-bloop" / "src" / "main"
val projectDirs = resourcesDir.listFiles().filter(_.isDirectory)
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
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")
}

if (enableCache) {
val cached = FileFunction.cached(cacheDirectory, sbt.util.FileInfo.hash) {
changedFiles =>
generate(changedFiles)
}

cached(projectsFiles ++ pluginFiles)
} else generate(Set.empty)
.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
}
val bloopConfigDir = projectDir / "bloop-config"
val bloopConfigExists =
bloopConfigDir.exists && bloopConfigDir.listFiles().exists(_.name.endsWith(".json"))
val onlyOnCacheChange = enableCache && bloopConfigExists
if (onlyOnCacheChange) {
val cached = FileFunction.cached(cacheDirectory, sbt.util.FileInfo.hash) { changedFiles =>
generate(changedFiles)
}

cached(projectsFiles ++ pluginFiles)
} else generate(Set.empty)
sbt.io.Path.allSubpaths(projectDir).map(_._1).toList
}.distinct
}

def getStagingDirectory(state: State): File = {
Expand Down