yes, kotlin scripting can do that
# announcements
n
yes, kotlin scripting can do that
l
Okay, but how?
Example of a command run from gradle:
def gitTag = 'git describe --dirty'.execute().text.trim()
n
i made a small example project for @amanda.hinchman-dominguez here: https://github.com/NikkyAI/MinimalScriptingExample it basically just reads from a file and created the Source input to the script evaluation fro mthat
💯 1
this is the most important part: https://github.com/NikkyAI/MinimalScriptingExample/blob/master/src/main/kotlin/example/Main.kt#L68 the rest is just input and/or interpreting the output
i also linked this in #C0BT46EL8
l
Hum, I think there's a misunderstanding. I just want to execute a string as a command (shell/bash/cmd) from a Kotlin script. It is a one liner in groovy based gradle, I'm just looking for something similar without gradle
(see my snippet in my previous message)
n
oh you mean execute them in the underlying shell ?
l
Yes
n
i have that somewhere too... let me dig that out
a
It's dope I can attest :) I actually just dropped my family off at the airport so I am free again haha
n
Copy code
private fun String.runCommand(workingDir: File = cacheHome): String {
        try {
            val parts = this.split("\\s".toRegex())
            val proc = ProcessBuilder(*parts.toTypedArray())
                .directory(workingDir)
                .redirectOutput(ProcessBuilder.Redirect.PIPE)
                .redirectError(ProcessBuilder.Redirect.PIPE)
                .start()

            proc.waitFor(60, TimeUnit.MINUTES)
            return proc.inputStream.bufferedReader().readText()
        } catch (e: IOException) {
            e.printStackTrace()
            throw Exception("cannot execute '$this'")
        }
    }
l
@amanda.hinchman-dominguez not sure I understand your message in this context… did you post in the right thread?
n
she is refering to the scripting..
which was a misunderstanding on my part.. i thought you wanted to evaluate kotlin within kotlin
l
Which is dope 😉
n
i sent this snippet with proper highlighting o #C0922A726 too
a
Yep! Referring to the scripting!
l
I understand now 🙂
Thanks for the snippet, will be useful!
a
It's seriously nice to look at projects that is clean and so minimal it's all you have to worry about. I think @Nikky made a great contribution to documentation with her expertise on scripting. Without it I would have had no idea where to start
l
About my misunderstanding, I also didn't notice at first Nikky mentioned Amanda. I should go to sleep now…
Since we're talking scripting, what do you use Kotlin scripting for?
I personally used it to migrate android projects from support libraries to AndroidX, migrate groovy-based Gradle to Gradle kts, have gradle module templates to make modularization faster and more convenient, and now, I'm making library release more automated (to replace a 19 manual steps process written in a markdown file)
a
Oh... Ha hahaha... I'm using it in a way that is probably not recommended. I want to see if I can use scripting to read uploaded kt files and compile it on the fly so I can use reflection at runtime
@louiscad "Abusive scripting"
l
It all started a few weeks ago, when Android Studio refactor to AndroidX option didn't work for my project and I was like: Nope, I won't do it by hand and I won't wait for this slow thing to maybe become bugless
n
i use it to make writing configuration / input for my program easier than yaml or json
and kotlin scripting when setup correctly gets highlighting in idea when a jar contains the correct stuff in MET-INF etc is in the classpath
a
I actually have a bug I want to share on scripting tonight - all the cool uses though!! I love how scripting helps everyone in such different ways
n
i see quite a bit of possibilities regarding #C4W52CFEZ and testing
👍 1
a
@louiscad you should def look at @Nikky 's example then. While I am not using it at the moment looking at that plus the Gradle kotlin documentation made it v easy to understand. Again, it's awesome!
l
@Nikky My current IDE highlighting setup is to have the files named
*.gradle.kts
. It is the easiest option to me for now, but I didn't explore other options
a
I loved the configuration folder too, idk if it's standard practice but it should be
n
the trick with kotlin scripting is that it also gives you the highlighting of the script base class.. which is fully under your control
l
@amanda.hinchman-dominguez What is the configuration folder?
n
but for gradle this is not necessary..
i think she means
buildSrc
a
Whoops
buildSrc
It makes gradle dependencies look really clean!
l
I used
buildSrc
for dependencies and versions sync. Do you use it for other usages?
n
well its great for prototyping small plugins
i made one that automatically counts up build numbers based on a key, make that key the first part of the version and you got a buildnumber thats resets on version bumps
l
I didn't make any gradle plugins yet. For my releasing script, I was considering making it a gradle task, but I then realized I could not make it interactive and get user input when running…
n
or plugins to generate kotlin src.. for compiling constants into the program.. like version numbers etc
well it can be a gradle task doing all the hard work and something else collecting user input and sending it to the task..
not sure how well gradle handled being interactive during a task, possibly works if you open a swing / torndaofx gui instead of the using the same console
l
Yes, but there is no hard work. It's just following these simple steps: https://github.com/LouisCAD/Splitties/blob/master/RELEASING.md#releasing-a-stable-beta-or-alpha-version
a
@louiscad what do you mean by interactive?
n
what i do is ... i check if
-Prelease
was passed to gradlew
otherwise i check if i am in a CI environment
and based on that i append -SNAPSHOT or similar to the version that isbuild and pushed to maven
l
@amanda.hinchman-dominguez In the link just above, there are steps where I want to open a file in the IDE, let the user (myself in this case) edit the file, then continue, ask for the version number, ask when packages are released on bintray…
n
i would recommend using a plugin there that makes use of kotlin poet to write these constants into a file
or gradle.properties
l
gradle.properties could be good too, yes. Not sure if it's better than the constant in buildSrc though
n
this file: https://github.com/LouisCAD/Splitties/blob/master/buildSrc/src/main/kotlin/ProjectVersions.kt would be trivial to generate by a plugin and in a step before compile
l
Sure, I can also find and replace. I was also thinking about doing the same for the README
its a super simple plugin... just 3 files for the plugin, extension and task
l
Note that my goal is to have it in a single file so I can comfortably copy paste it to other open source projects
n
well thats why i need to make it into its own project so it can just be applied as a gradle plugin
👍 2
and this can be put into a single file.. 3 classes per file should work fine