/*
 *  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']) {
    if (JavaVersion.current().java7Compatible) {
        dependsOn 'asciidocAll', 'assembleAsciidoc'
    }
    ext.footer = 'Copyright &copy; 2003-2018 The Apache Software Foundation. All rights reserved.'
    ext.title = "Groovy ${groovyVersion}"
}

if (JavaVersion.current().java7Compatible) {
    task assembleAsciidoc(type: Copy, dependsOn: 'asciidocAll') {
        subprojects {
            from project.asciidoctor
        }
        into "$buildDir/asciidoc"
    }

    task asciidocAll(dependsOn: 'asciidoctor')
}

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

def groovydocSpec = {
    // hack to avoid errors when using gradle 3 to get the wrapper
    // we won't ever build 2.4.x using a 3+ version - at least not
    // without further changes
    if (gradle.gradleVersion.startsWith('2.')) {
        use = true
        if (project != rootProject) source = project.sourceSets.main.allSource
        classpath = javadoc.classpath
        ext.windowtitle = doc.title
        ext.doctitle = doc.title
        header = doc.title
        footer = doc.footer
        overview = rootProject.file('src/main/overview.html')
        includePrivate = false
        link 'http://docs.oracle.com/javaee/7/api/', 'javax.servlet.', 'javax.management.'
        link 'http://docs.oracle.com/javase/8/docs/api/', 'java.', 'org.xml.', 'javax.', 'org.w3c.'
        link 'http://docs.groovy-lang.org/docs/ant/api/', 'org.apache.ant.', 'org.apache.tools.ant.'
        link 'http://junit.org/junit4/javadoc/latest/', 'org.junit.', 'junit.'
        link 'http://www.antlr2.org/javadoc/', 'antlr.'
        link 'http://commons.apache.org/proper/commons-cli/javadocs/api-release/', 'org.apache.commons.cli.'
    }
}

allprojects {
    javadoc javadocSpec
    groovydoc groovydocSpec
}

// 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
    }
}
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 groovydocSpec

// 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("${project(':groovy-docgenerator').buildDir}/classes/main")
    into('META-INF') {
        from('src/main/META-INF/groovy-release-info.properties') {
            filter(rootProject.propertiesFilter, org.apache.tools.ant.filters.ReplaceTokens)
        }
    }
    from('subprojects/groovy-docgenerator/src/main/resources')
}

task docGDK {
//    ext.extraDocGDKclasses = []
    dependsOn([project(':groovy-groovydoc'), project(':groovy-docgenerator')]*.classes)
    // TODO don't hard-code these
    dependsOn([project(':groovy-sql'), project(':groovy-xml'), project(':groovy-swing')]*.classes)
    if(JavaVersion.current().isJava7Compatible()) {
        dependsOn([project(':groovy-nio')]*.classes)
    }
    dependsOn docProjectVersionInfo
    ext.destinationDir = "$buildDir/html/groovy-jdk"
    inputs.files sourceSets.main.runtimeClasspath + configurations.tools
    outputs.dir destinationDir
    doLast { task ->
        try {
            ant {
                java(classname: 'org.codehaus.groovy.tools.DocGenerator',
                     fork: 'true',
                     failonerror: 'true',
                     classpath: (sourceSets.main.runtimeClasspath + configurations.tools + groovydocAll.groovyClasspath).asPath,
                     errorproperty: 'edr',
                     outputproperty: 'odr') {
                    arg(value: '-title')
                    arg(value: 'Groovy JDK enhancements')
                    arg(value: '-link')
                    arg(value: 'groovy,org.codehaus.groovy=http://docs.groovy-lang.org/latest/html/gapi/')
                    arg(value: '-link')
                    arg(value: 'java,org.xml,javax,org.w3c=http://docs.oracle.com/javase/7/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.DateGroovyMethods')
                    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.v6.PluginDefaultGroovyMethods')
                    arg(value: 'org.codehaus.groovy.vmplugin.v6.PluginStaticGroovyMethods')
                    // TODO don't hard-code these
                    arg(value: 'subprojects/groovy-sql/src/main/java/org/codehaus/groovy/runtime/SqlGroovyMethods.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')
                    if(JavaVersion.current().isJava7Compatible()) {
                        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 'src/tools/org/codehaus/groovy/tools/groovy.ico', 'src/tools/org/codehaus/groovy/tools/stylesheet.css'
        }
    }
}

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

if (JavaVersion.current().isJava7Compatible()) {
    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')
        }
    }
}
