Skip to content

Instantly share code, notes, and snippets.

@etiennestuder
Last active March 14, 2024 07:30
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save etiennestuder/5653547be88410d914fbf0700d4d6be4 to your computer and use it in GitHub Desktop.
Save etiennestuder/5653547be88410d914fbf0700d4d6be4 to your computer and use it in GitHub Desktop.
Voice notification when when Gradle build finishes (for Mac OS X)
// When runnning a Gradle build in the background, it is convenient to be notified immediately
// via voice once the build has finished - without having to actively switch windows to find out -
// and being told the actual exception in case of a build failure.
// Put this file into the folder ~/.gradle/init.d to enable the acoustic notifications for all builds
gradle.addBuildListener(new BuildAdapter() {
@Override
void buildFinished(BuildResult result) {
def projectName = gradle.rootProject.name
if (result.failure) {
playSound('Submarine.aiff')
def e = getExceptionParts(result)
"say '$projectName build failed: ${e.first} ${e.second}.'".execute()
} else {
if (projectName != "buildSrc") {
playSound('Glass.aiff')
"say '$projectName build successful.'".execute()
}
}
}
private Tuple2<String, String> getExceptionParts(BuildResult result) {
def exception = result.failure
if (exception.cause != null) {
exception = exception.cause
}
def className = exception.class.simpleName
def of = className.indexOf('Exception')
new Tuple2<String, String>(className.substring(0, of), className.substring(of))
}
private void playSound(def sound) {
"afplay /System/Library/Sounds/$sound".execute()
sleep(100)
}
})
@ShkurtiA
Copy link

ShkurtiA commented Feb 16, 2021

@andrii-ivantsiv
Copy link

Very nice! Thank you very much for sharing!

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