Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotations #178

Closed
dhakehurst opened this issue Sep 16, 2024 · 5 comments
Closed

Annotations #178

dhakehurst opened this issue Sep 16, 2024 · 5 comments

Comments

@dhakehurst
Copy link

Hi,
Is there a way to add annotations ?

UseCase:
use in kotlin multiplatform project with multiple modules.

I generate a BuildConfig for each module.

Kotlin (for some reason I don't understand and disagree with) generates javascript files that ignore all package names.
thus the typescript file that is generated contains multiple 'BuildConfig' classes.

I figure I can fix that by adding an '@JsName' annotation to the generated BuildConfig class that adds something unique.

(of course what I really want is that JS code generated by kotlin includes the namespaces, but that is an argument I am having with Kotlin)

@gmazzo
Copy link
Owner

gmazzo commented Sep 16, 2024

I never thought about this use case to be honest.

As a quick workaround, I'd suggest you change the default BuildConfig name for each class to something that is unique.

As a second, you can use the buildConfig.generator API to set your own generator, the issue is that it would be pretty much useless as you'll need to fully implement it yourself. Currently both Kotlin and Java generators are final as they are (necessary) data classes.

I think I craft a quick PR to change this, so you'll able to customize the final output yourself.

@dhakehurst
Copy link
Author

dhakehurst commented Sep 16, 2024 via email

@gmazzo
Copy link
Owner

gmazzo commented Sep 17, 2024

Released as v5.5.0

@gmazzo
Copy link
Owner

gmazzo commented Sep 18, 2024

You can check an example of how to do it here:

buildConfig {
    generator = object : BuildConfigKotlinGenerator() {
        override fun adaptSpec(spec: TypeSpec) = spec.toBuilder()
            .addAnnotation(AnnotationSpec.builder(ClassName.bestGuess("kotlin.js.JsName"))
                .addMember("name = %S", spec.name!!)
                .build())
            .build()
    }

https://github.com/gmazzo/gradle-buildconfig-plugin/blob/main/demo-project/kts-multiplatform/build.gradle.kts#L25

As it's a very concrete use case, I'm not going to document on the readme. generators are customizable and that's documented already.

@gmazzo gmazzo closed this as completed Sep 18, 2024
@dhakehurst
Copy link
Author

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants