Skip to content

Commit

Permalink
Prevent publishing invalid versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Virtlink committed Jul 24, 2024
1 parent e66dd1a commit ab809ca
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.metaborg.convention

import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.tasks.AbstractPublishToMaven
import org.gradle.api.publish.maven.tasks.PublishToMavenLocal
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.extra
import org.gradle.kotlin.dsl.withType
import java.util.*

Expand Down Expand Up @@ -100,7 +102,32 @@ class MavenPublishConventionPlugin: Plugin<Project> {
}
}

// Ensure we don't allow publishing if the repository is dirty or unversioned
val assertValidVersion = project.tasks.register("assertValidVersion") {
group = "Verification"
description = "Asserts that there is a valid version for $project."

doLast {
val versionString = project.version.toString()
if (versionString == Project.DEFAULT_VERSION) {
throw GradleException("Cannot publish, project has no version: ${project.version}")
} else if (versionString.endsWith("+dirty")) {
throw GradleException("Cannot publish, project has a dirty version: ${project.version}")
}
}
}

tasks.withType<AbstractPublishToMaven>().configureEach {
// Make publishing depend on having a valid version
dependsOn(assertValidVersion)
}

gradle.taskGraph.whenReady {
tasks.withType<PublishToMavenLocal>().configureEach {
doLast {
println("Published ${publication.name} to mavenLocal: ${project.group}:${project.name}:${project.version}")
}
}
tasks.withType<PublishToMavenRepository>().configureEach {
// Conditionally enable the tasks that publish to the respective repositories
when {
Expand All @@ -111,6 +138,10 @@ class MavenPublishConventionPlugin: Plugin<Project> {
onlyIf { extension.publishToGitHubPackages.get() }
}
}

doLast {
println("Published ${publication.name} to ${repository.name}: ${project.group}:${project.name}:${project.version}")
}
}
}
}
Expand Down

0 comments on commit ab809ca

Please sign in to comment.