Created
February 20, 2023 11:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* [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) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: