Created
May 15, 2023 10:45
-
-
Save alexanderankin/1458a33385738a5e735efd9a7969dff2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import org.gradle.api.internal.tasks.AbstractTaskDependency | |
| import org.gradle.api.plugins.jvm.JvmTestSuite | |
| static String printDeps(Task task) { | |
| StringBuilder buffer = new StringBuilder(50) | |
| printDeps(task, buffer, "", "") | |
| return buffer.toString() | |
| } | |
| @SuppressWarnings('ConfigurationAvoidance') | |
| static List<Task> toTasks(Task parent, Object anything) { | |
| if (anything instanceof Task) { | |
| return List.of(anything) | |
| } else if (anything instanceof String) { | |
| def t = parent.project.tasks.findByPath(anything) | |
| // if (t == null) println('problem with findByPath') | |
| return t != null ? List.of(t) : List.<Task>of() | |
| } else if (anything instanceof ConfigurableFileCollection) { | |
| return toTasks(parent, anything.getBuiltBy()) | |
| } else if (anything instanceof Collection) { | |
| return (anything as Collection).stream().map(e -> toTasks(parent, e)).flatMap(List::stream).collect() | |
| } else if (anything instanceof TaskProvider<Task>) { | |
| Task t = (anything as TaskProvider<Task>).get() | |
| // if (t == null) println('problem with TaskProvider.get') | |
| return t != null ? List.of(t) : List.<Task>of() | |
| } else if (anything instanceof AbstractTaskDependency) { | |
| Set<Task> dependencies = (anything as AbstractTaskDependency).getDependencies(parent) | |
| // if (dependencies.size() == 0) println('problem with AbstractTaskDependency') | |
| return new ArrayList<>(dependencies) | |
| } else if (anything instanceof Buildable) { | |
| // var c = Class.forName('org.springframework.boot.gradle.plugin.SinglePublishedArtifact') | |
| def dependencies = (anything as Buildable).getBuildDependencies().getDependencies(parent) | |
| // if (dependencies.size() == 0) println 'none found for Buildable' | |
| return new ArrayList<Task>(dependencies) | |
| } else if (anything instanceof NamedDomainObjectProvider) { | |
| // println((anything.getClass().getGenericSuperclass() as ParameterizedType).getActualTypeArguments()) | |
| NamedDomainObjectProvider ndop = anything as NamedDomainObjectProvider | |
| var item = ndop.get() | |
| if (item == null) return List.of() | |
| if (item instanceof JvmTestSuite) { | |
| def bd = (item as JvmTestSuite).getBuildDependencies() | |
| def d = bd.getDependencies(parent) | |
| return new ArrayList<Task>(d) | |
| } else { | |
| println 'NDOP with other item type: ' + item?.getClass() | |
| } | |
| } | |
| println 'we tried, but next is: ' + anything.getClass().getSimpleName() | |
| println 'we tried, but next is: ' + anything.getClass().getName() | |
| return List.of() | |
| } | |
| static void printDeps(Task task, StringBuilder buffer, String prefix, String childrenPrefix) { | |
| if (task == null || task.dependsOn.isEmpty()) return | |
| buffer.append(prefix) | |
| buffer.append('task: ' + task.name) | |
| buffer.append('\n') | |
| List<Object> deps = new ArrayList<>(task.dependsOn) | |
| for (int i = 0; i < deps.size(); i++) { | |
| Object dep = deps.get(i) | |
| List<Task> tasks = toTasks(task, dep) | |
| Task next = null | |
| if (tasks.size() == 1) { | |
| next = tasks.get(0) | |
| } else if (!tasks.empty) { | |
| println 'pushing ' + tasks | |
| for (final def t in tasks) deps.add(t) | |
| } | |
| if (next == null) return | |
| boolean hasNext = i != deps.size() - 1 | |
| if (hasNext) | |
| printDeps(next, buffer, childrenPrefix + "├── ", childrenPrefix + "│ ") | |
| else | |
| printDeps(next, buffer, childrenPrefix + "└── ", childrenPrefix + " ") | |
| } | |
| } | |
| tasks.register('taskGraph') { | |
| doLast { | |
| project.tasks.findAll().forEach { task -> | |
| def deps = printDeps(task) | |
| def found = -1 != deps.indexOf('\n') | |
| if ((found ? deps.substring(deps.indexOf('\n')) : deps).trim().isEmpty()) return | |
| println deps | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment