Skip to content
This repository was archived by the owner on Oct 7, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
language: ["c", "clojure", "crystal", "csharp", "elixir", "go", "haskell", "java", "javascript", "php", "python", "ruby", "rust"]
language: ["c", "clojure", "crystal", "csharp", "elixir", "go", "haskell", "java", "javascript", "kotlin", "php", "python", "ruby", "rust"]

steps:
- uses: actions/setup-ruby@v1
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ test_redis_rust: download_starter_testers
bundle exec ruby template_compiler/compile.rb redis rust
bundle exec ruby tests/test_course.rb redis rust

test_redis_kotlin: download_starter_testers
bundle exec ruby template_compiler/compile.rb redis kotlin
bundle exec ruby tests/test_course.rb redis kotlin

test_sqlite_python: download_starter_testers
bundle exec ruby template_compiler/compile.rb sqlite python
bundle exec ruby tests/test_course.rb sqlite python
Expand Down
4 changes: 2 additions & 2 deletions compiled_starters/git-starter-kotlin/codecrafters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ debug: false
# Use this to change the Kotlin version used to run your code
# on Codecrafters.
#
# Available versions: kotlin-1.4
language_pack: kotlin-1.4
# Available versions: kotlin-1.4, kotlin-1.6.21
language_pack: kotlin-1.6.21
5 changes: 2 additions & 3 deletions compiled_starters/git-starter-kotlin/your_git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ jarFile="$HOME/.cache/codecrafters-git-kotlin.jar"

