diff --git a/kotlin/.tool-versions b/kotlin/.tool-versions new file mode 100644 index 0000000..0ef52de --- /dev/null +++ b/kotlin/.tool-versions @@ -0,0 +1 @@ +java corretto-21 diff --git a/kotlin/README.md b/kotlin/README.md index 7b3ef4f..d654d70 100644 --- a/kotlin/README.md +++ b/kotlin/README.md @@ -1,40 +1,29 @@ -# Kotlin Pairing Test Project +# Kotlin -This is a skeleton project for the Guardian Kotlin [pair programming exercise](https://github.com/guardian/coding-exercises). +Skeleton project for Kotlin. -## Structure -- Code located in [`Main.kt`](./src/main/kotlin/com/gu/pairingtest/Main.kt) -- Tests located in [`MainTest.kt`](./src/test/kotlin/MainTest.kt) - -However, you're free to organise your code as you like. - -## Running in Android studio or IntelliJ IDEA -You should be able to import this project into Android studio or IntelliJ and click the play button next to each unit test to run the unit tests. If this is too slow or doesn't work try following the next section for setting up on the command line / any other IDE. - -## Using another IDE and running on the command line -If you'd like to use another IDE (e.g VSCode, Atom, Vim) you'll need to be able to run the unit tests via the command line. +## Prerequisites -To do this make sure you have the JDK installed and the JAVA_HOME environment variable set. -If on MacOS you can run `./script/setup` in a terminal to try to automate this process. -If this fails or you're on Windows you can [install the JDK manually](https://adoptium.net/) +The toolchain this project needs is declared in [`.tool-versions`](./.tool-versions): -Once the JDK is installed then: +``` +java corretto-21 +``` -If on MacOS / Linux open a terminal and run: -- `./script/setup` to make sure your JAVA_HOME is set -- `./script/test` to run the unit tests. -- `./script/start` to run the main function +Install those however you prefer, as long as they end up on your `PATH`. The quickest route is +[mise](https://mise.jdx.dev/), which reads the file for you: -If on Windows open Powershell and run: -- `./gradlew.bat test` to run the unit tests -- `./gradlew.bat run` to run the main function -- If these complain about JAVA_HOME not being set. Try running `$env:JAVA_HOME = Join-Path (Get-Command javac).path "../../" -Resolve` and running them again +```sh +mise install +``` -*Please note:* Unit tests will not output results if run twice without any test or code changes. See: https://blog.gradle.org/stop-rerunning-tests +## Usage +- `./script/setup` to download the Gradle distribution +- `./script/test` to run the tests, or `./script/test --watch` to re-run them on every change +- `./script/start` to run the app -## Manual setup - -If these scripts fail and you can't open the project in Android Studio, you may have to [install the JDK manually](https://adoptium.net/). JDK versions 8 and above should work. - -Once you've installed the JDK and set the JAVA_HOME environment variable to the location of your JDK install, then try running `./gradlew test` which will use the Gradle wrapper script to download Gradle and run the tests. If this fails [download gradle manually](https://gradle.org/install/) and run `gradle test`. +## Structure +- Code located in [`Main.kt`](./src/main/kotlin/com/gu/pairingtest/Main.kt) +- Tests located in [`MainTest.kt`](./src/test/kotlin/MainTest.kt) +However, you're free to organise your code as you like. diff --git a/kotlin/script/setup b/kotlin/script/setup index 9fa76d6..6b9582d 100755 --- a/kotlin/script/setup +++ b/kotlin/script/setup @@ -1,42 +1,7 @@ #!/usr/bin/env bash -set -e +set -euo pipefail -# If JAVA_HOME not set then search for jdk to set JAVA_HOME to -# This includes the JDK integrated into Android studio -if [[ -z "$JAVA_HOME" ]]; then - echo "JAVA_HOME not set" - android_studio_path="/Applications/Android Studio.app/Contents" - system_java_home=$(/usr/libexec/java_home) - potential_java_homes=("$JAVA_HOME" "$system_java_home" "$android_studio_path/jre/Contents/Home" "$android_studio_path/jre/jdk/Contents/Home") - for potential_java_home in "${potential_java_homes[@]}" - do - if [[ -f "$potential_java_home/bin/javac" ]]; then - echo "Found JDK at $potential_java_home" - JAVA_HOME=$potential_java_home - fi - done -fi +cd "$(dirname "${BASH_SOURCE[0]}")/.." -# If we still can't find a JDK on JAVA_HOME then ask user to install using homebrew -if [[ -z "$JAVA_HOME" ]]; then - echo "Failed to find JDK as no JAVA_HOME is set" - read -p "Install adoptopenjdk11 using homebrew? [y/n] " -n 1 -r - echo "" - if [[ $REPLY =~ ^[Yy]$ ]]; then - brew install adoptopenjdk11 - JAVA_HOME=$(/usr/libexec/java_home) - echo "Setting JAVA_HOME to $JAVA_HOME" - else - echo "Install JDK and set JAVA_HOME manually." - fi - exit -fi - -# Tell user to export JAVA_HOME to prevent setup script running again -if [[ ! -z "$JAVA_HOME" ]]; then - echo "To prevent setup script running again run:" - echo "" - echo "export JAVA_HOME=\"$JAVA_HOME\"" - echo "" -fi +./gradlew --version