Skip to content

Instantly share code, notes, and snippets.

@andrii-ivantsiv
Forked from etiennestuder/speak.gradle
Created March 30, 2023 11:53
Show Gist options
  • Save andrii-ivantsiv/f574255d4daede137b0ba478cba46847 to your computer and use it in GitHub Desktop.
Save andrii-ivantsiv/f574255d4daede137b0ba478cba46847 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)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment