switched build system from sbt to Gradle

This commit is contained in:
2021-06-16 08:14:33 +01:00
parent 6422a91568
commit 605e1ad644
21 changed files with 573 additions and 244 deletions

15
cli/build.gradle Normal file
View File

@@ -0,0 +1,15 @@
plugins {
id 'application'
id 'org.jetbrains.kotlin.jvm'
}
dependencies {
implementation group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: getProperty('version.kotlin')
implementation group: 'com.beust', name: 'jcommander', version: getProperty('version.jcommander')
implementation group: "org.slf4j", name: "slf4j-simple", version: getProperty('version.slf4j')
implementation rootProject
}
application {
mainClassName = 'net.woggioni.wson.cli.MainKt'
}

View File

@@ -1,8 +0,0 @@
organization := (organization in LocalRootProject).value
name := "worth-cli"
version := (version in LocalRootProject).value
resourceDirectory := (resourceDirectory in(LocalRootProject, Test)).value
skip in publish := true
mainClass := Some("net.woggioni.worth.cli.Main")
maintainer := "oggioni.walter@gmail.com"
libraryDependencies += "com.beust" % "jcommander" % Versions.jcommander

View File

@@ -0,0 +1,128 @@
package net.woggioni.wson.cli
import com.beust.jcommander.IStringConverter
import com.beust.jcommander.JCommander
import com.beust.jcommander.Parameter
import com.beust.jcommander.ParameterException
import com.beust.jcommander.converters.PathConverter
import kotlin.system.exitProcess
import net.woggioni.wson.serialization.binary.JBONDumper
import net.woggioni.wson.serialization.binary.JBONParser
import net.woggioni.wson.serialization.json.JSONDumper
import net.woggioni.wson.serialization.json.JSONParser
import net.woggioni.wson.xface.Value
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.nio.file.Files
import java.nio.file.Path
sealed class SerializationFormat(val name: String) {
override fun toString() = name
object JSON : SerializationFormat("json")
object JBON : SerializationFormat("jbon")
companion object {
fun parse(value: String) = when (value) {
JBON.name -> JBON
JSON.name -> JSON
else -> {
val availableValues = sequenceOf(
JSON,
JBON
).map(SerializationFormat::name).joinToString(", ")
throw IllegalArgumentException(
"Unknown serialization format '$value', possible values are $availableValues")
}
}
}
}
private class OutputTypeConverter : IStringConverter<SerializationFormat> {
override fun convert(value: String): SerializationFormat = SerializationFormat.parse(value)
}
private class CliArg {
@Parameter(names = ["-f", "--file"], description = "Name of the input file to parse", converter = PathConverter::class)
var fileName: Path? = null
@Parameter(names = ["--input-type"], description = "Input type", converter = OutputTypeConverter::class)
var inputType: SerializationFormat = SerializationFormat.JSON
@Parameter(names = ["-o", "--output"], description = "Name of the JSON file to generate", converter = PathConverter::class)
var output: Path? = null
@Parameter(names = ["-t", "--type"], description = "Output type", converter = OutputTypeConverter::class)
var outputType: SerializationFormat = SerializationFormat.JSON
@Parameter(names = ["-h", "--help"], help = true)
var help: Boolean = false
}
fun main(vararg args: String) {
val cliArg = CliArg()
val cliArgumentParser = JCommander.newBuilder()
.addObject(cliArg)
.build()
try {
cliArgumentParser.parse(*args)
} catch (pe: ParameterException) {
cliArgumentParser.usage()
exitProcess(-1)
}
if (cliArg.help) {
cliArgumentParser.usage()
exitProcess(0)
}
val cfg = Value.Configuration.builder().serializeReferences(true).build()
val inputStream = if (cliArg.fileName != null) {
BufferedInputStream(Files.newInputStream(cliArg.fileName))
} else {
System.`in`
}
val result = when(cliArg.inputType) {
SerializationFormat.JSON -> {
val reader = InputStreamReader(inputStream)
try {
JSONParser(cfg).parse(reader)
} finally {
reader.close()
}
}
SerializationFormat.JBON -> {
try {
JBONParser(cfg).parse(inputStream)
} finally {
inputStream.close()
}
}
}
val outputStream = if (cliArg.output != null) {
BufferedOutputStream(Files.newOutputStream(cliArg.output))
} else {
System.out
}
when(cliArg.outputType) {
SerializationFormat.JSON -> {
val writer = OutputStreamWriter(outputStream)
try {
JSONDumper(cfg).dump(result, writer)
} finally {
writer.close()
}
}
SerializationFormat.JBON -> {
try {
JBONDumper(cfg).dump(result, outputStream)
} finally {
outputStream.close()
}
}
}
}

