ayedo / ktsgenerator

Gradle plugin to generate Typescript defintions from Kotlin classes
Apache License 2.0
7 stars 0 forks source link

Working example with kotlin-multiplatform #1

Open avwie opened 4 years ago

avwie commented 4 years ago

Is it possible to generate a working example with kotlin-multiplatform project? Preferably with a build.gradle.kts file.

I've tried a lot of different ways to make it work, but I can not get ts-defs from commonMain.

ayedo commented 4 years ago

Hi avwie,

I'm not sure I understand your use case. If you are using Kotlin multi-platform doesn't the Kotlin compiler already generate JavaScript for you?

This plugin is meant to be used together with a TypeScript based frontend (for example, an angular based UI), so that you do not have to rewrite your definitions on the backend, and frontend. It's like a 'Swagger lite' kind of tool.

avwie commented 4 years ago

Hi ayedo,

you are exactly right. I generate a JS common library which I use in the frontend. As I use Typescript in my frontend I would really like Typescript definitions and I came upon your project.

However, when using the kotlin-multiplatform plugin as described here I run into problems because I am unable to run it alongside with your plugin: https://kotlinlang.org/docs/tutorials/mpp/multiplatform-library.html

I think I lack in gradle experience, because I can't get your Gradle plugin to work. Because with the multiplatform plugin the buildscript part shouldn't be used anymore.

ayedo commented 4 years ago

OK. Do you get an exception, or some message in the console?

The plugin depends on a library that is not published on Maven-Central, and therefore the buildscript part is absolutely required. Gradle plugins are not built as fat JARs, and by default only Maven-Central artifacts are supported as dependencies.

A future version of the plugin might bundle the dependencies as a fat JAR, so that the buildscript part won't be required any more.

itslenny commented 4 years ago

@ayedo

I'm having (maybe) a similar problem. Maybe some of what I've seen will shed some light on this.

I'm also doing a mutlti-platform app, but I'm not doing JS. Just Android and iOS. I have some typedefs that I want to share from kotlin to a webview.

I originally used the default typescriptGenerator { } specified in your readme. I only updated packageName to use the correct package name for my project.

My project is an android app, and has a second module that contains the files I want to create typedefs from. The main module is called "app" (default in android studio). The module I want is called "WebViewEventProxy"

When I ran it with the default settings it ran without issue, but created an empty d.ts file because it didn't see my files.

when I log getProject().sourceSets.main.runtimeClasspath it is:

.../android/build/classes/java/main
.../android/build/resources/main
~/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-script-runtime/1.3.60/eb1f56fb1b58a8dc481a6e5beeb73931d80d08be/kotlin-script-runtime-1.3.60.jar

If I manually set the class path to:

classPath = layout.files([project.projectDir.toPath().resolve("WebViewEventProxy/build/classes/kotlin/android/main")])

It seems to run and find my files, but it fails because it can't find a class.

@avwie this might fix your issue.

This seems to work, but is there a more correct way in gradle to get this module path (other than hard coding it)?


The other issue I'm having I think has to do with using the kotlinx serialize which uses reflection.

Is there a way to tell it to ignore that or do I need to include the plugin in some way?

This is the error I receive:

Execution failed for task ':generateTypescriptDefinitions'.
> kotlinx/serialization/internal/GeneratedSerializer

The class it's failing to create typedefs for looks like this:

import kotlinx.serialization.*

@Serializable
data class ClientInitProxyEvent(
    val clientBuild: String,
    override val eventId: String? = null,
    override val type: String? = null
): ProxyEvent { }

I also tried adding:

typeMappings = [
  "kotlinx.serialization.internal.GeneratedSerializer": "any"
]

but then I get the errror

java.lang.ClassNotFoundException: kotlinx.serialization.internal.GeneratedSerializer

thanks in advance for any help.

ayedo commented 4 years ago

thanks for your detailed description, but without reproduction it's kind of difficult to pinpoint the problem. Do you have a public repository, or a minimal project which I could look into?