buildscript {
    repositories { mavenCentral() }
    dependencies { classpath "org.codehaus.groovy:groovy-docgenerator:2.3.0" }
}


apply from: '../common.gradle'

apply plugin: 'maven'

defaultTasks 'clean', 'codenarc', 'build'

test {                     
  maxHeapSize = '320m'                                 
}            

// various directory places and file names
final tmpProj    = '../template-project'
final zipName    = "${buildDir}/gaelyk-template-project-${version}.zip"

final website    = '../website'
final apiDir     = "${website}/war/api"
final websiteLib = "${website}/war/WEB-INF/lib"

sourceSets {
    main {
        groovy {
            srcDir 'src/main'
        }
        resources {
            srcDir 'src/resources'
        }
    }

    test {
        groovy {
            srcDir 'src/test'
        }
        resources {
            srcDir 'src/testResources'
        }
    }
}

configurations {
    jansi.extendsFrom(runtime)
    tools.extendsFrom(runtime)
}

dependencies {
    compile "org.codehaus.groovy:groovy-all:${project.ext.gaelykGroovyVersion}", {
        force = true
    }
//    compile "org.codehaus.groovy:groovy-servlet:${project.ext.gaelykGroovyVersion}"
//    groovy "org.codehaus.groovy:groovy-ant:${project.ext.gaelykGroovyVersion}"  // for groovy-doc only
    compile "com.google.appengine:appengine-api-1.0-sdk:${project.ext.gaelykAppEngineVersion}"
    compile "com.google.appengine:appengine-api-labs:${project.ext.gaelykAppEngineVersion}"
    compile "com.google.appengine.tools:appengine-gcs-client:0.5"
    compile 'javax.servlet:servlet-api:2.5'
//    groovy "org.codehaus.groovy:groovy-test:${project.ext.gaelykGroovyVersion}" // for tests only
    compile 'javax.servlet.jsp:jsp-api:2.2'
    testCompile 'junit:junit:4.8.2'
    testCompile "com.google.appengine:appengine-testing:${project.ext.gaelykAppEngineVersion}"
    testCompile "com.google.appengine:appengine-api-stubs:${project.ext.gaelykAppEngineVersion}"
    testCompile "org.spockframework:spock-core:${project.ext.gaelykSpockVersion}", {
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }
    testCompile 'cglib:cglib-nodep:2.2.2'

    testCompile fileTree(dir: 'testLibs', include: ['gaelyk-resources-0.1.1-SNAPSHOT.jar'])
    
    jansi 'org.fusesource.jansi:jansi:1.11'
    
    tools project(':gaelyk-docgenerator')
//    tools "com.thoughtworks.qdox:qdox:1.12.1"
}

jar {
    baseName 'gaelyk'
    manifest {
        attributes 'Implementation-Title': 'Gaelyk', 'Implementation-Version': version
    }
    metaInf { from 'src/main/META-INF' }
}

groovydoc {
    def title = "Gaelyk ${version}"
    destinationDir file(apiDir)
    windowTitle title
    docTitle title
    link 'http://java.sun.com/javaee/5/docs/api/', 'javax.servlet.'
    link 'http://java.sun.com/j2se/1.5.0/docs/api', 'java.,org.xml.,org.xml.'
    link 'http://code.google.com/appengine/docs/java/javadoc/', 'com.google.appengine.'
    link 'http://groovy.codehaus.org/gapi/', 'org.codehaus.groovy.,groovy.'
    groovyClasspath = project.configurations.jansi
}

task template {
    description = 'Packaging the template project'

    doFirst {
        println 'Creating the template project ZIP file'
        ant.zip basedir: tmpProj, destfile: zipName, excludes: '__MACOSX,*.iml,.DS_Store,bin/**,.gradle/**,build/**,war/WEB-INF/appengine-generated/**'
    }
}

task dist(dependsOn: [jar, groovydoc, template]) {
    description = 'Updating the Gaelyk JAR of the website'

    doFirst {
        ant.copy file: jar.archivePath, todir: websiteLib
    }
}

// custom tasks for creating source/javadoc jars
task sourcesJar(type: Jar, dependsOn:classes) {
     baseName 'gaelyk'
     classifier 'sources'
     from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn:groovydoc) {
     baseName 'gaelyk'
     classifier 'javadoc'
     from apiDir
}

// add javadoc/source jar tasks as artifacts
artifacts {
    archives jar
    archives sourcesJar
    archives javadocJar
}

