2

All the other questions I've found on this topic are quite old.

I am building a scala project with sbt and the scala-style plugin but I can't find a way to exclude a specific folder where I have some generated code.

Is there a way to force the plugin to not check that particular folder?

Right now I am editing the files manually and adding:

// scalastyle:off

on the top of the file, but this is quite annoying.

In the official website http://www.scalastyle.org/sbt.html I couldn't find any documentation, although it seems that it is actually possible to exclude paths/files from it.

https://github.com/scalastyle/scalastyle/blob/e19b54eacb6502b47b1f84d7b2a6b5d33f3993bc/src/main/scala/org/scalastyle/Main.scala#L51

so it seems that we can actually pass:

println(" -x, --excludedFiles STRING      regular expressions to exclude file paths (delimited by semicolons)")

In my build.sbt I call:

lazy val compileScalastyle = taskKey[Unit]("compileScalastyle")
compileScalastyle := org.scalastyle.sbt.ScalastylePlugin.scalastyle.in(Compile).toTask("").value
(compile in Compile) <<= (compile in Compile) dependsOn compileScalastyle

Is there a way to achieve that with the sbt plugin?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dau_sama
  • 4,247
  • 2
  • 23
  • 30
  • Looking at the [source code](https://github.com/scalastyle/scalastyle-sbt-plugin/blob/master/src/main/scala/org/scalastyle/sbt/Plugin.scala#L112) you should probably override `scalastyleSources in Compile`. Default is the folder `src/main/scala`. – insan-e Jan 06 '17 at 15:54

1 Answers1

5

You could get all files/directories under "src/main/scala" and filter out your directory:

lazy val root = (project in file(".")).
  settings(
    (scalastyleSources in Compile) := {
      // all .scala files in "src/main/scala"
      val scalaSourceFiles = ((scalaSource in Compile).value ** "*.scala").get    
      val fSep = java.io.File.separator // "/" or "\"
      val dirNameToExclude = "com" + fSep + "folder_to_exclude" // "com/folder_to_exclude"
      scalaSourceFiles.filterNot(_.getAbsolutePath.contains(dirNameToExclude))
    }
  )

EDIT:
I've added a more "generic" solution where it checks the path of each file to exclude...

insan-e
  • 3,883
  • 3
  • 18
  • 43
  • weird, I tried to print the sources inside there but it only prints the initial name of a folder under `src/` I print it as: ` scalaSourceFiles.foreach((f: File) => println("name: " + f.getName))` and it only prints `com` for folder `com.mystuff` – dau_sama Jan 10 '17 at 14:38
  • This only goes one level deep (`listFiles()` method on `java.io.File`). If you want all files under src there are sbt's helpers like `**` method that will give you **all** files under a folder. See [here](http://www.scala-sbt.org/1.0/docs/Paths.html#Selecting+descendants). See how your folder structure looks like and exclude wanted files... – insan-e Jan 10 '17 at 14:53
  • thanks! could you help me excluding a folder under `scala/folder1/folder_to_exclude` ? I'm new in scala – dau_sama Jan 10 '17 at 14:59
  • I tried: `scalastyleSources in Compile -= baseDirectory.value / "src" / "scala" / "com" / "folder_to_exclude"` but it didn't work :( – dau_sama Jan 10 '17 at 16:07
  • sweet, that solved my problem! I couldn't figure out how to write this line: `val scalaSourceFiles = ((scalaSource in Compile).value ** "*.scala").get` – dau_sama Jan 11 '17 at 10:15