Skip to content

Commit

Permalink
Refactor buildSrc to get rid of duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed May 29, 2024
1 parent 1fa3c3a commit 28ba04f
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 311 deletions.
92 changes: 1 addition & 91 deletions bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,11 @@ import io.github.serpro69.semverkt.gradle.plugin.tasks.TagTask

plugins {
`java-platform`
`maven-publish`
signing
`faker-pub-conventions`
}

val bom = project

val isDev = provider { version.toString().startsWith("0.0.0") }
val isSnapshot = provider {
// QUESTION do we need to check if rootProject is also set to snapshot?
// Likely not, since "isRelease" will not just check for the version, but also for the actual tag creation
// rootProject.version.toString().endsWith("SNAPSHOT") &&
version.toString().endsWith("SNAPSHOT")
}
val isRelease = provider {
val tag = project.tasks.getByName("tag", TagTask::class)
/* all fakers have their own tags, so checking if tag.didWork is enough for them,
':core' shares the tag with 'root', ':bom' and ':cli-bot' modules,
and hence the tag might already exist and didWork will return false for ':core' */
val tagCreated = if (project.name != "core") tag.didWork else tag.didWork || tag.tagExists
!isDev.get() && !isSnapshot.get() && tagCreated
}

// Exclude subprojects that will never be published so that when configuring this project
// we don't force their configuration and do unnecessary work
val excludeFromBom = listOf(":cli-bot", ":docs", ":extension", ":faker", ":test", ":extension:kotest-property-test")
Expand Down Expand Up @@ -52,76 +35,3 @@ dependencies {
}
}
}

/**
* 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 = "${rootProject.name}-${project.name}"

publishing {
publications {
create<MavenPublication>("FakerBom") {
from(components["javaPlatform"])
groupId = project.group.toString()
artifactId = fullName
version = project.version.toString()
pom {
packaging = "pom"
name.set("kotlin-faker")
description.set("Generate realistically looking fake data such as names, addresses, banking details, and many more, that can be used for testing and data anonymization purposes.")
url.set("https://github.com/serpro69/kotlin-faker")
scm {
connection.set("scm:git:https://github.com/serpro69/kotlin-faker")
developerConnection.set("scm:git:https://github.com/serpro69")
url.set("https://github.com/serpro69/kotlin-faker")
}
issueManagement {
url.set("https://github.com/serpro69/kotlin-faker/issues")
}
licenses {
license {
name.set("MIT")
url.set("https://opensource.org/licenses/mit-license.php")
}
}
developers {
developer {
id.set("serpro69")
name.set("Serhii Prodan")
}
}
}
}
}
}

signing {
sign(publishing.publications["FakerBom"])
}

/*
* copy from faker-lib-conventions.gradle.kts:219
*/
tasks.withType<PublishToMavenRepository>().configureEach {
dependsOn(project.tasks.getByName("tag"))
dependsOn(project.tasks.withType(Sign::class.java))
onlyIf("Not dev") { !isDev.get() }
onlyIf("Release or snapshot") { isRelease.get() || isSnapshot.get() }
}

/*
* copy from faker-lib-conventions.gradle.kts:226
*/
tasks.withType<PublishToMavenLocal>().configureEach {
onlyIf("In development") { isDev.get() || isSnapshot.get() }
}

/*
* copy from faker-lib-conventions.gradle.kts:230
*/
tasks.withType<Sign>().configureEach {
dependsOn(project.tasks.getByName("tag"))
onlyIf("Not dev and snapshot") { !isDev.get() && !isSnapshot.get() }
onlyIf("Release") { isRelease.get() }
}
36 changes: 36 additions & 0 deletions buildSrc/src/main/kotlin/Projects.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import io.github.serpro69.semverkt.gradle.plugin.tasks.TagTask
import org.gradle.accessors.dm.LibrariesForLibs
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.kotlin.dsl.getByName
import org.gradle.kotlin.dsl.the

