/*
 * Copyright 2010 the original author or authors.
 *
 * 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.
 */

import java.text.DateFormat
import org.gradle.api.internal.artifacts.dependencies.DefaultSelfResolvingDependency

apply plugin: 'groovy'
apply from: "$rootDir/gradle/integTest.gradle"

configurations {
    testFixtures
    testFixturesRuntime {
        extendsFrom testFixtures, testRuntime
    }
    integTestFixtures {
        extendsFrom testFixtures
    }
    integTestFixturesRuntime {
        extendsFrom integTestFixtures, integTestRuntime
    }
}

dependencies {
    groovy libraries.groovy_depends

    compile "commons-httpclient:commons-httpclient:3.0@jar", "commons-codec:commons-codec:1.2@jar", libraries.jcl_to_slf4j

    compile libraries.ivy, "com.jcraft:jsch:0.1.42@jar", 'com.jcraft:jzlib:1.0.7@jar'

    compile libraries.ant,
            libraries.logback_classic,
            libraries.logback_core,
            libraries.slf4j_api,
            libraries.jul_to_slf4j,
            libraries.commons_io,
            libraries.commons_lang,
            "commons-codec:commons-codec:1.2@jar",
            libraries.google_collections,
            "commons-collections:commons-collections:3.2.1@jar",
            "slide:webdavlib:2.0@jar",
            "org.apache.maven:maven-ant-tasks:2.1.1@jar",
            libraries.asm_all,
            'org.fusesource.jansi:jansi:1.2.1',
            'org.jruby.ext.posix:jna-posix:1.0.3',
            'org.sonatype.pmaven:pmaven-common:0.8-20100325@jar',
            'org.sonatype.pmaven:pmaven-groovy:0.8-20100325@jar',
            'org.codehaus.plexus:plexus-component-annotations:1.5.2'

    runtime 'net.java.dev.jna:jna:3.2.2'

    runtime libraries.log4j_to_slf4j, libraries.jcl_to_slf4j

    testCompile libraries.xmlunit

    compile libraries.ant_launcher

    integTestCompile libraries.jetty_depends

    testFixtures sourceSets.test.classes, sourceSets.main.classes
    integTestFixtures sourceSets.integTest.classes

    // TODO - remove this duplication
    publishRuntime libraries.slf4j_api
}

task versionProperties(type: WriteVersionProperties) {
    propertiesFile = new File(sourceSets.main.classesDir, GradleVersion.FILE_NAME)
}

classes.dependsOn(versionProperties)

task ideVersionProperties(type: WriteVersionProperties) {
    propertiesFile = new File(ideDir, "resources/test/$GradleVersion.FILE_NAME")
}

ide.dependsOn ideVersionProperties

ideaModule {
    dependsOn ideVersionProperties
    scopes.COMPILE.plus.add(configurations.detachedConfiguration(new DefaultSelfResolvingDependency(files(new File(ideDir, "resources/test/")))))
}

eclipseClasspath {
    dependsOn ideVersionProperties
    plusConfigurations.add(configurations.detachedConfiguration(new DefaultSelfResolvingDependency(files(new File(ideDir, "resources/test/")))))
}

[compileGroovy, compileTestGroovy]*.groovyOptions*.fork(memoryInitialSize: '128M', memoryMaximumSize: '1G')

test {
    jvmArgs '-Xms128m', '-Xmx512m', '-XX:MaxPermSize=128m', '-XX:+HeapDumpOnOutOfMemoryError'
}

class WriteVersionProperties extends DefaultTask {
    @Input
    String getVersion() { return project.version.toString() }

    @Input
    Date getBuildTime() { return project.version.buildTime }

    @OutputFile
    File propertiesFile

    @TaskAction
    def void generate() {
        logger.info('Write version properties to: {}', propertiesFile)
        Properties versionProperties = new Properties()
        versionProperties.putAll([
                (GradleVersion.VERSION): version,
                (GradleVersion.BUILD_TIME): DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(buildTime)
        ])
        propertiesFile.withOutputStream {
            versionProperties.store(it, '')
        }

    }
}
