How to make custom task avoid redoing work if the input files are unchanged?

Viewed 990

I have a multi-project setup for a game. There is a very specific sub project called 'resources' that only contains files like images, sounds and texfiles to be packed into a jar.

I have a custom task that processes images and packs them. Inside 'src/main' I'm using a folder 'preprocess' where images should go and an 'unmanaged' folder where everything else goes. By running my task all images in 'preprocess' gets packed and output to 'resources' and everything in 'unmanaged' is copied as is.

val texturePacker = TaskKey[Unit]("texture-packer", "Runs libgdx's Texture Packer")

val texturePackerTask = texturePacker := {
  println("Packaging textures...")
  val inputDir = file("resources/src/main/preprocess")
  val outputDir = file("resources/src/main/resources")

  val folders = inputDir.asFile.listFiles filter (_.isDirectory)

  println("Sub-Folders:" + folders.mkString(", "))

  // Run Texture Packer
  for (subfolder <- folders) {
    println("Building assets for:" + subfolder)
    val args = Array(subfolder.toString, outputDir.toString, subfolder.getName)
    com.badlogic.gdx.tools.imagepacker.TexturePacker2.main(args)
  }

  // Copy unmanaged resources
  IO.copyDirectory(file("resources/src/main/unmanaged"), file("resources/src/main/resources"))
}

And then inside the settings the 'resources' project:

...
packageBin in Compile <<= packageBin in Compile dependsOn(texturePacker)
...

The other sub projects have a dependency on packageBin associated with their run. That way whenever I run the project I get the most up to date state of resources. I don't want it to be on demand. The problem is that it takes a long time to process for every run. I know SBT supports caching SBT FAQ but I don't understand how to adapt it to my task.

How can I make my custom task avoid redoing the work if the files in a subfolder from the folders list were not modified?

1 Answers
Related