val Project.libs: LibrariesForLibs
get() = the<LibrariesForLibs>()

val Project.isBomModule: Boolean get() = path.startsWith(":bom")
val Project.isCoreModule: Boolean get() = path.startsWith(":core")
val Project.isFakerModule: Boolean get() = path.startsWith(":faker")
val Project.isExtensionModule: Boolean get() = path.startsWith(":extension")
val Project.isShadow: Boolean get() = isCoreModule || isFakerModule

// versioning
val Project.isDev: Provider<Boolean>
get() = provider { version.toString().startsWith("0.0.0") }

val Project.isSnapshot: Provider<Boolean>
get() = provider {
// QUESTION do we need to check if rootProject is also set to snapshot?
// Likely not, since "isRelease" will not just check for the version, but also for the actual tag creation
// rootProject.version.toString().endsWith("SNAPSHOT") &&
version.toString().endsWith("SNAPSHOT")
}
val Project.isRelease: Provider<Boolean>
get() = provider {
val tag = tasks.getByName("tag", TagTask::class)
/* all fakers have their own tags, so checking if tag.didWork is enough for them,
':core' shares the tag with 'root', ':bom' and ':cli-bot' modules,
and hence the tag might already exist and didWork will return false for ':core' */
val tagCreated = if (project.name != "core") tag.didWork else tag.didWork || tag.tagExists
!isDev.get() && !isSnapshot.get() && tagCreated
}
59 changes: 59 additions & 0 deletions buildSrc/src/main/kotlin/Publishing.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import gradle.kotlin.dsl.accessors._617ff5292df7551646490c1442241820.dokkaJavadoc
import gradle.kotlin.dsl.accessors._617ff5292df7551646490c1442241820.sourceSets
import org.gradle.api.Project
import org.gradle.api.tasks.bundling.Jar
import org.gradle.configurationcache.extensions.capitalized
import org.gradle.kotlin.dsl.creating
import org.gradle.kotlin.dsl.getValue

/**
* 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
*/
val Project.fullName: String
get() = if (name == "core") rootProject.name
else "${rootProject.name}-${name}"

val Project.publicationName: String
get() = "Faker${path.split(":").joinToString("") { it.capitalized() }}"

private fun createSourcesJarTask(p: Project): Jar {
val sourcesJar by p.tasks.creating(Jar::class) {
archiveClassifier.set("sources")
from(p.sourceSets.getByName("main").allSource)
from("${p.rootProject.rootDir.resolve("LICENSE.adoc")}") {
into("META-INF")
}
}
return sourcesJar
}

private fun createJavadocJarTask(p: Project): Jar {
val dokkaJavadocJar by p.tasks.creating(Jar::class) {
archiveClassifier.set("javadoc")
dependsOn(p.tasks.dokkaJavadoc)
from(p.tasks.dokkaJavadoc.get().outputDirectory.orNull)
}
return dokkaJavadocJar
}

val Project.sourcesJar: Jar
get() {
val sourcesJar = tasks.findByName("sourcesJar")
if (sourcesJar != null && sourcesJar is Jar) {
return sourcesJar
}
return createSourcesJarTask(this)
}

val Project.dokkaJavadocJar: Jar
get() {
val dokkaJavadocJar = tasks.findByName("dokkaJavadocJar")
if (dokkaJavadocJar != null && dokkaJavadocJar is Jar) {
return dokkaJavadocJar
}
return createJavadocJarTask(this)
}

110 changes: 1 addition & 109 deletions buildSrc/src/main/kotlin/faker-ext-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
import io.github.serpro69.semverkt.gradle.plugin.tasks.TagTask
import org.gradle.accessors.dm.LibrariesForLibs
import org.jetbrains.dokka.gradle.DokkaTask
import java.util.Locale

plugins {
base
kotlin("jvm")
id("org.jetbrains.dokka")
`maven-publish`
signing
}

