Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala sbt - Cleaning files with a wildcard or regex

Tags:

scala

sbt

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".
like image 965
Brandon Amos Avatar asked Dec 22 '25 17:12

Brandon Amos


1 Answers

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].

like image 177
Alex Yarmula Avatar answered Dec 24 '25 07:12

Alex Yarmula



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!