From 544118f1a7fb5ccbf486fd72d00f9d8eacacd895 Mon Sep 17 00:00:00 2001 From: Philipp Schirmer Date: Fri, 1 Mar 2024 16:02:50 +0100 Subject: [PATCH] Create release plugin Fixes #28 --- release/build.gradle.kts | 11 ++++ .../com/bakdata/gradle/ReleasePlugin.kt | 54 +++++++++++++++ .../com/bakdata/gradle/ReleasePluginTest.kt | 66 +++++++++++++++++++ .../com.bakdata.release.properties | 1 + settings.gradle | 2 +- 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 release/build.gradle.kts create mode 100644 release/src/main/kotlin/com/bakdata/gradle/ReleasePlugin.kt create mode 100644 release/src/test/kotlin/com/bakdata/gradle/ReleasePluginTest.kt create mode 100644 release/src/test/resources/META-INF/gradle-plugins/com.bakdata.release.properties diff --git a/release/build.gradle.kts b/release/build.gradle.kts new file mode 100644 index 0000000..9727d45 --- /dev/null +++ b/release/build.gradle.kts @@ -0,0 +1,11 @@ +plugins { + kotlin("jvm") + id("org.jetbrains.dokka") version "1.9.10" +} +apply(plugin = "org.gradle.kotlin.kotlin-dsl") + +description = "Configures Gradle Release plugin for usage in CI" + +dependencies { + implementation("net.researchgate", "gradle-release", "3.0.2") +} diff --git a/release/src/main/kotlin/com/bakdata/gradle/ReleasePlugin.kt b/release/src/main/kotlin/com/bakdata/gradle/ReleasePlugin.kt new file mode 100644 index 0000000..08ac706 --- /dev/null +++ b/release/src/main/kotlin/com/bakdata/gradle/ReleasePlugin.kt @@ -0,0 +1,54 @@ +/* + * The MIT License + * + * Copyright (c) 2024 bakdata GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.bakdata.gradle + +import net.researchgate.release.ReleaseExtension +import org.gradle.api.GradleException +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply +import org.gradle.kotlin.dsl.configure + +class ReleasePlugin : Plugin { + + override fun apply(rootProject: Project) { + if (rootProject.parent != null) { + throw GradleException("Apply this plugin only to the top-level project.") + } + + with(rootProject) { + apply(plugin = "net.researchgate.release") + + val disablePushToRemote: String? = project.property("disablePushToRemote") as? String + disablePushToRemote?.let { + configure { + git { + pushToRemote.set(!it.toBoolean()) + } + } + } + } + } +} diff --git a/release/src/test/kotlin/com/bakdata/gradle/ReleasePluginTest.kt b/release/src/test/kotlin/com/bakdata/gradle/ReleasePluginTest.kt new file mode 100644 index 0000000..aec995b --- /dev/null +++ b/release/src/test/kotlin/com/bakdata/gradle/ReleasePluginTest.kt @@ -0,0 +1,66 @@ +/* + * The MIT License + * + * Copyright (c) 2024 bakdata GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.bakdata.gradle + +import net.researchgate.release.ReleasePlugin +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatCode +import org.assertj.core.api.Condition +import org.assertj.core.api.SoftAssertions +import org.gradle.api.Project +import org.gradle.api.internal.project.DefaultProject +import org.gradle.testfixtures.ProjectBuilder +import org.junit.jupiter.api.Test + +internal class ReleasePluginTest { + + private fun Project.evaluate() { + (this as DefaultProject).evaluate() + } + + @Test + fun testSingleModuleProjectWithJavaFirst() { + val project = ProjectBuilder.builder().build() + + assertThatCode { + project.pluginManager.apply("com.bakdata.release") + project.evaluate() + }.doesNotThrowAnyException() + + SoftAssertions.assertSoftly { softly -> + softly.assertThat(project.plugins) + .haveExactly(1, Condition({ it is ReleasePlugin }, "Has release plugin")) + } + } + + @Test + fun testWrongApplicationInMultiModuleProject() { + val parent = ProjectBuilder.builder().withName("parent").build() + val child1 = ProjectBuilder.builder().withName("child1").withParent(parent).build() + + assertThatCode { child1.pluginManager.apply("com.bakdata.release") } + .satisfies { assertThat(it.cause).hasMessageContaining("top-level project") } + } +} diff --git a/release/src/test/resources/META-INF/gradle-plugins/com.bakdata.release.properties b/release/src/test/resources/META-INF/gradle-plugins/com.bakdata.release.properties new file mode 100644 index 0000000..536c0a2 --- /dev/null +++ b/release/src/test/resources/META-INF/gradle-plugins/com.bakdata.release.properties @@ -0,0 +1 @@ +implementation-class=com.bakdata.gradle.ReleasePlugin diff --git a/settings.gradle b/settings.gradle index 7995469..78d1b80 100644 --- a/settings.gradle +++ b/settings.gradle @@ -7,4 +7,4 @@ pluginManagement { rootProject.name = 'gradle-plugins' -include 'sonar', 'sonatype' +include 'sonar', 'sonatype', 'release'