apply plugin: 'kotlin'
apply plugin: 'java'
apply plugin: 'org.jetbrains.dokka'

def ghPagesPathWithoutVersion = "../${project.name}-gh-pages"
def ghPagesPath = "$ghPagesPathWithoutVersion/$ghPages_version"

task copyIndex(type: Copy) {
    from "$ghPagesPathWithoutVersion/index.version.md"
    into ghPagesPath
    rename('index.version.md', 'index.md')
    expand(version: ghPages_version)
    filteringCharset = 'UTF-8'
}

task copyCSS(type: Copy) {
    from "$ghPagesPathWithoutVersion/style.css"
    into  ghPagesPath
}

task removeGhPages {
    doFirst {
        delete ghPagesPath
    }
}

task ghPages {
    docProjects.forEach { subProject ->
        def jar = subProject.tasks.findByName("jar")
        dependsOn jar
        dokka.mustRunAfter jar
    }
    dependsOn removeGhPages
    dependsOn dokka
    dokka.mustRunAfter removeGhPages
    finalizedBy copyCSS
    finalizedBy copyIndex
}

dokka {
    moduleName = 'doc'
    outputFormat = 'html'
    outputDirectory = "$ghPagesPathWithoutVersion/$ghPages_version"

    includes = ['misc/kdoc/packages.md']
    jdkVersion = 8
    reportUndocumented = false

    doFirst {
        println "going to generate kdoc including the projects: ${docProjects*.name}"
        println "====================="
    }
    linkMapping {
        dir = './'
        url = "$github_url/$dokka_sourceMapping/"
        suffix = '#L'
    }

    samples = findSamples(docProjects)

    sourceDirs = docProjects.collect { subProject -> file("${subProject.projectDir}/$srcKotlin") }
    rootProject.configure(docProjects) { subProject ->
        subProject.afterEvaluate {
            classpath += kotlin.target.compilations.getByName("main").compileDependencyFiles.files.collect { file -> file }
        }
    }

    [
        "ch.tutteli.atrium.api.verbs.internal",
        //TODO remove the below with 0.17.0
        "ch.tutteli.atrium.domain.builders.reporting.impl",
        //TODO remove with 0.18.0
        "ch.tutteli.atrium.assertions.builders.impl",

    ].each { pkg ->
        packageOptions {
            prefix = pkg
            suppress = true
        }
    }
}
