apply plugin: 'groovy'

repositories {
  mavenCentral()
}

dependencies {
    compileOnly localGroovy()
    compileOnly gradleApi()
    testCompile gradleTestKit()
    testCompile('org.spockframework:spock-core:1.1-groovy-2.4')  {
        exclude module: 'groovy-all'
    }
}

if (JavaVersion.current().isJava9Compatible()) {
    tasks.withType(GroovyCompile.class).configureEach {
        groovyOptions.fork 'jvmArgs': jpmsOptions
    }
}

tasks.named('test') {
    inputs.files tasks.named('jar')
    systemProperty 'bnd_version', bnd('bnd_version')
    File source = project.file('testresources')
    File target = new File(project.buildDir, 'testresources')
    doFirst { // copy test resources into build dir
        project.delete(target)
        copy {
            from source
            into target
        }
    }
}

tasks.named('testClasses') {
    def pluginClasspath = { project.files(configurations.runtimeClasspath.allArtifacts.files, 
       configurations.runtimeClasspath.resolvedConfiguration.files) }
    inputs.files(pluginClasspath).withPropertyName('pluginClasspath')
    def pluginClasspathFile = { new File(tasks.getByName('compileTestJava').destinationDir, 'plugin-under-test-metadata.properties') }
    outputs.file(pluginClasspathFile).withPropertyName('pluginClasspathFile')
    doLast {
        Properties properties = new Properties()
        properties.setProperty('implementation-classpath', pluginClasspath().asPath)
        pluginClasspathFile().withOutputStream {
          properties.store(it, 'plugin-under-test-metadata')
        }
    }
}

tasks.named('release') {
  dependsOn 'groovydoc'
}
