diff --git a/cli/yaml2json/build.gradle b/cli/yaml2json/build.gradle new file mode 100644 index 0000000..d67dbf1 --- /dev/null +++ b/cli/yaml2json/build.gradle @@ -0,0 +1,96 @@ +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import org.graalvm.buildtools.gradle.dsl.NativeImageOptions + +plugins { + id 'info.ankin.projects.app-conventions' +} + +version = '0.0.1' + +dependencies { + implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.2' +} + +// for shadowJar +application.mainClass.set 'info.ankin.projects.cli.yaml2json.Yaml2Json' + +shadowJar { + // output first jar as "shadow" + // so as not to confuse with yaml2json and json2Yaml + archiveBaseName.set 'shadow' +} + +void customShadowJar(ShadowJar s, String name, String className) { + s.group 'shadow' + s.description name + 'Jar shadow jar' + s.archiveBaseName.set name + s.manifest { + attributes('Main-Class': 'info.ankin.projects.cli.yaml2json.' + className) + } + s.from zipTree(tasks.shadowJar.getArchiveFile()) +} + +def yaml2JsonJar = tasks.register('yaml2JsonJar', ShadowJar) { + customShadowJar(it, 'yaml2Json', 'Yaml2Json') +} + +def json2YamlJar = tasks.register('json2YamlJar', ShadowJar) { + customShadowJar(it, 'json2Yaml', 'Json2Yaml') +} + +tasks { + shadowJar { + finalizedBy yaml2JsonJar, json2YamlJar + } +} + +task example { + doLast { + println + } +} + +// for graalvmNative +tasks.jar.enabled(true) + +// https://graalvm.github.io/native-build-tools/0.9.11/gradle-plugin.html#_configuration_options +graalvmNative { + binaries { + json2yaml { NativeImageOptions o -> + o.imageName.set 'json2yaml' + o.mainClass.set json2YamlJar.get().getManifest().getAttributes().get('Main-Class') + + o.debug.set false + o.verbose.set true + o.fallback.set false + o.buildArgs.add('-Ob') // faster development builds + o.useFatJar.set false + o.classpath sourceSets.main.output.files, project.tasks.shadowJar + } + + yaml2json { NativeImageOptions o -> + o.imageName.set 'yaml2json' + o.mainClass.set yaml2JsonJar.get().getManifest().getAttributes().get('Main-Class') + o.debug.set false + o.verbose.set true + o.fallback.set false + o.buildArgs.add('-Ob') // faster development builds + o.useFatJar.set false + o.classpath sourceSets.main.output.files, project.tasks.shadowJar + } + } +} + +tasks { + nativeCompile { + dependsOn nativeYaml2jsonCompile, nativeJson2yamlCompile + doLast { + copy { + from tasks.nativeJson2yamlCompile.outputs.files.files + from tasks.nativeYaml2jsonCompile.outputs.files.files + + into tasks.nativeCompile.outputs.files.singleFile + } + } + } +} diff --git a/cli/yaml2json/src/main/java/info/ankin/projects/cli/yaml2json/Json2Yaml.java b/cli/yaml2json/src/main/java/info/ankin/projects/cli/yaml2json/Json2Yaml.java new file mode 100644 index 0000000..35a1e88 --- /dev/null +++ b/cli/yaml2json/src/main/java/info/ankin/projects/cli/yaml2json/Json2Yaml.java @@ -0,0 +1,20 @@ +package info.ankin.projects.cli.yaml2json; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.json.JsonMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; +import lombok.SneakyThrows; + +public class Json2Yaml { + @SneakyThrows + public static void main(String[] args) { + JsonMapper jsonMapper = new JsonMapper(); + YAMLMapper yamlMapper = new YAMLMapper(); + yamlMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); + + JsonNode jsonNode = jsonMapper.reader().readTree(System.in); + yamlMapper.writeValue(System.out, jsonNode); + System.out.println(); + } +} diff --git a/cli/yaml2json/src/main/java/info/ankin/projects/cli/yaml2json/Yaml2Json.java b/cli/yaml2json/src/main/java/info/ankin/projects/cli/yaml2json/Yaml2Json.java new file mode 100644 index 0000000..fa46c6c --- /dev/null +++ b/cli/yaml2json/src/main/java/info/ankin/projects/cli/yaml2json/Yaml2Json.java @@ -0,0 +1,20 @@ +package info.ankin.projects.cli.yaml2json; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.json.JsonMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; +import lombok.SneakyThrows; + +public class Yaml2Json { + @SneakyThrows + public static void main(String[] args) { + JsonMapper jsonMapper = new JsonMapper(); + jsonMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); + YAMLMapper yamlMapper = new YAMLMapper(); + + JsonNode jsonNode = yamlMapper.reader().readTree(System.in); + jsonMapper.writeValue(System.out, jsonNode); + System.out.println(); + } +} diff --git a/settings.gradle b/settings.gradle index 672b332..937ae6a 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,5 @@ rootProject.name = 'java-projects' include 'cli:killport' +include 'cli:yaml2json' include 'system:infer-platform' include 'system:terminal:supports-hyperlinks'