plugins {
    // Apply the common versioning conventions.
    id 'org.jdrupes.vmoperator.versioning-conventions'
    
    id 'org.ajoberstar.git-publish'
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

var docDestinationDir = file("${rootProject.buildDir}/javadoc")

configurations {
    javadocTaglets
}

configurations {
    markdownDoclet
    javadocTaglets
}

dependencies {
    markdownDoclet "org.jdrupes.mdoclet:doclet:4.0.0"
    javadocTaglets "org.jdrupes.taglets:plantuml-taglet:3.0.0"
}

task apidocs (type: JavaExec) {
    // Does not work on JitPack, no /usr/bin/dot
    enabled = JavaVersion.current() == JavaVersion.VERSION_21

    outputs.dir(docDestinationDir)
    
    inputs.file rootProject.file('overview.md')
    inputs.file "${rootProject.rootDir}/misc/javadoc-overwrites.css"

    jvmArgs = ['--add-exports=jdk.compiler/com.sun.tools.doclint=ALL-UNNAMED',
        '--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
        '--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
        '--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
        '--add-exports=jdk.javadoc/jdk.javadoc.internal.tool=ALL-UNNAMED',
        '--add-exports=jdk.javadoc/jdk.javadoc.internal.doclets.toolkit=ALL-UNNAMED',
        '--add-opens=jdk.javadoc/jdk.javadoc.internal.doclets.toolkit.resources.releases=ALL-UNNAMED',
        '-Duser.language=en', '-Duser.region=US']
    mainClass = 'jdk.javadoc.internal.tool.Main'

    gradle.projectsEvaluated {
        // Make sure that other projects' compileClasspaths are resolved
        rootProject.subprojects
            .findAll({project -> project.name.startsWith("org.jdrupes")})
        .each{project -> dependsOn project.tasks.getByName('assemble')}
        
        classpath = files(rootProject.subprojects
            .findAll({project -> project.name.startsWith("org.jdrupes")})
            .collect {project -> project.sourceSets.main.compileClasspath})
        var sourceDirs = rootProject.subprojects
            .findAll({project -> project.name.startsWith("org.jdrupes")})
            .collect{project -> project.sourceSets.main.allJava.getSrcDirs() }
            .flatten().findAll(dir -> dir.exists())
        sourceDirs.each { inputs.dir(it) }
        
        args = ['-doctitle', """VM-Operator 
                (runner.qemu-${-> findProject(':org.jdrupes.vmoperator.runner.qemu').version}, 
                manager-${-> findProject(':org.jdrupes.vmoperator.manager').version})""",
            '-package',
            '-use',
            '-linksource',
            '-link', 'https://docs.oracle.com/en/java/javase/21/docs/api/',
            '-link', 'https://jgrapes.org/latest-release/javadoc/',
            '-link', 'https://freemarker.apache.org/docs/api/',
            '--add-exports', 'jdk.javadoc/jdk.javadoc.internal.tool=ALL-UNNAMED',
            '--add-exports', 'jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
            '-doclet', 'org.jdrupes.mdoclet.MDoclet',
            '-docletpath', configurations.markdownDoclet.files.asType(List).join(":"),
            '--disable-auto-highlight',
            '-tagletpath', configurations.javadocTaglets.files.asType(List).join(":"),
            '-taglet', 'org.jdrupes.taglets.plantUml.PlantUml',
            '-taglet', 'org.jdrupes.taglets.plantUml.StartUml',
            '-taglet', 'org.jdrupes.taglets.plantUml.EndUml',
            '-overview', "${rootProject.file('overview.md')}",
            '-d', docDestinationDir,
            '-sourcepath', sourceDirs.join(':'),
            '-subpackages', 'org.jdrupes',
            '-bottom', rootProject.file("misc/javadoc.bottom.txt").text,
            '--allow-script-in-comments',
            '-Xdoclint:-html',
            '--add-stylesheet', "${rootProject.rootDir}/misc/javadoc-overwrites.css",
            '--add-exports=jdk.javadoc/jdk.javadoc.internal.doclets.formats.html=ALL-UNNAMED',
            '-quiet'
            ]
    }
    
    ignoreExitValue true
}

task testJavadoc(type: Javadoc) {
    enabled = JavaVersion.current() == JavaVersion.VERSION_21

    source = fileTree(dir: 'testfiles', include: '**/*.java')
    destinationDir = project.file("build/testfiles-gradle")
    options.docletpath = configurations.markdownDoclet.files.asType(List)
    options.doclet = 'org.jdrupes.mdoclet.MDoclet'
    options.overview = 'testfiles/overview.md'
    options.addStringOption('Xdoclint:-html', '-quiet')

    options.setJFlags([
        '--add-exports=jdk.compiler/com.sun.tools.doclint=ALL-UNNAMED',
        '--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED',
        '--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED',
        '--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED',
        '--add-exports=jdk.javadoc/jdk.javadoc.internal.tool=ALL-UNNAMED',
        '--add-exports=jdk.javadoc/jdk.javadoc.internal.doclets.toolkit=ALL-UNNAMED',
        '--add-opens=jdk.javadoc/jdk.javadoc.internal.doclets.toolkit.resources.releases=ALL-UNNAMED'])
}
// Prepare github authentication for plugins
if (System.properties['org.ajoberstar.grgit.auth.username'] == null) {
    System.setProperty('org.ajoberstar.grgit.auth.username',
        project.rootProject.properties['website.push.token'] ?: "nouser")
}