View File

@@ -1,126 +0,0 @@
package net.woggioni.wson.cli
import java.io._
import com.beust.jcommander.{IStringConverter, JCommander, Parameter, ParameterException}
import net.woggioni.wson.serialization.binary.{JBONDumper, JBONParser}
import net.woggioni.wson.serialization.json.{JSONDumper, JSONParser}
import net.woggioni.wson.xface.Value
sealed abstract class SerializationFormat(val name : String) {
override def toString = name
}
object SerializationFormat {
case object JSON extends SerializationFormat("json")
case object JBON extends SerializationFormat("jbon")
def parse(value : String) : SerializationFormat = {
value match {
case JBON.name => JBON
case JSON.name => JSON
case _ => {
val arr = Stream(
SerializationFormat.JSON,
SerializationFormat.JBON
).map(_.name).toArray
val availableValues = String.join(", ", arr :_*)
throw new IllegalArgumentException(
s"Unknown serialization format '$value', possible values are $availableValues")
}
}
}
}
class OutputTypeConverter extends IStringConverter[SerializationFormat] {
override def convert(value: String): SerializationFormat = SerializationFormat.parse(value)
}
class CliArg {
@Parameter(names = Array("-f", "--file"), description = "Name of the .cz file to parse")
var fileName : String = _
@Parameter(names = Array("--input-type"), description = "Input type", converter = classOf[OutputTypeConverter])
var inputType : SerializationFormat = SerializationFormat.JSON
@Parameter(names = Array("-o", "--output"), description = "Name of the JSON file to generate")
var outPut : String = _
@Parameter(names = Array("-t", "--type"), description = "Output type", converter = classOf[OutputTypeConverter])
var outputType : SerializationFormat = SerializationFormat.JSON
@Parameter(names = Array("-h", "--help"), help = true)
var help : Boolean = _
}
object Main {
def main(argv : Array[String]): Unit = {
val cliArg = new CliArg
val cliArgumentParser = JCommander.newBuilder()
.addObject(cliArg)
.build()
try {
cliArgumentParser.parse(argv :_*)
} catch {
case _ : ParameterException => {
cliArgumentParser.usage()
System.exit(-1)
}
case e : Throwable => throw e
}
if(cliArg.help) {
cliArgumentParser.usage()
System.exit(0)
}
val cfg = Value.Configuration.builder().serializeReferences(true).build()
val inputStream = if(cliArg.fileName != null) {
new BufferedInputStream(new FileInputStream(cliArg.fileName))
} else {
System.in
}
val result = cliArg.inputType match {
case SerializationFormat.JSON => {
val reader = new InputStreamReader(inputStream)
try {
new JSONParser(cfg).parse(reader)
} finally {
reader.close()
}
}
case SerializationFormat.JBON => {
try {
new JBONParser(cfg).parse(inputStream)
} finally {
inputStream.close()
}
}
}
val outputStream = if(cliArg.outPut != null) {
new BufferedOutputStream(new FileOutputStream(cliArg.outPut))
} else {
System.out
}
cliArg.outputType match {
case SerializationFormat.JSON => {
val writer = new OutputStreamWriter(outputStream)
try {
new JSONDumper(cfg).dump(result, writer)
} finally {
writer.close()
}
}
case SerializationFormat.JBON => {
try {
new JBONDumper(cfg).dump(result, outputStream)
} finally {
outputStream.close()
}
}
}
}
}