From 2eba022b4e238549bd11f2cfcb326c574e98ff7a Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sun, 24 Dec 2023 10:23:42 -0800 Subject: [PATCH 1/2] =?UTF-8?q?Replace=20`=E5=B9=B2.file('base/changelog.g?= =?UTF-8?q?radle')`=20with=20an=20in-repo=20script=20as=20we=20workout=20K?= =?UTF-8?q?MP.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- gradle/changelog.gradle | 85 +++++++++++++++++++++++++++++++ selfie-lib/build.gradle | 2 +- selfie-runner-junit5/build.gradle | 2 +- 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 gradle/changelog.gradle diff --git a/build.gradle b/build.gradle index a919587b..db9e251e 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ repositories { } group = 干.proj('maven_group', 'the maven group, recommend com.diffplug') apply from: rootProject.file('gradle/spotless.gradle') -apply from: 干.file('base/changelog.gradle') +apply from: rootProject.file('gradle/changelog.gradle') apply from: 干.file('base/sonatype.gradle') // ./gradlew :dokkatooGeneratePublicationHtml diff --git a/gradle/changelog.gradle b/gradle/changelog.gradle new file mode 100644 index 00000000..fb452504 --- /dev/null +++ b/gradle/changelog.gradle @@ -0,0 +1,85 @@ +// no parameters needed, just apply `com.diffplug.spotless-changelog` on whichever project has +// CHANGELOG.md +// +// this script will look for the changelog tasks in either this project or the parent project, +// and then set the version from the spotless-changelog plugin. It will also hook the jar task +// (if present) to depend on changelogCheck, so that a jar will never get published with a bad +// changelog. Lastly, changelogBump will depend on a successful closeAndReleaseSonatypeStagingRepository. + +def requireEnablePublishing(Task task) { + if (project.findProperty('enable_publishing') == 'true') { + return true + } else { + task.doFirst { + throw new Error("Cannot run " + task + " without specifying enable_publishing=true") + } + return false + } +} + +if (tasks.names.contains('changelogCheck')) { + spotlessChangelog { + branch 'release' + appendDashSnapshotUnless_dashPrelease = true + } + // set the project version for POM, jar manifest, etc. + version = spotlessChangelog.versionNext + // ensures that nothing will be built if changelogPush will end up failing + afterEvaluate { + if (tasks.names.contains('jar')) { + tasks.named('jar').configure { + dependsOn tasks.named('changelogCheck') + } + } + // ensures that changelog bump and push only happens if the publish was successful + tasks.named('changelogBump').configure { + if (requireEnablePublishing(it)) { + if (plugins.hasPlugin('maven-publish')) { + if (plugins.hasPlugin('io.github.gradle-nexus.publish-plugin')) { + String pubName = plugins.hasPlugin('java-gradle-plugin') ? 'PluginMaven' : 'MavenJava' + dependsOn "publish${pubName}PublicationToSonatypeRepository" + // only the root project has closeSonatypeStagingRepository, and it finalizes all publishToSonatype tasks + // https://github.com/gradle-nexus/publish-plugin + dependsOn rootProject.tasks.named('closeAndReleaseSonatypeStagingRepository') + } else { + dependsOn "publishAllPublicationsToGitLabRepository" + } + } + // if we have a gradle plugin, we need to push it up to the plugin portal too + if (tasks.names.contains('publishPlugins')) { + dependsOn tasks.named('publishPlugins') + } + } + } + } +} else { + assert parent != null : 'If you apply this script to the root project, you must have plugin com.diffplug.spotless-changelog' + assert parent.tasks.names.contains('changelogCheck') : 'Neither this project nor its parent had the required plugin com.diffplug.spotless-changelog' + + // same as above, but pull changelog stuff from the parent changelog + version = parent.spotlessChangelog.versionNext + def childProject = project; + afterEvaluate { + if (tasks.names.contains('jar')) { + tasks.named('jar').configure { + dependsOn parent.tasks.named('changelogCheck') + } + } + parent.tasks.named('changelogBump').configure { + if (requireEnablePublishing(it)) { + if (childProject.plugins.hasPlugin('maven-publish')) { + if (rootProject.plugins.hasPlugin('io.github.gradle-nexus.publish-plugin')) { + String pubName = childProject.plugins.hasPlugin('java-gradle-plugin') ? 'PluginMaven' : 'MavenJava' + dependsOn childProject.tasks.named("publish${pubName}PublicationToSonatypeRepository") + dependsOn rootProject.tasks.named('closeAndReleaseSonatypeStagingRepository') + } else { + dependsOn childProject.tasks.named("publishAllPublicationsToGitLabRepository") + } + } + if (childProject.tasks.names.contains('publishPlugins')) { + dependsOn childProject.tasks.named('publishPlugins') + } + } + } + } +} diff --git a/selfie-lib/build.gradle b/selfie-lib/build.gradle index 83e59c72..ea486491 100644 --- a/selfie-lib/build.gradle +++ b/selfie-lib/build.gradle @@ -11,7 +11,7 @@ ext { maven_desc = 'Core logic and parsing for Selfie' } -apply from: 干.file('base/changelog.gradle') +apply from: rootProject.file('gradle/changelog.gradle') apply from: rootProject.file('gradle/spotless.gradle') kotlin { jvm { diff --git a/selfie-runner-junit5/build.gradle b/selfie-runner-junit5/build.gradle index 6a2da6ce..99a853b9 100644 --- a/selfie-runner-junit5/build.gradle +++ b/selfie-runner-junit5/build.gradle @@ -9,7 +9,7 @@ ext { maven_desc = 'JUnit 5 test runner for Selfie' } -apply from: 干.file('base/changelog.gradle') +apply from: rootProject.file('gradle/changelog.gradle') apply from: rootProject.file('gradle/spotless.gradle') apply plugin: 'java-library' dependencies { From a932ccf56b0e4de0430e94a6a594ec379c41e517 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sun, 24 Dec 2023 10:35:07 -0800 Subject: [PATCH 2/2] Publish selfie-lib-jvm. --- CHANGELOG.md | 1 + gradle/changelog.gradle | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65f10269..3cc2d161 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +* Publish `selfie-lib-jvm` instead of just `selfie-lib` to Maven Central. ## [0.1.1] - 2023-12-24 * Initial release, take 2. diff --git a/gradle/changelog.gradle b/gradle/changelog.gradle index fb452504..90765cfb 100644 --- a/gradle/changelog.gradle +++ b/gradle/changelog.gradle @@ -17,6 +17,16 @@ def requireEnablePublishing(Task task) { } } +def pubName() { + if (plugins.hasPlugin('java-gradle-plugin')) { + return 'PluginMaven' + } else if (plugins.hasPlugin('org.jetbrains.kotlin.multiplatform')) { + return 'Jvm' + } else { + return 'MavenJava' + } +} + if (tasks.names.contains('changelogCheck')) { spotlessChangelog { branch 'release' @@ -36,8 +46,7 @@ if (tasks.names.contains('changelogCheck')) { if (requireEnablePublishing(it)) { if (plugins.hasPlugin('maven-publish')) { if (plugins.hasPlugin('io.github.gradle-nexus.publish-plugin')) { - String pubName = plugins.hasPlugin('java-gradle-plugin') ? 'PluginMaven' : 'MavenJava' - dependsOn "publish${pubName}PublicationToSonatypeRepository" + dependsOn "publish${pubName()}PublicationToSonatypeRepository" // only the root project has closeSonatypeStagingRepository, and it finalizes all publishToSonatype tasks // https://github.com/gradle-nexus/publish-plugin dependsOn rootProject.tasks.named('closeAndReleaseSonatypeStagingRepository') @@ -69,8 +78,7 @@ if (tasks.names.contains('changelogCheck')) { if (requireEnablePublishing(it)) { if (childProject.plugins.hasPlugin('maven-publish')) { if (rootProject.plugins.hasPlugin('io.github.gradle-nexus.publish-plugin')) { - String pubName = childProject.plugins.hasPlugin('java-gradle-plugin') ? 'PluginMaven' : 'MavenJava' - dependsOn childProject.tasks.named("publish${pubName}PublicationToSonatypeRepository") + dependsOn childProject.tasks.named("publish${pubName()}PublicationToSonatypeRepository") dependsOn rootProject.tasks.named('closeAndReleaseSonatypeStagingRepository') } else { dependsOn childProject.tasks.named("publishAllPublicationsToGitLabRepository")