Skip to content

Instantly share code, notes, and snippets.

@bkahlert
Created February 20, 2023 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkahlert/1be749424d1e7fdb718caab4bf754dc5 to your computer and use it in GitHub Desktop.
Save bkahlert/1be749424d1e7fdb718caab4bf754dc5 to your computer and use it in GitHub Desktop.
Remove .DS_Store from Gradle build directory before running tasks—globally, that is from any built project
/**
* [Gradle init script](https://docs.gradle.org/current/userguide/init_scripts.html) that has
* to be places in one of the [documented locations](https://docs.gradle.org/current/userguide/init_scripts.html#sec:using_an_init_script),
* for example
* - `$GRADLE_USER_HOME/init.gradle.kts`, respectively
* - `$HOME/init.gradle.kts
*/
rootProject {
logger.info("Running init script on $name")
afterEvaluate {
val project = this
val removeOsJunk by tasks.creating(Delete::class) {
description = "Removes OS created files (i.e. `.DS_Store`) from the build directory"
val junk = fileTree(project.layout.buildDirectory) {
include { fileTreeElement ->
if (fileTreeElement.name == ".DS_Store") {
// It couldn't be more annoying, but certain files are excluded
// by default, see https://github.com/gradle/gradle/issues/1348.
// Therefor simply delete them during the evaluation.
fileTreeElement.file.runCatching {
if (delete() && logger.isInfoEnabled) {
logger.info(
"Removed .DS_Store from build directory of {}: {}",
project.name,
relativeTo(project.layout.buildDirectory.asFile.get()),
)
}
}.getOrElse { /* ignore failures due to concurrent OS operations, etc. */ }
}
fileTreeElement.isDirectory // otherwise, the file tree is not checked recursively
}
}
delete(junk)
}
tasks.configureEach {
if (this != removeOsJunk) {
dependsOn(removeOsJunk)
}
}
}
}
@bkahlert
Copy link
Author

Sample output:

> Task :buildSrc:validatePlugins UP-TO-DATE
> Task :buildSrc:check UP-TO-DATE
> Task :buildSrc:build UP-TO-DATE

> Configure project :
Removed .DS_Store from build directory of foo-service: .DS_Store
Removed .DS_Store from build directory of foo-service: bar/.DS_Store

> Task :removeOsJunk UP-TO-DATE
> Task :kaptGenerateStubsKotlin UP-TO-DATE
> Task :kaptKotlin UP-TO-DATE
> Task :compileKotlin UP-TO-DATE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment