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

164 lines
5.6 KiB
Kotlin
Raw Normal View History

2020-09-22 16:21:09 +03:00
package ru.mipt.npm.gradle
import groovy.text.SimpleTemplateEngine
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.extra
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.provideDelegate
import org.jetbrains.changelog.ChangelogPlugin
2020-09-25 09:55:01 +03:00
import java.io.File
2020-09-22 16:21:09 +03:00
import kotlin.collections.component1
import kotlin.collections.component2
2020-09-25 09:55:01 +03:00
import kotlin.reflect.KFunction
2020-09-22 16:21:09 +03:00
class KSciencePublishingExtension(val project: Project) {
var githubOrg: String? by project.extra
var githubProject: String? by project.extra
var spaceRepo: String? by project.extra
var spaceUser: String? by project.extra
var spaceToken: String? by project.extra
var bintrayOrg: String? by project.extra
var bintrayUser: String? by project.extra
var bintrayApiKey: String? by project.extra
var bintrayRepo: String? by project.extra
}
2020-09-25 09:55:01 +03:00
enum class Maturity {
PROTOTYPE,
EXPERIMENTAL,
DEVELOPMENT,
PRODUCTION
}
2020-09-22 16:21:09 +03:00
class KScienceReadmeExtension(val project: Project) {
2020-09-25 09:55:01 +03:00
var description: String = ""
var maturity: Maturity = Maturity.EXPERIMENTAL
var readmeTemplate: File = project.file("docs/README-TEMPLATE.md")//"docs/README-TEMPLATE.md"
2020-09-22 16:21:09 +03:00
data class Feature(val id: String, val ref: String, val description: String, val name: String = id)
2020-09-25 09:55:01 +03:00
val features = ArrayList<Feature>()
2020-09-22 16:21:09 +03:00
fun feature(id: String, ref: String, description: String, name: String = id) {
features.add(Feature(id, ref, description, name))
}
2020-09-25 09:55:01 +03:00
val properties: MutableMap<String, Any?> = mutableMapOf(
"name" to project.name,
"group" to project.group,
"version" to project.version,
"features" to featuresString()
)
private val actualizedProperties get() = properties.mapValues {(_,value)->
if(value is KFunction<*>){
value.call()
} else {
value
}
}
fun property(key: String, value: Any?) {
properties[key] = value
}
fun propertyByTemplate(key: String, template: String){
properties[key] = SimpleTemplateEngine().createTemplate(template).make(actualizedProperties).toString()
}
fun propertyByTemplate(key: String, template: File){
properties[key] = SimpleTemplateEngine().createTemplate(template).make(actualizedProperties).toString()
}
2020-09-22 16:21:09 +03:00
/**
* Generate a markdown string listing features
*/
fun featuresString(itemPrefix: String = " - ", pathPrefix: String = "") = buildString {
features.forEach {
appendln("$itemPrefix[${it.id}]($pathPrefix${it.ref}) : ${it.description}")
}
}
/**
* Generate a readme string from the stub
*/
fun readmeString(): String? {
2020-09-25 09:55:01 +03:00
return if (readmeTemplate.exists()) {
SimpleTemplateEngine().createTemplate(readmeTemplate).make(actualizedProperties).toString()
2020-09-22 16:21:09 +03:00
} else {
null
}
}
}
/**
* Apply extension and repositories
*/
open class KScienceProjectPlugin : Plugin<Project> {
override fun apply(target: Project): Unit = target.run {
apply<ChangelogPlugin>()
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-22 16:21:09 +03:00
tasks.create("generateReadme") {
group = "documentation"
description = "Generate a README file if stub is present"
doLast {
val readmeString = readmeExtension.readmeString()
if (readmeString != null) {
2020-09-25 09:55:01 +03:00
val readmeFile = this@subprojects.file("README.md")
2020-09-22 16:21:09 +03:00
readmeFile.writeText(readmeString)
}
}
}
}
tasks.create("generateReadme") {
group = "documentation"
description = "Generate a README file and a feature matrix if stub is present"
doLast {
val reader = groovy.json.JsonSlurper()
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-22 16:21:09 +03:00
appendln("### [$name]($name)")
2020-09-25 09:55:01 +03:00
if (ext != null) {
appendln(ext.description)
appendln("**Maturity**: ${ext.maturity}")
appendln("#### Features:")
appendln(ext.featuresString(pathPrefix = "$name/"))
}
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
)
val readmeFile = project.file("README.md")
readmeFile.writeText(
2020-09-25 09:55:01 +03:00
SimpleTemplateEngine().createTemplate(rootReadmeExtension.readmeTemplate).make(rootReadmeProperties).toString()
2020-09-22 16:21:09 +03:00
)
}
}
}
}
}