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

Support for Kotlin Serialization? #267

Open
ivoanjo opened this issue Jun 30, 2020 · 6 comments
Open

Support for Kotlin Serialization? #267

ivoanjo opened this issue Jun 30, 2020 · 6 comments

Comments

@ivoanjo
Copy link
Contributor

ivoanjo commented Jun 30, 2020

Hello there, and thanks for kscript!

I would like to request the addition of support for Kotlin Serialization. Since it needs an extra plug-in during compilation, I couldn't see any way to use it when using kscript.

@holgerbrandl
Copy link
Collaborator

Couldn't this be configured with an option parameter? Either for the compiler or runtime. kscript supports both, see the examples under
https://github.com/holgerbrandl/kscript#annotation-driven-script-configuration

@ivoanjo
Copy link
Contributor Author

ivoanjo commented Jul 2, 2020

Interesting! You are right -- on a closer look, I could get it to work, although it's a bit of a hack since I need to hardcode a path to the jar.

#!/usr/bin/env kscript

@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0", "org.jetbrains.kotlin:kotlin-maven-serialization:1.3.72")
@file:CompilerOpts("-Xplugin=/home/knuckles/.m2/repository/org/jetbrains/kotlin/kotlin-maven-serialization/1.3.72/kotlin-maven-serialization-1.3.72.jar")

import kotlinx.serialization.*
import kotlinx.serialization.json.*

@Serializable
data class Data(val a: Int, val b: String = "42")

fun main() {
    // Json also has .Default configuration which provides more reasonable settings,
    // but is subject to change in future versions
    val json = Json(JsonConfiguration.Stable)
    // serializing objects
    val jsonData = json.stringify(Data.serializer(), Data(42))
    // serializing lists
    println(jsonData) // {"a": 42, "b": "42"}

    // parsing data back
    val obj = json.parse(Data.serializer(), """{"a":42}""") // b is optional since it has default value
    println(obj) // Data(a=42, b="42")
}

main()

Is there a way to make this a bit more portable?

@holgerbrandl
Copy link
Collaborator

No clue. I've forwarded the question to the experts https://kotlinlang.slack.com/archives/C7A1U5PTM/p1593722985324800

@pvegh
Copy link

pvegh commented May 15, 2021

This doesn't seem to work anymore:
warning: the following compiler arguments are ignored on script compilation: -Xplugin
(also CompilerOpts isn't available, seems to have been renamed to CompilerOptions)

@jplewa
Copy link

jplewa commented Oct 19, 2022

Interesting! You are right -- on a closer look, I could get it to work, although it's a bit of a hack since I need to hardcode a path to the jar.
[...]
Is there a way to make this a bit more portable?

I managed to get this approach to work, thanks! I have one question, though. Since I'm hoping to make this script portable, I don't really want to hard-code my own home path. Is there any way to interpolate the user's home path in the annotation string?

The following actually does work:

@file:CompilerOpts("-Xplugin=$HOME/.m2/repository/org/jetbrains/kotlin/kotlin-maven-serialization/1.7.20/kotlin-maven-serialization-1.7.20.jar")

However, it makes IntelliJ really angry:
image

I also tried "-Xplugin=\$HOME/.m2...", "-Xplugin={{HOME}}/.m2..." (according to the README, that works with @file:Repository) and multiple other random things, but none of them work :(

Has anyone figured out to make this work without infuriating IntelliJ?

jplewa added a commit to VirtuslabRnD/pulumi-kotlin that referenced this issue Nov 4, 2022
## Task

Resolves: #44

## Description

All versions will now be configured in the
`src/main/resources/version-config.json` file. There are two release
paths:
1. If you want update provider schemas and build new versions of the
libraries, run the Gradle task `prepareReleaseOfUpdatedSchemas`.
2. If you want to release a new version of all libraries due to some
update in our generator, run the Gradle task
`prepareReleaseAfterGeneratorUpdate`.

These tasks will create a new commit which updates the versions in
`version-config.json` accordingly. Push this commit out to a new branch
and create a PR (like [this
one](#98)). Once this
PR is approved and merged, a GitHub Action will be triggered (like [this
one](https://github.com/VirtuslabRnD/pulumi-kotlin/actions/runs/3328060887)),
which will:
1. Tag the merge commit with the appropriate release versions.
2. Release the requested libraries to the
[Packages](https://github.com/orgs/VirtuslabRnD/packages?repo_name=pulumi-kotlin)
section on GitHub.
3. Fast-forward the released versions to SNAPSHOT versions and create a
PR (like [this
one](#99)). Once this
PR is approved and merged, the release cycle is complete.

In the future, the task `prepareReleaseOfUpdatedSchemas` could be run
automatically as a cron job. For now, it will need to be run manually by
one of the team members.

## Useful links

### Serialization in Gradle scripts
-
https://discuss.gradle.org/t/how-to-apply-kotlin-serialization-in-buildscript/41171
-
https://slack-chats.kotlinlang.org/t/544960/i-m-trying-to-upgrade-kotlin-to-1-7-0-and-i-m-no-longer-able

### Maven versioning
-
https://stackoverflow.com/questions/13004443/how-does-maven-sort-version-numbers/31482463#31482463

### KScript adventures
- kscripting/kscript#267
- kscripting/kscript#191

### Publishing libraries
-
https://docs.github.com/en/actions/publishing-packages/publishing-java-packages-with-gradle#publishing-packages-to-github-packages
- https://github.com/orgs/community/discussions/13836

### JGit
- https://www.nuxeo.com/blog/jgit-example/
@TJSomething
Copy link

TJSomething commented Oct 8, 2023

Has anyone figured out to make this work without infuriating IntelliJ?

I know it's pretty late, but I found that you could use $(echo ~), like so:

@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0", "org.jetbrains.kotlin:kotlin-maven-serialization:1.9.0")
@file:CompilerOptions("-Xplugin=$(echo ~)/.m2/repository/org/jetbrains/kotlin/kotlin-maven-serialization/1.9.0/kotlin-maven-serialization-1.9.0.jar")

It also works with --package while $HOME breaks that.

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

5 participants