michelou / kotlin-examples

Playing with Kotlin on Windows

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Playing with Kotlin on Windows

Kotlin project This repository gathers Kotlin code examples coming from various websites and books.
It also includes several build scripts (Bash scripts, batch files, Gradle scripts, Maven scripts) for experimenting with Kotlin on a Windows machine.

Ada, Akka, C++, Dart, Deno, Docker, Flix, Golang, GraalVM, Haskell, Kafka, LLVM, Modula-2, Node.js, Rust, Scala 3, Spark, Spring, TruffleSqueak and WiX Toolset are other topics we are continuously monitoring.

Project dependencies

This project depends on the following external software for the Microsoft Windows platform:

Optionally one may also install the following software:

For instance our development environment looks as follows (March 2024) 3:

C:\opt\apache-ant\                           ( 39 MB)
C:\opt\apache-maven\                         (  9 MB)
C:\opt\detekt-cli\                           ( 55 MB)
C:\opt\Git\                                  (367 MB)
C:\opt\gradle\                               (135 MB)
C:\opt\jdk-temurin-17.0.10_7\                (301 MB)
C:\opt\kotlinc-1.9.22\                       ( 83 MB)
C:\opt\kotlin-native-windows-x86_64-1.9.22\  (269 MB)
C:\opt\ktlint\                               ( 53 MB)
C:\opt\make-3.81\                            (  2 MB)

Installation policy
When possible we install software from a Zip archive rather than via a Windows installer. In our case we defined C:\opt\ as the installation directory for optional software tools (in reference to the /opt/ directory on Unix).

Directory structure

This project is organized as follows:

bin\
bin\kotlin\build.bat
concurrency-in-kotlin\{README.md}
docs\
examples\{README.md, HelloWorld, JavaToKoltin, ..}
how-to-kotlin\{README.md, 01_bean, 02_properties, ..}
kotlin\   (Github submodule)
kotlin-cookbook\{README.md, Example_03-10, Example_03-13, ..}
learn-kotlin\{README.md, Unit_02, Unit_04, ..}
CONTRIBUTIONS.md
README.md
RESOURCES.md
setenv.bat

where

We also define a virtual drive – e.g. drive I: – in our working environment in order to reduce/hide the real path of our project directory (see article "Windows command prompt limitation" from Microsoft Support).

🔎 We use the Windows external command subst to create virtual drives; for instance:

> subst I: %USERPROFILE%\workspace\kotlin-examples

In the next section we give a brief description of the batch files present in this project.

Batch commands

We execute command setenv.bat once to setup our development environment; it makes external tools such as git.exe, gradle.bat and sh.exe directly available from the command prompt.

> setenv -verbose
Tool versions:
   ant 1.10.14, bazel 7.0.2, gradle 8.6, java 17.0.10, detekt-cli 1.23.5,
   kotlinc 1.9.22, kotlinc-native 1.9.22, ktlint 1.2.1, cfr 0.152,
   make 3.81, mvn 3.9.6, git 2.44.0.windows.1, diff 3.10, bash 4.4.26(1)-release
Tool paths:
   C:\opt\apache-ant\bin\ant.bat
   C:\opt\bazel\bazel.exe
   C:\opt\gradle\bin\gradle.bat
   C:\opt\jdk-temurin-17.0.10_7\bin\java.exe
   C:\opt\detekt-cli\bin\detekt-cli.bat
   C:\opt\kotlinc-1.9.22\bin\kotlinc.bat
   C:\opt\kotlin-native-windows-x86_64-1.9.22\bin\kotlinc.bat
   C:\opt\kotlin-native-windows-x86_64-1.9.22\bin\kotlinc-native.bat
   C:\opt\ktlint\ktlint.bat
   C:\opt\cfr-0.152\bin\cfr.bat
   C:\opt\make-3.81\bin\make.exe
   C:\opt\apache-maven\bin\mvn.cmd
   C:\opt\Git\bin\git.exe
   C:\opt\Git\mingw64\bin\git.exe
   C:\opt\Git\usr\bin\diff.exe
Environment variables:
   "ANT_HOME=C:\opt\apache-ant"
   "CFR_HOME=C:\opt\cfr-0.152"
   "DETEKT_HOME=C:\opt\detekt-cli"
   "GIT_HOME=C:\opt\Git"
   "GRADLE_HOME=C:\opt\gradle"
   "JAVA_HOME=C:\opt\jdk-temurin-17.0.10_7"
   "KOTLIN_HOME=C:\opt\kotlinc-1.9.22"
   "KOTLIN_NATIVE_HOME=C:\opt\kotlinc-1.9.22"
   "KTLINT_HOME=C:\opt\ktlint"
   "MAKE_HOME=C:\opt\make-3.81"
   "MAVEN_HOME=C:\opt\apache-maven"

This batch command generates the Kotlin binary distribution on a Windows machine.

Footnotes

[1] Kotlin/Native

Kotlin/Native is an LLVM backend for the Kotlin compiler, runtime implementation, and native code generation facility using the LLVM toolchain.
Kotlin/NativeLLVM
1.9.2211.1.0
1.9.2111.1.0
1.9.2011.1.0
1.9.1011.1.0
1.9.011.1.0
1.8.2011.1.0
1.8.011.1.0
1.7.2011.1.0
1.6.011.1.0
1.3.608.0

[2] KtLint on Windows

No Windows distribution is available from the KtLint repository.
Fortunately the KtLint tool is packed into a shell script (i.e. embedded JAR file in binary form), so we simply extracted the JAR file to create a "universal" KtLint distribution (in the same way as the Mill assembly distribution):
  • we create an installation directory c:\opt\ktlint\bin.
  • we download the shell script from the Github repository pinterest/ktlint.
  • we extract the JAR file from the bash script (and check it with command jar tf).
  • we create batch file ktlint.bat from the binary concatenation of header file ktlint_header.bin and the extracted JAR file.
Here are the performed operations:
> mkdir c:\opt\ktlint\bin
> cd c:\opt\ktlint\bin
 
> curl -sL -o ktlint.sh https://github.com/pinterest/ktlint/releases/download/1.1.0/ktlint
> tail -n+5 ktlint.sh > ktlint.jar
> %JAVA_HOME%\bin\jar tf ktlint.jar | findstr ktlint/Main
com/pinterest/ktlint/Main.class
> copy /y /b i:\bin\ktlint_header.bin + /b ktlint.jar ktlint.bat
The installation directory now contains one single file, namely ktlint.bat:
> dir /b c:\opt\ktlint\bin
ktlint.bat
 
> c:\opt\ktlint\bin\ktlint.bat --version
1.2.1

[3] Downloads

In our case we downloaded the following installation files (see section 1):
apache-ant-1.10.14-bin.zip                        (  9 MB)
apache-maven-3.9.6-bin.zip                        (  9 MB)
detekt-cli-1.23.5.zip                             ( 54 MB)
gradle-8.6-bin.zip                                (115 MB)
kotlin-compiler-1.9.22.zip                        ( 71 MB)
kotlin-native-windows-x86_64-1.9.22.zip           (174 MB)
ktlint (1.2.1)                                    ( 63 MB)
make-3.81-bin.zip                                 ( 10 MB)
OpenJDK11U-jdk_x64_windows_hotspot_17.0.10_7.zip  ( 99 MB)
PortableGit-2.44.0-64-bit.7z.exe                  ( 43 MB)

mics/March 2024  

About

Playing with Kotlin on Windows