Skip to content

Provide a common way to setup publication to central #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 63 additions & 7 deletions main/src/kotlinx/team/infra/Publishing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ open class PublishingConfiguration @Inject constructor(val objects: ObjectFactor
sonatype.isSelected = true
}

val central = objects.newInstance<CentralViaSpaceConfiguration>()
fun central(configure: Action<CentralViaSpaceConfiguration>) {
configure.execute(central)
central.isSelected = true
}

var includeProjects: MutableList<String> = mutableListOf()
fun include(vararg name: String) {
includeProjects.addAll(name)
Expand All @@ -42,8 +48,9 @@ open class SonatypeConfiguration {
internal var isSelected: Boolean = false
}

// TODO: Add space configuration

open class CentralViaSpaceConfiguration {
internal var isSelected: Boolean = false
}

fun Project.configureProjectVersion() {
val ext = extensions.getByType(ExtraPropertiesExtension::class.java)
Expand Down Expand Up @@ -90,6 +97,13 @@ internal fun Project.configurePublishing(publishing: PublishingConfiguration) {
}
}
}
if (publishing.central.isSelected) {
if (verifyCentralViaSpaceConfiguration()) {
includeProjects.forEach { subproject ->
subproject.createCentralViaSpaceRepository()
}
}
}

if (project.hasProperty("teamcity")) {
includeProjects.forEach { subproject ->
Expand Down Expand Up @@ -173,7 +187,43 @@ private fun Project.verifySonatypeConfiguration(): Boolean {

sonatypeUsername ?: return missing("username")
val password = sonatypePassword ?: return missing("password")
return verifyRepositoryPassword(password)
}

private fun Project.createSonatypeRepository() {
val username = project.sonatypeUsername
?: throw KotlinInfrastructureException("Cannot setup publication. User has not been specified.")
val password = project.sonatypePassword
?: throw KotlinInfrastructureException("Cannot setup publication. Password (API key) has not been specified.")

extensions.configure(PublishingExtension::class.java) {
repositories.maven {
name = "sonatype"
url = sonatypeRepositoryUri()
credentials {
this.username = username
this.password = password.trim()
}
}
}
}

private fun Project.verifyCentralViaSpaceConfiguration(): Boolean {
fun missing(what: String): Boolean {
logger.warn("INFRA: Space publishing will not be possible due to missing $what.")
return false
}

if (spaceCentralRepository.isNullOrEmpty()) {
return missing("repository url 'libs.repo.url'.")
}

spaceCentralUsername ?: return missing("username")
val password = spaceCentralPassword ?: return missing("password")
return verifyRepositoryPassword(password)
}

private fun Project.verifyRepositoryPassword(password: String): Boolean {
if (password.startsWith("credentialsJSON")) {
logger.warn("INFRA: API key secure token was not expanded, publishing is not possible.")
return false
Expand All @@ -189,16 +239,18 @@ private fun Project.verifySonatypeConfiguration(): Boolean {
return true
}

private fun Project.createSonatypeRepository() {
val username = project.sonatypeUsername
private fun Project.createCentralViaSpaceRepository() {
val url = project.spaceCentralRepository
?: throw KotlinInfrastructureException("Cannot setup publication. Repository URL has not been specified.")
val username = project.spaceCentralUsername
?: throw KotlinInfrastructureException("Cannot setup publication. User has not been specified.")
val password = project.sonatypePassword
val password = project.spaceCentralPassword
?: throw KotlinInfrastructureException("Cannot setup publication. Password (API key) has not been specified.")

extensions.configure(PublishingExtension::class.java) {
repositories.maven {
name = "sonatype"
url = sonatypeRepositoryUri()
name = "Central"
this.url = uri(url)
credentials {
this.username = username
this.password = password.trim()
Expand Down Expand Up @@ -300,3 +352,7 @@ private fun Project.sonatypeRepositoryUri(): URI {
private val Project.stagingRepositoryId: String? get() = propertyOrEnv("libs.repository.id")
private val Project.sonatypeUsername: String? get() = propertyOrEnv("libs.sonatype.user")
private val Project.sonatypePassword: String? get() = propertyOrEnv("libs.sonatype.password")

private val Project.spaceCentralRepository: String? get() = propertyOrEnv("libs.repo.url")
private val Project.spaceCentralUsername: String? get() = propertyOrEnv("libs.repo.user")
private val Project.spaceCentralPassword: String? get() = propertyOrEnv("libs.repo.password")