Skip to content

Commit

Permalink
Add precompiled plugin for yaml-to-json dict conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed Oct 30, 2022
1 parent 41a2397 commit d066cbd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
12 changes: 12 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
}

dependencies {
implementation("com.fasterxml.jackson.core:jackson-databind:2.13.4.2")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.4")
}
Empty file added buildSrc/settings.gradle.kts
Empty file.
32 changes: 32 additions & 0 deletions buildSrc/src/main/kotlin/yaml-to-json.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import org.gradle.kotlin.dsl.create
import kotlin.system.exitProcess

interface Yaml2JsonPluginExtension {
val input: Property<File>
val output: Property<File>
}

class Yaml2JsonPlugin : Plugin<Project> {
val jsonMapper = ObjectMapper()
val yamlMapper = ObjectMapper(YAMLFactory())

override fun apply(p: Project) {
val ext = p.extensions.create<Yaml2JsonPluginExtension>("yaml2jsonExt")
p.tasks.register("yaml2json") {
val input = ext.input.get().absoluteFile
val output = ext.output.get().absoluteFile

doFirst {
if (!input.isDirectory) throw IllegalArgumentException("$input is not a directory or does not exist")
}

doLast {
val map = yamlMapper.readValue(input.inputStream(), Map::class.java)
jsonMapper.writeValue(output, map)
}
}
}
}

11 changes: 11 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import Yaml_to_json_gradle.Yaml2JsonPlugin
import Yaml_to_json_gradle.Yaml2JsonPluginExtension
import com.adarshr.gradle.testlogger.theme.ThemeType
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

//import io.github.serpro69.YamlToJsonPlugin

plugins {
kotlin("jvm")
id("org.jetbrains.dokka") version "1.7.20"
`maven-publish`
signing
`yaml-to-json`
}

dependencies {
Expand All @@ -20,6 +25,12 @@ dependencies {
shadow("com.github.mifmif:generex:1.0.2")
}

apply<Yaml2JsonPlugin>() // this shouldn't really be needed since the plugin is supposed to be applied in the plugins{} block
configure<Yaml2JsonPluginExtension> {
input.set(File("ar.yml"))
output.set(File("test.json"))
}

configurations {
create("integrationImplementation") { extendsFrom(testImplementation.get()) }
create("integrationRuntimeOnly") { extendsFrom(testRuntimeOnly.get()) }
Expand Down

0 comments on commit d066cbd

Please sign in to comment.