initial commit
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* This Kotlin source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package my.first.plugin
|
||||
|
||||
import java.io.File
|
||||
import org.gradle.testkit.runner.GradleRunner
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* A simple functional test for the 'my.first.plugin.greeting' plugin.
|
||||
*/
|
||||
class MyFirstPluginPluginFunctionalTest {
|
||||
@Test
|
||||
fun `can run task`() {
|
||||
// Setup the test build
|
||||
val projectDir = File("build/functionalTest")
|
||||
projectDir.mkdirs()
|
||||
projectDir.resolve("settings.gradle").writeText("")
|
||||
projectDir.resolve("build.gradle").writeText("""
|
||||
plugins {
|
||||
id('my.first.plugin.greeting')
|
||||
}
|
||||
""")
|
||||
|
||||
// Run the build
|
||||
val runner = GradleRunner.create()
|
||||
runner.forwardOutput()
|
||||
runner.withPluginClasspath()
|
||||
runner.withArguments("greeting")
|
||||
runner.withProjectDir(projectDir)
|
||||
val result = runner.build();
|
||||
|
||||
// Verify the result
|
||||
Assert.assertTrue(result.output.contains("Hello from plugin 'my.first.plugin.greeting'"))
|
||||
}
|
||||
}
|
39
src/main/kotlin/my/first/plugin/MyFirstPluginPlugin.kt
Normal file
39
src/main/kotlin/my/first/plugin/MyFirstPluginPlugin.kt
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* This Kotlin source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package my.first.plugin
|
||||
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.Plugin
|
||||
|
||||
|
||||
///**
|
||||
// * A simple 'hello world' plugin.
|
||||
// */
|
||||
//class MyFirstPluginPlugin: Plugin<Project> {
|
||||
// override fun apply(project: Project) {
|
||||
// // Register a task
|
||||
// project.tasks.register("greeting") { task ->
|
||||
// task.doLast {
|
||||
// println("Hello from plugin 'my.first.plugin.greeting'")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
open class MyFirstPluginPluginExtension {
|
||||
var message: String? = null
|
||||
var greeter: String? = null
|
||||
}
|
||||
|
||||
class MyFirstPluginPlugin : Plugin<Project> {
|
||||
override fun apply(project: Project) {
|
||||
val extension = MyFirstPluginPluginExtension()
|
||||
project.extensions.add(MyFirstPluginPluginExtension::class.java, "my_first_plugin", MyFirstPluginPluginExtension())
|
||||
project.tasks.register("hello") {
|
||||
it.doLast {
|
||||
println("${extension.message} from ${extension.greeter}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
33
src/main/kotlin/my/first/plugin/Powerup.kt
Normal file
33
src/main/kotlin/my/first/plugin/Powerup.kt
Normal file
@@ -0,0 +1,33 @@
|
||||
package my.first.plugin
|
||||
|
||||
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)
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* This Kotlin source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package my.first.plugin.test
|
||||
|
||||
import my.first.plugin.installResource
|
||||
import org.gradle.testfixtures.ProjectBuilder
|
||||
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.File
|
||||
import java.net.URLClassLoader
|
||||
import java.nio.file.Path
|
||||
|
||||
|
||||
/**
|
||||
* A simple unit test for the 'my.first.plugin.greeting' plugin.
|
||||
*/
|
||||
class MyFirstPluginPluginTest {
|
||||
|
||||
@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", System.getProperty("test.gradle.user.home", "."))
|
||||
.withPluginClasspath()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun fooKotlin() {
|
||||
installResource("build.gradle.kts", testProjectDir)
|
||||
installResource("settings.gradle.kts", testProjectDir)
|
||||
installResource("gradle.properties", testProjectDir)
|
||||
val runner = getStandardGradleRunnerFor("hello")
|
||||
val result = runner.build()
|
||||
println(result.output)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun fooGroovy() {
|
||||
installResource("build.gradle", testProjectDir)
|
||||
installResource("settings.gradle.kts", testProjectDir)
|
||||
installResource("gradle.properties", testProjectDir)
|
||||
val runner = getStandardGradleRunnerFor("hello")
|
||||
val result = runner.build()
|
||||
println(result.output)
|
||||
}
|
||||
|
||||
// @Test
|
||||
// fun `plugin registers task`() {
|
||||
// // Create a test project and apply the plugin
|
||||
// val project = ProjectBuilder.builder().build()
|
||||
// project.plugins.apply("my.first.plugin.MyFirstPlugin")
|
||||
//
|
||||
// // Verify the result
|
||||
// val task = project.tasks.findByName("hello")!!
|
||||
// println(task.outputs.hasOutput)
|
||||
// }
|
||||
}
|
2
src/test/resources/gradle.properties
Normal file
2
src/test/resources/gradle.properties
Normal file
@@ -0,0 +1,2 @@
|
||||
org.gradle.jvmargs=-Xmx256m
|
||||
org.gradle.caching=false
|
8
src/test/resources/my/first/plugin/test/build.gradle
Normal file
8
src/test/resources/my/first/plugin/test/build.gradle
Normal file
@@ -0,0 +1,8 @@
|
||||
plugins {
|
||||
id "net.corda.my-first-plugin"
|
||||
}
|
||||
|
||||
my_first_plugin {
|
||||
message = 'Hi'
|
||||
greeter = 'Gradle'
|
||||
}
|
21
src/test/resources/my/first/plugin/test/build.gradle.kts
Normal file
21
src/test/resources/my/first/plugin/test/build.gradle.kts
Normal file
@@ -0,0 +1,21 @@
|
||||
//buildscript {
|
||||
// repositories {
|
||||
// mavenLocal()
|
||||
// jcenter()
|
||||
// mavenCentral()
|
||||
// }
|
||||
// dependencies {
|
||||
// classpath("net.corda:my-first-plugin:0.1")
|
||||
// }
|
||||
//}
|
||||
|
||||
plugins {
|
||||
id("net.corda.my-first-plugin")
|
||||
}
|
||||
//apply(plugin = "net.corda.my-first-plugin")
|
||||
import my.first.plugin.MyFirstPluginPluginExtension
|
||||
|
||||
configure<my.first.plugin.MyFirstPluginPluginExtension> {
|
||||
message = "Hi"
|
||||
greeter = "Gradle"
|
||||
}
|
0
src/test/resources/settings.gradle.kts
Normal file
0
src/test/resources/settings.gradle.kts
Normal file
Reference in New Issue
Block a user