/**
 * Since Grails is a mixed Groovy/Java codebase the javadoc tool needs stubs in order for javadoc compilation to pass
 * This target generates a set of stubs for the Groovy sources
 */
task stubs {
    destinationDir = "$buildDir/docs/stubs"
    doFirst {
        def antBuilderClasspath = services.get(org.gradle.api.internal.ClassPathRegistry).getClassPathFiles('ANT') +
                configurations.groovy.files
        def isolatedAnt = services.get(org.gradle.api.internal.project.IsolatedAntBuilder)
        isolatedAnt.execute(antBuilderClasspath) {
            mkdir(dir: destinationDir)
            taskdef(name: "generatestubs", classname: "org.codehaus.groovy.grails.cli.GenerateStubsTask", classpath: sourceSets.main.runtimeClasspath.asPath)
            generatestubs(destdir: destinationDir) {
                classpath {
                    pathelement path: sourceSets.main.runtimeClasspath.asPath
                }
                src {
                    sourceSets.main.groovy.srcDirs.each { srcDir ->
                        dirset(dir: srcDir) {
                            exclude name: "**/*.properties"
                        }
                    }
                }
            }
        }
    }
}

task docs {
    destinationDir = 'doc'
}

javadoc {
    dependsOn stubs
    maxMemory = '256M'
    destinationDir = file("$docs.destinationDir/javadoc")
    source stubs.destinationDir, defaultSource
    include "org/codehaus/groovy/grails/**", "grails/**"
    project.configure(options) {
        windowTitle = "Grails $version"
        docTitle = "<h1>Grails</h1>"
        encoding = "UTF-8"
        memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PACKAGE
        author = true
        version = true
        use = true
        breakIterator = true
        links("http://java.sun.com/j2ee/1.4/docs/api", "http://java.sun.com/j2se/1.5.0/docs/api", "http://static.springframework.org/spring/docs/2.5.x/api")
//        tags("todo:a:To do")  todo fix the javadoc task to accept this option
    }
    verbose = false
}

groovydoc {
    destinationDir = file("$docs.destinationDir/api")
    windowTitle = "Grails $version"
    docTitle = "Grails $version"
    use = true
// Can't make these link methods to work
//    link("http://static.springsource.org/spring/docs/3.0.x/javadoc-api", "org.springframework.")
//    link("http://download.oracle.com/javase/1.5.0/docs/api", "java." , "javax.")
//    link("http://download.oracle.com/javaee/5/api", "javax.")
}

task gdoc(dependsOn: groovydoc) << {
    ant {
        mkdir dir: buildDir
        get src:"http://nodeload.github.com/grails/grails-doc/zipball/master", dest:"${buildDir}/grails-docs-src.zip", verbose:"true"
        unzip(src: "${buildDir}/grails-docs-src.zip", dest: buildDir) {
            mapper type: 'regexp', from:/(grails-grails-doc-[\w]*\/)(.*)/, to:/grails-docs-src\/\2/
        }
        ant(target: 'distclean', dir: "${buildDir}/grails-docs-src/", inheritAll: false)
        ant(target: 'dist-nojavadoc', dir: "${buildDir}/grails-docs-src/", inheritAll: false) {
            property(name: "base.dir", value: "${buildDir}/grails-docs-src/")
            property(name: "grails.home", value:projectDir)
        }
        copy(todir:"$projectDir/dist") {
            fileset(dir: "${buildDir}/grails-docs-src/dist")
        }
    }
    copy {
        from(new File("${buildDir}/grails-docs-src/output"))
        into docs.destinationDir
    }
}

docs.dependsOn gdoc
