Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ocaml support #243

Merged
merged 2 commits into from
Aug 7, 2024
Merged
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
11 changes: 11 additions & 0 deletions compiled_starters/ocaml/.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

dune build --build-dir /tmp/codecrafters-build-redis-ocaml
11 changes: 11 additions & 0 deletions compiled_starters/ocaml/.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 /tmp/codecrafters-build-redis-ocaml/default/main.exe "$@"
1 change: 1 addition & 0 deletions compiled_starters/ocaml/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
1 change: 1 addition & 0 deletions compiled_starters/ocaml/.ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
profile = default
34 changes: 34 additions & 0 deletions compiled_starters/ocaml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png)

This is a starting point for OCaml 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.ml`. 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 `dune` installed locally
1. Run `./your_program.sh` to run your Redis server, which is implemented in
`src/main.ml`.
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/ocaml/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 OCaml version used to run your code
# on Codecrafters.
#
# Available versions: ocaml-5.2.0
language_pack: ocaml-5.2.0
27 changes: 27 additions & 0 deletions compiled_starters/ocaml/codecrafters_redis.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.1"
synopsis: "Starter code for the Build your own Redis challenge"
maintainer: ["CodeCrafters <hello@codecrafters.io>"]
authors: ["CodeCrafters <hello@codecrafters.io>"]
license: "MIT"
homepage: "https://codecrafters.io"
bug-reports: "https://github.com/codecrafters-io/build-your-own-redis/issues"
depends: [
"dune" {>= "3.16"}
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
5 changes: 5 additions & 0 deletions compiled_starters/ocaml/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(include_subdirs unqualified)

(executable
(name main)
(libraries unix))
17 changes: 17 additions & 0 deletions compiled_starters/ocaml/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(lang dune 3.16)

(name codecrafters_redis)

(generate_opam_files true)

(package
(name codecrafters_redis)
(maintainers "CodeCrafters <hello@codecrafters.io>")
(authors "CodeCrafters <hello@codecrafters.io>")
(synopsis "Starter code for the Build your own Redis challenge")
(homepage "https://codecrafters.io")
(bug_reports
"https://github.com/codecrafters-io/build-your-own-redis/issues")
(license "MIT")
(allow_empty true)
(version 0.1))
16 changes: 16 additions & 0 deletions compiled_starters/ocaml/src/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
open Unix

let () =
(* You can use print statements as follows for debugging, they'll be visible when running tests. *)
Printf.eprintf "Logs from your program will appear here!\n";

(* Create a TCP server socket *)
let server_socket = socket PF_INET SOCK_STREAM 0 in
setsockopt server_socket SO_REUSEADDR true;
bind server_socket (ADDR_INET (inet_addr_of_string "127.0.0.1", 6379));
listen server_socket 1;

(* Uncomment this block to pass the first stage *)
(* let (client_socket, _) = accept server_socket in *)
(* close client_socket; *)
(* close server_socket *)
24 changes: 24 additions & 0 deletions compiled_starters/ocaml/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
dune build --build-dir /tmp/codecrafters-build-redis-ocaml
)

# 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 /tmp/codecrafters-build-redis-ocaml/default/main.exe "$@"
32 changes: 32 additions & 0 deletions dockerfiles/ocaml-5.2.0.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# syntax=docker/dockerfile:1.7-labs
FROM ocaml/opam:alpine-3.20-ocaml-5.2

# The image uses opam as the user, so let's set OPAMROOT to re-use whatever is already built
ENV OPAMROOT /home/opam/.opam

# Ensures the container is re-built if dune/dune-project changes
ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="dune,dune-project"

# Change to root user. All other images seem to use root, so let's do the same here
# hadolint ignore=DL3002
USER root

# Install dune
RUN opam update && opam install dune.3.16.0 --yes

# Dune path is /home/opam/.opam/5.2/bin/dune
ENV PATH="${OPAMROOT}/5.2/bin:${PATH}"

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 opam install . --yes

# This runs dune build
RUN .codecrafters/compile.sh

# Once the heavy steps are done, we can copy all files back
COPY . /app
11 changes: 11 additions & 0 deletions solutions/ocaml/01-jm1/code/.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

dune build --build-dir /tmp/codecrafters-build-redis-ocaml
11 changes: 11 additions & 0 deletions solutions/ocaml/01-jm1/code/.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 /tmp/codecrafters-build-redis-ocaml/default/main.exe "$@"
1 change: 1 addition & 0 deletions solutions/ocaml/01-jm1/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
1 change: 1 addition & 0 deletions solutions/ocaml/01-jm1/code/.ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
profile = default
34 changes: 34 additions & 0 deletions solutions/ocaml/01-jm1/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png)

This is a starting point for OCaml 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.ml`. 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 `dune` installed locally
1. Run `./your_program.sh` to run your Redis server, which is implemented in
`src/main.ml`.
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 solutions/ocaml/01-jm1/code/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 OCaml version used to run your code
# on Codecrafters.
#
# Available versions: ocaml-5.2.0
language_pack: ocaml-5.2.0
27 changes: 27 additions & 0 deletions solutions/ocaml/01-jm1/code/codecrafters_redis.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.1"
synopsis: "Starter code for the Build your own Redis challenge"
maintainer: ["CodeCrafters <hello@codecrafters.io>"]
authors: ["CodeCrafters <hello@codecrafters.io>"]
license: "MIT"
homepage: "https://codecrafters.io"
bug-reports: "https://github.com/codecrafters-io/build-your-own-redis/issues"
depends: [
"dune" {>= "3.16"}
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
5 changes: 5 additions & 0 deletions solutions/ocaml/01-jm1/code/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(include_subdirs unqualified)

(executable
(name main)
(libraries unix))
17 changes: 17 additions & 0 deletions solutions/ocaml/01-jm1/code/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(lang dune 3.16)

(name codecrafters_redis)

(generate_opam_files true)

(package
(name codecrafters_redis)
(maintainers "CodeCrafters <hello@codecrafters.io>")
(authors "CodeCrafters <hello@codecrafters.io>")
(synopsis "Starter code for the Build your own Redis challenge")
(homepage "https://codecrafters.io")
(bug_reports
"https://github.com/codecrafters-io/build-your-own-redis/issues")
(license "MIT")
(allow_empty true)
(version 0.1))
12 changes: 12 additions & 0 deletions solutions/ocaml/01-jm1/code/src/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
open Unix

let () =
(* Create a TCP server socket *)
let server_socket = socket PF_INET SOCK_STREAM 0 in
setsockopt server_socket SO_REUSEADDR true;
bind server_socket (ADDR_INET (inet_addr_of_string "127.0.0.1", 6379));
listen server_socket 1;

let (client_socket, _) = accept server_socket in
close client_socket;
close server_socket
24 changes: 24 additions & 0 deletions solutions/ocaml/01-jm1/code/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
dune build --build-dir /tmp/codecrafters-build-redis-ocaml
)

# 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 /tmp/codecrafters-build-redis-ocaml/default/main.exe "$@"
20 changes: 20 additions & 0 deletions solutions/ocaml/01-jm1/diff/src/main.ml.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@@ -1,16 +1,12 @@
open Unix

let () =
- (* You can use print statements as follows for debugging, they'll be visible when running tests. *)
- Printf.eprintf "Logs from your program will appear here!\n";
-
(* Create a TCP server socket *)
let server_socket = socket PF_INET SOCK_STREAM 0 in
setsockopt server_socket SO_REUSEADDR true;
bind server_socket (ADDR_INET (inet_addr_of_string "127.0.0.1", 6379));
listen server_socket 1;

- (* Uncomment this block to pass the first stage *)
- (* let (client_socket, _) = accept server_socket in *)
- (* close client_socket; *)
- (* close server_socket *)
+ let (client_socket, _) = accept server_socket in
+ close client_socket;
+ close server_socket
Loading
Loading