Skip to content

Commit

Permalink
Automatically apply the mappie dependency via the Gradle plugin (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoppier authored Jan 2, 2025
1 parent b619935 commit 608ccae
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ plugins {
id("tech.mappie.plugin") version "x.y.z"
}
```
The `mappie-api` dependency must be added to the `build.gradle.kts` file for the programming interface
When using the plugin before version `1.0.0`, the `mappie-api` dependency must be added to the `build.gradle.kts` file for the programming interface
```kotlin
dependencies {
implementation("tech.mappie:mappie-api:x.y.z")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import java.io.File
class NoVisibleConstructorTest {

data class Input(val name: String)

@ConsistentCopyVisibility
data class Output private constructor(val name: String)

@TempDir
Expand Down
2 changes: 1 addition & 1 deletion gradle-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
src/main/resources/version.properties
src/main/resources/mappie.properties
8 changes: 4 additions & 4 deletions gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ gradlePlugin {
}
}

tasks.register("updateCompilerPluginVersion") {
tasks.register("updateMappieProperties") {
group = "build"
description = "Update version.properties file for Gradle plugin."
description = "Update mappie.properties file for Gradle plugin."
doLast {
val directory = project.mkdir("src/main/resources")
File(directory, "version.properties").writeText("version=${project.version}")
File(directory, "mappie.properties").writeText("VERSION=${project.version}")
}
}

tasks.compileKotlin {
dependsOn("updateCompilerPluginVersion")
dependsOn("updateMappieProperties")
}

tasks.test {
Expand Down
18 changes: 7 additions & 11 deletions gradle-plugin/src/main/kotlin/tech/mappie/MappieGradlePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package tech.mappie
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.jetbrains.kotlin.gradle.plugin.*
import java.util.*

@Suppress("unused")
class MappieGradlePlugin : KotlinCompilerPluginSupportPlugin {

override fun apply(target: Project) {
Expand All @@ -16,6 +16,10 @@ class MappieGradlePlugin : KotlinCompilerPluginSupportPlugin {
with (kotlinCompilation.project) {
logger.info("Mappie plugin ${getPluginArtifact().version} applied")

kotlinCompilation.dependencies {
implementation(dependencies.create("tech.mappie:mappie-api:${getPluginArtifact().version}"))
}

val extension = extensions.getByType(MappieExtension::class.java)
return provider {
buildList {
Expand All @@ -42,18 +46,10 @@ class MappieGradlePlugin : KotlinCompilerPluginSupportPlugin {
SubpluginArtifact(
groupId = "tech.mappie",
artifactId = "mappie-compiler-plugin",
version = javaClass.classLoader.getResourceAsStream("version.properties").use {
Properties().apply { load(it) }.getProperty("version")
},
version = MappieProperties.version,
)

override fun isApplicable(kotlinCompilation: KotlinCompilation<*>) =
kotlinCompilation.target.project.run {
hasMappiePlugin()
}

private fun Project.hasMappiePlugin() =
plugins.hasPlugin(MappieGradlePlugin::class.java)
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>) = true

private fun Project.checkCompatibility() {
val version = getKotlinPluginVersion()
Expand Down
12 changes: 12 additions & 0 deletions gradle-plugin/src/main/kotlin/tech/mappie/MappieProperties.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tech.mappie

import java.util.Properties

object MappieProperties {

private val properties = javaClass.classLoader.getResourceAsStream("mappie.properties").use {
Properties().apply { load(it) }
}

val version = properties["VERSION"] as String
}
84 changes: 64 additions & 20 deletions gradle-plugin/src/test/kotlin/tech/mappie/testing/TestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import org.junit.jupiter.api.io.TempDir
import java.io.File
import java.util.*

enum class KotlinPlatform { JVM, MULTIPLATFORM }

abstract class TestBase {

@TempDir
protected lateinit var directory: File

protected lateinit var runner: GradleRunner

protected open val platform: KotlinPlatform = KotlinPlatform.JVM

protected open val gradleVersion: String? = null

protected open val kotlinVersion = "2.1.0"
Expand All @@ -27,10 +31,6 @@ abstract class TestBase {
gradleVersion?.let { withGradleVersion(gradleVersion) }
}

directory.resolve("src/main/kotlin").mkdirs()
directory.resolve("src/main/java").mkdirs()
directory.resolve("src/test/kotlin").mkdirs()

gradleVersion?.let { println("Using Gradle version $it") }
println("Using Kotlin version $kotlinVersion")

Expand All @@ -45,22 +45,44 @@ abstract class TestBase {
""".trimIndent()
)

when (platform) {
KotlinPlatform.JVM -> jvm()
KotlinPlatform.MULTIPLATFORM -> multiplatform()
}
}

protected fun kotlin(file: String, @Language("kotlin") code: String) {
directory.resolve(file).apply {
appendText(code)
}
}

protected fun java(file: String, @Language("java") code: String) {
directory.resolve(file).apply {
appendText(code)
}
}

private fun jvm() {
directory.resolve("src/main/kotlin").mkdirs()
directory.resolve("src/main/java").mkdirs()
directory.resolve("src/test/kotlin").mkdirs()

kotlin("build.gradle.kts",
"""
plugins {
id("org.jetbrains.kotlin.jvm") version "$kotlinVersion"
id("tech.mappie.plugin") version "$version"
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
implementation("tech.mappie:mappie-api:$version")
testImplementation(kotlin("test"))
}
repositories {
mavenLocal()
mavenCentral()
}
tasks.test {
useJUnitPlatform()
Expand All @@ -73,21 +95,43 @@ abstract class TestBase {
)
}

protected fun kotlin(file: String, @Language("kotlin") code: String) {
directory.resolve(file).apply {
appendText(code)
}
}
private fun multiplatform() {
directory.resolve("src/commonMain/kotlin").mkdirs()
directory.resolve("src/commonTest/kotlin").mkdirs()
directory.resolve("src/jvmMain/kotlin").mkdirs()

protected fun java(file: String, @Language("java") code: String) {
directory.resolve(file).apply {
appendText(code)
}
kotlin("build.gradle.kts",
"""
plugins {
id("org.jetbrains.kotlin.multiplatform") version "$kotlinVersion"
id("tech.mappie.plugin") version "$version"
}
repositories {
mavenLocal()
mavenCentral()
}
kotlin {
applyDefaultHierarchyTemplate()
sourceSets {
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
}
jvm()
}
""".trimIndent()
)
}

companion object {
private val version = javaClass.classLoader.getResourceAsStream("version.properties").use {
Properties().apply { load(it) }.getProperty("version")
private val version = javaClass.classLoader.getResourceAsStream("mappie.properties").use {
Properties().apply { load(it) }.getProperty("VERSION")
}

@BeforeAll
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.mappie.testing.compatibility.gradle

import org.junit.jupiter.api.Test

class Gradle812CompatibilityTest : GradleCompatibilityTestBase() {

override val gradleVersion = "8.12"

@Test
fun `test compatibility with gradle 8_12`() {
runner.withArguments("build").build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package tech.mappie.testing.compatibility.multiplatform

import org.junit.jupiter.api.Test
import tech.mappie.testing.KotlinPlatform
import tech.mappie.testing.TestBase

class MultiplatformCompatibilityTest : TestBase() {

override val platform = KotlinPlatform.MULTIPLATFORM

@Test
fun `test compatibility with multiplatform`() {
kotlin("src/commonMain/kotlin/CommonMapper.kt",
"""
import tech.mappie.api.ObjectMappie
data class CommonInput(val string: String)
data class CommonOutput(val string: String)
object CommonMapper : ObjectMappie<CommonInput, CommonOutput>()
""".trimIndent()
)

kotlin("src/commonTest/kotlin/CommonMapperTest.kt",
"""
import kotlin.test.*
class CommonMapperTest {
@Test
fun `map CommonInput to CommonOutput`() {
assertEquals(
CommonOutput("value"),
CommonMapper.map(CommonInput("value")),
)
}
}
""".trimIndent()
)

kotlin("src/jvmMain/kotlin/JvmMapper.kt",
"""
import tech.mappie.api.ObjectMappie
data class JvmInput(val string: String, val int: Int)
data class JvmOutput(val string: String, val int: Int)
object JvmMapper : ObjectMappie<JvmInput, JvmOutput>()
""".trimIndent()
)

runner.withArguments("build").build()
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.10.0
version=0.11.0-SNAPSHOT
4 changes: 4 additions & 0 deletions website/src/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
title: "Changelog"
layout: "layouts/changelog.html"
changelog:
- date: "tbd"
title: "v1.0.0"
items:
- "[#148](https://github.com/Mr-Mappie/mappie/issues/148) the `mappie-api` dependency is now applied automatically."
- date: "2024-12-15"
title: "v0.10.0"
items:
Expand Down
3 changes: 2 additions & 1 deletion website/src/posts/getting-started/posts/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ plugin. We can apply Mappie by adding the following plugin to the Gradle build f
</div>
{% endraw %}

The `mappie-api` dependency must be added to the Gradle build file file for the programming interface
When using mappie version below `1.0.0` or when you want to add the `mappie-api` dependency manually,
the `mappie-api` dependency can be added as follows
{% raw %}
<div class="nav-container">
<ul class="nav">
Expand Down

0 comments on commit 608ccae

Please sign in to comment.