buildscript {
    repositories {
        maven { url 'http://maven.parim.net/nexus/content/groups/public/' }
        mavenCentral()
    }
    dependencies {
        classpath("org.asciidoctor:asciidoctor-gradle-plugin:1.5.3")
        classpath("org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.15")
        classpath("org.asciidoctor:asciidoctorj-epub3:1.5.0-alpha.6")
    }
}
apply plugin: 'org.asciidoctor.convert'

import org.asciidoctor.gradle.AsciidoctorTask

asciidoctorj {
    version = '1.5.5'
}

def attrs = [
        'spring-version': project.version,
        'revnumber' : project.version,
        'source-highlighter': 'coderay',
        'docinfo' : "",
        'docinfo1': '',
        'toc': 'left',
        'toc-title': '目录',
        'toclevels': '3',
        'icons': 'font',
        'setanchors': '',
        'sectnums': ''
]

tasks.withType(AsciidoctorTask) { task ->
    sourceDir project.file('src/doc/asciidoc')
    sources {
        include '*.adoc'
    }
}

task asciidoctorHtml(type: AsciidoctorTask, description: 'Generates single page HTML') {

    backends = ['html5']
    options doctype: 'book', eruby: 'erubis'
    attributes attrs

    doLast {
        fileTree(sourceDir).include("**/images/**").each {File file ->
            copy {
                from file
                into "${outputDir.path}/html5/images"
            }
        }
    }

}

task asciidoctorPdf(type: AsciidoctorTask, description: 'Generates PDF') {
    def tempDir = "${outputDir}/tmp"

    doFirst {
        if (new File(tempDir).exists()) {
            delete tempDir
        }

        new File(tempDir+'/images').mkdirs()

        fileTree(sourceDir).include("**/images/**").each {File file ->
            copy {
                from file
                into "${tempDir}/images"
            }
        }
    }

    backends = ['pdf']
    options doctype: 'book', eruby: 'erubis'
    attributes attrs + [
            'imagesdir': tempDir,
            'pdf-style': "${sourceDir.path}/styles/pdf/themes/default-theme.yml",
            'pdf-fontsdir': "${sourceDir.path}/styles/pdf/fonts",
    ]

    doLast {
        if (new File(tempDir).exists()) {
            delete tempDir
        }
    }
}

// don't publish the default jar for the root project
//configurations.archives.artifacts.clear()

task api(type: Javadoc) {
    group = "Documentation"
    description = "Generates aggregated Javadoc API documentation."
    title = "${rootProject.description} ${version} API"

    dependsOn {
        subprojects.collect {
            it.tasks.getByName("jar")
        }
    }
    options.memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PROTECTED
    options.author = true
    options.header = rootProject.description
    options.overview = "src/doc/api/overview.html"
    options.stylesheetFile = file("src/doc/api/stylesheet.css")
    //options.splitIndex = true
    //options.links(project.ext.javadocLinks)
    //options.addStringOption('Xdoclint:none', '-quiet') //可能只有jdk1.8才支持
    options.encoding= "UTF-8"
    options.charSet= "UTF-8"
    
    // suppress warnings due to cross-module @see and @link references;
    logging.captureStandardError LogLevel.INFO
    logging.captureStandardOutput LogLevel.INFO

    source subprojects.collect { project ->
        project.sourceSets.main.allJava
    }

    maxMemory = "1024m"
    destinationDir = new File(buildDir, "api")

    doFirst {
        classpath = files(
            // ensure Servlet 3.x and Hibernate 4.x have precedence on the javadoc
            // classpath over their respective 2.5 and 3.x variants
            //project(":spring-webmvc").sourceSets.main.compileClasspath.files.find { it =~ "servlet-api" },
            //rootProject.sourceSets.test.compileClasspath.files.find { it =~ "hibernate-core" },
            // ensure the javadoc process can resolve types compiled from .aj sources
            //project(":spring-aspects").sourceSets.main.output
        )
        classpath += files(subprojects.collect { it.sourceSets.main.compileClasspath })
    }
}

task docsZip(type: Zip, dependsOn: ['asciidoctorHtml', 'asciidoctorPdf']) {
    group = "Distribution"
    baseName = "parim-spark"
    classifier = "docs"
    description = "Builds -${classifier} archive containing api and reference " +
        "for deployment at docs website."

    from("src/doc/dist") {
        include "changelog.txt"
    }
    
    // 'api' 任务出错，暂时关闭
    /*from (api) {
        into "javadoc-api"
    }*/

    from (asciidoctor.outputDir.path) {
        into "reference"
    }
}