if(hasProperty('nexusUsername') && hasProperty('nexusPassword')){
    boolean signingEnabled = !hasProperty('skipSigning') || skipSigning != 'true'
    if(signingEnabled){
        apply plugin: 'signing'
        signing {
            sign configurations.archives
        }
    }
        

    uploadArchives {
        repositories {
            mavenDeployer {
                if(signingEnabled){
                    beforeDeployment { deployment -> signing.signPom(deployment) }
                }

                def auth = {
                    authentication(userName: nexusUsername, password: nexusPassword)
                }

                repository(url: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/', auth)
                snapshotRepository(url: 'https://oss.sonatype.org/content/repositories/snapshots', auth)


                pom.project {
                    name 'Gaelyk'
                    packaging 'jar'
                    description 'Gaelyk is a lightweight toolkit for developing applications in Groovy for the Google App Engine environment.'
                    url 'http://www.gaelyk.org'

                    scm {
                        url 'scm:git://github.com/gaelyk/gaelyk.git'
                        connection 'scm:git://github.com/gaelyk/gaelyk.git'
                        developerConnection 'scm:git://github.com/gaelyk/gaelyk.git'
                    }

                    licenses {
                        license {
                            name 'The Apache Software License, Version 2.0'
                            url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                            distribution 'repo'
                        }
                    }

                    developers {
                        developer {
                            id 'glaforge'
                            name 'Guillaume Laforge'
                        }
                        developer {
                            id 'vladimirorany'
                            name 'Vladimir Orany'
                        }
                        developer {
                            id 'bmuschko'
                            name 'Benjamin Muschko'
                        }
                        developer {
                            id 'codeconsole'
                            name 'Scott Murphy'
                        }
                        developer {
                            id 'alkemist'
                            name 'Luke Daley'
                        }
                        developer {
                            id 'lguzzon'
                            name 'Luca Guzzon'
                        }
                        developer {
                            id 'fabito'
                            name 'Fábio Franco Uechi'
                        }
                    }
                }

                //mess with the generated pom to set the 'packaging' tag
//                pom.withXml { XmlProvider xmlProvider ->
//                    def xml = xmlProvider.asString()
//                    def pomXml = new XmlParser().parse(xml.toString())
//
//                    pomXml.version[0] + { packaging('jar') }
//
//                    def newXml = new StringWriter()
//                    def printer = new XmlNodePrinter(new PrintWriter(newXml))
//                    printer.preserveWhitespace = true
//                    printer.print(pomXml)
//                    xml.setLength(0)
//                    xml.append(newXml.toString())
//                }
            }
        }
    }
}


task docGDK {
    //    project.ext.extraDocGDKclasses = []
    dependsOn(':gaelyk-docgenerator:build')
    // TODO don't hard-code these
    ext.destinationDir = "$buildDir/html/groovy-jdk"
    inputs.files sourceSets.main.runtimeClasspath
    outputs.dir destinationDir
    doLast {task ->
        try {
            ant {
                java(
                    classname: "groovyx.gaelyk.tools.docs.DocGenerator", 
                    fork: "true", 
                    failonerror: "true",
                    classpath: (sourceSets.main.runtimeClasspath + groovydoc.groovyClasspath + configurations.tools).asPath,
                    errorproperty: 'edr',
                    outputproperty: 'odr'
                ) {
                    // either package name if in core or fully qualified path otherwise
                    // arg(value: "src/main/groovy/servlet/ServletCategory.groovy")
                    arg(value: "src/main/groovyx/gaelyk/extensions/MiscExtensions.java")
                    arg(value: "src/main/groovyx/gaelyk/extensions/ExpirationTimeExtensionMethods.java")
                    arg(value: "src/main/groovyx/gaelyk/extensions/ServletExtensions.java")
                    arg(value: "src/main/groovyx/gaelyk/extensions/DatastoreExtensions.java")
                    arg(value: "src/main/groovyx/gaelyk/extensions/MemcacheExtensions.java")
                    arg(value: "src/main/groovyx/gaelyk/extensions/BlobstoreExtensions.java")
                    arg(value: "src/main/groovyx/gaelyk/extensions/FilesExtensions.java")
                    arg(value: "src/main/groovyx/gaelyk/extensions/MailExtensions.java")
                    arg(value: "src/main/groovyx/gaelyk/extensions/TaskQueueExtensions.java")
                    arg(value: "src/main/groovyx/gaelyk/extensions/SearchExtensions.java")
                    arg(value: "src/main/groovyx/gaelyk/extensions/UrlFetchExtensions.java")
                    arg(value: "src/main/groovyx/gaelyk/extensions/CapabilitiesExtensions.java")
                    arg(value: "src/main/groovyx/gaelyk/extensions/ImageExtensions.java")
//                    jvmarg(value: "-Xdebug")
//                    jvmarg(value: "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005")
                }
            }
        } finally {
            if (ant.properties.odr) {
                logger.info "GDK Docs Out: " + ant.properties.odr
            }
            if (ant.properties.edr) {
                logger.error "GDK Docs 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"
        }
    }
}

