Skip to content

Commit

Permalink
Add MavenPublishConvention
Browse files Browse the repository at this point in the history
  • Loading branch information
Virtlink committed May 29, 2024
1 parent b6589e1 commit 97bf407
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 2 deletions.
11 changes: 11 additions & 0 deletions convention-plugin/src/main/kotlin/org/metaborg/gradle/Developer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.metaborg.gradle

/** Specifies a developer. */
data class Developer(
/** The developer's ID or username. */
val id: String,
/** The developer's full name. */
val name: String,
/** The developer's email address. */
val email: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import org.gradle.kotlin.dsl.*
@Suppress("unused")
class JavaConventionPlugin: Plugin<Project> {
override fun apply(project: Project): Unit = with(project) {
// Apply the configuration extension
// Add the configuration extension
val extension = extensions.create<JavaConventionExtension>("javaConvention")
extension.setConvention()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.metaborg.gradle

import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.jvm.toolchain.JavaLanguageVersion

/** Configuration for the Maven Publish convention. */
interface MavenPublishConventionExtension {
/** The owner of the repository. */
val repoOwner: Property<String>
/** The name of the repository. */
val repoName: Property<String>
/** The year the project was started. */
val inceptionYear: Property<String>
/** The developers of the project. */
val developers: ListProperty<Developer>
/** The source control management system. */
val scm: Property<SCM>
/** The open source license that the project is published under. */
val license: Property<OpenSourceLicense>
/** Whether to publish to GitHub packages. */
val publishToGitHubPackages: Property<Boolean>
/** Whether to publish to Metaborg Artifacts. */
val publishToMetaborgArtifacts: Property<Boolean>
/** Whether to sign the artifacts. */
val sign: Property<Boolean>
/** The HTTP SCM URL format. */
val httpUrlFormat: Property<String>
/** The SCM URL format. */
val scmUrlFormat: Property<String>


/**
* Sets the convention (default values) for the configuration extension.
*/
fun setConvention() {
scm.convention(SCM.GitHub)
license.convention(OpenSourceLicense.Apache2)
publishToGitHubPackages.convention(scm.map { it == SCM.GitHub })
publishToMetaborgArtifacts.convention(true)
sign.convention(false) // Not yet supported
httpUrlFormat.convention(scm.map { it.httpUrlFormat })
scmUrlFormat.convention(scm.map { it.scmUrlFormat })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,67 @@ package org.metaborg.gradle

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.kotlin.dsl.configure
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.withType
import java.util.*

class MavenPublishConventionPlugin: Plugin<Project> {
override fun apply(target: Project) {
override fun apply(project: Project): Unit = with(project) {
// Add the configuration extension
val extension = extensions.create<MavenPublishConventionExtension>("mavenPublishConvention")
extension.setConvention()

// Apply the Maven Publish plugin
plugins.apply("maven-publish")

configure<PublishingExtension> {
afterEvaluate {
publications {
withType<MavenPublication> {
pom {
description.set(project.description)
url.set(extension.httpUrlFormat.with(extension.repoOwner, extension.repoName) { format, owner, name ->
String.format(Locale.ROOT, format, owner, name)
})
inceptionYear.set(extension.inceptionYear)
licenses {
license {
name.set(extension.license.map { it.name })
url.set(extension.license.map { it.url })
distribution.set("repo")
}
}
developers {
extension.developers.map {
for (dev in it) {
developer {
id.set(dev.id)
name.set(dev.name)
email.set(dev.email)
}
}
}
}
scm {
connection.set(extension.scmUrlFormat.with(extension.repoOwner, extension.repoName) { format, owner, name ->
String.format(Locale.ROOT, format, owner, name)
})
developerConnection.set(extension.scmUrlFormat.with(extension.repoOwner, extension.repoName) { format, owner, name ->
String.format(Locale.ROOT, format, owner, name)
})
url.set(extension.scmUrlFormat.with(extension.repoOwner, extension.repoName) { format, owner, name ->
String.format(Locale.ROOT, format, owner, name)
})
}
}
}
}
}
}

// maven-publish plugin
// - Configure maven-publish
// configure SCM (GitHub by default, etc)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.metaborg.gradle

/** Specifies the open source license. */
enum class OpenSourceLicense(
/** The unique identifier of the license. */
val id: String,
/** The license URL. */
val url: String,
) {
// From: https://spdx.org/licenses/

/** Apache 2 */
Apache2("Apache-2.0", "https://www.apache.org/licenses/LICENSE-2.0.txt"),
}
12 changes: 12 additions & 0 deletions convention-plugin/src/main/kotlin/org/metaborg/gradle/SCM.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.metaborg.gradle

/** Specifies a source control management system. */
enum class SCM(
/** The HTTP URL for the SCM. */
val httpUrlFormat: String,
/** The SCM URL for the SCM. */
val scmUrlFormat: String,
) {
/** GitHub */
GitHub("https://github.com/%s/%s", "scm:[email protected]:%s/%s.git"),
}
15 changes: 15 additions & 0 deletions convention-plugin/src/main/kotlin/org/metaborg/gradle/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.metaborg.gradle

import org.gradle.api.provider.Provider

private fun <A0, A1> zipId(p0: Provider<A0>, p1: Provider<A1>): Provider<Pair<A0, A1>> =
p0.zip(p1) { a0, a1 -> Pair(a0, a1) }

fun <A0, A1, R> Provider<A0>.with(p1: Provider<A1>, transformer: (A0, A1) -> R): Provider<R> =
this.zip(p1) { a0, a1 -> transformer(a0, a1) }

fun <A0, A1, A2, R> Provider<A0>.with(p1: Provider<A1>, p2: Provider<A2>, transformer: (A0, A1, A2) -> R): Provider<R> =
this.zip(zipId(p1, p2)) { a0, (a1, a2) -> transformer(a0, a1, a2) }

fun <A0, A1, A2, A3, R> Provider<A0>.with(p1: Provider<A1>, p2: Provider<A2>, p3: Provider<A3>, transformer: (A0, A1, A2, A3) -> R): Provider<R> =
this.zip(zipId(p1, zipId(p2, p3))) { a0, (a1, a23) -> val (a2, a3) = a23; transformer(a0, a1, a2, a3) }

0 comments on commit 97bf407

Please sign in to comment.