3

Suppose I have a Scala program that creates files ending in .foo.

I'm building with sbt and want to remove these files whenever sbt clean is called.

Add additional directory to clean task in SBT build shows that a singe file can be added by

cleanFiles <+= baseDirectory { _ / "test.foo" }

However, it's unclear how to extend this to do:

cleanFiles <append> <*.foo>

All .foo files will be in the same directory, so I don't need to recursively check directories. Though, that would also be interesting to see.

  1. How can I configure sbt to clean files matching a wildcard, or regex?
  2. Is it a bad design decision to have sbt clean remove files my program generates? Should I instead use a flag in my program? Using sbt clean seems cleaner to me rather than having to call sbt clean then sbt "run --clean".
Community
  • 1
  • 1
Brandon Amos
  • 940
  • 1
  • 9
  • 19

1 Answers1

5

This will find anything that matches *.foo in the base directory (but not child directories):

cleanFiles <++= baseDirectory (_ * "*.foo" get)

This works because Seq[File] gets implicitly converted to a PathFinder, which has methods such as * (match the pattern in the base directory) and ** (match the pattern including child directories). Then get converts it back to a Seq[File].

Alex Yarmula
  • 10,477
  • 5
  • 33
  • 32
  • Note for fellow travelers, the only wildcard supported by sbt currently is "*" and not "[0-9]", for example. See http://www.scala-sbt.org/release/docs/Detailed-Topics/Paths#file-filters – Traveler Jun 12 '13 at 01:04