From 8052a9a4b5f64191d19912d53e16d7e49a587a17 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 4 Jun 2019 01:02:35 -0400 Subject: Convert plugin to kotlin code --- .../kotlin/ca/allanwang/kau/ChangelogGenerator.kt | 104 +++++++++++++++++++++ .../main/kotlin/ca/allanwang/kau/Dependencies.kt | 14 +++ .../src/main/kotlin/ca/allanwang/kau/KauPlugin.kt | 17 ++++ .../src/main/kotlin/ca/allanwang/kau/Plugins.kt | 16 ++++ .../src/main/kotlin/ca/allanwang/kau/Versions.kt | 80 ++++++++++++++++ 5 files changed, 231 insertions(+) create mode 100644 buildSrc/src/main/kotlin/ca/allanwang/kau/ChangelogGenerator.kt create mode 100644 buildSrc/src/main/kotlin/ca/allanwang/kau/Dependencies.kt create mode 100644 buildSrc/src/main/kotlin/ca/allanwang/kau/KauPlugin.kt create mode 100644 buildSrc/src/main/kotlin/ca/allanwang/kau/Plugins.kt create mode 100644 buildSrc/src/main/kotlin/ca/allanwang/kau/Versions.kt (limited to 'buildSrc/src/main/kotlin') diff --git a/buildSrc/src/main/kotlin/ca/allanwang/kau/ChangelogGenerator.kt b/buildSrc/src/main/kotlin/ca/allanwang/kau/ChangelogGenerator.kt new file mode 100644 index 0000000..8200484 --- /dev/null +++ b/buildSrc/src/main/kotlin/ca/allanwang/kau/ChangelogGenerator.kt @@ -0,0 +1,104 @@ +package ca.allanwang.kau + +import groovy.util.Node +import groovy.util.XmlParser +import org.gradle.api.GradleException +import org.gradle.api.Project +import java.io.File + +/** + * Given an xml of the format + * + * + * + * + * + * + * + * + * + * + * Outputs a changelog in markdown format + */ +open class ChangelogGenerator(private val project: Project) { + + class ChangelogException(message: String) : GradleException(message) + + private fun fail(message: String): Nothing = + throw ChangelogException(message) + + class ChangelogEntry(val version: String, val items: Array) + + private fun Node.forEachNode(action: (Node) -> Unit) { + children().forEach { + action(it as Node) + } + } + + fun read(inputUri: String): List { + val input = File(inputUri) + if (!input.exists()) { + fail("Could not generate changelog from ${input.absolutePath}") + } + + val parser = XmlParser().parse(inputUri) + + val entries: MutableList = mutableListOf() + var version: String? = null + val items: MutableList = mutableListOf() + + fun addEntry() { + version?.also { v -> + entries.add(ChangelogEntry(v, items.toTypedArray())) + items.clear() + } + } + + parser.depthFirst().mapNotNull { it as? Node }.forEach { n -> + when (n.name()) { + "version" -> { + addEntry() + version = n.attribute("title")?.toString() ?: "" + } + "item" -> { + n.attribute("text")?.toString()?.takeIf(String::isNotBlank)?.let { + items.add(it) + } + } + } + } + addEntry() + return entries + } + + @JvmOverloads + fun generate(inputUri: String, outputUri: String = "${project.rootDir}/docs/Changelog.md") { + val entries = read(inputUri) + val output = File(outputUri) + if (output.exists()) { + if (output.isDirectory) { + fail("Cannot save changelog at directory ${output.absolutePath}") + } + if (output.isFile && !output.delete()) { + fail("Could not delete changelog at ${output.absolutePath}") + } + } else { + output.parentFile.mkdirs() + } + + if (!output.createNewFile()) { + fail("Could not create changelog file at ${output.absolutePath}") + } + val markdown = buildString { + append("# Changelog\n") + entries.forEach { e -> + append("\n## ${e.version}\n") + e.items.forEach { + append("* $it\n") + } + } + } + output.writeText(markdown) + println("Generated changelog at ${output.absolutePath}") + } +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/ca/allanwang/kau/Dependencies.kt b/buildSrc/src/main/kotlin/ca/allanwang/kau/Dependencies.kt new file mode 100644 index 0000000..9e23ac4 --- /dev/null +++ b/buildSrc/src/main/kotlin/ca/allanwang/kau/Dependencies.kt @@ -0,0 +1,14 @@ +package ca.allanwang.kau + +/** + * Some common dependencies, backed by the supplied versions + */ +open class Dependencies { + private val v = Versions() + val kotlin = "org.jetbrains.kotlin:kotlin-stdlib:${v.kotlin}" + val kotlinTest = "org.jetbrains.kotlin:kotlin-test-junit:${v.kotlin}" + val junit = "junit:junit:${v.junit}" + val espresso = "androidx.test.espresso:espresso-core:${v.espresso}" + val testRunner = "androidx.test.ext:junit:${v.testRunner}" + val testRules = "androidx.test:rules:${v.testRules}" +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/ca/allanwang/kau/KauPlugin.kt b/buildSrc/src/main/kotlin/ca/allanwang/kau/KauPlugin.kt new file mode 100644 index 0000000..6be15e5 --- /dev/null +++ b/buildSrc/src/main/kotlin/ca/allanwang/kau/KauPlugin.kt @@ -0,0 +1,17 @@ +package ca.allanwang.kau + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.kotlin.dsl.create + +class KauPlugin : Plugin { + + override fun apply(project: Project) { + project.extensions.run { + create("kau") + create("kauDependency") + create("kauPlugin") + create("kauChangelog", project) + } + } +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/ca/allanwang/kau/Plugins.kt b/buildSrc/src/main/kotlin/ca/allanwang/kau/Plugins.kt new file mode 100644 index 0000000..c8265ab --- /dev/null +++ b/buildSrc/src/main/kotlin/ca/allanwang/kau/Plugins.kt @@ -0,0 +1,16 @@ +package ca.allanwang.kau + +/** + * Some common buildscript plugins, backed by the supplied versions + */ +open class Plugins { + private val v = Versions() + val android = "com.android.tools.build:gradle:${v.gradlePlugin}" + val kotlin = "org.jetbrains.kotlin:kotlin-gradle-plugin:${v.kotlin}" + val androidMaven = "com.github.dcendents:android-maven-gradle-plugin:${v.mavenPlugin}" + val playPublisher = "com.github.triplet.gradle:play-publisher:${v.playPublishPlugin}" + val dexCount = "com.getkeepsafe.dexcount:dexcount-gradle-plugin:${v.dexCountPlugin}" + val gitVersion = + "gradle.plugin.com.gladed.gradle.androidgitversion:gradle-android-git-version:${v.gitVersionPlugin}" + val spotless = "com.diffplug.spotless:spotless-plugin-gradle:${v.spotless}" +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/ca/allanwang/kau/Versions.kt b/buildSrc/src/main/kotlin/ca/allanwang/kau/Versions.kt new file mode 100644 index 0000000..2bcedaa --- /dev/null +++ b/buildSrc/src/main/kotlin/ca/allanwang/kau/Versions.kt @@ -0,0 +1,80 @@ +package ca.allanwang.kau + +open class Versions { + val coreMinSdk = 19 + val minSdk = 21 + val targetSdk = 28 + + // https://developer.android.com/studio/releases/build-tools + val buildTools = "28.0.3" + + // https://mvnrepository.com/artifact/androidx.appcompat/appcompat?repo=google + val appcompat = "1.0.2" + + // https://mvnrepository.com/artifact/com.google.android.material/material + val googleMaterial = "1.0.0" + + // https://mvnrepository.com/artifact/androidx.recyclerview/recyclerview + val recyclerView = "1.0.0" + + // https://mvnrepository.com/artifact/androidx.cardview/cardview + val cardView = "1.0.0" + + // https://mvnrepository.com/artifact/androidx.constraintlayout/constraintlayout + val constraintLayout = "1.1.3" + + // https://kotlinlang.org/docs/reference/using-gradle.html + val kotlin = "1.3.31" + + // https://github.com/Kotlin/kotlinx.coroutines/releases + val coroutines = "1.2.1" + + // https://github.com/mikepenz/AboutLibraries/releases + val aboutLibraries = "6.2.3" + + // https://github.com/wasabeef/Blurry/releases + val blurry = "3.0.0" + + // https://github.com/mikepenz/FastAdapter#using-maven + val fastAdapter = "3.3.1" + val fastAdapterCommons = fastAdapter + + // https://github.com/bumptech/glide/releases + val glide = "4.9.0" + + // https://github.com/mikepenz/Android-Iconics#1-provide-the-gradle-dependency + val iconics = "3.2.5" + val iconicsGoogle = "3.0.1.3" + val iconicsMaterial = "2.2.0.5" + val iconicsCommunity = "3.5.95.1" + + // https://github.com/afollestad/material-dialogs/releases + val materialDialog = "0.9.6.0" + + // https://mvnrepository.com/artifact/androidx.test.espresso/espresso-core?repo=google + val espresso = "3.1.1" + + // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api + val junit = "4.12" + + val testRunner = "1.1.0" + + // https://mvnrepository.com/artifact/androidx.test/rules?repo=google + val testRules = "1.1.1" + + // https://github.com/diffplug/spotless/blob/master/plugin-gradle/CHANGES.md + val spotless = "3.18.0" + + // https://mvnrepository.com/artifact/com.android.tools.build/gradle?repo=google + val gradlePlugin = "3.4.1" + // https://github.com/dcendents/android-maven-gradle-plugin/releases + val mavenPlugin = "2.1" + // https://github.com/Triple-T/gradle-play-publisher/releases + val playPublishPlugin = "2.1.0" + + // https://github.com/KeepSafe/dexcount-gradle-plugin/releases + val dexCountPlugin = "0.8.6" + + // https://github.com/gladed/gradle-android-git-version/releases + val gitVersionPlugin = "0.4.7" +} \ No newline at end of file -- cgit v1.2.3