/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

task doc(dependsOn: ['javadocAll', 'groovydocAll', 'docGDK', 'asciidocAll']) {
    ext.footer = 'Copyright &copy; 2003-2021 The Apache Software Foundation. All rights reserved.'
    ext.title = "Groovy ${groovyVersion}"
}

task asciidocAll(type: Copy) {
    allprojects {
        dependsOn asciidoctor
        from asciidoctor.outputDir
    }
    into "$buildDir/asciidocAll"
}

def javadocSpec = {
    maxMemory = javaDoc_mx
    project.configure(options) {
        if (JavaVersion.current().isJava9Compatible()) {
            options.addBooleanOption('html5', true)
        }
        windowTitle = doc.title
        docTitle = doc.title
        classpath += project.file('src/main/java')
        locale = 'en_US'
        encoding = 'UTF-8'
        version = true
        overview = rootProject.file('src/main/java/overviewj.html')
        footer = doc.footer
        source = '1.7'
        links('https://docs.oracle.com/javase/8/docs/api/', 'https://docs.oracle.com/javaee/7/api/',
                'https://commons.apache.org/proper/commons-cli/javadocs/api-release/', 'https://junit.org/junit4/javadoc/latest/',
                'https://docs.oracle.com/javaee/6/api/', 'https://www.antlr2.org/javadoc/')
    }
}

def groovydocBaseSpec = {
    use = true
    ext.windowtitle = doc.title
    ext.doctitle = doc.title
    header = doc.title
    footer = doc.footer
    overviewText = rootProject.resources.text.fromFile('src/main/java/overview.html')
    includePrivate = false
    link 'https://docs.oracle.com/javaee/7/api/', 'javax.servlet.', 'javax.management.'
    link 'https://docs.oracle.com/javase/8/docs/api/', 'java.', 'org.xml.', 'javax.', 'org.w3c.'
    link 'https://docs.groovy-lang.org/docs/ant/api/', 'org.apache.ant.', 'org.apache.tools.ant.'
    link 'https://junit.org/junit4/javadoc/latest/', 'org.junit.', 'junit.'
    link 'https://junit.org/junit5/docs/current/api/', 'org.junit.jupiter.', 'org.junit.platform.'
    link 'https://www.antlr2.org/javadoc/', 'antlr.'
    link 'https://www.antlr.org/api/Java/', 'org.antlr.v4.'
    link 'https://commons.apache.org/proper/commons-cli/javadocs/api-release/', 'org.apache.commons.cli.'
    link 'https://picocli.info/apidocs/', 'picocli.'
}

def groovydocSpec = groovydocBaseSpec >> {
    source = project.sourceSets.main.allSource
    classpath = javadoc.classpath
}

allprojects {
    javadoc javadocSpec
    groovydoc groovydocSpec
}

javadoc {
    exclude '**/GroovyRecognizer.java' // generated file
}

evaluationDependsOn('groovy-jaxb')

// Root project has an extra 'all' javadoc task
task javadocAll(type: Javadoc)
javadocAll {
    destinationDir = new File(buildDir, 'alljavadoc')
    source = javadoc.source
    classpath = javadoc.classpath
    subprojects.each { sp ->
        source += sp.javadoc.source
        classpath += sp.javadoc.classpath
    }
    exclude '**/GroovyRecognizer.java' // generated file
}
javadocAll javadocSpec

// Root project has an extra 'all' groovydoc task
task groovydocAll(type: Groovydoc)
groovydocAll {
    dependsOn( { project(':groovy-groovydoc').classes })
    dependsOn( { project(':groovy-docgenerator').classes })
    destinationDir = new File(buildDir, 'allgroovydoc')
    source = groovydoc.source
    classpath = groovydoc.classpath
    groovyClasspath = groovydoc.groovyClasspath
    subprojects.each { sp ->
        source += sp.groovydoc.source
        classpath += sp.groovydoc.classpath
        groovyClasspath += sp.groovydoc.groovyClasspath
    }
}
groovydocAll groovydocBaseSpec

// when docgenerator is run by the build, it requires a groovy-release-info file
// but the file is only generated by the 'jar' task, so as a workaround, we copy
// it into the docgenerator classes
task docProjectVersionInfo(type: Copy) {
    destinationDir = file("${buildDir}/docgenerator-resources")
    into('META-INF') {
        from(generateReleaseInfo)
    }
    from('subprojects/groovy-docgenerator/src/main/resources')
}

