iurysza / module-graph

A Gradle Plugin for visualizing your project's structure, powered by mermaidjs

Home Page:https://plugins.gradle.org/plugin/dev.iurysouza.modulegraph

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support applying this plugin in the root build.gradle file

StylianosGakis opened this issue · comments

So I'll start off by saying that I am not sure if this is a bug and setting this plugin in the root build.gradle doesn't work or if my setup is somehow unconventional and brings this problem, but let me explain.

In the line dependencies.forEach { (source, targets) ->, for all the other modules it works perfectly fine. But one of those is also the root module, where source is ":", just a string with a colon.
This results in when these lines are hit, and we try to do last() but the list after trying to split on ":" is empty, so it crashes.

One thing I did locally to make this work was replacing

targets.forEach { target ->
    val sourceName = source.split(":").last { it.isNotBlank() }
    val targetName = target.split(":").last { it.isNotBlank() }

with

targets.forEach targetsForEach@{ target ->
  val sourceName = source.split(":").lastOrNull() { it.isNotBlank() } ?: return@targetsForEach
  val targetName = target.split(":").lastOrNull() { it.isNotBlank() } ?: return@targetsForEach

And this just works, since it ignores that module. Could even replace that section with something like this

dependencies.forEach { (source, targets) ->
  if (source == ":") return@forEach
  targets.forEach { target ->
    val sourceName = source.split(":").last { it.isNotBlank() }
    val targetName = target.split(":").last { it.isNotBlank() }
    if (sourceName != targetName) {
      arrows += "  $sourceName --> $targetName\n"
    }
  }
}

and it also works fine that way too.

Also, while trying this out, I wasn't sure if I should be applying it to the root project or the "base" module which brings in every other module, like the :app module typically is.
Setting it up in the root build.gradle made the most sense though, as this means that it also contains in the resulting graph other modules which may be head-less, like a micro-app module made to iterate faster without bringing all the baggage from the :app module or something like that in general.
If this is an addition that you'd like to make in the documentation (suggesting to set this up in the root project) maybe I can open a new issue to track that progress.

Can we consider this done now? Do you want to keep this open to potentially add something in the getting started docs regarding where to apply the plugin, or should this already be good enough since this works now if you put it in the root build.gradle.kts file?

Yeah, I think we can consider it done.
In the getting started section I do mention that

You'll just need to add it to your project's root build.gradle or build.gradle.kts file.

But maybe it's too easy to miss 😅

I did miss it, but that's just me, the important thing is that it's there. All good then!