Monitor meta build sources

We want to recursively monitor the project meta build, but we also want
to avoid listing directories that don't exist. To compromise, I rework
the buildSourceFileInputs to add the nested project directories if they
exist. Because the fileInputs are a setting, this means that adding a
new project directory and *.sbt or *.scala will not immediately trigger
a rebuild, but in most common cases, it will. I added a scripted test
for this.
This commit is contained in:
Ethan Atkins 2019-09-16 14:21:16 -07:00
parent 26e60e9b6a
commit 48947b8283
6 changed files with 58 additions and 9 deletions

View File

@ -13,7 +13,10 @@ import sbt.SlashSyntax0._
import sbt.io.syntax._
import sbt.nio.FileChanges
import sbt.nio.Keys._
import sbt.nio.file.{ Glob, RecursiveGlob }
import sbt.nio.file.{ Glob, ** }
import sbt.nio.file.syntax._
import scala.annotation.tailrec
private[sbt] object CheckBuildSources {
private[sbt] def needReloadImpl: Def.Initialize[Task[StateTransform]] = Def.task {
@ -60,15 +63,20 @@ private[sbt] object CheckBuildSources {
private[sbt] def buildSourceFileInputs: Def.Initialize[Seq[Glob]] = Def.setting {
if (onChangedBuildSource.value != IgnoreSourceChanges) {
val baseDir = (LocalRootProject / baseDirectory).value
val sourceFilter = "*.{sbt,scala,java}"
val projectDir = baseDir / "project"
Seq(
Glob(baseDir, "*.sbt"),
Glob(projectDir, sourceFilter),
// We only want to recursively look in source because otherwise we have to search
// the project target directories which is expensive.
Glob(projectDir / "src", RecursiveGlob / sourceFilter),
)
@tailrec
def projectGlobs(projectDir: File, globs: Seq[Glob]): Seq[Glob] = {
val glob = projectDir.toGlob
val base = Seq(
glob / "*.{sbt,scala,java}",
// We only want to recursively look in source because otherwise we have to search
// the project target directories which is expensive.
glob / "src" / ** / "*.{scala,java}"
)
val nextLevel = projectDir / "project"
if (nextLevel.exists) projectGlobs(nextLevel, base) else base
}
projectGlobs(projectDir, baseDir.toGlob / "*.sbt" :: Nil)
} else Nil
}
}

View File

@ -0,0 +1,21 @@
version = 2.0.0
maxColumn = 100
project.git = true
project.excludeFilters = [ "\\Wsbt-test\\W", "\\Winput_sources\\W", "\\Wcontraband-scala\\W" ]
# http://docs.scala-lang.org/style/scaladoc.html recommends the JavaDoc style.
# scala/scala is written that way too https://github.com/scala/scala/blob/v2.12.2/src/library/scala/Predef.scala
docstrings = JavaDoc
# This also seems more idiomatic to include whitespace in import x.{ yyy }
spaces.inImportCurlyBraces = true
# This is more idiomatic Scala.
# http://docs.scala-lang.org/style/indentation.html#methods-with-numerous-arguments
align.openParenCallSite = false
align.openParenDefnSite = false
# For better code clarity
danglingParentheses = true
trailingCommas = preserve

View File

@ -0,0 +1,3 @@
object ScalafmtVersion {
val value = "2.0.4"
}

View File

@ -0,0 +1,8 @@
libraryDependencies ++= {
if (ScalafmtVersion.value == "2.0.4") {
val sbtV = (sbtBinaryVersion in pluginCrossBuild).value
val scalaV = (scalaBinaryVersion in update).value
val dep = "org.scalameta" % "sbt-scalafmt" % ScalafmtVersion.value
sbt.Defaults.sbtPluginExtra(dep, sbtV, scalaV) :: Nil
} else Nil
}

View File

@ -0,0 +1,3 @@
object ScalafmtVersion {
val value = "2.0.3"
}

View File

@ -23,3 +23,9 @@ $ copy-file changes/working.sbt build.sbt
$ copy-file changes/sub.sbt sub/build.sbt
> compile
-> scalafmt
$ copy-file changes/ScalafmtVersion.scala project/project/ScalafmtVersion.scala
> scalafmt