val libs = the<LibrariesForLibs>()

/**
* 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
*/
private val fullName: String = "${rootProject.name}-${project.name}"

val isDev = provider { version.toString().startsWith("0.0.0") }
val isSnapshot = provider {
// QUESTION do we need to check if rootProject is also set to snapshot?
// Likely not, since "isRelease" will not just check for the version, but also for the actual tag creation
// rootProject.version.toString().endsWith("SNAPSHOT") &&
version.toString().endsWith("SNAPSHOT")
}
val isRelease = provider {
val tag = project.tasks.getByName("tag", TagTask::class)
/* all fakers have their own tags, so checking if tag.didWork is enough for them,
':core' shares the tag with 'root', ':bom' and ':cli-bot' modules,
and hence the tag might already exist and didWork will return false for ':core' */
val tagCreated = if (project.name != "core") tag.didWork else tag.didWork || tag.tagExists
!isDev.get() && !isSnapshot.get() && tagCreated
id("faker-pub-conventions")
}

configurations {
Expand Down Expand Up @@ -94,97 +64,19 @@ tasks.withType<Jar> {
dependsOn(integrationTest)
}

val sourcesJar by tasks.creating(Jar::class) {
archiveClassifier.set("sources")
from(sourceSets.getByName("main").allSource)
from("${rootProject.rootDir.resolve("LICENSE.adoc")}") {
into("META-INF")
}
}

val dokkaJavadocJar by tasks.creating(Jar::class) {
archiveClassifier.set("javadoc")
dependsOn(tasks.dokkaJavadoc)
from(tasks.dokkaJavadoc.get().outputDirectory.orNull)
}

artifacts {
archives(sourcesJar)
archives(dokkaJavadocJar)
}

val publicationName =
"faker${project.name.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }}"

publishing {
publications {
create<MavenPublication>(publicationName) {
groupId = project.group.toString()
artifactId = fullName
version = project.version.toString()
from(components["java"])
artifact(sourcesJar)
artifact(dokkaJavadocJar) //TODO configure dokka or use defaults?

pom {
packaging = "jar"
name.set(fullName)
description.set("Generate realistically looking fake data such as names, addresses, banking details, and many more, that can be used for testing and data anonymization purposes.")
url.set("https://github.com/serpro69/kotlin-faker")
scm {
connection.set("scm:git:https://github.com/serpro69/kotlin-faker")
developerConnection.set("scm:git:https://github.com/serpro69")
url.set("https://github.com/serpro69/kotlin-faker")
}
issueManagement {
url.set("https://github.com/serpro69/kotlin-faker/issues")
}
licenses {
license {
name.set("MIT")
url.set("https://opensource.org/licenses/mit-license.php")
}
}
developers {
developer {
id.set("serpro69")
name.set("Serhii Prodan")
}
}
}
}
}
}

tasks {
assemble {
dependsOn(integrationTest)
dependsOn(jar)
}
}

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

tasks.withType<DokkaTask>().configureEach {
onlyIf("Not dev") { !isDev.get() }
onlyIf("Release or snapshot") { isRelease.get() || isSnapshot.get() }
}

tasks.withType<PublishToMavenRepository>().configureEach {
dependsOn(project.tasks.getByName("tag"))
dependsOn(project.tasks.withType(Sign::class.java))
onlyIf("Not dev") { !isDev.get() }
onlyIf("Release or snapshot") { isRelease.get() || isSnapshot.get() }
}

tasks.withType<PublishToMavenLocal>().configureEach {
onlyIf("In development") { isDev.get() || isSnapshot.get() }
}

tasks.withType<Sign>().configureEach {
dependsOn(project.tasks.getByName("tag"))
onlyIf("Not dev and snapshot") { !isDev.get() && !isSnapshot.get() }
onlyIf("Release") { isRelease.get() }
}
Loading

0 comments on commit 28ba04f

Please sign in to comment.