refactor of dependency-export plugin to allow for cli argument configuration

This commit is contained in:
2021-02-08 14:19:53 +01:00
parent 6f7ddc42ce
commit 2ff8c958c1
24 changed files with 537 additions and 447 deletions

View File

@@ -0,0 +1,93 @@
package net.woggioni.gradle.dependency.export;
import lombok.SneakyThrows;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.stream.Stream;
public class DependencyExportPluginTest {
private static InputStream resourceFromClass(Class<?> cls, String resourceName) {
return cls.getResourceAsStream(resourceName);
}
private static InputStream resourceFromClassLoader(Class<?> cls, String resourceName) {
return cls.getClassLoader().getResourceAsStream(resourceName);
}
@SneakyThrows
private static void installResource(Class<?> cls, String resourceName, Path destination) {
Path outputFile;
Path realDestination;
if (Files.isSymbolicLink(destination)) {
realDestination = destination.toRealPath();
} else {
realDestination = destination;
}
if(!Files.exists(realDestination)) {
Files.createDirectories(realDestination.getParent());
outputFile = realDestination;
} else if(Files.isDirectory(realDestination)) {
outputFile = realDestination.resolve(resourceName.substring(1 + resourceName.lastIndexOf('/')));
} else if(Files.isRegularFile(realDestination)) {
outputFile = realDestination;
} else throw new IllegalStateException("Path '${realDestination}' is neither a file nor a directory");
Optional<InputStream> inputStreamOptional = Stream.<BiFunction<Class<?>, String, InputStream>>of(
DependencyExportPluginTest::resourceFromClass,
DependencyExportPluginTest::resourceFromClassLoader
).map(f -> f.apply(cls, resourceName)).filter(Objects::nonNull).findFirst();
try(InputStream inputStream = inputStreamOptional.orElseThrow(() -> new FileNotFoundException(resourceName))){
Files.copy(inputStream, outputFile, StandardCopyOption.REPLACE_EXISTING);
}
}
@TempDir
public Path testGradleHomeDir;
@TempDir
public Path testProjectDir;
public Path buildFile;
@BeforeEach
public void setup() {
buildFile = testProjectDir.resolve("build.gradle.kts");
}
public GradleRunner getStandardGradleRunnerFor(String taskName) {
return GradleRunner.create()
.withDebug(true)
.withProjectDir(testProjectDir.toFile())
.withArguments(taskName, "-s", "--info", "-g", testGradleHomeDir.toString())
.withPluginClasspath();
}
@Test
public void testKotlin() {
installResource(getClass(), "build.gradle.kts", testProjectDir);
installResource(getClass(),"settings.gradle.kts", testProjectDir);
installResource(getClass(),"gradle.properties", testProjectDir);
GradleRunner runner = getStandardGradleRunnerFor("exportDependencies");
runner.build();
}
@Test
public void testGroovy() {
installResource(getClass(),"build.gradle", testProjectDir);
installResource(getClass(),"settings.gradle.kts", testProjectDir);
installResource(getClass(),"gradle.properties", testProjectDir);
GradleRunner runner = getStandardGradleRunnerFor("exportDependencies");
runner.build();
}
}

View File

@@ -1,52 +0,0 @@
/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package net.woggioni.plugins.dependency.export
import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.nio.file.Path
class DependencyExportPluginTest {
@TempDir
lateinit var testGradleHomeDir : Path
@TempDir
lateinit var testProjectDir : Path
lateinit var buildFile : Path
@BeforeEach
fun setup() {
buildFile = testProjectDir.resolve("build.gradle.kts")
}
fun getStandardGradleRunnerFor(taskName: String): GradleRunner {
return GradleRunner.create()
.withDebug(true)
.withProjectDir(testProjectDir.toFile())
.withArguments(taskName, "-s", "--info", "-g", testGradleHomeDir.toString())
.withPluginClasspath()
}
@Test
fun testKotlin() {
installResource("build.gradle.kts", testProjectDir)
installResource("settings.gradle.kts", testProjectDir)
installResource("gradle.properties", testProjectDir)
val runner = getStandardGradleRunnerFor("exportDependencies")
runner.build()
}
@Test
fun testGroovy() {
installResource("build.gradle", testProjectDir)
installResource("settings.gradle.kts", testProjectDir)
installResource("gradle.properties", testProjectDir)
val runner = getStandardGradleRunnerFor("exportDependencies")
runner.build()
}
}

View File

@@ -1,33 +0,0 @@
package net.woggioni.plugins.dependency.export
import java.io.FileNotFoundException
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
@Throws(IOException::class)
fun Any.installResource(resourceName: String, destination: Path) {
val outputFile = run {
val realDestination = if (Files.isSymbolicLink(destination)) {
destination.toRealPath()
} else {
destination
}
when {
!Files.exists(realDestination) -> {
Files.createDirectories(realDestination.parent)
realDestination
}
Files.isDirectory(realDestination) ->
realDestination.resolve(resourceName.substring(1 + resourceName.lastIndexOf('/')))
Files.isRegularFile(realDestination) -> realDestination
else -> throw IllegalStateException("Path '${realDestination}' is neither a file nor a directory")
}
}
(javaClass.getResourceAsStream(resourceName)
?: javaClass.classLoader.getResourceAsStream(resourceName))?.use { inputStream ->
Files.copy(inputStream, outputFile, StandardCopyOption.REPLACE_EXISTING)
} ?: throw FileNotFoundException(resourceName)
}

View File

@@ -0,0 +1,18 @@
plugins {
id 'java-library'
id "net.woggioni.gradle.dependency-export"
}
exportDependencies {
configurationName = 'compileClasspath'
}
repositories {
jcenter()
mavenLocal()
}
dependencies {
runtimeOnly("org.hibernate:hibernate-core:5.4.13.Final")
}

View File

@@ -0,0 +1,17 @@
plugins {
id("java-library")
id("net.woggioni.gradle.dependency-export")
}
repositories {
jcenter()
mavenLocal()
}
dependencies {
runtimeOnly("org.hibernate:hibernate-core:5.4.13.Final")
}
tasks.named<net.woggioni.gradle.dependency.export.ExportDependencies>("exportDependencies") {
configurationName.set("compileClasspath")
}

View File

@@ -1,17 +0,0 @@
plugins {
id "org.jetbrains.kotlin.jvm" version "1.3.61"
id "net.woggioni.plugins.dependency-export"
}
exportDependencies {
configurationName = 'runtime'
}
repositories {
jcenter()
mavenLocal()
}
dependencies {
runtime("org.hibernate:hibernate-core:5.4.13.Final")
}

View File

@@ -1,14 +0,0 @@
plugins {
kotlin("jvm") version "1.3.71"
id("net.woggioni.plugins.dependency-export")
}
repositories {
jcenter()
mavenLocal()
}
dependencies {
runtime("org.hibernate:hibernate-core:5.4.13.Final")
}