Skip to content

Commit

Permalink
Use kotlinx io (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel authored Feb 20, 2025
1 parent ce0d70a commit 2979a8e
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 256 deletions.
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ WORKDIR /app

SHELL ["/bin/bash", "-c"]

CMD /app/wirespec compile -d $(pwd)/types -l Java -o $(pwd)/types/out/docker/java && \
/app/wirespec compile -d $(pwd)/types -l Kotlin -o $(pwd)/types/out/docker/kotlin && \
/app/wirespec compile -d $(pwd)/types -l Scala -o $(pwd)/types/out/docker/scala && \
/app/wirespec compile -d $(pwd)/types -l TypeScript -o $(pwd)/types/out/docker/typescript && \
/app/wirespec compile -d $(pwd)/types -l Wirespec -o $(pwd)/types/out/docker/wirespec
CMD /app/wirespec compile -i $(pwd)/types -l Java -o $(pwd)/types/out/docker/java && \
/app/wirespec compile -i $(pwd)/types -l Kotlin -o $(pwd)/types/out/docker/kotlin && \
/app/wirespec compile -i $(pwd)/types -l Scala -o $(pwd)/types/out/docker/scala && \
/app/wirespec compile -i $(pwd)/types -l TypeScript -o $(pwd)/types/out/docker/typescript && \
/app/wirespec compile -i $(pwd)/types -l Wirespec -o $(pwd)/types/out/docker/wirespec
2 changes: 1 addition & 1 deletion examples/npm-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"clean:wirespec": "npx rimraf node_modules/.bin/wirespec",
"clean:node_modules": "npx rimraf ./node_modules",
"clean:generated": "npx rimraf ./src/gen",
"generate": "wirespec compile -d ./wirespec -o ./src/gen -l TypeScript -p ''",
"generate": "wirespec compile -i ./wirespec -o ./src/gen -l TypeScript -p ''",
"test": "npm run test:client && npm run test:server",
"test:client": "ts-node src/clientSimple.ts && ts-node src/clientProxy.ts",
"test:server": "ts-node src/server.ts",
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ kotest_arrow = "1.4.0"
kotlin = "2.0.21"
kotlin_compiler = "2.0"
kotlinx_coroutines = "1.9.0"
kotlinx_io = "0.6.0"
kotlinx_openapi_bindings = "0.0.24"
kotlinx_resources = "0.9.0"
kotlinx_rgxgen = "0.0.1"
Expand Down Expand Up @@ -45,6 +46,7 @@ kotlin_test_common = { module = "org.jetbrains.kotlin:kotlin-test-common", versi
kotlin_test_annotations_common = { module = "org.jetbrains.kotlin:kotlin-test-annotations-common", version.ref = "kotlin" }
kotlin_test_junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
kotlinx_coroutines_reactor = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-reactor", version.ref = "kotlinx_coroutines" }
kotlinx_io_core = { module = "org.jetbrains.kotlinx:kotlinx-io-core", version.ref = "kotlinx_io" }
kotlinx_openapi_bindings = { module = "community.flock.kotlinx.openapi.bindings:kotlin-openapi-bindings", version.ref = "kotlinx_openapi_bindings" }
kotlinx_resources = { module = "com.goncalossilva:resources", version.ref = "kotlinx_resources" }
kotlinx_rgxgen = { module = "community.flock.kotlinx.rgxgen:kotlin-rgxgen", version.ref = "kotlinx_rgxgen" }
Expand Down
60 changes: 30 additions & 30 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,42 @@ convertPackage="community.flock.openapi.generated"

echo "Test macOS artifact"
macWirespec=./src/plugin/$artifactName/build/bin/$macosArch/releaseExecutable/$artifactName.kexe
"$macWirespec" compile -d "$(pwd)"/types -l Java -p "$compilePackage" -o "$(pwd)"/types/out/native/java
"$macWirespec" compile -d "$(pwd)"/types -l Kotlin -p "$compilePackage" -o "$(pwd)"/types/out/native/kotlin
"$macWirespec" compile -d "$(pwd)"/types -l Scala -p "$compilePackage" -o "$(pwd)"/types/out/native/scala
"$macWirespec" compile -d "$(pwd)"/types -l TypeScript -p "$compilePackage" -o "$(pwd)"/types/out/native/typescript
"$macWirespec" compile -d "$(pwd)"/types -l Wirespec -p "$compilePackage" -o "$(pwd)"/types/out/native/wirespec
"$macWirespec" convert -f "$(pwd)"/types/petstore.json openapiv2 -l Java -p "$convertPackage" -o "$(pwd)"/types/out/native/java
"$macWirespec" convert -f "$(pwd)"/types/petstore.json openapiv2 -l Kotlin -p "$convertPackage" -o "$(pwd)"/types/out/native/kotlin
"$macWirespec" convert -f "$(pwd)"/types/petstore.json openapiv2 -l Scala -p "$convertPackage" -o "$(pwd)"/types/out/native/scala
"$macWirespec" convert -f "$(pwd)"/types/petstore.json openapiv2 -l TypeScript -p "$convertPackage" -o "$(pwd)"/types/out/native/typescript
"$macWirespec" convert -f "$(pwd)"/types/petstore.json openapiv2 -l Wirespec -p "$convertPackage" -o "$(pwd)"/types/out/native/wirespec
"$macWirespec" compile -i "$(pwd)"/types -l Java -p "$compilePackage" -o "$(pwd)"/types/out/native/java
"$macWirespec" compile -i "$(pwd)"/types -l Kotlin -p "$compilePackage" -o "$(pwd)"/types/out/native/kotlin
"$macWirespec" compile -i "$(pwd)"/types -l Scala -p "$compilePackage" -o "$(pwd)"/types/out/native/scala
"$macWirespec" compile -i "$(pwd)"/types -l TypeScript -p "$compilePackage" -o "$(pwd)"/types/out/native/typescript
"$macWirespec" compile -i "$(pwd)"/types -l Wirespec -p "$compilePackage" -o "$(pwd)"/types/out/native/wirespec
"$macWirespec" convert -i "$(pwd)"/types/petstore.json openapiv2 -l Java -p "$convertPackage" -o "$(pwd)"/types/out/native/java
"$macWirespec" convert -i "$(pwd)"/types/petstore.json openapiv2 -l Kotlin -p "$convertPackage" -o "$(pwd)"/types/out/native/kotlin
"$macWirespec" convert -i "$(pwd)"/types/petstore.json openapiv2 -l Scala -p "$convertPackage" -o "$(pwd)"/types/out/native/scala
"$macWirespec" convert -i "$(pwd)"/types/petstore.json openapiv2 -l TypeScript -p "$convertPackage" -o "$(pwd)"/types/out/native/typescript
"$macWirespec" convert -i "$(pwd)"/types/petstore.json openapiv2 -l Wirespec -p "$convertPackage" -o "$(pwd)"/types/out/native/wirespec

echo "Test JVM artifact"
jvmWirespec=./src/plugin/$artifactName/build/libs/$artifactName-jvm-0.0.0-SNAPSHOT.jar
java -jar "$jvmWirespec" compile -d "$(pwd)"/types -l Java -p "$compilePackage" -o "$(pwd)"/types/out/jvm/java
java -jar "$jvmWirespec" compile -d "$(pwd)"/types -l Kotlin -p "$compilePackage" -o "$(pwd)"/types/out/jvm/kotlin
java -jar "$jvmWirespec" compile -d "$(pwd)"/types -l Scala -p "$compilePackage" -o "$(pwd)"/types/out/jvm/scala
java -jar "$jvmWirespec" compile -d "$(pwd)"/types -l TypeScript -p "$compilePackage" -o "$(pwd)"/types/out/jvm/typescript
java -jar "$jvmWirespec" compile -d "$(pwd)"/types -l Wirespec -p "$compilePackage" -o "$(pwd)"/types/out/jvm/wirespec
java -jar "$jvmWirespec" convert -f "$(pwd)"/types/petstore.json openapiv2 -l Java -p "$convertPackage" -o "$(pwd)"/types/out/jvm/java
java -jar "$jvmWirespec" convert -f "$(pwd)"/types/petstore.json openapiv2 -l Kotlin -p "$convertPackage" -o "$(pwd)"/types/out/jvm/kotlin
java -jar "$jvmWirespec" convert -f "$(pwd)"/types/petstore.json openapiv2 -l Scala -p "$convertPackage" -o "$(pwd)"/types/out/jvm/scala
java -jar "$jvmWirespec" convert -f "$(pwd)"/types/petstore.json openapiv2 -l TypeScript -p "$convertPackage" -o "$(pwd)"/types/out/jvm/typescript
java -jar "$jvmWirespec" convert -f "$(pwd)"/types/petstore.json openapiv2 -l Wirespec -p "$convertPackage" -o "$(pwd)"/types/out/jvm/wirespec
java -jar "$jvmWirespec" compile -i "$(pwd)"/types -l Java -p "$compilePackage" -o "$(pwd)"/types/out/jvm/java
java -jar "$jvmWirespec" compile -i "$(pwd)"/types -l Kotlin -p "$compilePackage" -o "$(pwd)"/types/out/jvm/kotlin
java -jar "$jvmWirespec" compile -i "$(pwd)"/types -l Scala -p "$compilePackage" -o "$(pwd)"/types/out/jvm/scala
java -jar "$jvmWirespec" compile -i "$(pwd)"/types -l TypeScript -p "$compilePackage" -o "$(pwd)"/types/out/jvm/typescript
java -jar "$jvmWirespec" compile -i "$(pwd)"/types -l Wirespec -p "$compilePackage" -o "$(pwd)"/types/out/jvm/wirespec
java -jar "$jvmWirespec" convert -i "$(pwd)"/types/petstore.json openapiv2 -l Java -p "$convertPackage" -o "$(pwd)"/types/out/jvm/java
java -jar "$jvmWirespec" convert -i "$(pwd)"/types/petstore.json openapiv2 -l Kotlin -p "$convertPackage" -o "$(pwd)"/types/out/jvm/kotlin
java -jar "$jvmWirespec" convert -i "$(pwd)"/types/petstore.json openapiv2 -l Scala -p "$convertPackage" -o "$(pwd)"/types/out/jvm/scala
java -jar "$jvmWirespec" convert -i "$(pwd)"/types/petstore.json openapiv2 -l TypeScript -p "$convertPackage" -o "$(pwd)"/types/out/jvm/typescript
java -jar "$jvmWirespec" convert -i "$(pwd)"/types/petstore.json openapiv2 -l Wirespec -p "$convertPackage" -o "$(pwd)"/types/out/jvm/wirespec

echo "Test Node.js artifact"
wirespecJs=build/js/packages/wirespec-src-plugin-$artifactName/kotlin/wirespec-src-plugin-$artifactName.js
node "$wirespecJs" compile -d "$(pwd)"/types -l Java -p "$compilePackage" -o "$(pwd)"/types/out/java
node "$wirespecJs" compile -d "$(pwd)"/types -l Kotlin -p "$compilePackage" -o "$(pwd)"/types/out/kotlin
node "$wirespecJs" compile -d "$(pwd)"/types -l Scala -p "$compilePackage" -o "$(pwd)"/types/out/scala
node "$wirespecJs" compile -d "$(pwd)"/types -l TypeScript -p "$compilePackage" -o "$(pwd)"/types/out/typescript
node "$wirespecJs" compile -d "$(pwd)"/types -l Wirespec -p "$compilePackage" -o "$(pwd)"/types/out/wirespec
node "$wirespecJs" convert -f "$(pwd)"/types/petstore.json openapiv2 -l Java -p "$convertPackage" -o "$(pwd)"/types/out/java
node "$wirespecJs" convert -f "$(pwd)"/types/petstore.json openapiv2 -l Kotlin -p "$convertPackage" -o "$(pwd)"/types/out/kotlin
node "$wirespecJs" convert -f "$(pwd)"/types/petstore.json openapiv2 -l Scala -p "$convertPackage" -o "$(pwd)"/types/out/scala
node "$wirespecJs" convert -f "$(pwd)"/types/petstore.json openapiv2 -l TypeScript -p "$convertPackage" -o "$(pwd)"/types/out/typescript
node "$wirespecJs" convert -f "$(pwd)"/types/petstore.json openapiv2 -l Wirespec -p "$convertPackage" -o "$(pwd)"/types/out/wirespec
node "$wirespecJs" compile -i "$(pwd)"/types -l Java -p "$compilePackage" -o "$(pwd)"/types/out/java
node "$wirespecJs" compile -i "$(pwd)"/types -l Kotlin -p "$compilePackage" -o "$(pwd)"/types/out/kotlin
node "$wirespecJs" compile -i "$(pwd)"/types -l Scala -p "$compilePackage" -o "$(pwd)"/types/out/scala
node "$wirespecJs" compile -i "$(pwd)"/types -l TypeScript -p "$compilePackage" -o "$(pwd)"/types/out/typescript
node "$wirespecJs" compile -i "$(pwd)"/types -l Wirespec -p "$compilePackage" -o "$(pwd)"/types/out/wirespec
node "$wirespecJs" convert -i "$(pwd)"/types/petstore.json openapiv2 -l Java -p "$convertPackage" -o "$(pwd)"/types/out/java
node "$wirespecJs" convert -i "$(pwd)"/types/petstore.json openapiv2 -l Kotlin -p "$convertPackage" -o "$(pwd)"/types/out/kotlin
node "$wirespecJs" convert -i "$(pwd)"/types/petstore.json openapiv2 -l Scala -p "$convertPackage" -o "$(pwd)"/types/out/scala
node "$wirespecJs" convert -i "$(pwd)"/types/petstore.json openapiv2 -l TypeScript -p "$convertPackage" -o "$(pwd)"/types/out/typescript
node "$wirespecJs" convert -i "$(pwd)"/types/petstore.json openapiv2 -l Wirespec -p "$convertPackage" -o "$(pwd)"/types/out/wirespec

echo "Test docker image"
docker run $archSpecific --rm -it -v "$(pwd)"/types:/app/types wirespec
1 change: 1 addition & 0 deletions src/plugin/cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ kotlin {
implementation(project(":src:converter:avro"))
implementation(project(":src:converter:openapi"))
implementation(libs.clikt)
implementation(libs.kotlinx.io.core)
}
}
val commonTest by getting {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ import community.flock.wirespec.plugin.Operation
import community.flock.wirespec.plugin.Output
import community.flock.wirespec.plugin.PackageName
import community.flock.wirespec.plugin.cli.io.File
import kotlinx.io.files.Path
import kotlinx.io.files.SystemFileSystem

enum class Options(vararg val flags: String) {
InputDir("-d", "--input-dir"),
InputFile("-f", "--input-file"),
OutputDir("-o", "--output-dir"),
Input("-i", "--input"),
Output("-o", "--output"),
Language("-l", "--language"),
PackageName("-p", "--package"),
LogLevel("--log-level"),
Expand All @@ -56,21 +57,22 @@ class WirespecCli : NoOpCliktCommand(name = "wirespec") {
}

private abstract class CommonOptions : CliktCommand() {
private val inputFile by option(*Options.InputFile.flags, help = "Input file")
val inputDir by option(*Options.InputDir.flags, help = "Input directory")
val outputDir by option(*Options.OutputDir.flags, help = "Output directory")
val input by option(*Options.Input.flags, help = "Input")
val output by option(*Options.Output.flags, help = "Output")
val packageName by option(*Options.PackageName.flags, help = "Package name").default(DEFAULT_GENERATED_PACKAGE_STRING)
val logLevel by option(*Options.LogLevel.flags, help = "Log level: $Level").default("$ERROR")
val shared by option(*Options.Shared.flags, help = "Generate shared wirespec code").flag(default = false)
val strict by option(*Options.Strict.flags, help = "Strict mode").flag()

fun getInput(inputDir: String? = null): Input = if (inputDir != null && inputFile != null) {
throw CliktError("Choose either a file or a directory. Not Both.")
} else {
inputFile
?.let(FullFilePath.Companion::parse)
?: inputDir?.let(::FullDirPath)
?: Console
fun getInput(input: String? = null): Input {
if (input != null) {
val path = Path(input)
val meta = SystemFileSystem.metadataOrNull(path)
?: throw CliktError("Cannot access file or directory: $input")
if (meta.isDirectory) return FullDirPath(input)
if (meta.isRegularFile) return FullFilePath.parse(input)
}
return Console
}

fun String.toLogLevel() = when (trim().uppercase()) {
Expand All @@ -94,8 +96,8 @@ private class Compile(
override fun run() {
CompilerArguments(
operation = Operation.Compile,
input = getInput(inputDir),
output = Output(outputDir),
input = getInput(input),
output = Output(output),
languages = languages.toSet(),
packageName = PackageName(packageName),
logLevel = logLevel.toLogLevel(),
Expand All @@ -121,11 +123,14 @@ private class Convert(
.multiple(listOf(Wirespec))

override fun run() {
inputDir?.let { echo("To convert, please specify a file", err = true) }
val inp = getInput(input)
if (inp is FullDirPath) {
echo("To convert, please specify a file", err = true)
}
CompilerArguments(
operation = Operation.Convert(format = format),
input = getInput(null),
output = Output(outputDir),
input = inp,
output = Output(output),
languages = languages.toSet().ifEmpty { setOf(Wirespec) },
packageName = PackageName(packageName),
logLevel = logLevel.toLogLevel(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
package community.flock.wirespec.plugin.cli.io

expect class Directory(path: String) {
import community.flock.wirespec.plugin.FileExtension
import community.flock.wirespec.plugin.FileName
import community.flock.wirespec.plugin.FullFilePath
import kotlinx.io.files.Path
import kotlinx.io.files.SystemFileSystem

fun wirespecFiles(): Set<WirespecFile>
private val Path.extension: String
get() = name.substringAfterLast('.', "")

class Directory(private val path: String) {
fun wirespecFiles(): Set<WirespecFile> = Path(path)
.let { SystemFileSystem.list(it) }
.asSequence()
.filter { SystemFileSystem.metadataOrNull(it)?.isRegularFile ?: false }
.filter { it.extension == FileExtension.Wirespec.value }
.map { it.name }
.map { it.dropLast(FileExtension.Wirespec.value.length + 1) }
.map { FullFilePath(path, FileName(it)) }
.map(::WirespecFile)
.toSet()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@ import community.flock.wirespec.plugin.FileName
import community.flock.wirespec.plugin.FullFilePath
import community.flock.wirespec.plugin.Reader
import community.flock.wirespec.plugin.Writer
import kotlinx.io.buffered
import kotlinx.io.files.Path
import kotlinx.io.files.SystemFileSystem
import kotlinx.io.readString
import kotlinx.io.writeString

interface Copy {
fun copy(fileName: FileName): File
}

expect abstract class File(path: FullFilePath) :
abstract class File(val path: FullFilePath) :
Reader,
Writer,
Copy {

val path: FullFilePath
override fun read(): String = Path(path.toString())
.let { SystemFileSystem.source(it).buffered().readString() }

override fun read(): String

override fun write(string: String)
override fun write(string: String) = Path(path.toString())
.let { path ->
path.parent
?.takeIf { !SystemFileSystem.exists(it) }
?.let { SystemFileSystem.createDirectories(it, true) }
SystemFileSystem.sink(path).buffered()
.apply { writeString(string) }
.flush()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ class CommandLineEntitiesTest {
listOfNotNull(
it.flags.first(),
when (it) {
Options.InputFile -> null
Options.InputDir -> "input"
Options.OutputDir -> "output"
Options.Input -> "src/commonTest/resources/openapi"
Options.Output -> "output"
Options.Language -> "Wirespec"
Options.PackageName -> "packageName"
Options.LogLevel -> "error"
Expand All @@ -39,10 +38,10 @@ class CommandLineEntitiesTest {
Options.Strict -> null
},
)
}.filterNot { it == "-f" }.toTypedArray()
}.toTypedArray()
WirespecCli.provide(
noopInput {
it.input.shouldBeTypeOf<FullDirPath>().path shouldBe "input"
it.input.shouldBeTypeOf<FullDirPath>().path shouldBe "src/commonTest/resources/openapi"
it.operation.shouldBeTypeOf<Operation.Compile>()
it.output?.value shouldBe "output"
it.languages shouldBe setOf(Wirespec)
Expand Down Expand Up @@ -81,7 +80,7 @@ class CommandLineEntitiesTest {
noopInput {
it.operation.shouldBeTypeOf<Operation.Convert>()
it.input.shouldBeTypeOf<FullFilePath>().run {
fileName.value shouldBe "swagger"
fileName.value shouldBe "keto"
extension shouldBe FileExtension.Json
}
it.output.shouldBeNull()
Expand All @@ -92,7 +91,7 @@ class CommandLineEntitiesTest {
it.strict shouldBe false
},
noopWriter,
)(arrayOf("convert", "-f", "swagger.json", "openapiv2"))
)(arrayOf("convert", "-i", "src/commonTest/resources/openapi/keto.json", "openapiv2"))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class WirespecCliTest {
val input = "$inputDir/wirespec"
val output = outputDir()

WirespecCli.provide(::compile, ::convert, ::write)(arrayOf("compile", "-d", input, "-o", output, "-l", "Kotlin"))
WirespecCli.provide(::compile, ::convert, ::write)(arrayOf("compile", "-i", input, "-o", output, "-l", "Kotlin"))

val file = KotlinFile(FullFilePath("$output/$packageDir", FileName("Type"))).read()

Expand All @@ -47,7 +47,7 @@ class WirespecCliTest {
WirespecCli.provide(::compile, ::convert, ::write)(
arrayOf(
"compile",
"-d", input,
"-i", input,
"-o", output,
"-l", "Java",
"-p", "community.flock.next",
Expand Down Expand Up @@ -79,7 +79,7 @@ class WirespecCliTest {
WirespecCli.provide(::compile, ::convert, ::write)(
arrayOf(
"convert", "openapiv2",
"-f", input,
"-i", input,
"-o", output,
"-l", "Kotlin",
"-p", "community.flock.openapi",
Expand Down Expand Up @@ -113,7 +113,7 @@ class WirespecCliTest {
WirespecCli.provide(::compile, ::convert, ::write)(
arrayOf(
"convert", "openapiv3",
"-f", input,
"-i", input,
"-o", output,
"-l", "Kotlin",
"-p", "community.flock.openapi",
Expand Down Expand Up @@ -146,7 +146,7 @@ class WirespecCliTest {
WirespecCli.provide(::compile, ::convert, ::write)(
arrayOf(
"convert", "openapiv2",
"-f", input,
"-i", input,
"-o", output,
"-l", "TypeScript",
"-p", "community.flock.openapi",
Expand Down
Loading

0 comments on commit 2979a8e

Please sign in to comment.