Skip to content

Commit

Permalink
Refactor build logic to only publish on new tags
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed Mar 28, 2024
1 parent 6cdf97e commit 9aaab68
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ release-major: ## publishes next major release version
.PHONY: release-minor
release-minor: ## publishes next minor release version
./gradlew test integrationTest \
tag \
nativeCompile \
publishToSonatype \
closeSonatypeStagingRepository \
tag \
-Prelease -Pincrement=minor \
--info

Expand Down
36 changes: 27 additions & 9 deletions bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ plugins {
}

val bom = project
val notSnapshot by lazy {
provider {
!version.toString().endsWith("SNAPSHOT")
&& !version.toString().startsWith("0.0.0")
}
}
val newTag by lazy { provider { project.tasks.getByName("tag").didWork } }

// Exclude subprojects that will never be published so that when configuring this project
// we don't force their configuration and do unnecessary work
Expand Down Expand Up @@ -34,13 +41,10 @@ dependencies {
}

/**
* For additional providers, use a combination of rootProject and subproject names for artifact name and similar things.
* i.e. kotlin-faker-books, kotlin-faker-movies, kotlin-faker-tv, ...
*
* The "core" lib retains the same name as before: kotlin-faker
* For additional modules, we use a combination of rootProject and subproject names for artifact name and similar things,
* i.e. kotlin-faker-bom
*/
private val fullName: String =
if (project.name == "core") rootProject.name else "${rootProject.name}-${project.name}"
private val fullName: String = "${rootProject.name}-${project.name}"

publishing {
publications {
Expand Down Expand Up @@ -80,7 +84,21 @@ publishing {
}

signing {
if (!version.toString().endsWith("SNAPSHOT")) {
sign(publishing.publications["FakerBom"])
}
sign(publishing.publications["FakerBom"])
}

tasks.withType<PublishToMavenRepository>().configureEach {
dependsOn(project.tasks.getByName("tag"))
onlyIf("Not snapshot") { notSnapshot.get() }
onlyIf("New tag") { newTag.get() }
}

tasks.withType<PublishToMavenLocal>().configureEach {
val predicate = provider { version.toString().startsWith("0.0.0") }
onlyIf("In development") { predicate.get() }
}

tasks.withType<Sign>().configureEach {
onlyIf("Not snapshot") { notSnapshot.get() }
onlyIf("New tag") { newTag.get() }
}
2 changes: 2 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ dependencies {
implementation("com.fasterxml.jackson.core:jackson-databind:2.15.3")
// use snakeyaml instead of jackson-dataformat-yaml to properly handle yaml anchors and write them as actual values to json
implementation("org.yaml:snakeyaml:2.2")
// NB! remember to set same version in settings.gradle.kts:13
implementation("io.github.serpro69:semantic-versioning:0.12.0")
}
39 changes: 30 additions & 9 deletions buildSrc/src/main/kotlin/faker-lib-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.github.jengelman.gradle.plugins.shadow.ShadowExtension
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.dokka.gradle.DokkaTask
import java.util.*

plugins {
Expand All @@ -20,6 +21,14 @@ plugins {
private val fullName: String =
if (project.name == "core") rootProject.name else "${rootProject.name}-${project.name}"

val notSnapshot by lazy {
provider {
!version.toString().endsWith("SNAPSHOT")
&& !version.toString().startsWith("0.0.0")
}
}
val newTag by lazy { provider { project.tasks.getByName("tag").didWork } }

configurations {
create("integrationImplementation") { extendsFrom(configurations.getByName("testImplementation")) }
create("integrationRuntimeOnly") {
Expand Down Expand Up @@ -189,20 +198,32 @@ publishing {
}
}

signing {
if (!version.toString().endsWith("SNAPSHOT")) {
sign(publishing.publications[publicationName])
}
}

tasks {
assemble {
dependsOn(shadowJar)
}
}

signing {
sign(publishing.publications[publicationName])
}

tasks.withType<PublishToMavenRepository>().configureEach {
doFirst {
if (version == "0.0.0") throw IllegalArgumentException("Unable to publish version 0.0.0")
}
dependsOn(project.tasks.getByName("tag").didWork)
onlyIf("Not snapshot") { notSnapshot.get() }
onlyIf("New tag") { newTag.get() }
}

tasks.withType<PublishToMavenLocal>().configureEach {
val predicate = provider { version.toString().startsWith("0.0.0") }
onlyIf("In development") { predicate.get() }
}

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

tasks.withType<Sign>().configureEach {
onlyIf("Not snapshot") { notSnapshot.get() }
onlyIf("New tag") { newTag.get() }
}
23 changes: 19 additions & 4 deletions buildSrc/src/main/kotlin/faker-provider-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import io.github.serpro69.semverkt.spec.Semver

plugins {
}

val core = rootProject.subprojects.first { it.path == ":core" }
?: throw GradleException(":core project not found")

dependencies {
val compileOnly by configurations
val testImplementation by configurations
val testRuntimeOnly by configurations
val integrationImplementation by configurations
// In order to use an additional fake data provider,
// core faker needs to be on the classpath.
// Don't add it as transitive dependency to each faker provider
compileOnly(project(path = ":core", configuration = "shadow"))
/* :core is versioned separately,
during development versions will always equal
(both are set to a version placeholder via gradle.properties),
but during publishing they might not (depending on changes to a given module)
hence we first check the versions equality, and then
EITHER set a dependency on a published :core artifact with latest version
(latest is handled by semver.kt plugin for multi-tag monorepos),
OR a project-type dependency on the :core submodule */
if (Semver(project.version.toString()) != (Semver(core.version.toString()))) {
// In order to use an additional fake data provider, core faker needs to be on the classpath.
// Don't add it as transitive dependency to each faker provider
compileOnly("io.github.serpro69:kotlin-faker:${core.version}")
} else {
compileOnly(project(path = ":core", configuration = "shadow"))
}
// we need implementation dependency for tests to be able to access 'core' functionality
testImplementation(project(path = ":core", configuration = "shadow"))
// provides helpers for integration tests
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pluginManagement {
}

plugins {
id("io.github.serpro69.semantic-versioning") version "0.10.0"
// NB! remember to set same version in buildSrc/build.gradle.kts:20
id("io.github.serpro69.semantic-versioning") version "0.12.0"
}

rootProject.name = "kotlin-faker"
Expand Down

0 comments on commit 9aaab68

Please sign in to comment.