/*
 * Copyright 2011-2021 Asakusa Framework Team.
 *
 * Licensed 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
 *
 *     http://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.
 */
ext.parentPom = { f ->
    if (!f.exists()) {
        return null
    }
    def xml = new XmlSlurper().parse(f)
    def props = xml['properties']
    Properties results = new Properties()
    results.putAll([
        'plugin-version' : xml.version.text(),
        'framework-version' : xml.version.text(),
        'hadoop-version' : props['hadoop.version'].text(),
        'slf4j-version' : props['slf4j.version'].text(),
        'logback-version' : props['logback.version'].text(),
        'jsch-version' : props['jsch.version'].text(),
        'gson-version' : props['gson.version'].text(),
        'http-client-version' : props['httpclient.version'].text(),
        'http-core-version' : props['httpcore.version'].text(),
        'commons-cli-version' : props['commons.cli.version'].text(),
        'commons-codec-version' : props['commons.codec.version'].text(),
        'commons-io-version' : props['commons.io.version'].text(),
        'commons-lang-version' : props['commons.lang.version'].text(),
        'commons-logging-version' : props['commons.logging.version'].text(),
        'hadoop-version' : props['hadoop.version'].text(),
        'hive-version' : props['sdk.hive.version'].text(),
    ])
    return results
}(project.file('../pom.xml'))

group = 'com.asakusafw'
version = parentPom['plugin-version']

defaultTasks 'clean', 'build', 'install'

apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'eclipse'

configurations {
    backports
    deployerJars
}

repositories {
    if (project.hasProperty('mavenLocal')) {
        logger.lifecycle 'enabling maven local repository'
        mavenLocal()
    }
    mavenCentral()
    maven { url 'https://asakusafw.s3.amazonaws.com/maven/releases' }
    maven { url 'https://asakusafw.s3.amazonaws.com/maven/snapshots' }
}

dependencies {
    compile gradleApi()
    testCompile gradleTestKit()
    testCompile 'junit:junit:4.12'
    backports "org.codehaus.groovy:groovy-backports-compat23:${GroovySystem.version}"
    deployerJars 'org.springframework.build:aws-maven:5.0.0.RELEASE'
}

project.sourceCompatibility = JavaVersion.toVersion('1.8')

tasks.withType(AbstractCompile) { task ->
    task.options.encoding = 'UTF-8'
    if (System.getProperty('compiler.bootclasspath') != null) {
        task.options.compilerArgs += ['-bootclasspath', System.getProperty('compiler.bootclasspath')]
    }
}

eclipse.jdt {
    javaRuntimeName = "JavaSE-${sourceCompatibility}"
}

groovydoc {
    docTitle "Asakusa Gradle Plugins ${version}"
    windowTitle "Asakusa Gradle Plugins ${version}"
    footer "Copyright 2011-${new Date().format('yyyy')} Asakusa Framework Team."
}

processResources {
    File outputFile = new File(destinationDir, 'META-INF/asakusa-gradle/artifact.properties')
    inputs.properties parentPom
    outputs.file outputFile
    doLast {
        logger.info "injecting artifact versions: ${parentPom}"
        if (!outputFile.parentFile.exists()) {
            mkdir outputFile.parentFile
        }
        outputFile.withOutputStream { s ->
            parentPom.store(s, null)
        }
    }
}

jar {
    from({ configurations.backports.collect { zipTree(it) } }) {
        exclude '**/META-INF/MANIFEST.MF'
    }
}

uploadArchives {
    repositories {
        mavenDeployer {
            configuration = project.configurations.deployerJars
            repository id: 'com.asakusafw.releases', url: 's3://asakusafw/maven/releases'
            snapshotRepository id: 'com.asakusafw.snapshots', url: 's3://asakusafw/maven/snapshots'
        }
    }
}

task sourcesJar(type: Jar) {
    classifier 'sources'
    from sourceSets.main.allSource
}

task testsJar(type: Jar) {
    classifier 'tests'
    from sourceSets.test.output
}

artifacts {
    archives tasks.sourcesJar
    archives tasks.testsJar
}

