Skip to content

Commit b7ef2b2

Browse files
committedDec 19, 2021
Major refactor. Added linux support via spd-say
1 parent c7cf34e commit b7ef2b2

26 files changed

+295
-151
lines changed
 

‎buildLogic/serenador-plugin/build.gradle.kts

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ repositories {
2121

2222
tasks.withType<Test>().configureEach {
2323
useJUnitPlatform()
24+
testLogging { showStandardStreams = true }
2425
}
25-
tasks.test {
26-
useJUnitPlatform()
27-
}
26+
2827
configure<JavaPluginExtension> {
2928
sourceCompatibility = JavaVersion.VERSION_1_8
3029
targetCompatibility = JavaVersion.VERSION_1_8

‎buildLogic/serenador-plugin/src/main/kotlin/io/github/thanosfisherman/serenador/CommandExecutor.kt

-35
This file was deleted.

‎buildLogic/serenador-plugin/src/main/kotlin/io/github/thanosfisherman/serenador/PhraseBook.kt

-51
This file was deleted.

‎buildLogic/serenador-plugin/src/main/kotlin/io/github/thanosfisherman/serenador/SerenadorPlugin.kt

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package io.github.thanosfisherman.serenador
22

3+
import io.github.thanosfisherman.serenador.commandexecutors.linux.LinuxCommandExecutor
4+
import io.github.thanosfisherman.serenador.commandexecutors.macos.MacCommandExecutor
35
import io.github.thanosfisherman.serenador.extensions.SerenadorExtension
46
import io.github.thanosfisherman.serenador.extensions.SerenadorExtension.Companion.initSerenadorExtension
57
import io.github.thanosfisherman.serenador.listeners.MyBuildListener
8+
import io.github.thanosfisherman.serenador.repositories.linux.PhraseRepoLinuxImpl
9+
import io.github.thanosfisherman.serenador.repositories.macos.PhraseRepoMacImpl
10+
import io.github.thanosfisherman.serenador.sources.PhrasesSource
611
import org.apache.tools.ant.taskdefs.condition.Os
712
import org.gradle.api.Plugin
813
import org.gradle.api.Project
@@ -16,12 +21,21 @@ class SerenadorPlugin : Plugin<Project> {
1621
if (!serenadorExt.isShutTheFuckUp.getOrElse(false))
1722
applyTaskExecutorListener(project, serenadorExt)
1823
}
19-
2024
}
2125

2226
private fun applyTaskExecutorListener(project: Project, serenadorExt: SerenadorExtension) {
2327

24-
if (Os.isFamily(Os.FAMILY_MAC))
25-
project.gradle.addBuildListener(MyBuildListener(project, serenadorExt))
28+
val listener: MyBuildListener? = when {
29+
Os.isFamily(Os.FAMILY_MAC) -> {
30+
project.provideMacBuildListener(serenadorExt)
31+
}
32+
Os.isFamily(Os.FAMILY_UNIX) -> {
33+
project.provideLinuxBuildListener(serenadorExt)
34+
}
35+
else -> {
36+
null
37+
}
38+
}
39+
listener?.let { project.gradle.addBuildListener(it) }
2640
}
2741
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.thanosfisherman.serenador
2+
3+
import io.github.thanosfisherman.serenador.commandexecutors.linux.LinuxCommandExecutor
4+
import io.github.thanosfisherman.serenador.commandexecutors.macos.MacCommandExecutor
5+
import io.github.thanosfisherman.serenador.extensions.SerenadorExtension
6+
import io.github.thanosfisherman.serenador.listeners.MyBuildListener
7+
import io.github.thanosfisherman.serenador.repositories.linux.PhraseRepoLinuxImpl
8+
import io.github.thanosfisherman.serenador.repositories.macos.PhraseRepoMacImpl
9+
import io.github.thanosfisherman.serenador.sources.PhrasesSource
10+
import org.gradle.api.Project
11+
12+
13+
fun Project.provideMacBuildListener(serenadorExtension: SerenadorExtension): MyBuildListener {
14+
return MyBuildListener(MacCommandExecutor(this), PhraseRepoMacImpl(PhrasesSource), serenadorExtension)
15+
}
16+
17+
fun Project.provideLinuxBuildListener(serenadorExtension: SerenadorExtension): MyBuildListener {
18+
return MyBuildListener(
19+
LinuxCommandExecutor(this),
20+
PhraseRepoLinuxImpl(PhrasesSource),
21+
serenadorExtension
22+
)
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.github.thanosfisherman.serenador.commandexecutors
2+
3+
import io.github.thanosfisherman.serenador.commands.Command
4+
import org.gradle.process.ExecResult
5+
6+
interface CommandExecutor{
7+
8+
fun execute(command: Command): ExecResult
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.thanosfisherman.serenador.commandexecutors.linux
2+
3+
import io.github.thanosfisherman.serenador.commandexecutors.CommandExecutor
4+
import io.github.thanosfisherman.serenador.commands.Command
5+
import org.gradle.api.Project
6+
import org.gradle.process.ExecResult
7+
8+
class LinuxCommandExecutor constructor(private val project: Project) : CommandExecutor {
9+
override fun execute(command: Command): ExecResult {
10+
return project.exec {
11+
commandLine("bash", "-c", command)
12+
isIgnoreExitValue = true
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.thanosfisherman.serenador.commandexecutors.macos
2+
3+
import io.github.thanosfisherman.serenador.commandexecutors.CommandExecutor
4+
import io.github.thanosfisherman.serenador.commands.Command
5+
import org.gradle.api.Project
6+
import org.gradle.process.ExecResult
7+
8+
class MacCommandExecutor constructor(private val project: Project) : CommandExecutor {
9+
override fun execute(command: Command): ExecResult {
10+
return project.exec {
11+
commandLine("bash", "-c", command)
12+
isIgnoreExitValue = true
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
package io.github.thanosfisherman.serenador.commands
22

3-
import io.github.thanosfisherman.serenador.failPhraseList
4-
import io.github.thanosfisherman.serenador.successPhraseList
5-
63

74
abstract class Command {
8-
abstract val arguments: String
95

6+
abstract val arguments: String
107
override fun toString() = arguments
11-
12-
companion object {
13-
val sayCommandsSuccessList: List<Command> = successPhraseList.map { SayCommand(it.text, it.voice) }
14-
val sayCommandsFailList: List<Command> = failPhraseList.map { SayCommand(it.text, it.voice) }
15-
val sayCommandsSuccessDefaultVoiceList: List<Command> = successPhraseList.map { SayCommand(it.text) }
16-
val sayCommandsFailDefaultVoiceList: List<Command> = failPhraseList.map { SayCommand(it.text) }
17-
}
188
}

‎buildLogic/serenador-plugin/src/main/kotlin/io/github/thanosfisherman/serenador/commands/SayCommand.kt

-6
This file was deleted.

‎buildLogic/serenador-plugin/src/main/kotlin/io/github/thanosfisherman/serenador/commands/SearchVoiceCommand.kt

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.github.thanosfisherman.serenador.commands.linux
2+
3+
import io.github.thanosfisherman.serenador.commands.Command
4+
5+
class SpdSayLinuxCommand(private val text: String, var voice: String? = null) : Command() {
6+
override val arguments: String
7+
get() = voice?.let { "spd-say -t \"$it\" \"$text\"" } ?: "spd-say \"$text\""
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.github.thanosfisherman.serenador.commands.macos
2+
3+
import io.github.thanosfisherman.serenador.commands.Command
4+
5+
class SayMacCommand(private val text: String, var voice: String? = null) : Command() {
6+
override val arguments: String
7+
get() = voice?.let { "say -v \"$it\" \"$text\"" } ?: "say \"$text\""
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.github.thanosfisherman.serenador.commands.macos
2+
3+
import io.github.thanosfisherman.serenador.commands.Command
4+
5+
class SearchVoiceMacCommand(private val voice: String) : Command() {
6+
override val arguments: String
7+
get() = "say -v '?' | grep -o \"$voice\""
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package io.github.thanosfisherman.serenador.listeners
22

3-
import io.github.thanosfisherman.serenador.CommandExecutor
4-
import io.github.thanosfisherman.serenador.executeCustom
5-
import io.github.thanosfisherman.serenador.executeFailure
6-
import io.github.thanosfisherman.serenador.executeSuccess
3+
import io.github.thanosfisherman.serenador.commandexecutors.CommandExecutor
74
import io.github.thanosfisherman.serenador.extensions.SerenadorExtension
5+
import io.github.thanosfisherman.serenador.repositories.PhraseRepo
86
import org.gradle.BuildListener
97
import org.gradle.BuildResult
10-
import org.gradle.api.Project
118
import org.gradle.api.initialization.Settings
129
import org.gradle.api.invocation.Gradle
1310

14-
class MyBuildListener(private val project: Project, private val serenadorExt: SerenadorExtension) : BuildListener {
11+
class MyBuildListener constructor(
12+
private val commandExecutor: CommandExecutor,
13+
private val phraseRepo: PhraseRepo,
14+
private val serenadorExt: SerenadorExtension
15+
) : BuildListener {
1516
override fun settingsEvaluated(settings: Settings) {
1617
}
1718

@@ -22,21 +23,27 @@ class MyBuildListener(private val project: Project, private val serenadorExt: Se
2223
}
2324

2425
override fun buildFinished(result: BuildResult) {
25-
val executor = CommandExecutor(project)
26+
2627
if (result.failure != null) {
27-
customOrDefault(serenadorExt.phraseBook.failPhrases, true, executor)
28+
customOrDefault(serenadorExt.phraseBook.failPhrases, true, commandExecutor)
2829
} else {
29-
customOrDefault(serenadorExt.phraseBook.successPhrases, false, executor)
30+
customOrDefault(serenadorExt.phraseBook.successPhrases, false, commandExecutor)
3031
}
3132
}
3233

3334
private fun customOrDefault(phrases: List<String>, isFail: Boolean, executor: CommandExecutor) {
3435
if (phrases.isEmpty() && isFail) {
35-
executor.executeFailure()
36+
val result = executor.execute(phraseRepo.getFailPhrasesWithVoice().random())
37+
if (result.exitValue == 1) {
38+
executor.execute(phraseRepo.getFailPhrases().random())
39+
}
3640
} else if (phrases.isEmpty()) {
37-
executor.executeSuccess()
41+
val result = executor.execute(phraseRepo.getSuccessPhrasesWithVoice().random())
42+
if (result.exitValue == 1) {
43+
executor.execute(phraseRepo.getSuccessPhrases().random())
44+
}
3845
} else {
39-
executor.executeCustom(phrases)
46+
executor.execute(phraseRepo.getCustomPhrases(phrases).random())
4047
}
4148
}
4249
}
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
11
package io.github.thanosfisherman.serenador.listeners
22

3-
import io.github.thanosfisherman.serenador.CommandExecutor
4-
import io.github.thanosfisherman.serenador.executeFailure
5-
import io.github.thanosfisherman.serenador.executeSuccess
63
import org.gradle.api.Project
74
import org.gradle.api.Task
85
import org.gradle.api.execution.TaskExecutionListener
96
import org.gradle.api.tasks.TaskState
107

8+
//Unused
119
class TaskListener(private val project: Project) : TaskExecutionListener {
12-
var isExecutedOnce = false
10+
1311
override fun beforeExecute(task: Task) {
1412
}
1513

1614
override fun afterExecute(task: Task, state: TaskState) {
17-
println("TASK NAME " + task.name)
18-
if (task.name.contains("compile")) {
19-
if (!isExecutedOnce) {
20-
val executor = CommandExecutor(project)
21-
if (state.failure != null) {
22-
executor.executeFailure()
23-
} else if (state.executed) {
24-
executor.executeSuccess()
25-
}
26-
isExecutedOnce = true
27-
}
28-
}
2915

3016
}
3117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.thanosfisherman.serenador.repositories
2+
3+
import io.github.thanosfisherman.serenador.commands.Command
4+
5+
interface PhraseRepo {
6+
fun getSuccessPhrases(): List<Command>
7+
fun getSuccessPhrasesWithVoice(): List<Command>
8+
fun getFailPhrases(): List<Command>
9+
fun getFailPhrasesWithVoice(): List<Command>
10+
fun getCustomPhrases(customPhrases: List<String>): List<Command>
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.github.thanosfisherman.serenador.repositories.linux
2+
3+
import io.github.thanosfisherman.serenador.commands.Command
4+
import io.github.thanosfisherman.serenador.commands.linux.SpdSayLinuxCommand
5+
import io.github.thanosfisherman.serenador.repositories.PhraseRepo
6+
import io.github.thanosfisherman.serenador.sources.PhrasesSource
7+
8+
internal class PhraseRepoLinuxImpl(private val source: PhrasesSource) : PhraseRepo {
9+
10+
11+
override fun getSuccessPhrases(): List<Command> {
12+
return source.getSuccessPhrasesList().map { SpdSayLinuxCommand(it) }.toList()
13+
}
14+
15+
override fun getSuccessPhrasesWithVoice(): List<Command> {
16+
val phrases = getSuccessPhrases().map { it as SpdSayLinuxCommand }
17+
phrases.getOrNull(0)?.voice = "male1"
18+
phrases.getOrNull(1)?.voice = "female2"
19+
phrases.getOrNull(2)?.voice = "child_male"
20+
phrases.getOrNull(3)?.voice = "child_female"
21+
return phrases
22+
}
23+
24+
override fun getFailPhrases(): List<Command> {
25+
return source.getFailPhrasesList().map { SpdSayLinuxCommand(it) }.toList()
26+
}
27+
28+
override fun getFailPhrasesWithVoice(): List<Command> {
29+
val phrases = getFailPhrases().map { it as SpdSayLinuxCommand }
30+
31+
phrases.getOrNull(0)?.voice = "male1"
32+
phrases.getOrNull(1)?.voice = "male1"
33+
phrases.getOrNull(2)?.voice = "male1"
34+
phrases.getOrNull(3)?.voice = "male2"
35+
phrases.getOrNull(4)?.voice = "male2"
36+
phrases.getOrNull(5)?.voice = "male2"
37+
phrases.getOrNull(6)?.voice = "male2"
38+
return phrases
39+
}
40+
41+
override fun getCustomPhrases(customPhrases: List<String>): List<Command> {
42+
return customPhrases.map { SpdSayLinuxCommand(it) }
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.thanosfisherman.serenador.repositories.macos
2+
3+
import io.github.thanosfisherman.serenador.commands.Command
4+
import io.github.thanosfisherman.serenador.commands.macos.SayMacCommand
5+
import io.github.thanosfisherman.serenador.repositories.PhraseRepo
6+
import io.github.thanosfisherman.serenador.sources.PhrasesSource
7+
8+
9+
private const val BAD_NEWS = "Bad News"
10+
private const val GOOD_NEWS = "Good News"
11+
private const val CELLO = "Cello"
12+
private const val BELLS = "Bells"
13+
14+
internal class PhraseRepoMacImpl(private val source: PhrasesSource) : PhraseRepo {
15+
16+
override fun getSuccessPhrases(): List<Command> {
17+
return source.getSuccessPhrasesList().map { SayMacCommand(it) }.toList()
18+
}
19+
20+
override fun getSuccessPhrasesWithVoice(): List<Command> {
21+
val phrases = getSuccessPhrases().map { it as SayMacCommand }
22+
phrases.getOrNull(0)?.voice = GOOD_NEWS
23+
phrases.getOrNull(1)?.voice = GOOD_NEWS
24+
phrases.getOrNull(2)?.voice = BELLS
25+
phrases.getOrNull(3)?.voice = BELLS
26+
return phrases
27+
}
28+
29+
override fun getFailPhrases(): List<Command> {
30+
return source.getFailPhrasesList().map { SayMacCommand(it) }.toList()
31+
}
32+
33+
override fun getFailPhrasesWithVoice(): List<Command> {
34+
val phrases = getFailPhrases().map { it as SayMacCommand }
35+
36+
phrases.getOrNull(0)?.voice = BAD_NEWS
37+
phrases.getOrNull(1)?.voice = BAD_NEWS
38+
phrases.getOrNull(2)?.voice = BAD_NEWS
39+
phrases.getOrNull(3)?.voice = BAD_NEWS
40+
phrases.getOrNull(4)?.voice = CELLO
41+
phrases.getOrNull(5)?.voice = CELLO
42+
phrases.getOrNull(6)?.voice = CELLO
43+
return phrases
44+
}
45+
46+
override fun getCustomPhrases(customPhrases: List<String>): List<Command> {
47+
return customPhrases.map { SayMacCommand(it) }
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.thanosfisherman.serenador.sources
2+
3+
object PhrasesSource {
4+
5+
fun getFailPhrasesList(): List<String> {
6+
return getPhrases("failPhrases.txt")
7+
}
8+
9+
fun getSuccessPhrasesList(): List<String> {
10+
return getPhrases("successPhrases.txt")
11+
}
12+
13+
private fun getPhrases(path: String): List<String> {
14+
val resInputStream = javaClass.classLoader.getResourceAsStream(path) ?: return emptyList()
15+
return resInputStream.bufferedReader().use { buff ->
16+
buff.readLines().filter { it.isNotBlank() && !it.contains("#") }
17+
}
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Bad News
2+
Build process fail oh my god you look so pale
3+
Your project failed now it's time to serenade
4+
Your project failed and I will now serenade
5+
Don't git commit cause your project is like shit your project manager will be mad a bit
6+
7+
# Cello
8+
Gradle project fail fail fail oh my god EPIC fail try again tomorrow maybe you get better luck
9+
In the hall of mountain king I can see your work sink what a sudden failure for your project fa la la
10+
Sorry but your coding sucks now it's time for some guts try to ask your peeps because your project gently weeps
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Recommended with Mac's Good News voice
2+
3+
Congratulations boy your project succeeded
4+
Congratulations
5+
6+
# Bells Mac voice
7+
8+
Your build is done
9+
Ready to go
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.thanosfisherman.serenador.plugin
22

3-
import org.junit.jupiter.api.Assertions
43
import org.junit.jupiter.api.Test
54

65

@@ -9,6 +8,6 @@ class SerenadorPluginTest {
98

109
@Test
1110
fun testApply() {
12-
Assertions.assertEquals(1,1)
11+
println("HELLO WORLD")
1312
}
1413
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Bad News
2+
Build process fail oh my god you look so pale
3+
Your project failed now it's time to serenade
4+
Your project failed and I will now serenade
5+
Don't git commit cause your project is like shit your project manager will be mad a bit
6+
7+
# Cello
8+
Gradle project fail fail fail oh my god EPIC fail try again tomorrow maybe you get better luck
9+
In the hall of mountain king I can see your work sink what a sudden failure for your project fa la la
10+
Sorry but your coding sucks now it's time for some guts try to ask your peeps because your project gently weeps
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Recommended with Mac's Good News voice
2+
3+
Congratulations boy your project succeeded
4+
Congratulations
5+
6+
# Bells Mac voice
7+
8+
Your build is done
9+
Ready to go

‎serenador-sample/build.gradle.kts

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
2727
serenadorExtension {
2828
shutTheFuckUp(false)
2929

30-
/* phraseBook {
31-
addSuccessPhrase("Success1")
32-
addSuccessPhrase("Success2")
33-
addFailPhrase("EPIC FAIL 1")
34-
addFailPhrase("EPIC FAIL 2")
35-
}*/
30+
/* phraseBook {
31+
addSuccessPhrase("Success1")
32+
addSuccessPhrase("Success2")
33+
addFailPhrase("EPIC FAIL 1")
34+
addFailPhrase("EPIC FAIL 2")
35+
}*/
3636
}

0 commit comments

Comments
 (0)
Please sign in to comment.