/*
 *  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
 *
 *    https://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.
 */

import org.asciidoctor.gradle.jvm.AsciidoctorTask

apply plugin: 'org.asciidoctor.jvm.convert'

configurations.register('documentation') {
    canBeConsumed = false
    canBeResolved = true
    attributes {
        attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.LIBRARY))
        attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling, Bundling.EXTERNAL))
        attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME))
    }

}

dependencies {
    documentation platform("org.apache.grails:grails-bom:$grailsVersion")
    documentation 'org.apache.groovy:groovy-ant'
    documentation 'org.apache.groovy:groovy-groovydoc'
}

tasks.withType(Groovydoc).configureEach {
    access = GroovydocAccess.PROTECTED
    processScripts = false
    includeMainForScripts = false
    includeAuthor = false
    classpath = configurations.documentation
    groovyClasspath = configurations.documentation
}

tasks.named('asciidoctor', AsciidoctorTask) {
    group = 'documentation'
    description = 'Generates the reference documentation'
    sourceDir = layout.projectDirectory.dir('src/docs')
    outputDir = layout.buildDirectory.dir('docs/guide').get().asFile
    baseDirFollowsSourceDir()
    jvm {
        jvmArgs(
                '--add-opens', 'java.base/sun.nio.ch=ALL-UNNAMED',
                '--add-opens', 'java.base/java.io=ALL-UNNAMED'
        )
    }
    attributes(
            copyright            : 'Apache License, Version 2.0',
            docinfo1             : 'true',
            doctype              : 'book',
            encoding             : 'utf-8',
            icons                : 'font',
            id                   : name + ':' + version,
            idprefix             : '',
            idseparator          : '-',
            lang                 : 'en',
            linkattrs            : true,
            numbered             : '',
            producer             : 'Asciidoctor',
            revnumber            : version,
            setanchors           : true,
            'source-highlighter' : 'prettify',
            toc                  : 'left',
            toc2                 : '',
            toclevels            : '2'
    )
    inputs.dir(sourceDir)
    outputs.dir(outputDir)
}

tasks.register('docs') {
    group = 'documentation'
    description = 'Generates both API and Guide documentation'
    dependsOn('groovydoc', 'asciidoctor')
    def redirectFile = layout.buildDirectory.file('docs/index.html')
    outputs.file(redirectFile)
    doLast {
        redirectFile.get().asFile.text = """\
            <html lang="en">
                <head>
                    <title>Grails Quartz Plugin Documentation</title>
                    <meta http-equiv="refresh" content="0; url=guide/index.html" />
                </head>
                <body>
                </body>
            </html>
        """.stripIndent(12)
    }
}