Skip to content

Commit

Permalink
Update yaml-to-json to support directories
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed Oct 30, 2022
1 parent d066cbd commit f0bcafe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
33 changes: 29 additions & 4 deletions buildSrc/src/main/kotlin/yaml-to-json.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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>
Expand All @@ -19,14 +18,40 @@ class Yaml2JsonPlugin : Plugin<Project> {
val output = ext.output.get().absoluteFile

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

doLast {
val map = yamlMapper.readValue(input.inputStream(), Map::class.java)
jsonMapper.writeValue(output, map)
output.deleteRecursively()
if (input.isDirectory) {
input.getYmlFiles().forEach { src ->
val outFile = src.relativeTo(input.parentFile)
val dest = output
.resolve(File("${(outFile.parentFile.resolve(outFile.nameWithoutExtension))}.json"))
.also {
it.parentFile.mkdirs()
it.createNewFile()
}
writeYamlToJson(src, dest)
}
} else if (input.extension == "yml" || input.extension == "yaml") {
val dest = output.resolve("${input.nameWithoutExtension}.json").also {
it.parentFile.mkdirs()
it.createNewFile()
}
writeYamlToJson(input, dest)
} else throw IllegalArgumentException("Unable to process file $input")
}
}
}

private fun File.getYmlFiles(): Sequence<File> {
return walk().filter { f -> f.isFile && (f.extension == "yml" || f.extension == "yaml") }
}

private fun writeYamlToJson(src: File, dest: File) {
val map = yamlMapper.readValue(src.inputStream(), Map::class.java)
jsonMapper.writeValue(dest, map)
}
}

11 changes: 9 additions & 2 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ dependencies {

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"))
input.set(File("core/src/main/resources/locales"))
output.set(File("core/build/generated/src/main/resources"))
}

tasks.processResources.get().dependsOn(tasks["yaml2json"])

configurations {
create("integrationImplementation") { extendsFrom(testImplementation.get()) }
create("integrationRuntimeOnly") { extendsFrom(testRuntimeOnly.get()) }
Expand All @@ -42,6 +44,11 @@ sourceSets {
compileClasspath += main.get().compileClasspath + test.get().compileClasspath
runtimeClasspath += main.get().runtimeClasspath + test.get().runtimeClasspath
}
main {
resources {
this.srcDir("build/generated/src/main/resources")
}
}
}

val integrationTest by tasks.creating(Test::class) {
Expand Down

0 comments on commit f0bcafe

Please sign in to comment.