From 687516df3ce957ec0784b5dd2d38fda72d2db979 Mon Sep 17 00:00:00 2001 From: Vadim Chelyshov Date: Fri, 1 Apr 2022 19:03:43 +0300 Subject: [PATCH] fix: move resource generation out from onLoad --- build.sbt | 8 +++- .../shared/src/main/scala/hello/App.scala | 1 + project/BuildPlugin.scala | 44 +++++++++---------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/build.sbt b/build.sbt index 39bb586a4f..54c731db1c 100644 --- a/build.sbt +++ b/build.sbt @@ -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 = { @@ -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 := { diff --git a/frontend/src/test/resources/compiler-plugin-allowlist/allowlist/shared/src/main/scala/hello/App.scala b/frontend/src/test/resources/compiler-plugin-allowlist/allowlist/shared/src/main/scala/hello/App.scala index ca5642002e..7f62b60936 100644 --- a/frontend/src/test/resources/compiler-plugin-allowlist/allowlist/shared/src/main/scala/hello/App.scala +++ b/frontend/src/test/resources/compiler-plugin-allowlist/allowlist/shared/src/main/scala/hello/App.scala @@ -5,3 +5,4 @@ object App { println("Allowed application was compiled successfully v3.0") } } + \ No newline at end of file diff --git a/project/BuildPlugin.scala b/project/BuildPlugin.scala index a84259e117..5935f95bbb 100644 --- a/project/BuildPlugin.scala +++ b/project/BuildPlugin.scala @@ -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 @@ -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 ) @@ -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")) @@ -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 @@ -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 } @@ -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)