apply plugin: "maven-publish"

configurations {
    asciidoctorExtensions
}

dependencies {
    asciidoctorExtensions "io.spring.asciidoctor.backends:spring-asciidoctor-backends:0.0.7"
}

asciidoctorPdf {
    baseDirFollowsSourceFile()
    asciidoctorj {
        sources {
            include 'index.adoc'
        }
        options doctype: 'book'
        attributes 'icons': 'font',
                'sectanchors': '',
                'sectnums': '',
                'toc': '',
                'source-highlighter' : 'coderay',
                revnumber: project.version,
                'project-version': project.version
    }
}

asciidoctor {
    baseDirFollowsSourceFile()
    configurations "asciidoctorExtensions"
    outputOptions {
        backends "spring-html"
    }
    sources {
        include 'index.adoc'
    }
    options doctype: 'book'

    attributes 'docinfo': 'shared',
            stylesdir: 'css/',
            stylesheet: 'spring.css',
            'linkcss': true,
            'icons': 'font',
            'sectanchors': '',
            'source-highlighter': 'highlight.js',
            'highlightjsdir': 'js/highlight',
            'highlightjs-theme': 'github',
            'idprefix': '',
            'idseparator': '-',
            'spring-version': project.version,
            'allow-uri-read': '',
            'toc': 'left',
            'toclevels': '4',
            revnumber: project.version,
            'project-version': project.version
}

task api(type: Javadoc) {
    group = "Documentation"
    description = "Generates aggregated Javadoc API documentation."
    title = "${rootProject.description} ${version} API"
    options.memberLevel = JavadocMemberLevel.PROTECTED
    options.author = true
    options.header = rootProject.description
    options.overview = "src/api/overview.html"
    source subprojects.collect { project ->
        project.sourceSets.main.allJava
    }
    destinationDir = new File(buildDir, "api")
    classpath = files(subprojects.collect { project ->
        project.sourceSets.main.compileClasspath
    })
    maxMemory = "1024m"
}

task docsZip(type: Zip) {
    group = "Distribution"
    archiveBaseName = "spring-webflow"
    archiveClassifier = "docs"
    description = "Builds -${archiveClassifier.get()} archive containing api and reference " +
            "for deployment at static.springframework.org/spring-webflow/docs."

    from (api) {
        into "api"
    }
    from (asciidoctor) {
        into "reference"
    }
    from (asciidoctorPdf) {
        into "reference"
    }
}

task schemaZip(type: Zip) {
    group = "Distribution"
    archiveBaseName = "spring-webflow"
    archiveClassifier = "schema"
    description = "Builds -${archiveClassifier.get()} archive containing all " +
            "XSDs for deployment at static.springframework.org/schema."

    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    subprojects.each { subproject ->
        Properties schemas = new Properties()

        subproject.sourceSets.main.resources.find {
            it.path.endsWith("META-INF/spring.schemas")
        }?.withInputStream { schemas.load(it) }

        for (def key : schemas.keySet()) {
            def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
            assert shortName != key
            File xsdFile = subproject.sourceSets.main.allSource.find {
                it.path.endsWith(schemas.get(key))
            } as File
            assert xsdFile != null
            into (shortName) {
                from xsdFile.path
            }
        }
    }

    project(":spring-webflow").sourceSets.main.resources.matching {
        include '**/engine/model/builder/xml/*.xsd'
    }.each { File file ->
        into ('webflow') {
            from file.path
        }
    }
}

task distZip(type: Zip, dependsOn: [docsZip, schemaZip]) {
    group = "Distribution"
    archiveBaseName = "spring-webflow"
    archiveClassifier = "dist"
    description = "Builds -${archiveClassifier.get()} archive, containing all jars and docs, " +
            "suitable for community download page."

    def baseDir = "${archiveBaseName.get()}-${project.version}"

    from("src/dist") {
        include "notice.txt"
        into "${baseDir}"
        expand(copyright: new Date().format("yyyy"), version: project.version)
    }
    from("src/dist") {
        include "readme.txt"
        include "license.txt"
        into "${baseDir}"
        expand(version: project.version)
    }
    from(zipTree(docsZip.archiveFile)) {
        into "${baseDir}/docs"
    }
    from(zipTree(schemaZip.archiveFile)) {
        into "${baseDir}/schema"
    }

    subprojects.each { subproject ->
        into ("${baseDir}/libs") {
            from subproject.jar
            if (subproject.tasks.findByPath("sourcesJar")) {
                from subproject.sourcesJar
            }
            if (subproject.tasks.findByPath("javadocJar")) {
                from subproject.javadocJar
            }
        }
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifact docsZip
            artifact schemaZip
            artifact distZip
        }
    }
}
