ntrrgc / ts-generator

Generate TypeScript definitions from Kotlin/Java/JVM classes
Other
262 stars 45 forks source link

Gradle plugin #6

Open GuiSim opened 6 years ago

GuiSim commented 6 years ago

This is a very cool project. However, wouldn't it make more sense to run this as part of a Gradle plugin, during the build process, and not as a part of the application's code itself?

I see this as something similar to the kotlin2js plugin.

ntrrgc commented 6 years ago

You can create a gradle task if it makes sense for you. Hopefully there is no inconvenience, it's just instantiating a class, calling a method and writing the results to a file, all of which vary a lot depending on your project.

GuiSim commented 6 years ago

I'm really not familiar on how to do this but I'll give it a try and report back here. I suspect that this is a common usage scenario, especially when combined with Kotlin Multiplatform projects

alexvas commented 5 years ago

Hello. Here is the Gradle plugin: https://plugins.gradle.org/plugin/kotlin2ts.kt2ts

forresthopkinsa commented 5 years ago

@alexvas doesn't work

alexvas commented 5 years ago

@forresthopkinsa may you provide a sample that doesn't work?

forresthopkinsa commented 5 years ago

I ended up just implementing it myself, it was much easier to do.

TypeDefGenerator.kt:

package com.forresthopkinsa.braze.model

import me.ntrrgc.tsGenerator.TypeScriptGenerator

fun main() {
    val classes = setOf(
        RestController.Constants::class,
        JavaVersion::class,
    )
    val ignored = setOf(
        Element::class,
        SimpleElement::class,
        Data::class
    )
    val definitions = TypeScriptGenerator(rootClasses = classes, ignoreSuperclasses = ignored).definitionsText
    println(definitions)
}

build.gradle.kts:

repositories {
    ...
    maven { url = uri("https://jitpack.io") }
}

dependencies {
    ...
    compile("com.github.ntrrgc", "ts-generator", "1.1.1")
}

val generateDefinitions by tasks.creating(JavaExec::class) {
    classpath = sourceSets["main"].runtimeClasspath
    main = "com.forresthopkinsa.braze.model.TypeDefGeneratorKt"
    standardOutput = File(buildDir, "kotlin-model.d.ts").outputStream()
}

val copyDefinitions by tasks.creating(Copy::class) {
    from(File(buildDir, "kotlin-model.d.ts"))
    destinationDir = File(projectDir, "src/frontend/src")
}
ayedo commented 4 years ago

I've create a Gradle plugin for ts-generator:

ktsgenerator

Let me know what you think. If you like it, I'd appreciate a ⭐️ :)