import groovy.text.SimpleTemplateEngine

buildscript {
    repositories {
        maven { url "http://repo.springsource.org/plugins-release" }
    }
    dependencies {
        classpath("org.springframework.build.gradle:propdeps-plugin:0.0.5")
        classpath("org.springframework.build.gradle:bundlor-plugin:0.1.2")
        classpath("org.gradle.api.plugins:gradle-tomcat-plugin:0.9.8")
        classpath('me.champeau.gradle:gradle-javadoc-hotfix-plugin:0.1')
        classpath('org.asciidoctor:asciidoctor-gradle-plugin:0.7.0')
        classpath('org.asciidoctor:asciidoctor-java-integration:0.1.4.preview.1')
    }
}

apply plugin: 'sonar-runner'
apply plugin: 'base'

description = 'Spring Security'

allprojects {
    apply plugin: 'idea'
    apply plugin: 'eclipse'

    ext.releaseBuild = version.endsWith('RELEASE')
    ext.snapshotBuild = version.endsWith('SNAPSHOT')
    ext.springVersion = '3.2.8.RELEASE'
    ext.spring4Version = '4.0.2.RELEASE'
    ext.springLdapVersion = '1.3.2.RELEASE'
    ext.springLdap2Version = '2.0.1.RELEASE'

    group = 'org.springframework.security'

    repositories {
       mavenCentral()
       maven { url "https://repo.spring.io/libs-snapshot" }
       maven { url "https://repo.spring.io/plugins-release" }
       maven { url "http://repo.terracotta.org/maven2/" }
    }

    eclipse.project.name = "${project.name}-3.2.x"
}

sonarRunner {
    sonarProperties {
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.projectName", "Spring Security"
        property "sonar.jacoco.reportPath", "${buildDir.name}/jacoco.exec"
        property "sonar.links.homepage", 'https://www.springsource.org/spring-security'
        property "sonar.links.ci", 'https://build.springsource.org/browse/SEC-B32X'
        property "sonar.links.issue", 'https://jira.springsource.org/browse/SEC'
        property "sonar.links.scm", 'https://github.com/SpringSource/spring-security'
        property "sonar.links.scm_dev", 'https://github.com/SpringSource/spring-security.git'
        property "sonar.java.coveragePlugin", "jacoco"
    }
}

// Set up different subproject lists for individual configuration
ext.javaProjects = subprojects.findAll { project -> project.name != 'docs' && project.name != 'manual' && project.name != 'guides' && project.name != 'spring-security-bom' }
ext.sampleProjects = subprojects.findAll { project -> project.name.startsWith('spring-security-samples') }
ext.itestProjects = subprojects.findAll { project -> project.name.startsWith('itest') }
ext.coreModuleProjects = javaProjects - sampleProjects - itestProjects
ext.aspectjProjects = [project(':spring-security-aspects'), project(':spring-security-samples-aspectj-xml')]

configure(allprojects - javaProjects) {
    task afterEclipseImport {
        ext.srcFile = file('.classpath')
        inputs.file srcFile
        outputs.dir srcFile

        onlyIf { !srcFile.exists() }

        doLast {
            srcFile << """<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="output" path="bin"/>
</classpath>
"""
        }
    }
}

configure(subprojects - coreModuleProjects - project(':spring-security-samples-messages-jc') - project(':spring-security-bom')) {
    tasks.findByPath("artifactoryPublish")?.enabled = false
    sonarRunner {
        skipProject = true
    }
}

configure(javaProjects) {
    ext.TOMCAT_GRADLE = "$rootDir/gradle/tomcat.gradle"
    ext.WAR_SAMPLE_GRADLE = "$rootDir/gradle/war-sample.gradle"
    apply from: "$rootDir/gradle/javaprojects.gradle"
    apply from: "$rootDir/gradle/release-checks.gradle"
    apply from: "$rootDir/gradle/maven-deployment.gradle"
}

configure(coreModuleProjects) {
    apply plugin: 'bundlor'
    apply plugin: 'emma'

    bundlor.doFirst {
        def templateText = file("template.mf").text
        bundlor.manifestTemplate = new SimpleTemplateEngine().createTemplate(templateText).make(bundlorProperties).toString()
    }
    configurations {
        jacoco //Configuration Group used by Sonar to provide Code Coverage using JaCoCo
        spring4TestRuntime.extendsFrom testRuntime
    }
    configurations.spring4TestRuntime {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            if (details.requested.group == 'org.springframework') {
                details.useVersion spring4Version
            }
            if (details.requested.name == 'ehcache') {
                details.useVersion '2.6.5'
            }
            if (details.requested.name == 'ehcache-terracotta') {
                details.useVersion '2.1.1'
            }
            if (details.requested.group == 'org.springframework.ldap') {
                details.useVersion springLdap2Version
            }
        }
    }

    dependencies {
        jacoco "org.jacoco:org.jacoco.agent:0.6.2.201302030002:runtime"
    }
    test {
        jvmArgs "-javaagent:${configurations.jacoco.asPath}=destfile=${buildDir}/jacoco.exec,includes=${project.group}.*"
    }
    integrationTest {
        jvmArgs "-javaagent:${configurations.jacoco.asPath}=destfile=${buildDir}/jacoco.exec,includes=${project.group}.*"
    }
    task spring4Test(type: Test) {
        jvmArgs = ['-ea', '-Xmx500m', '-XX:MaxPermSize=128M']
        exclude "**/EhCacheBasedAclCacheTests.class", "**/Issue55Tests.class"
        classpath = sourceSets.test.output + sourceSets.main.output + configurations.spring4TestRuntime
        testResultsDir = file("$buildDir/spring4-test-results/")
        testReportDir = file("$buildDir/reports/spring4-tests/")
    }
    check.dependsOn spring4Test
}

configure (aspectjProjects) {
    apply plugin: 'java'
    apply plugin: 'aspectj'
}

task coreBuild {
    dependsOn coreModuleProjects*.tasks*.matching { task -> task.name == 'build' }
}

// Task for creating the distro zip

task dist(type: Zip) {
    dependsOn subprojects*.tasks*.matching { task -> task.name == 'assemble' || task.name.endsWith('Zip') || task.name.endsWith('generatePom') }
    classifier = 'dist'

    evaluationDependsOn(':docs')
    evaluationDependsOn(':docs:manual')

    def zipRootDir = "${project.name}-$version"
    into(zipRootDir) {
        from(rootDir) {
            include '*.txt'
        }
        into('docs') {
            with(project(':docs').apiSpec)
            with(project(':docs:manual').spec)
            with(project(':docs:guides').spec)
        }
        into('dist') {
            from coreModuleProjects.collect {project -> project.libsDir }
        }
        sampleProjects.each { project->
            into("$zipRootDir/samples/$project.name") {
                from(project.projectDir) {
                    include "src/main/**"
                    include "pom.xml"
                }
            }
        }
    }
}

artifacts {
    archives dist
    archives project(':docs').docsZip
    archives project(':docs').schemaZip
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.10-rc-2'
}
