Kotlin / dokka

API documentation engine for Kotlin
https://kotl.in/dokka
Apache License 2.0
3.41k stars 404 forks source link

How to use "documentedVisibilities" in groovy DSL? #2981

Closed rimdoo closed 1 year ago

rimdoo commented 1 year ago

Question I get below error message when add documentedVisibilities property.

Execution failed for task ':uikit:dokkaHtml'.
> Error while evaluating property 'unsuppressedSourceSets' of task ':uikit:dokkaHtml'
   > Could not create domain object 'debug' (GradleDokkaSourceSetBuilder)

Screenshots

build.gradle file

plugins {
    ...
    id 'org.jetbrains.dokka' version '1.8.10'
}

...

dokkaHtml {
    print "uikit doc path=$UIKIT_DOC_PATH"
    outputDirectory.set(file(UIKIT_DOC_PATH))

    // Set module name displayed in the final output
    moduleName.set("UIKit")
    dokkaSourceSets.configureEach {
        // The set of visibility modifiers that should be documented.
        documentedVisibilities.set(
            setOf(
                org.jetbrains.dokka.DokkaConfiguration.Visibility.PUBLIC, // Same for both Kotlin and Java
                org.jetbrains.dokka.DokkaConfiguration.Visibility.PRIVATE, // Same for both Kotlin and Java
                org.jetbrains.dokka.DokkaConfiguration.Visibility.PROTECTED, // Same for both Kotlin and Java
                org.jetbrains.dokka.DokkaConfiguration.Visibility.INTERNAL, // Kotlin-specific internal modifier
                org.jetbrains.dokka.DokkaConfiguration.Visibility.PACKAGE, // Java-specific package-private visibility
            )
        )
        // Whether to document declarations annotated with @Deprecated.
        skipDeprecated.set(false)
        // Whether to document, analyze generated files.
        suppressGeneratedFiles.set(false)

        // Suppress a package
        perPackageOption {
            matchingRegex.set(".*\\.internal.*")
            suppress.set(true)
        }
    }
}

Installation

plugins {
    id 'com.android.application' version '7.4.2' apply false
    id 'com.android.library' version '7.4.2' apply false
    id 'com.google.gms.google-services' version '4.3.15' apply false
}

Additional context I want to write PROTECTED methods in out api reference. Please let me know how to use it in groovy dsl!

IgnatBeresnev commented 1 year ago

It looks like you are trying to copy Kotlin DSL example snippets, and that means that you might be reading the old documentation for like Dokka 1.4... The new documentation has groovy-dsl examples for what you are trying to do.

setOf(..) is a Kotlin function, it doesn't exist in Groovy, that's why it's not working.

        documentedVisibilities.set(
            setOf(
                org.jetbrains.dokka.DokkaConfiguration.Visibility.PUBLIC, // Same for both Kotlin and Java
                org.jetbrains.dokka.DokkaConfiguration.Visibility.PRIVATE, // Same for both Kotlin and Java
                org.jetbrains.dokka.DokkaConfiguration.Visibility.PROTECTED, // Same for both Kotlin and Java
                org.jetbrains.dokka.DokkaConfiguration.Visibility.INTERNAL, // Kotlin-specific internal modifier
                org.jetbrains.dokka.DokkaConfiguration.Visibility.PACKAGE, // Java-specific package-private visibility
            )
        )

Change it to be [..], and it should work :) Let me know if it helps

        documentedVisibilities.set(
                [
                        org.jetbrains.dokka.DokkaConfiguration.Visibility.PUBLIC, // Same for both Kotlin and Java
                        org.jetbrains.dokka.DokkaConfiguration.Visibility.PRIVATE, // Same for both Kotlin and Java
                        org.jetbrains.dokka.DokkaConfiguration.Visibility.PROTECTED, // Same for both Kotlin and Java
                        org.jetbrains.dokka.DokkaConfiguration.Visibility.INTERNAL, // Kotlin-specific internal modifier
                        org.jetbrains.dokka.DokkaConfiguration.Visibility.PACKAGE, // Java-specific package-private visibility
                ]
        )
rimdoo commented 1 year ago

Thank you for your response!

