forked from serpro69/kotlin-faker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add precompiled plugin for yaml-to-json dict conversion
- Loading branch information
Showing
4 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters