Skip to content

Commit

Permalink
Merge pull request #240 from codecrafters-io/add-kotlin
Browse files Browse the repository at this point in the history
add kotlin, update templates
  • Loading branch information
rohitpaulk authored Jul 16, 2024
2 parents 1c4be78 + be06b07 commit 07f3658
Show file tree
Hide file tree
Showing 50 changed files with 702 additions and 27 deletions.
5 changes: 4 additions & 1 deletion compiled_starters/c/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
gcc -o /tmp/codecrafters-build-redis-c app/*.c
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
gcc -o /tmp/codecrafters-build-redis-c app/*.c
)

# Copied from .codecrafters/run.sh
#
Expand Down
7 changes: 5 additions & 2 deletions compiled_starters/clojure/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
lein deps
lein uberjar
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
lein deps
lein uberjar
)

# Copied from .codecrafters/run.sh
#
Expand Down
7 changes: 5 additions & 2 deletions compiled_starters/cpp/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
cmake --build ./build
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake
cmake --build ./build
)

# Copied from .codecrafters/run.sh
#
Expand Down
5 changes: 4 additions & 1 deletion compiled_starters/crystal/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
crystal build -o /tmp/codecrafters-build-redis-crystal app/main.cr
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
crystal build -o /tmp/codecrafters-build-redis-crystal app/main.cr
)

# Copied from .codecrafters/run.sh
#
Expand Down
5 changes: 4 additions & 1 deletion compiled_starters/csharp/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
dotnet build --configuration Release --output /tmp/codecrafters-build-redis-csharp codecrafters-redis.csproj
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
dotnet build --configuration Release --output /tmp/codecrafters-build-redis-csharp codecrafters-redis.csproj
)

# Copied from .codecrafters/run.sh
#
Expand Down
5 changes: 4 additions & 1 deletion compiled_starters/go/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
go build -o /tmp/codecrafters-build-redis-go app/*.go
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
go build -o /tmp/codecrafters-build-redis-go app/*.go
)

# Copied from .codecrafters/run.sh
#
Expand Down
5 changes: 4 additions & 1 deletion compiled_starters/haskell/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
stack build
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
stack build
)

# Copied from .codecrafters/run.sh
#
Expand Down
5 changes: 4 additions & 1 deletion compiled_starters/java/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
mvn -B package -Ddir=/tmp/codecrafters-build-redis-java
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
mvn -B package -Ddir=/tmp/codecrafters-build-redis-java
)

# Copied from .codecrafters/run.sh
#
Expand Down
11 changes: 11 additions & 0 deletions compiled_starters/kotlin/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to compile your program on CodeCrafters
#
# This runs before .codecrafters/run.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

mvn -B package -Ddir=/tmp/codecrafters-build-dir
11 changes: 11 additions & 0 deletions compiled_starters/kotlin/.codecrafters/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to run your program on CodeCrafters
#
# This runs after .codecrafters/compile.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

exec java -jar /tmp/codecrafters-build-dir/build-your-own-redis.jar "$@"
1 change: 1 addition & 0 deletions compiled_starters/kotlin/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
2 changes: 2 additions & 0 deletions compiled_starters/kotlin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.idea/
35 changes: 35 additions & 0 deletions compiled_starters/kotlin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png)

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 try the challenge.

# 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 (>=2.0)` installed locally
1. Run `./your_program.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/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-2.0
language_pack: kotlin-2.0
95 changes: 95 additions & 0 deletions compiled_starters/kotlin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.codecrafters</groupId>
<artifactId>build-your-own-redis</artifactId>
<version>1.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.jvmTarget>21</kotlin.compiler.jvmTarget>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<java.version>21</java.version>
</properties>

<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-bom</artifactId>
<version>2.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>


<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>2.0.0</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}</finalName> <!-- Please do not change this final artifact name-->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- This is the main class of your program which will be executed-->
<mainClass>MainKt</mainClass>
</manifest>
</archive>
<outputDirectory>${dir}</outputDirectory>
</configuration>

<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>
</dependencies>
</project>
16 changes: 16 additions & 0 deletions compiled_starters/kotlin/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.net.ServerSocket

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

// Uncomment this block to pass the first stage
// var serverSocket = ServerSocket(6379)
//
// // Since the tester restarts your program quite often, setting SO_REUSEADDR
// // ensures that we don't run into 'Address already in use' errors
// serverSocket.reuseAddress = true
//
// serverSocket.accept() // Wait for connection from client.
// println("accepted new connection")
}
24 changes: 24 additions & 0 deletions compiled_starters/kotlin/your_program.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh
#
# Use this script to run your program LOCALLY.
#
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit early if any commands fail

# Copied from .codecrafters/compile.sh
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
mvn -B package -Ddir=/tmp/codecrafters-build-dir
)

# Copied from .codecrafters/run.sh
#
# - Edit this to change how your program runs locally
# - Edit .codecrafters/run.sh to change how your program runs remotely
exec java -jar /tmp/codecrafters-build-dir/build-your-own-redis.jar "$@"
5 changes: 4 additions & 1 deletion compiled_starters/rust/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
cargo build --release --target-dir=/tmp/codecrafters-redis-target --manifest-path Cargo.toml
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
cargo build --release --target-dir=/tmp/codecrafters-redis-target --manifest-path Cargo.toml
)

# Copied from .codecrafters/run.sh
#
Expand Down
5 changes: 4 additions & 1 deletion compiled_starters/scala/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
sbt assembly
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
sbt assembly
)

# Copied from .codecrafters/run.sh
#
Expand Down
5 changes: 4 additions & 1 deletion compiled_starters/zig/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
zig build
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
zig build
)

# Copied from .codecrafters/run.sh
#
Expand Down
2 changes: 1 addition & 1 deletion course-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ languages:
- slug: "haskell"
- slug: "java"
- slug: "javascript"
- slug: "kotlin"
- slug: "php"
- slug: "python"
- slug: "ruby"
- slug: "rust"
- slug: "typescript"
- slug: "scala"
release_status: "beta"
- slug: "zig"

marketing:
Expand Down
16 changes: 16 additions & 0 deletions dockerfiles/kotlin-2.0.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# syntax=docker/dockerfile:1.7-labs
FROM maven:3.9.8-eclipse-temurin-22-alpine

# Ensures the container is re-built if pom.xml changes
ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="pom.xml"

WORKDIR /app

# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses
COPY --exclude=.git --exclude=README.md . /app

# Cache dependencies
RUN mvn -B package -Ddir=/tmp/codecrafters-build-dir

# Once the heavy steps are done, we can copy all files back
COPY . /app
5 changes: 4 additions & 1 deletion solutions/c/01-jm1/code/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program compiles locally
# - Edit .codecrafters/compile.sh to change how your program compiles remotely
gcc -o /tmp/codecrafters-build-redis-c app/*.c
(
cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory
gcc -o /tmp/codecrafters-build-redis-c app/*.c
)

# Copied from .codecrafters/run.sh
#
Expand Down
Loading

0 comments on commit 07f3658

Please sign in to comment.