-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MBS-10126 Add posibility to user defined custom task verdict producers (
#675)
- Loading branch information
1 parent
246b6f3
commit 3d5219e
Showing
19 changed files
with
246 additions
and
120 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
32 changes: 32 additions & 0 deletions
32
...verdict/src/main/kotlin/com/avito/android/build_verdict/UserDefinedTaskVerdictProducer.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.avito.android.build_verdict | ||
|
||
import org.gradle.api.Task | ||
|
||
class UserDefinedTaskVerdictProducer( | ||
predicate: TaskPredicate, | ||
producer: TaskVerdictProducer | ||
) : TaskPredicate by predicate, TaskVerdictProducer by producer | ||
|
||
interface TaskPredicate { | ||
fun accept(task: Task): Boolean | ||
|
||
class ByName(private val name: String) : TaskPredicate { | ||
override fun accept(task: Task) = task.name == name | ||
} | ||
|
||
class ByType(private val acceptedClass: Class<in Task>) : TaskPredicate { | ||
override fun accept(task: Task) = acceptedClass.isInstance(task) | ||
} | ||
} | ||
|
||
interface TaskVerdictProducer { | ||
fun produce(task: Task): String | ||
|
||
companion object { | ||
internal inline fun create(crossinline producer: (Task) -> String): TaskVerdictProducer { | ||
return object : TaskVerdictProducer { | ||
override fun produce(task: Task) = producer(task) | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
37 changes: 0 additions & 37 deletions
37
...ct/src/main/kotlin/com/avito/android/build_verdict/internal/TaskExecutionErrorsCapture.kt
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
24 changes: 24 additions & 0 deletions
24
...in/com/avito/android/build_verdict/internal/task/lifecycle/TaskExecutionListenerBridge.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.avito.android.build_verdict.internal.task.lifecycle | ||
|
||
import org.gradle.BuildAdapter | ||
import org.gradle.BuildResult | ||
import org.gradle.api.Task | ||
import org.gradle.api.execution.TaskExecutionListener | ||
import org.gradle.api.tasks.TaskState | ||
|
||
internal class TaskExecutionListenerBridge( | ||
private val listeners: List<TaskLifecycleListener<*>> | ||
) : TaskExecutionListener, BuildAdapter() { | ||
|
||
override fun beforeExecute(task: Task) { | ||
listeners.forEach { it.beforeExecute(task) } | ||
} | ||
|
||
override fun afterExecute(task: Task, state: TaskState) { | ||
listeners.forEach { it.afterExecute(task, state) } | ||
} | ||
|
||
override fun buildFinished(result: BuildResult) { | ||
result.gradle?.removeListener(this) | ||
} | ||
} |
41 changes: 28 additions & 13 deletions
41
...n/kotlin/com/avito/android/build_verdict/internal/task/lifecycle/TaskLifecycleListener.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,40 @@ | ||
package com.avito.android.build_verdict.internal.task.lifecycle | ||
|
||
import com.avito.android.build_verdict.internal.LogsTextBuilder | ||
import org.gradle.api.Task | ||
import org.gradle.util.Path | ||
import org.gradle.api.tasks.TaskState | ||
|
||
abstract class TaskLifecycleListener<in T> { | ||
internal abstract class TaskLifecycleListener<in T> { | ||
|
||
protected abstract val logs: MutableMap<Path, LogsTextBuilder> | ||
abstract val acceptedTask: Class<in T> | ||
|
||
abstract fun beforeExecute(task: T) | ||
@Suppress("UNCHECKED_CAST") | ||
fun beforeExecute(task: Task) { | ||
if (acceptedTask.isInstance(task)) { | ||
beforeExecuteTyped(task as T) | ||
} | ||
} | ||
|
||
protected open fun beforeExecuteTyped(task: T) { | ||
// empty | ||
} | ||
|
||
protected abstract fun afterSucceedExecute(task: T) | ||
protected open fun afterSucceedExecute(task: T) { | ||
// empty | ||
} | ||
|
||
protected abstract fun afterFailedExecute(task: T) | ||
protected open fun afterFailedExecute(task: T, error: Throwable) { | ||
// empty | ||
} | ||
|
||
fun afterExecute(task: T, failure: Throwable?) { | ||
if (failure == null) { | ||
logs.remove(Path.path((task as Task).path)) | ||
afterSucceedExecute(task) | ||
} else { | ||
afterFailedExecute(task) | ||
@Suppress("UNCHECKED_CAST") | ||
fun afterExecute(task: Task, state: TaskState) { | ||
if (acceptedTask.isInstance(task)) { | ||
val failure = state.failure | ||
if (failure != null) { | ||
afterFailedExecute(task as T, failure) | ||
} else { | ||
afterSucceedExecute(task as T) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.