Skip to content

Instantly share code, notes, and snippets.

@arunkumar9t2
Last active July 21, 2023 15:43

Revisions

  1. arunkumar9t2 revised this gist May 25, 2022. 1 changed file with 18 additions and 3 deletions.
    21 changes: 18 additions & 3 deletions DependantModulesTask.kt
    Original file line number Diff line number Diff line change
    @@ -50,10 +50,19 @@ abstract class DependantModulesTask : DefaultTask() {
    }

    val projectToFind = rootProject.project(projectPath)
    val dependants = Graphs.reachableNodes(buildGraph.asGraph(), projectToFind)

    logger.quiet("\n${projectToFind.path}'s dependants : \n${dependants.joinToString(separator = "\n") { it.path }}")
    val dependants = Graphs.reachableNodes(buildGraph.asGraph(), projectToFind)

    logger.quiet(buildString {
    appendLine()
    append("${projectToFind.path}'s dependants :")
    appendLine()
    append(
    dependants
    .filter { it != projectToFind }
    .joinToString(separator = "\n") { it.path }
    )
    })
    logger.quiet("----------\n")

    val subgraph = Graphs.inducedSubgraph(buildGraph, dependants)
    @@ -64,7 +73,13 @@ abstract class DependantModulesTask : DefaultTask() {
    .edgeValue(endpointPair)
    .map { it.name }
    .orElse("")
    logger.quiet("${endpointPair.nodeV().path} ---$configuration---> ${endpointPair.nodeU().path}")
    val sourceProject = endpointPair.nodeV().path
    val dependencyProject = endpointPair.nodeU().path
    logger.quiet(
    """|$sourceProject
    | $configuration(project("$dependencyProject"))"""
    .trimMargin()
    )
    }
    }
    }
  2. arunkumar9t2 created this gist May 24, 2022.
    70 changes: 70 additions & 0 deletions DependantModulesTask.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    import com.google.common.graph.Graphs
    import com.google.common.graph.MutableValueGraph
    import com.google.common.graph.ValueGraphBuilder
    import org.gradle.api.DefaultTask
    import org.gradle.api.Project
    import org.gradle.api.artifacts.Configuration
    import org.gradle.api.artifacts.ProjectDependency
    import org.gradle.api.tasks.Input
    import org.gradle.api.tasks.TaskAction
    import org.gradle.api.tasks.options.Option

    abstract class DependantModulesTask : DefaultTask() {
    @Option(
    option = "project-path",
    description = "The project path to find dependencies for"
    )
    @Input
    lateinit var projectPath: String

    @TaskAction
    fun action() {
    val rootProject = project.rootProject

    val buildGraph: MutableValueGraph<Project, Configuration> =
    ValueGraphBuilder.directed()
    .allowsSelfLoops(false)
    .expectedNodeCount(rootProject.subprojects.size)
    .build()


    rootProject.subprojects.forEach { sourceProject ->
    buildGraph.addNode(sourceProject)
    sourceProject
    .configurations
    .flatMap { configuration ->
    configuration
    .dependencies
    .filterIsInstance<ProjectDependency>()
    .map { projectDependency -> configuration to projectDependency }
    }.forEach { (configuration, projectDependency) ->
    if (sourceProject != projectDependency.dependencyProject) {
    // Invert the order since we are only interested in reverse dependencies
    buildGraph.putEdgeValue(
    projectDependency.dependencyProject,
    sourceProject,
    configuration
    )
    }
    }
    }

    val projectToFind = rootProject.project(projectPath)
    val dependants = Graphs.reachableNodes(buildGraph.asGraph(), projectToFind)

    logger.quiet("\n${projectToFind.path}'s dependants : \n${dependants.joinToString(separator = "\n") { it.path }}")

    logger.quiet("----------\n")

    val subgraph = Graphs.inducedSubgraph(buildGraph, dependants)
    subgraph
    .edges()
    .forEach { endpointPair ->
    val configuration = buildGraph
    .edgeValue(endpointPair)
    .map { it.name }
    .orElse("")
    logger.quiet("${endpointPair.nodeV().path} ---$configuration---> ${endpointPair.nodeU().path}")
    }
    }
    }