adamko-dev / dokkatoo

Generates documentation for Kotlin Gradle projects (based on Dokka)
https://adamko-dev.github.io/dokkatoo/
Apache License 2.0
56 stars 7 forks source link

MultiModules: Depend on all child projects by default #91

Open hfhbd opened 1 year ago

hfhbd commented 1 year ago

The current design requires to add all projects manually in the dependency block of the root project:

dependencies {
  dokkatoo(projects.foo)
}

Adding some default mechanism would be nice to not add all projects manually. This is somehow annoying with projects containing many projects.

One option: Use a build service to register a project when the dokkatoo plugin is applied. The root project should create the build service (it is evaluated at first) and could depend on the registered projects using dependencies.addLater.

Originally posted by @hfhbd in https://github.com/adamko-dev/dokkatoo/issues/14#issuecomment-1566132897

aSemy commented 1 year ago

Thanks for the request, I'll have to consider it.

At the moment I'm reluctant to add such behaviour

In the meantime, could you try this workaround? It should automatically add all subprojects.

// build.gradle.kts

plugins {
  id("dev.adamko.dokkatoo-html")
}

configurations.dokkatoo.configure {
  dependencies.addAllLater(
    // lazily add subprojects
    provider {
      // get all projects
      rootProject.allprojects
        // don't add _this_ subproject as a self-dependency
        .filter { it.path != project.path }
        // only get projects that have the Dokkatoo HTML plugin 
        // (adjust to include the other plugins if necessary)
        .filter { it.pluginManager.hasPlugin("dev.adamko.dokkatoo-html") }
        // create a dependency on the other project
        .map { project.dependencies.create(it) }
    }
  )
}

Filtering by plugin is necessary because current Dokkatoo doesn't use artifactView { lenient(true) }. If Dokkatoo was updated to fetch dependencies leniently, then it should be possible to just add all projects as dependencies and then Dokkatoo won't complain if a subproject doesn't have the plugin.

I also found this suggestion on SO: https://stackoverflow.com/q/65092844/4161471