gradle-tools/src/main/kotlin/ru/mipt/npm/gradle/KScienceProjectPlugin.kt

179 lines
6.3 KiB
Kotlin
Raw Normal View History

2020-09-22 16:21:09 +03:00
package ru.mipt.npm.gradle
import groovy.text.SimpleTemplateEngine
2020-09-30 20:43:02 +03:00
import kotlinx.validation.BinaryCompatibilityValidatorPlugin
2020-09-22 16:21:09 +03:00
import org.gradle.api.Plugin
import org.gradle.api.Project
2020-09-27 17:45:40 +03:00
import org.gradle.kotlin.dsl.*
2020-09-22 16:21:09 +03:00
import org.jetbrains.changelog.ChangelogPlugin
2020-09-27 17:45:40 +03:00
import org.jetbrains.dokka.gradle.DokkaPlugin
import org.jetbrains.dokka.gradle.DokkaTask
2020-09-22 16:21:09 +03:00
import kotlin.collections.component1
import kotlin.collections.component2
class KSciencePublishingExtension(val project: Project) {
var vcs: String? by project.extra
2021-02-16 18:19:22 +03:00
// github publishing
2020-09-22 16:21:09 +03:00
var githubOrg: String? by project.extra
var githubProject: String? by project.extra
2021-02-16 18:19:22 +03:00
// Space publishing
2020-09-22 16:21:09 +03:00
var spaceRepo: String? by project.extra
var spaceUser: String? by project.extra
var spaceToken: String? by project.extra
2021-02-16 18:19:22 +03:00
// Bintray publishing
2020-09-22 16:21:09 +03:00
var bintrayOrg: String? by project.extra
var bintrayUser: String? by project.extra
var bintrayApiKey: String? by project.extra
var bintrayRepo: String? by project.extra
2021-02-16 18:19:22 +03:00
// Sonatype publising
var sonatypeUser: String? by project.extra
var sonatypePassword: String? by project.extra
2020-09-22 16:21:09 +03:00
}
/**
* Apply extension and repositories
*/
open class KScienceProjectPlugin : Plugin<Project> {
override fun apply(target: Project): Unit = target.run {
apply<ChangelogPlugin>()
2020-09-27 17:45:40 +03:00
apply<DokkaPlugin>()
2020-09-30 20:43:02 +03:00
apply<BinaryCompatibilityValidatorPlugin>()
2020-09-22 16:21:09 +03:00
val rootReadmeExtension = KScienceReadmeExtension(this)
extensions.add("ksciencePublish", KSciencePublishingExtension(this))
2020-09-25 09:55:01 +03:00
extensions.add("readme", rootReadmeExtension)
2020-09-22 16:21:09 +03:00
//Add readme generators to individual subprojects
subprojects {
val readmeExtension = KScienceReadmeExtension(this)
2020-09-25 09:55:01 +03:00
extensions.add("readme", readmeExtension)
2020-09-27 17:45:40 +03:00
val generateReadme by tasks.creating {
2020-09-22 16:21:09 +03:00
group = "documentation"
description = "Generate a README file if stub is present"
2020-09-27 17:45:40 +03:00
2021-02-21 16:06:25 +03:00
if (readmeExtension.readmeTemplate.exists()) {
2020-09-27 17:45:40 +03:00
inputs.file(readmeExtension.readmeTemplate)
}
readmeExtension.additionalFiles.forEach {
2021-02-21 16:06:25 +03:00
if (it.exists()) {
2020-09-27 17:45:40 +03:00
inputs.file(it)
}
}
val readmeFile = this@subprojects.file("README.md")
outputs.file(readmeFile)
2020-09-22 16:21:09 +03:00
doLast {
val readmeString = readmeExtension.readmeString()
if (readmeString != null) {
readmeFile.writeText(readmeString)
}
}
}
2021-02-21 16:06:25 +03:00
tasks.withType<DokkaTask> {
2020-09-27 17:45:40 +03:00
dependsOn(generateReadme)
}
2020-09-22 16:21:09 +03:00
}
2020-09-27 17:45:40 +03:00
val generateReadme by tasks.creating {
2020-09-22 16:21:09 +03:00
group = "documentation"
description = "Generate a README file and a feature matrix if stub is present"
2020-09-27 17:45:40 +03:00
subprojects {
tasks.findByName("generateReadme")?.let {
dependsOn(it)
}
}
2021-02-21 16:06:25 +03:00
if (rootReadmeExtension.readmeTemplate.exists()) {
2020-09-27 17:45:40 +03:00
inputs.file(rootReadmeExtension.readmeTemplate)
}
rootReadmeExtension.additionalFiles.forEach {
2021-02-21 16:06:25 +03:00
if (it.exists()) {
2020-09-27 17:45:40 +03:00
inputs.file(it)
}
}
val readmeFile = project.file("README.md")
outputs.file(readmeFile)
2020-09-22 16:21:09 +03:00
doLast {
val projects = subprojects.associate {
it.name to it.extensions.findByType<KScienceReadmeExtension>()
}
2020-09-25 09:55:01 +03:00
if (rootReadmeExtension.readmeTemplate.exists()) {
2020-09-22 16:21:09 +03:00
val modulesString = buildString {
2020-09-25 09:55:01 +03:00
projects.entries.forEach { (name, ext) ->
2020-09-27 17:45:40 +03:00
appendln("<hr/>")
appendln("\n* ### [$name]($name)")
2020-09-25 09:55:01 +03:00
if (ext != null) {
2020-09-27 17:45:40 +03:00
appendln("> ${ext.description}")
appendln(">\n> **Maturity**: ${ext.maturity}")
val featureString = ext.featuresString(itemPrefix = "> - ", pathPrefix = "$name/")
2021-02-21 16:06:25 +03:00
if (featureString.isNotBlank()) {
2020-09-27 17:45:40 +03:00
appendln(">\n> **Features:**")
appendln(featureString)
}
2020-09-25 09:55:01 +03:00
}
2020-09-22 16:21:09 +03:00
}
2020-09-27 17:45:40 +03:00
appendln("<hr/>")
2020-09-22 16:21:09 +03:00
}
2020-09-25 09:55:01 +03:00
val rootReadmeProperties: Map<String, Any?> = mapOf(
2020-09-22 16:21:09 +03:00
"name" to project.name,
"group" to project.group,
"version" to project.version,
2020-09-25 09:55:01 +03:00
"modules" to modulesString
2020-09-22 16:21:09 +03:00
)
readmeFile.writeText(
2020-09-27 17:45:40 +03:00
SimpleTemplateEngine().createTemplate(rootReadmeExtension.readmeTemplate)
.make(rootReadmeProperties).toString()
2020-09-22 16:21:09 +03:00
)
}
}
}
2020-09-27 17:45:40 +03:00
2021-02-21 16:06:25 +03:00
tasks.withType<DokkaTask> {
2020-09-27 17:45:40 +03:00
dependsOn(generateReadme)
}
val patchChangelog by tasks.getting
2021-02-21 16:06:25 +03:00
afterEvaluate {
val release by tasks.creating {
2021-02-16 18:19:22 +03:00
group = RELEASE_GROUP
description = "Publish development or production release based on version suffix"
dependsOn(generateReadme)
2021-02-21 16:06:25 +03:00
val publicationPlatform = project.findProperty("ci.publication.platform") as? String
val publicationName = if(publicationPlatform == null){
"AllPublications"
} else {
publicationPlatform.capitalize() + "Publication"
}
tasks.findByName("publish${publicationName}ToSonatypeRepository")?.let {
2021-02-19 22:33:34 +03:00
dependsOn(it)
}
2021-02-21 16:06:25 +03:00
tasks.findByName("publish${publicationName}ToBintrayRepository")?.let {
2021-02-16 18:19:22 +03:00
dependsOn(it)
}
2021-02-21 16:06:25 +03:00
tasks.findByName("publish${publicationName}ToSpaceRepository")?.let {
2021-02-16 18:19:22 +03:00
dependsOn(it)
}
2020-09-27 17:45:40 +03:00
}
}
}
2021-02-21 16:06:25 +03:00
companion object {
2020-09-27 17:45:40 +03:00
const val RELEASE_GROUP = "release"
2020-09-22 16:21:09 +03:00
}
}