Couldn't find anything on SO: I'm trying to custom...
# gradle
e
Couldn't find anything on SO: I'm trying to customize the
clean
task, by adding my own directories. Can I exclude sub-directories tho? Example:
Copy code
my-dir-to-delete
  file.txt
  file2.bin
  subfolder
I'd like to delete everything but
subfolder
not kotlin but kotlin colored 1
This is what I came up with
Copy code
val cleanGenerated = project.task<Delete>("cleanGenerated") {
  val rootDir = project.rootProject.layout.projectDirectory
  val generatedDir = rootDir.dir("generated-packages/${project.name}")
  generatedDir.asFileTree.visit {
    if (!(isDirectory && name == "node_modules")) {
      delete.add(file)
    }
  }
}

project.tasks.findByName("clean")?.dependsOn(cleanGenerated)
It doesn't look very good, but it works
a
another approach: convert a Directory to a FileTree and then filter the contents
Copy code
tasks.clean {
  delete(
      layout.projectDirectory
          .dir("generated-packages/${project.name}")
          .asFileTree
          .matching {
            include("**/node_modules/**")
          }
  )
}
thank you color 1
c
How are the files you want to delete generated? If they are created by another task, let's say "generateFiles", you can just:
Copy code
tasks.clean {
    dependsOn(":cleanGenerateFiles")
}
Gradle automatically creates a task prefixed with
clean
for all tasks, which cleans things generated by that task. This is a much better solution, because if the paths change etc, the clean task will still work.
think smart 2
e
@CLOVIS damn, that's pretty neat. Exactly what I need, thanks!