# Kotlin doesn't do incremental compilation, so let's hack this in ourselves.
previousHash=$(cat "$cacheFile" 2>/dev/null || echo '')
currentHash=$(tar -cP "$(dirname "$0")/app" | openssl sha1)
currentHash=$(tar -c "$(dirname "$0")/app" | sha1sum)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the commit -P isn't present in alpine tar and we don't need it (we can't silence the warning, though). OpenSSL is missing by default, but if it's really better we could install it, only ~1mb.


if [ "$previousHash" != "$currentHash" ]
then
# The JAVA_OPTS here prevents a warning on 1.4, see: https://youtrack.jetbrains.com/issue/KT-43704#focus=Comments-27-4625141.0-0
JAVA_OPTS="--add-opens java.base/java.util=ALL-UNNAMED" kotlinc "$(dirname $0)/app/main.kt" -include-runtime -d "$jarFile"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the new compiler, there is no longer such option, and the bug was fixed

kotlinc "$(dirname $0)/app/main.kt" -include-runtime -d "$jarFile"
echo "$currentHash" > "$cacheFile"
fi

Expand Down
33 changes: 33 additions & 0 deletions compiled_starters/redis-starter-kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
This is a starting point for Kotlin solutions to the
["Build Your Own Redis" Challenge](https://codecrafters.io/challenges/redis).

In this challenge, you'll build a toy Redis clone that's capable of handling
basic commands like `PING`, `SET` and `GET`. Along the way we'll learn about
event loops, the Redis protocol and more.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to signup for early access.

# Passing the first stage

The entry point for your Redis implementation is in `src/main/kotlin/Main.kt`.
Study and uncomment the relevant code, and push your changes to pass the first
stage:

```sh
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

That's all!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `kotlin` installed locally
1. Run `./spawn_redis_server.sh` to run your Redis server, which is implemented
in `src/main/kotlin/Main.kt`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
11 changes: 11 additions & 0 deletions compiled_starters/redis-starter-kotlin/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the Kotlin version used to run your code
# on Codecrafters.
#
# Available versions: kotlin-1.4, kotlin-1.6.21
language_pack: kotlin-1.6.21
22 changes: 22 additions & 0 deletions compiled_starters/redis-starter-kotlin/spawn_redis_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
set -e

mkdir -p "$HOME/.cache"
cacheFile="$HOME/.cache/codecrafters-git-kotlin-hash"
jarFile="$HOME/.cache/codecrafters-git-kotlin.jar"

# Kotlin doesn't do incremental compilation, so let's hack this in ourselves.
previousHash=$(cat "$cacheFile" 2>/dev/null || echo '')
currentHash=$(tar -c "$(dirname "$0")/src" | sha1sum)

if [ "$previousHash" != "$currentHash" ]
then
kotlinc src/main/kotlin/Main.kt -include-runtime -d "$jarFile"
echo "$currentHash" > "$cacheFile"
fi

exec kotlin -classpath "$jarFile" MainKt "$@"
16 changes: 16 additions & 0 deletions compiled_starters/redis-starter-kotlin/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.net.ServerSocket

const val PORT = 6379;

fun main() {
// You can use print statements as follows for debugging, they'll be visible when running tests.
println("Logs from your program will appear here!")

// Uncomment this block to pass the first stage
// ServerSocket(PORT).apply {
// reuseAddress = true
// }.use { serverSocket ->
// // Wait for connection from client.
// val clientSocket = serverSocket.accept()
// };
}
13 changes: 13 additions & 0 deletions dockerfiles/git/kotlin-1.6.21.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM openjdk:17-jdk-alpine

WORKDIR /usr/lib

# kotlinc wants bash for some reason
RUN apk add --no-cache bash && \
wget -q https://github.com/JetBrains/kotlin/releases/download/v1.6.21/kotlin-compiler-1.6.21.zip && \
unzip kotlin-compiler-*.zip && \
rm kotlin-compiler-*.zip

ENV PATH $PATH:/usr/lib/kotlinc/bin

CMD ["kotlinc"]
13 changes: 13 additions & 0 deletions dockerfiles/redis/kotlin-1.6.21.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM openjdk:17-jdk-alpine

WORKDIR /usr/lib

# kotlinc wants bash for some reason
RUN apk add --no-cache bash && \
wget -q https://github.com/JetBrains/kotlin/releases/download/v1.6.21/kotlin-compiler-1.6.21.zip && \
unzip kotlin-compiler-*.zip && \
rm kotlin-compiler-*.zip

ENV PATH $PATH:/usr/lib/kotlinc/bin

CMD ["kotlinc"]
2 changes: 1 addition & 1 deletion docs/adding_support_for_a_new_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ send us a PR, like [this one](https://github.com/codecrafters-io/languages/pull/
We have extensive CI tooling available that'll test your changes and make sure they're compatible. These
tests run when you open a PR.

Once you're PR is approved and merged, we'll then activate the language on [app.codecrafters.io](https://app.codecrafters.io/)
Once your PR is approved and merged, we'll then activate the language on [app.codecrafters.io](https://app.codecrafters.io/)
for you to try out.

If you get stuck anywhere, feel free to ping @rohitpaulk in Discord! Use the `#contributors`
Expand Down
4 changes: 2 additions & 2 deletions starter_templates/codecrafters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ language_pack: haskell-8.8
language_pack: elixir-1.10
{{/ language_is_elixir }}
{{# language_is_kotlin }}
# Available versions: kotlin-1.4
language_pack: kotlin-1.4
# Available versions: kotlin-1.4, kotlin-1.6.21
language_pack: kotlin-1.6.21
{{/ language_is_kotlin }}
{{# language_is_java }}
# Available versions: java-1.8
Expand Down
5 changes: 2 additions & 3 deletions starter_templates/git/kotlin/your_git.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ jarFile="$HOME/.cache/codecrafters-git-kotlin.jar"

# Kotlin doesn't do incremental compilation, so let's hack this in ourselves.
previousHash=$(cat "$cacheFile" 2>/dev/null || echo '')
currentHash=$(tar -cP "$(dirname "$0")/app" | openssl sha1)
currentHash=$(tar -c "$(dirname "$0")/app" | sha1sum)

if [ "$previousHash" != "$currentHash" ]
then
# The JAVA_OPTS here prevents a warning on 1.4, see: https://youtrack.jetbrains.com/issue/KT-43704#focus=Comments-27-4625141.0-0
JAVA_OPTS="--add-opens java.base/java.util=ALL-UNNAMED" kotlinc "$(dirname $0)/app/main.kt" -include-runtime -d "$jarFile"
kotlinc "$(dirname $0)/app/main.kt" -include-runtime -d "$jarFile"
echo "$currentHash" > "$cacheFile"
fi

Expand Down
22 changes: 22 additions & 0 deletions starter_templates/redis/kotlin/spawn_redis_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
set -e

mkdir -p "$HOME/.cache"
cacheFile="$HOME/.cache/codecrafters-git-kotlin-hash"
jarFile="$HOME/.cache/codecrafters-git-kotlin.jar"

# Kotlin doesn't do incremental compilation, so let's hack this in ourselves.
previousHash=$(cat "$cacheFile" 2>/dev/null || echo '')
currentHash=$(tar -c "$(dirname "$0")/src" | sha1sum)

if [ "$previousHash" != "$currentHash" ]
then
kotlinc src/main/kotlin/Main.kt -include-runtime -d "$jarFile"
echo "$currentHash" > "$cacheFile"
fi

exec kotlin -classpath "$jarFile" MainKt "$@"
16 changes: 16 additions & 0 deletions starter_templates/redis/kotlin/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.net.ServerSocket

const val PORT = 6379;

fun main() {
// You can use print statements as follows for debugging, they'll be visible when running tests.
println("Logs from your program will appear here!")

// Uncomment this block to pass the first stage
// ServerSocket(PORT).apply {
// reuseAddress = true
// }.use { serverSocket ->
// // Wait for connection from client.
// val clientSocket = serverSocket.accept()
// };
}
14 changes: 14 additions & 0 deletions template_compiler/definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@
"user_editable_file": "app/main.js"
}
),
Repo.new(
course: redis,
language: kotlin,
file_mappings: [
FM.new("README.md", "redis/README.md"),
FM.new("codecrafters.yml", "codecrafters.yml"),
FM.new("src/main/kotlin/Main.kt", "redis/kotlin/src/main/kotlin/Main.kt"),
FM.new("spawn_redis_server.sh", "redis/kotlin/spawn_redis_server.sh", is_executable=true),
],
template_attrs: {
"required_executable": "kotlin",
"user_editable_file": "src/main/kotlin/Main.kt"
}
),

# ------------------- DOCKER ------------------------------

Expand Down