// DMDirc plugins build script
subprojects {
    def jgit = new org.mdonoughe.JGitDescribeTask()
    jgit.dir = new File(projectDir.parent, '.git')
    jgit.subDir = projectDir
    project.version = jgit.getDescription() + '-SNAPSHOT'

    apply plugin: 'java'
    apply plugin: 'findbugs'
    apply plugin: 'pmd'

    configurations {
        bundle

        compile {
            extendsFrom bundle
        }
    }

    sourceSets {
        main {
            java.srcDirs = ['src']
            resources.srcDirs = ['res']
        }
        test {
            java.srcDirs = ['test']
            resources.srcDirs = ['test-res']
        }
    }

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    repositories {
        mavenCentral()
        maven {
            url 'http://artifactory.dmdirc.com/artifactory/repo'
        }
    }

    dependencies {
        compile group: 'com.dmdirc', name: 'client', version: '+', changing: true

        compile group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.0'
        compile group: 'com.squareup.dagger', name: 'dagger-compiler', version: '1.2.1'

        testCompile group: 'junit', name: 'junit', version: '4.12'
        testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.17'

        pmd group: 'net.sourceforge.pmd', name: 'pmd-core', version: '5.2.3'
        pmd group: 'net.sourceforge.pmd', name: 'pmd-java', version: '5.2.3'
    }

    task copyPluginConfig(type: Copy) {
        from 'plugin.config'
        into "$buildDir"
    }

    task updatePluginConfig(dependsOn: copyPluginConfig) << {
        def targetFile = new File(buildDir, 'plugin.config')
        targetFile << "\n\nversion:\n  number=${project.version}"

        targetFile << "\n\nbuildenv:\n"
        def compileConfiguration = project.configurations.getByName("compile")
        def resolvedConfiguration = compileConfiguration.resolvedConfiguration
        def resolvedArtifacts = resolvedConfiguration.resolvedArtifacts
        resolvedArtifacts.each { dp ->
            def version = dp.moduleVersion.id
            targetFile << "  " + version.group + " " + version.name + " " + version.version + "\n"
        }
    }

    pmd {
        ruleSets = []
        ruleSetFiles = files("$rootDir/etc/pmd/full.xml")
        toolVersion = '5.2.3'
        ignoreFailures = true
    }

    pmdMain.doLast {
        copy {
            from pmd.reportsDir
            into "../pmd/"
            include "*.html"
            rename "main.html", "${project.name}-main.html"
        }
    }

    tasks.withType(FindBugs) {
        reports {
            html.enabled = true
            xml.enabled = false
        }
    }

    findbugs {
        reportLevel = 'low'
        effort = 'max'
        ignoreFailures = true
    }

    jar {
        from("$buildDir/plugin.config") {
            into 'META-INF'
        }

        from { configurations.bundle.collect { it.isDirectory() ? it : zipTree(it) } } {
            exclude 'META-INF/**'
        }
    }

    jar.doLast {
        copy {
            from jar.archivePath
            into "../dist/"
            rename ".*", "${project.name}.jar"
        }

        copy {
            from jar.archivePath
            into "../dist-versioned/"
            rename ".*", "${project.name}-${project.version}.jar"
        }
    }
    jar.outputs.file "../dist/${project.name}.jar"
    jar.outputs.file "../dist-versioned/${project.name}-${project.version}.jar"

    jar.dependsOn updatePluginConfig
}

allprojects {
    configurations.all {
        resolutionStrategy.cacheDynamicVersionsFor 2, 'minutes'
        resolutionStrategy.cacheChangingModulesFor 2, 'minutes'
    }
}


buildscript {
    repositories {
        mavenCentral()
        maven {
            url 'http://artifactory.dmdirc.com/artifactory/repo'
        }
    }

    dependencies {
        classpath group: 'com.github.shanemcc', name: 'jgit-describe', version: '0.5'
        classpath group: 'org.eclipse.jgit', name: 'org.eclipse.jgit', version: '2.3.1.+'
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.2.1'
}

subprojects.each { evaluationDependsOn it.path }
task jar(dependsOn: subprojects*.jar)
task test(dependsOn: subprojects*.test)
tasks.dependencies.dependsOn subprojects*.tasks.dependencies

def plugin(name) {
    if (allprojects.find { it.name == name }) {
        project(name)
    } else if (allprojects.find { it.name == 'plugins:' + name }) {
        project('plugins:' + name)
    } else if (allprojects.find { it.name == 'modules:plugins:' + name }) {
        project('modules:plugins:' + name)
    } else {
        println "Couldn't find project $name"
    }
}
