Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove or Exclude WatchSource in sbt 1.0.x

Overview

After looking around the internet for a while, I have not found a good way to omit certain folders from being watched by sbt 1.0.x in a Play Framework application.

Solutions posted for older versions of sbt:

  • How to exclude a folder from compilation
  • How to not watch a file for changes in Play Framework
  • There are a few more, but all more or less the same.

And the release notes for 1.0.2 show that the += and ++= behavior was maintained, but everything else was dropped.

  • https://www.scala-sbt.org/1.x/docs/sbt-1.0-Release-Notes.html
  • Source code verifies: https://www.scala-sbt.org/1.0.4/api/sbt/Watched$.html

Would love to see if anyone using sbt 1.0.x has found a solution or workaround to this issue. Thanks!

like image 823
adu Avatar asked Feb 06 '18 22:02

adu


1 Answers

Taking the approach of how SBT excludes managedSources from watchSources I was able to omit a custom folder from being watched like so:

watchSources := {
  val directoryToExclude = "/Users/mgalic/sandbox/scala/scala-seed-project/src/main/scala/dirToExclude"
  val filesToExclude = (new File(directoryToExclude) ** "*.scala").get.toSet
  val customSourcesFilter = new FileFilter {
    override def accept(pathname: File): Boolean = filesToExclude.contains(pathname)
    override def toString = s"CustomSourcesFilter($filesToExclude)"
  }

  watchSources.value.map { source =>
    new Source(
      source.base,
      source.includeFilter,
      source.excludeFilter || customSourcesFilter,
      source.recursive
    )
  }
},

Here we use PathFinder to get all the *.scala sources from directoryToExclude:

val filesToExclude = (new File(directoryToExclude) ** "*.scala").get.toSet

Then we create customSourcesFilter using filesToExclude, which we then add to every current WatchSource:

  watchSources.value.map { source =>
    new Source(
      ...
      source.excludeFilter || customSourcesFilter,
      ...
    )
  }

Note the above solution is just something that worked for me, that is, I do not know what is the recommend approach of solving this problem.

like image 89
Mario Galic Avatar answered Oct 24 '22 15:10

Mario Galic