Skip to content

Commit

Permalink
Add integration with Gradle Plugin Development Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed May 18, 2024
1 parent b24a1de commit 67c2671
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.withType
import org.gradle.plugin.devel.GradlePluginDevelopmentExtension

/**
* @author Silvio Giebl
Expand All @@ -31,6 +32,9 @@ class MetadataPlugin : Plugin<Project> {
project.plugins.withId("biz.aQute.bnd.builder") {
setBndMetadata(project, metadata)
}
project.plugins.withId("org.gradle.java-gradle-plugin") {
setGradlePluginMetadata(project, metadata)
}
}

private fun setPomMetadata(project: Project, metadata: MetadataExtensionImpl) {
Expand Down Expand Up @@ -101,6 +105,18 @@ class MetadataPlugin : Plugin<Project> {
}
}
}

private fun setGradlePluginMetadata(project: Project, metadata: MetadataExtensionImpl) {
val gradlePluginExtension = project.extensions.getByType<GradlePluginDevelopmentExtension>()
gradlePluginExtension.website.convention(metadata.url)
gradlePluginExtension.vcsUrl.convention(metadata.scm.provider.flatMap { it.url })
project.afterEvaluate {
gradlePluginExtension.plugins.configureEach {
if (displayName == null) displayName = metadata.readableName.get()
if (description == null) description = metadata.description.get()
}
}
}
}

const val EXTENSION_NAME = "metadata"
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package io.github.sgtsilvio.gradle.metadata

import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File
import java.util.Properties

/**
* @author Silvio Giebl
*/
internal class GradlePluginDevelopmentInteroperabilityTest {

@Test
fun test(@TempDir projectDir: File) {
projectDir.resolve("settings.gradle.kts").writeText(
"""
rootProject.name = "test"
""".trimIndent()
)
projectDir.resolve("build.gradle.kts").writeText(
"""
import java.util.Properties
plugins {
`java-gradle-plugin`
id("io.github.sgtsilvio.gradle.metadata")
}
group = "com.example"
version = "1.2.3"
description = "Test is a generic gradle plugin provided by Example Org"
metadata {
readableName.set("Test")
url.set("www.example.com/test")
github {}
}
gradlePlugin {
plugins {
create("test") {
id = "com.example.test"
implementationClass = "com.example.test.TestPlugin"
}
create("test2") {
id = "com.example.test2"
implementationClass = "com.example.test.Test2Plugin"
}
create("test3") {
id = "com.example.test3"
implementationClass = "com.example.test.Test3Plugin"
displayName = "Test 3 (custom)"
description = "Test 3 description (custom)"
}
}
}
tasks.register("gradlePluginProperties") {
doLast {
val properties = Properties()
properties["website"] = gradlePlugin.website.get()
properties["vcsUrl"] = gradlePlugin.vcsUrl.get()
val testPlugin = gradlePlugin.plugins["test"]
properties["plugins.test.displayName"] = testPlugin.displayName
properties["plugins.test.description"] = testPlugin.description
val test2Plugin = gradlePlugin.plugins["test2"]
properties["plugins.test2.displayName"] = test2Plugin.displayName
properties["plugins.test2.description"] = test2Plugin.description
val test3Plugin = gradlePlugin.plugins["test3"]
properties["plugins.test3.displayName"] = test3Plugin.displayName
properties["plugins.test3.description"] = test3Plugin.description
file("gradle-plugin.properties").outputStream().use {
properties.store(it, null)
}
}
}
""".trimIndent()
)

val result = GradleRunner.create()
.withProjectDir(projectDir)
.withPluginClasspath()
.withArguments("gradlePluginProperties")
.build()

assertEquals(TaskOutcome.SUCCESS, result.task(":gradlePluginProperties")?.outcome)

val properties = Properties()
projectDir.resolve("gradle-plugin.properties").inputStream().use {
properties.load(it)
}
assertEquals("www.example.com/test", properties["website"])
assertEquals("https://github.com/example/test.git", properties["vcsUrl"])
assertEquals("Test", properties["plugins.test.displayName"])
assertEquals("Test is a generic gradle plugin provided by Example Org", properties["plugins.test.description"])
assertEquals("Test", properties["plugins.test2.displayName"])
assertEquals("Test is a generic gradle plugin provided by Example Org", properties["plugins.test2.description"])
assertEquals("Test 3 (custom)", properties["plugins.test3.displayName"])
assertEquals("Test 3 description (custom)", properties["plugins.test3.description"])
}
}

0 comments on commit 67c2671

Please sign in to comment.