Skip to content

Commit

Permalink
Update plugin to expose 'tagExists' property in Tag task
Browse files Browse the repository at this point in the history
DidWork might not always be enough in monorepos where some modules share
the version tag with 'root', so we add an additional property that will
be set to true if the tag was attempted to be made but already existed.
  • Loading branch information
serpro69 committed Mar 28, 2024
1 parent a9a883a commit 6ae4c46
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
25 changes: 19 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io.github.serpro69.semverkt.gradle.plugin.tasks.TagTask
import org.gradle.api.tasks.testing.TestResult.ResultType
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
Expand Down Expand Up @@ -42,8 +43,8 @@ subprojects {

val isSnapshot by lazy {
provider {
version.toString().startsWith("0.0.0")
|| version.toString().endsWith("SNAPSHOT")
subProject.version.toString().startsWith("0.0.0")
|| subProject.version.toString().endsWith("SNAPSHOT")
}
}
val newTag by lazy { provider { subProject.tasks.getByName("tag").didWork } }
Expand Down Expand Up @@ -257,16 +258,20 @@ subprojects {
tasks.withType<PublishToMavenLocal>().configureEach {
onlyIf("In development") { isSnapshot.get() }
}

tasks.withType<Sign>().configureEach {
dependsOn(subProject.tasks.getByName("tag"))
onlyIf("Not snapshot") { !isSnapshot.get() }
onlyIf("New tag") { newTag.get() }
}
}

tasks.withType<DokkaTask>().configureEach {
tasks.withType<TagTask>().configureEach {
onlyIf("Not snapshot") { !isSnapshot.get() }
}

tasks.withType<Sign>().configureEach {
dependsOn(subProject.tasks.getByName("tag"))
tasks.withType<DokkaTask>().configureEach {
onlyIf("Not snapshot") { !isSnapshot.get() }
onlyIf("New tag") { newTag.get() }
}

tasks {
Expand All @@ -276,6 +281,14 @@ subprojects {
}
}

tasks.withType<TagTask>().configureEach {
val isSnapshot = provider {
version.toString().startsWith("0.0.0")
|| version.toString().endsWith("SNAPSHOT")
}
onlyIf("Not snapshot") { !isSnapshot.get() }
}

val jar by tasks.getting(Jar::class) {
enabled = false // nothing to build in root project
}
Expand Down
26 changes: 20 additions & 6 deletions semantic-versioning/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import com.gradle.publish.PublishTask
import io.github.serpro69.semverkt.gradle.plugin.tasks.TagTask
import io.github.serpro69.semverkt.spec.Semver
import org.jetbrains.kotlin.gradle.utils.`is`

plugins {
`java-gradle-plugin`
Expand Down Expand Up @@ -82,7 +84,7 @@ gradlePlugin {
id = "${project.group}.${name}"
displayName = "Automated semantic versioning of gradle projects through git tags"
description = "This plugin helps you to automatically version your gradle project according to semver rules"
tags = listOf("semver", "release", "semantic", "versioning", "semantic-release", "semver-release")
tags = listOf("semantic-release", "semantic-versioning", "semver-release", "semver", "release", "semantic", "versioning")
implementationClass = "io.github.serpro69.semverkt.gradle.plugin.SemverKtPlugin"
}
}
Expand All @@ -106,9 +108,23 @@ publishing {
}
}

val isSnapshot by lazy {
provider {
version.toString().startsWith("0.0.0")
|| version.toString().endsWith("SNAPSHOT")
}
}
val newTag by lazy { provider { tasks.getByName("tag").didWork } }

tasks.withType<TagTask>().configureEach {
onlyIf("Not snapshot") { !isSnapshot.get() }
}

tasks.withType<PublishTask>().configureEach {
dependsOn(tasks.getByName("tag"))
val predicate = provider {
!version.toString().startsWith("0.0.0")
!isSnapshot.get()
&& newTag.get()
&& group == "Plugin Portal"
}
onlyIf("New release") { predicate.get() }
Expand All @@ -117,13 +133,11 @@ tasks.withType<PublishTask>().configureEach {
// workaround for https://github.com/gradle-nexus/publish-plugin/issues/84
tasks.withType<PublishToMavenRepository>().configureEach {
val predicate = provider {
version.toString().startsWith("0.0.0")
&& repository.name == "localPluginRepo"
isSnapshot.get() && repository.name == "localPluginRepo"
}
onlyIf("In development") { predicate.get() }
}

tasks.withType<PublishToMavenLocal>().configureEach {
val predicate = provider { version.toString().startsWith("0.0.0") }
onlyIf("In development") { predicate.get() }
onlyIf("In development") { isSnapshot.get() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import io.github.serpro69.semverkt.spec.Semver
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import org.gradle.api.GradleException
import org.gradle.api.internal.provider.DefaultProperty
import org.gradle.api.logging.Logging
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction

abstract class TagTask : SemverReleaseTask() {
Expand All @@ -26,6 +28,14 @@ abstract class TagTask : SemverReleaseTask() {
@get:Internal
internal abstract val moduleConfig: Property<ModuleConfig>

/**
* Returns `true` if the tag that this task tried to create already exists in the repository,
* or `false` otherwise
*/
@get:Internal
var tagExists: Boolean = false
private set

init {
// check if tag already set, if so tell plugin it's UP-TO-DATE
// NB! it won't handle multi-module projects because currentVersion will be set to all projects
Expand All @@ -36,6 +46,7 @@ abstract class TagTask : SemverReleaseTask() {

@TaskAction
fun tag() {
tagExists = false
didWork = false // set to true only after we create a git tag with this task
// check if tag already set, if so tell plugin it's UP-TO-DATE
val (current, latest, next) = semanticProject.get()
Expand Down Expand Up @@ -68,7 +79,7 @@ abstract class TagTask : SemverReleaseTask() {
logger.lifecycle("Can't create a tag for a snapshot version")
} else if (!dryRun.get()) run {
// check if tag exists, don't try to create a duplicate
val tagExists = GitRepository(config).use { repo ->
tagExists = GitRepository(config).use { repo ->
// check if repo is clean
when (config.git.repo.cleanRule) {
CleanRule.ALL -> if (!repo.isClean()) throw GradleException("Release with non-clean repository is not allowed")
Expand Down

0 comments on commit 6ae4c46

Please sign in to comment.