Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change format of dictionary files from yml to json #159

Merged
merged 3 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
57 changes: 57 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,57 @@
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import org.gradle.kotlin.dsl.create

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 (output.exists() && !output.isDirectory) throw IllegalArgumentException("$output is not a directory")
}

doLast {
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)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{"pattern":"\\Qkotlin/ranges/ranges.kotlin_builtins\\E"},
{"pattern":"\\Qkotlin/reflect/reflect.kotlin_builtins\\E"},
{"pattern":"\\Qsun/text/resources/LineBreakIteratorData\\E"},
{"pattern": ".*/*.yml$"}
{"pattern": ".*/*.json$"}
]
}
}
19 changes: 18 additions & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
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 {
implementation("com.fasterxml.jackson.core:jackson-databind:2.13.4.2")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.4")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.4")
implementation("org.yaml:snakeyaml:1.33")
implementation("com.ibm.icu:icu4j:71.1")
Expand All @@ -20,6 +24,14 @@ 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("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 @@ -31,6 +43,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
10 changes: 5 additions & 5 deletions core/src/main/kotlin/io/github/serpro69/kfaker/FakerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ internal class FakerService {
secondaryCategory: Category?
): InputStream {
return secondaryCategory?.let {
requireNotNull(javaClass.classLoader.getResourceAsStream("locales/$locale/${it.lowercase()}.yml"))
} ?: requireNotNull(javaClass.classLoader.getResourceAsStream("locales/$locale/${category.lowercase()}.yml"))
requireNotNull(javaClass.classLoader.getResourceAsStream("locales/$locale/${it.lowercase()}.json"))
} ?: requireNotNull(javaClass.classLoader.getResourceAsStream("locales/$locale/${category.lowercase()}.json"))
}

private fun getCategoryFileStreamOrNull(
Expand All @@ -74,12 +74,12 @@ internal class FakerService {
secondaryCategory: Category?
): InputStream? {
return secondaryCategory?.let {
javaClass.classLoader.getResourceAsStream("locales/$locale/${it.lowercase()}.yml")
} ?: javaClass.classLoader.getResourceAsStream("locales/$locale/${category.lowercase()}.yml")
javaClass.classLoader.getResourceAsStream("locales/$locale/${it.lowercase()}.json")
} ?: javaClass.classLoader.getResourceAsStream("locales/$locale/${category.lowercase()}.json")
}

private fun getLocaleFileStream(locale: String): InputStream? {
return javaClass.classLoader.getResourceAsStream("locales/$locale.yml")
return javaClass.classLoader.getResourceAsStream("locales/$locale.json")
}

/**
Expand Down
11 changes: 5 additions & 6 deletions core/src/main/kotlin/io/github/serpro69/kfaker/Mapper.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package io.github.serpro69.kfaker

import com.fasterxml.jackson.databind.*
import com.fasterxml.jackson.dataformat.yaml.*
import com.fasterxml.jackson.module.kotlin.*
import java.io.*
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import java.io.InputStream

internal object Mapper {
private val mapper = ObjectMapper(YAMLFactory())
private val mapper = ObjectMapper()

init {
mapper.registerModule(KotlinModule())
mapper.registerModule(KotlinModule.Builder().build())
}

fun <T> readValue(inputStream: InputStream, type: Class<T>): T = mapper.readerFor(type).readValue(inputStream)
Expand Down