I have 2 questions for other conditions!

  1. When I use AGP version 7.1.2, I got the same error :( Is there any way to use it on AGP 7.1.2?

  2. When I implement it in other script. I got the error like below

    > Error while evaluating property 'unsuppressedSourceSets' of task ':uikit:dokkaHtml'
    > NamedDomainObjectContainer#create(String) on GradleDokkaSourceSetBuilder container cannot be executed in the current context.

Screenshots

build.gradle (root)

buildscript {
    ext.dokka_version = '1.8.10'

    dependencies {
        ...
        classpath("org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version")
    }
}

build.gradle

...

apply from: 'publish.gradle'

...

publish.gradle (other script)

apply plugin: "org.jetbrains.dokka"

dokkaHtml {
    print "uikit doc path=$UIKIT_DOC_PATH"
    outputDirectory.set(file(UIKIT_DOC_PATH))

    // Set module name displayed in the final output
    moduleName.set("UIKit")
    dokkaSourceSets.configureEach {
        // The set of visibility modifiers that should be documented.
        documentedVisibilities.set(
                [
                        org.jetbrains.dokka.DokkaConfiguration.Visibility.PUBLIC, // Same for both Kotlin and Java
                        org.jetbrains.dokka.DokkaConfiguration.Visibility.PRIVATE, // Same for both Kotlin and Java
                        org.jetbrains.dokka.DokkaConfiguration.Visibility.PROTECTED, // Same for both Kotlin and Java
                        org.jetbrains.dokka.DokkaConfiguration.Visibility.INTERNAL, // Kotlin-specific internal modifier
                        org.jetbrains.dokka.DokkaConfiguration.Visibility.PACKAGE, // Java-specific package-private visibility
                ]
        )
        // Whether to document declarations annotated with @Deprecated.
        skipDeprecated.set(false)
        // Whether to document, analyze generated files.
        suppressGeneratedFiles.set(false)

        // Suppress a package
        perPackageOption {
            matchingRegex.set(".*\\.internal.*")
            suppress.set(true)
        }
    }
}
...

I want to know why it doesn't work ...!

IgnatBeresnev commented 1 year ago

Oh, after the update to AGP 7.1 you are now running into #2472 - it has a long explanation for the problem (AGP is to blame), but the solutions are simple:

Option №1: Upgrade to AGP 7.3, they fixed this bug and it should work.

Option №2: Apply the Dokka plugin in the root build.gradle file, like so:

plugins {
    id 'org.jetbrains.dokka' version '1.8.10'
}

It should work after that

rimdoo commented 1 year ago

Option №2:

It doesn't work... I got same error.. If I move dokkaHtml block to build.gradle, it works well. However, I implement dokkaHtml block in my own gradle script, it doesn't work.

Execution failed for task ':uikit:dokkaHtml'.
> Error while evaluating property 'unsuppressedSourceSets' of task ':uikit:dokkaHtml'
   > NamedDomainObjectContainer#create(String) on GradleDokkaSourceSetBuilder container cannot be executed in the current context.

if I remove apply plugin: "org.jetbrains.dokka" in my script file and add below code in root build.gradle

plugins {
    id 'org.jetbrains.dokka' version '1.8.10'
}

I failed it when synchronizing, I got below error...

org.gradle.api.GradleScriptException: A problem occurred evaluating script.
...

Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'dokkaHtml' for task ':uikit:generateDokkaHtml' of type org.gradle.api.DefaultTask.
rimdoo commented 1 year ago

@IgnatBeresnev Could you investigate it?!

IgnatBeresnev commented 1 year ago

These again look like groovy-specific problems :( We have extensive test coverage around multi-module projects and groovy-dsl (and a lot of projects use Dokka, we would've heard about a massive problem by now), so the problem is most likely in your configuration somewhere, and debugging groovy build scripts is not fun...

Have you been able to resolve it? If I found the right 'ui-kit' project, it looks like you have: https://github.com/sendbird/sendbird-uikit-android/blame/main/uikit/build.gradle#L90

rimdoo commented 1 year ago

Thank you! I wanted to declare it in the gradle file outside of build.gradle, but I couldn't find a way to do that. Currently, it is declared in build.gradle like what you refered to.

IgnatBeresnev commented 1 year ago

@rimdoo have a look at how it's implemented in coroutines, it's the easy way: link

However, the right (idiomatic) way to extract Dokka's configuration into a separate build.gradle file would be to use convention plugins. We're actually configuring Dokka this way: link