ts-generator icon indicating copy to clipboard operation
ts-generator copied to clipboard

Gradle plugin

Open GuiSim opened this issue 7 years ago • 7 comments

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.

GuiSim avatar Apr 20 '18 13:04 GuiSim

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.

ntrrgc avatar Apr 20 '18 13:04 ntrrgc

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

GuiSim avatar Apr 27 '18 20:04 GuiSim

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

alexvas avatar Oct 15 '18 04:10 alexvas

@alexvas doesn't work

forresthopkinsa avatar Feb 11 '19 19:02 forresthopkinsa

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

alexvas avatar Feb 12 '19 04:02 alexvas

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")
}

forresthopkinsa avatar Feb 13 '19 18:02 forresthopkinsa

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

ktsgenerator

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

ayedo avatar Nov 09 '19 14:11 ayedo