From 8599b314736f472aa707be22356684260818ffcd Mon Sep 17 00:00:00 2001 From: klee0kai Date: Sun, 6 Aug 2023 15:00:30 +0400 Subject: [PATCH 1/5] diagon DAG graph drawing --- .../klee0kai/tasktree/TaskTreePlugin.kt | 9 ++- .../klee0kai/tasktree/tasks/DiagonDagTask.kt | 75 +++++++++++++++++++ .../klee0kai/tasktree/utils/ProjectExt.kt | 8 +- 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 tasktree/src/main/kotlin/com/github/klee0kai/tasktree/tasks/DiagonDagTask.kt diff --git a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/TaskTreePlugin.kt b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/TaskTreePlugin.kt index ccd17ca..e745d6c 100644 --- a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/TaskTreePlugin.kt +++ b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/TaskTreePlugin.kt @@ -1,7 +1,10 @@ package com.github.klee0kai.tasktree +import com.github.klee0kai.tasktree.tasks.DiagonDagTask import com.github.klee0kai.tasktree.tasks.TaskTreeTask +import com.github.klee0kai.tasktree.utils.isDiagonGraphRequested import com.github.klee0kai.tasktree.utils.isTaskTreeRequested +import com.github.klee0kai.tasktree.utils.requestedTasks import com.github.klee0kai.tasktree.utils.taskGraph import org.gradle.api.Plugin import org.gradle.api.Project @@ -13,11 +16,11 @@ open class TaskTreePlugin : Plugin { private fun Project.applyOnProject() { val ext = extensions.create("tasktree", TaskTreeExtension::class.java) tasks.register("tasktree", TaskTreeTask::class.java, ext) + tasks.register("diagonDAG", DiagonDagTask::class.java) taskGraph.whenReady { - if (isTaskTreeRequested) { - tasks.filter { it !is TaskTreeTask } - .forEach { it.enabled = false } + if (isTaskTreeRequested || isDiagonGraphRequested) { + requestedTasks?.forEach { it.enabled = false } } } } diff --git a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/tasks/DiagonDagTask.kt b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/tasks/DiagonDagTask.kt new file mode 100644 index 0000000..1805681 --- /dev/null +++ b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/tasks/DiagonDagTask.kt @@ -0,0 +1,75 @@ +package com.github.klee0kai.tasktree.tasks + +import com.github.klee0kai.tasktree.utils.getAllDeps +import com.github.klee0kai.tasktree.utils.getDeps +import com.github.klee0kai.tasktree.utils.requestedTasks +import com.github.klee0kai.tasktree.utils.taskGraph +import org.apache.tools.ant.util.TeeOutputStream +import org.gradle.api.Project +import org.gradle.api.model.ObjectFactory +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.diagnostics.ProjectReportTask +import org.gradle.process.internal.ExecActionFactory +import org.gradle.process.internal.ExecException +import java.io.ByteArrayOutputStream +import java.io.IOException +import javax.inject.Inject + +open class DiagonDagTask @Inject constructor( + @Input + val objectFactory: ObjectFactory, + @Input + val execAction: ExecActionFactory, +) : ProjectReportTask() { + + @Internal + override fun getDescription(): String = + "Draw tasktree graph use Diagon. More: https://github.com/ArthurSonzogni/Diagon" + + + override fun generate(project: Project) { + super.generate(project) + val allTasks = project.requestedTasks?.flatMap { + setOf(it) + project.taskGraph.getAllDeps(it) + }?.toSet() ?: emptySet() + + + val depsCode = allTasks.joinToString("\n") { task -> + project.taskGraph.getDeps(task).joinToString("\n") { dep -> + "${dep.name} -> ${task.name}" + } + } + + val result = sh(cmd = arrayOf("diagon", "GraphDAG"), input = depsCode) + + println("------------------ Diagon DAG graph ----------- ") + println(result) + println("------------------ ---------------- ----------- ") + } + + + private fun sh(cmd: Array, input: String): String { + val execAction = execAction.newExecAction() + + val localErrStream = ByteArrayOutputStream() + val localOutputStream = ByteArrayOutputStream() + + try { + execAction.commandLine(*cmd) + execAction.errorOutput = TeeOutputStream(localErrStream, System.err) + execAction.standardInput = input.byteInputStream() + execAction.standardOutput = localOutputStream + val result = execAction.execute() + + if (result.exitValue != 0) { + throw ExecException("Cmd ${cmd.joinToString(" ")} finished with exit code ${result.exitValue}") + } + return String(localOutputStream.toByteArray()) + } catch (e: Exception) { + val errStreamText = String(localErrStream.toByteArray()) + throw IOException("can't run ${cmd.joinToString(" ")}\n ${e.message} $errStreamText", e) + } + } + +} \ No newline at end of file diff --git a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/utils/ProjectExt.kt b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/utils/ProjectExt.kt index 96e8c35..b89d294 100644 --- a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/utils/ProjectExt.kt +++ b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/utils/ProjectExt.kt @@ -1,5 +1,6 @@ package com.github.klee0kai.tasktree.utils +import com.github.klee0kai.tasktree.tasks.DiagonDagTask import com.github.klee0kai.tasktree.tasks.TaskTreeTask import org.gradle.api.Project import org.gradle.api.Task @@ -13,7 +14,12 @@ val Project.executionPlan get() = taskGraph.executionPlan val Project.isTaskTreeRequested get() = executionPlan?.requestedTasks?.any { it is TaskTreeTask } ?: false -val Project.requestedTasks get() = executionPlan?.requestedTasks?.filter { it !is TaskTreeTask } +val Project.isDiagonGraphRequested get() = executionPlan?.requestedTasks?.any { it is DiagonDagTask } ?: false + +val Project.requestedTasks + get() = executionPlan?.requestedTasks?.filter { + it !is TaskTreeTask && it !is DiagonDagTask + } fun DefaultTaskExecutionGraph.getAllDeps(task: Task): Set = getDeps(task) From bb32537874d25dba6bfa338d2c5dcfe0423b333f Mon Sep 17 00:00:00 2001 From: klee0kai Date: Sun, 6 Aug 2023 15:00:38 +0400 Subject: [PATCH 2/5] run configs --- .run/diagonDAG.run.xml | 25 +++++++++++++++++++++++++ .run/tasktree.run.xml | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 .run/diagonDAG.run.xml create mode 100644 .run/tasktree.run.xml diff --git a/.run/diagonDAG.run.xml b/.run/diagonDAG.run.xml new file mode 100644 index 0000000..0573e23 --- /dev/null +++ b/.run/diagonDAG.run.xml @@ -0,0 +1,25 @@ + + + + + + + false + true + false + true + + + \ No newline at end of file diff --git a/.run/tasktree.run.xml b/.run/tasktree.run.xml new file mode 100644 index 0000000..9973e59 --- /dev/null +++ b/.run/tasktree.run.xml @@ -0,0 +1,25 @@ + + + + + + + false + true + false + true + + + \ No newline at end of file From bda5d76db7f3b68ac9a4dd8ae51c1041e06f6b20 Mon Sep 17 00:00:00 2001 From: klee0kai Date: Sun, 6 Aug 2023 16:23:25 +0400 Subject: [PATCH 3/5] print tasks full names in graph --- .../klee0kai/tasktree/tasks/DiagonDagTask.kt | 7 ++----- .../github/klee0kai/tasktree/utils/ProjectExt.kt | 2 ++ .../github/klee0kai/tasktree/utils/TaskExt.kt | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 tasktree/src/main/kotlin/com/github/klee0kai/tasktree/utils/TaskExt.kt diff --git a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/tasks/DiagonDagTask.kt b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/tasks/DiagonDagTask.kt index 1805681..1b3c8c2 100644 --- a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/tasks/DiagonDagTask.kt +++ b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/tasks/DiagonDagTask.kt @@ -1,9 +1,6 @@ package com.github.klee0kai.tasktree.tasks -import com.github.klee0kai.tasktree.utils.getAllDeps -import com.github.klee0kai.tasktree.utils.getDeps -import com.github.klee0kai.tasktree.utils.requestedTasks -import com.github.klee0kai.tasktree.utils.taskGraph +import com.github.klee0kai.tasktree.utils.* import org.apache.tools.ant.util.TeeOutputStream import org.gradle.api.Project import org.gradle.api.model.ObjectFactory @@ -37,7 +34,7 @@ open class DiagonDagTask @Inject constructor( val depsCode = allTasks.joinToString("\n") { task -> project.taskGraph.getDeps(task).joinToString("\n") { dep -> - "${dep.name} -> ${task.name}" + "${dep.fullName} -> ${task.fullName}" } } diff --git a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/utils/ProjectExt.kt b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/utils/ProjectExt.kt index b89d294..61088f5 100644 --- a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/utils/ProjectExt.kt +++ b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/utils/ProjectExt.kt @@ -21,6 +21,8 @@ val Project.requestedTasks it !is TaskTreeTask && it !is DiagonDagTask } +val Project.parents get() = generateSequence(this) { it.parent } + fun DefaultTaskExecutionGraph.getAllDeps(task: Task): Set = getDeps(task) .flatMap { diff --git a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/utils/TaskExt.kt b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/utils/TaskExt.kt new file mode 100644 index 0000000..8f0e9ee --- /dev/null +++ b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/utils/TaskExt.kt @@ -0,0 +1,16 @@ +package com.github.klee0kai.tasktree.utils + +import org.gradle.api.Task + +val Task.fullName + get() = buildString { + project.parents + .toList() + .reversed() + .forEach { project -> + val isRoot = project.parent == null + if (!isRoot) append(":${project.name}") + } + append(":${name}") + } + From b41d029d0fd1b5e9f69a81e6befbad47ee30aab7 Mon Sep 17 00:00:00 2001 From: klee0kai Date: Sun, 6 Aug 2023 16:23:34 +0400 Subject: [PATCH 4/5] disable all tasks --- .../com/github/klee0kai/tasktree/TaskTreePlugin.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/TaskTreePlugin.kt b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/TaskTreePlugin.kt index e745d6c..782dbc7 100644 --- a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/TaskTreePlugin.kt +++ b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/TaskTreePlugin.kt @@ -2,10 +2,7 @@ package com.github.klee0kai.tasktree import com.github.klee0kai.tasktree.tasks.DiagonDagTask import com.github.klee0kai.tasktree.tasks.TaskTreeTask -import com.github.klee0kai.tasktree.utils.isDiagonGraphRequested -import com.github.klee0kai.tasktree.utils.isTaskTreeRequested -import com.github.klee0kai.tasktree.utils.requestedTasks -import com.github.klee0kai.tasktree.utils.taskGraph +import com.github.klee0kai.tasktree.utils.* import org.gradle.api.Plugin import org.gradle.api.Project @@ -20,7 +17,11 @@ open class TaskTreePlugin : Plugin { taskGraph.whenReady { if (isTaskTreeRequested || isDiagonGraphRequested) { - requestedTasks?.forEach { it.enabled = false } + requestedTasks + ?.flatMap { project.taskGraph.getAllDeps(it) } + ?.forEach { + it.enabled = false + } } } } From ff6c3b470550b92eb061e907b910681095576854 Mon Sep 17 00:00:00 2001 From: klee0kai Date: Sun, 6 Aug 2023 16:24:38 +0400 Subject: [PATCH 5/5] print task full name everywhere --- .../kotlin/com/github/klee0kai/tasktree/tasks/TaskTreeTask.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/tasks/TaskTreeTask.kt b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/tasks/TaskTreeTask.kt index d9a78d5..7fb34e0 100644 --- a/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/tasks/TaskTreeTask.kt +++ b/tasktree/src/main/kotlin/com/github/klee0kai/tasktree/tasks/TaskTreeTask.kt @@ -52,7 +52,7 @@ open class TaskTreeTask @Inject constructor( } withStyle(Identifier) - .text(task.name) + .text(task.fullName) if (ext.printPrice) { withStyle(Description)