task docGDK {
    outputs.cacheIf { true }
    dependsOn([project(':groovy-groovydoc'), project(':groovy-docgenerator'), project(':groovy-cli-picocli')]*.classes)
    dependsOn docProjectVersionInfo
    ext.destinationDir = "$buildDir/html/groovy-jdk"
    inputs.files sourceSets.main.runtimeClasspath + configurations.tools + files(docProjectVersionInfo.destinationDir)
    outputs.dir destinationDir
    def docGeneratorPath = files(project(':groovy-docgenerator').sourceSets.main.output.classesDirs) + files(project(':groovy-cli-picocli').sourceSets.main.output.classesDirs)
    doLast { task ->
        try {
            ant {
                java(classname: 'org.apache.groovy.docgenerator.DocGenerator',
                     fork: 'true',
                     failonerror: 'true',
                     classpath: (sourceSets.main.runtimeClasspath + rootProject.files(docProjectVersionInfo.destinationDir) + configurations.tools + groovydocAll.groovyClasspath + docGeneratorPath).asPath,
                     errorproperty: 'edr',
                     outputproperty: 'odr') {
                    arg(value: '-title')
                    arg(value: 'Groovy JDK enhancements')
                    arg(value: '-link')
                    arg(value: 'groovy,org.codehaus.groovy=https://docs.groovy-lang.org/latest/html/gapi/')
                    arg(value: '-link')
                    arg(value: 'java,org.xml,javax,org.w3c=https://docs.oracle.com/javase/8/docs/api/')
                    // either package name if in core or fully qualified path otherwise
                    arg(value: 'org.codehaus.groovy.runtime.DefaultGroovyMethods')
                    arg(value: 'org.codehaus.groovy.runtime.DefaultGroovyStaticMethods')
                    arg(value: 'org.codehaus.groovy.runtime.EncodingGroovyMethods')
                    arg(value: 'org.codehaus.groovy.runtime.IOGroovyMethods')
                    arg(value: 'org.codehaus.groovy.runtime.ProcessGroovyMethods')
                    arg(value: 'org.codehaus.groovy.runtime.ResourceGroovyMethods')
                    arg(value: 'org.codehaus.groovy.runtime.SocketGroovyMethods')
                    arg(value: 'org.codehaus.groovy.runtime.StringGroovyMethods')
                    arg(value: 'org.codehaus.groovy.vmplugin.v5.PluginDefaultGroovyMethods')
                    arg(value: 'org.codehaus.groovy.vmplugin.v8.PluginDefaultGroovyMethods')
                    // TODO don't hard-code these
                    arg(value: 'subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilExtensions.java')
                    arg(value: 'subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeExtensions.java')
                    arg(value: 'subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeStaticExtensions.java')
                    arg(value: 'subprojects/groovy-jaxb/src/main/groovy/org/apache/groovy/jaxb/extensions/JaxbExtensions.java')
                    arg(value: 'subprojects/groovy-sql/src/main/java/org/apache/groovy/sql/extensions/SqlExtensions.java')
                    arg(value: 'subprojects/groovy-swing/src/main/java/org/codehaus/groovy/runtime/SwingGroovyMethods.java')
                    arg(value: 'subprojects/groovy-xml/src/main/java/org/codehaus/groovy/runtime/XmlGroovyMethods.java')
                    arg(value: 'subprojects/groovy-nio/src/main/java/org/codehaus/groovy/runtime/NioGroovyMethods.java')
                    arg(value: 'subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/ScriptExtensions.java')
                    arg(value: 'subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/ScriptStaticExtensions.java')
                }
            }
        } finally {
            if (ant.properties.odr) {
                logger.info 'Out: ' + ant.properties.odr
            }
            if (ant.properties.edr) {
                logger.error 'Err: ' + ant.properties.edr
            }
        }
        copy {
            into task.destinationDir
            from 'subprojects/groovy-docgenerator/src/main/resources/org/apache/groovy/docgenerator/groovy.ico',
                    'subprojects/groovy-docgenerator/src/main/resources/org/apache/groovy/docgenerator/stylesheet.css'
        }
    }
}

// apply javadoc fix tool to generated javadocs - ensures no vulnerability even on old JDK versions
allprojects {
    apply plugin: 'javadocHotfix'
}

javadocAll.options.source = '1.7'

if (JavaVersion.current().isJava8Compatible()) {
    allprojects {
        tasks.withType(Javadoc) {
            // disable the crazy super-strict doclint tool in Java 8
            options.addStringOption('Xdoclint:none', '-quiet')
        }
    }
}
