<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="https://angrynerds.co/blog/rss/xslt"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Blog</title>
    <link>https://angrynerds.co/blog</link>
    <description />
    <generator>Articulate, blogging built on Umbraco</generator>
    <item>
      <guid isPermaLink="false">4607</guid>
      <link>https://angrynerds.co/blog/how-to-write-and-run-kotlin-script/</link>
      <category>Business</category>
      <category>Culture</category>
      <title>How to quickly write and run Kotlin script - practical guide</title>
      <description>&lt;p&gt;Recently, we completed a test with a large address book containing thousands of contacts. To have such a vast collection, we needed to generate a vCard file with random contacts.
​&lt;/p&gt;
&lt;p&gt;The easiest way to execute Kotlin code quickly in Android Studio project is by creating an empty Kotlin file, and typing &lt;code&gt;psvm&lt;/code&gt; to auto-generate main function (or &lt;code&gt;psvma&lt;/code&gt; for one with args). Inside it, we can write our Kotlin code. Last step is to click the green run button next to the main and viola — code is executed with the output presented.
​&lt;/p&gt;
&lt;p&gt;Since parameters should always be given via altering configuration in the &amp;quot;Program Arguments&amp;quot; area, it is not a simple or easily reproducible setup from my perspective.
​&lt;/p&gt;
&lt;p&gt;I came to the conclusion that it will be easier to have it in the form of an executable script from the command line. As Kotlin has that ability with &lt;code&gt;.kts&lt;/code&gt; (Kotlin script) files, I started looking for ways to achieve it in the fastest and simplest way possible. &lt;/p&gt;
&lt;p&gt;Following is the summary of my discoveries:&lt;/p&gt;
&lt;h2&gt;Setup&lt;/h2&gt;
&lt;p&gt;• What you need is &lt;code&gt;kotlin compiler&lt;/code&gt; in order to run our script from command line.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    brew update
    brew install kotlin
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;• As I’m using mac and brew, this command was the simplest to get, but if you have a different hardware/software setup, use these &lt;a href="https://kotlinlang.org/docs/command-line.html#install-the-compiler"&gt;instructions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;• Text editor - I’ve just used Android Studio, but anything that understands Kotlin will be enough.&lt;/p&gt;
&lt;h2&gt;Script&lt;/h2&gt;
&lt;p&gt;There are several ways of creating and running scripts with Kotlin. The easiest one to use is called &lt;strong&gt;Kotlin-main-kts&lt;/strong&gt;. &lt;/p&gt;
&lt;p&gt;Let’s create the easiest hello-world script with it:&lt;/p&gt;
&lt;p&gt;• create file with &lt;code&gt;.main.kts&lt;/code&gt; extension e.g. &lt;code&gt;helloworld.main.kts&lt;/code&gt; &lt;/p&gt;
&lt;p&gt;• add “header” as a first line to indicate it should be our Kotlin script &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;   #!/usr/bin/env kotlin
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;
• below it insert your script content e.g. &lt;code&gt;println(&amp;quot;Hello world.&amp;quot;)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;• make your file executable with &lt;code&gt;chmod +x helloworld.main.kts&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;• now you can run your script with standard command of running executable files &lt;code&gt;./hellowrold.main.kts&lt;/code&gt; — &lt;strong&gt;remember that in order to run it, other people will also need to install Kotlin compiler&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;Arguments&lt;/h2&gt;
&lt;p&gt;Arguments are accessible right away by calling &lt;code&gt;args&lt;/code&gt; e.g. &lt;code&gt;args[0]&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;No main function needed as we’re inside of it. So all code is wrote top-level like.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if (args.size != 2) {
    println(&amp;quot;Please specify exactly 2 arguments in this example&amp;quot;)
} else {
    println(&amp;quot;Your arguments are: ${args[0]}, ${args[1]}&amp;quot;)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt;Now run it with: &lt;code&gt;./helloworld.main.kts “Hello” “World”&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Which is equivalent to: &lt;code&gt;kotlinc -script helloworld.main.kts &amp;quot;Hello&amp;quot; &amp;quot;World&amp;quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;That’s it - quick and easy to use or run.&lt;/p&gt;
&lt;p&gt;For a curious real-life yet very basic example here’s our vCard file generating script:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#!/usr/bin/env kotlin

import java.io.File

fun generateVCard(numberOfContacts: Int): String {
    val vCard: StringBuilder = StringBuilder()
    (1..numberOfContacts).forEach { number -&amp;gt;
        vCard.append(
            &amp;quot;&amp;quot;&amp;quot;
            BEGIN:VCARD
            VERSION:3.0
            PRODID:-//Apple Inc.//macOS 12.4//EN
            N:Surname$number;Name$number;;;
            FN:Name$number Surname$number
            TEL;type=CELL;type=VOICE;type=pref:+48 101 000 $number
            END:VCARD

            &amp;quot;&amp;quot;&amp;quot;.trimIndent()
        )
    }
    return vCard.toString()
}

if (args.size != 1) {
    println(&amp;quot;No argument. Please specify number and try again&amp;quot;)
} else {
    val numberOfContacts = args[0].toIntOrNull()

    if (numberOfContacts == null) {
        println(&amp;quot;Specified number is not Integer, please try again&amp;quot;)
    } else {
        println(&amp;quot;Executing script&amp;quot;)

        val vCard = generateVCard(numberOfContacts)

        File(&amp;quot;contacts$numberOfContacts.vcf&amp;quot;).writeText(vCard)

        println(&amp;quot;Generating vCard completed.&amp;quot;)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;br /&gt; &lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;strong&gt;About the Author&lt;/strong&gt;&lt;br /&gt; &lt;/p&gt;
&lt;p&gt;&lt;a href="https://www.linkedin.com/in/marczewski-krzysztof/"&gt;Krzysztof Marczewski&lt;/a&gt; is an Android Developer at Angry Nerds, focused on developing great code and constantly growing his tech skills.
Delighted with Kotlin, Compose, and multiplatform. Non-fiction bookworm who loves minimal UI, smart UX, and clever copywriting. He writes his development-related blog - &lt;a href="https://selfformat.com/"&gt;https://selfformat.com/&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Tue, 30 Aug 2022 15:47:29 Z</pubDate>
      <a10:updated>2022-08-30T15:47:29Z</a10:updated>
    </item>
  </channel>
</rss>