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 support for Swift #261

Merged
merged 2 commits into from
Sep 30, 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/swift/.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

swift build -c release --build-path /tmp/codecrafters-build-redis-swift
11 changes: 11 additions & 0 deletions compiled_starters/swift/.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 swift run -c release --skip-build --build-path /tmp/codecrafters-build-redis-swift build-your-own-redis "$@"
1 change: 1 addition & 0 deletions compiled_starters/swift/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
8 changes: 8 additions & 0 deletions compiled_starters/swift/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
.build
Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
42 changes: 42 additions & 0 deletions compiled_starters/swift/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"originHash" : "c36d8cad89fe8f5758a63a0c19924ef816667b8179783721c583fc3b73e5aff7",
"pins" : [
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "cd142fd2f64be2100422d658e7411e39489da985",
"version" : "1.2.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "671108c96644956dddcd89dd59c203dcdb36cec7",
"version" : "1.1.4"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "1b33db2dea6a64d5b619b9e888175133c6d7f410",
"version" : "2.73.0"
}
},
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-system.git",
"state" : {
"revision" : "d2ba781702a1d8285419c15ee62fd734a9437ff5",
"version" : "1.3.2"
}
}
],
"version" : 3
}
21 changes: 21 additions & 0 deletions compiled_starters/swift/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "build-your-own-redis",
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.73.0")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "build-your-own-redis",
dependencies: [
.product(name: "NIO", package: "swift-nio")
]
),
]
)
33 changes: 33 additions & 0 deletions compiled_starters/swift/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png)

This is a starting point for Swift 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 `Sources/main.swift`. Study
and uncomment the relevant code, and push your changes to pass the first stage:

```sh
git commit -am "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 `swift (>=6.0)` installed locally
1. Run `./your_program.sh` to run your Redis server, which is implemented in
`Sources/main.swift`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
30 changes: 30 additions & 0 deletions compiled_starters/swift/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Foundation
import NIO

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

// Create an event loop group to manage network events
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)

// Shut down the event loop group
defer {
try? group.syncShutdownGracefully()
}

// Set up a channel for the server to listen on port 6379
let serverBootstrap = ServerBootstrap(group: group)
// Set SO_REUSEADDR to true
.serverChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
// Handle incoming connections
.childChannelInitializer { channel in
// You can add channel handlers here if needed
return channel.eventLoop.makeSucceededFuture(())
}

// Uncomment this block to pass the first stage
//// Bind the server to port 6379 and start accepting connections
//let channel = try serverBootstrap.bind(host: "localhost", port: 6379).wait()
//print("Server started and listening on \(channel.localAddress!)")
//try channel.closeFuture.wait()
//print("Server closed")
11 changes: 11 additions & 0 deletions compiled_starters/swift/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 Swift version used to run your code
# on Codecrafters.
#
# Available versions: swift-6.0
language_pack: swift-6.0
24 changes: 24 additions & 0 deletions compiled_starters/swift/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
swift build -c release --build-path /tmp/codecrafters-build-redis-swift
)

# 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 swift run -c release --skip-build --build-path /tmp/codecrafters-build-redis-swift build-your-own-redis "$@"
15 changes: 15 additions & 0 deletions dockerfiles/swift-6.0.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# syntax=docker/dockerfile:1.7-labs
FROM swift:6.0-focal

# Ensures the container is re-built if Package.swift changes
ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="Package.swift"

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

RUN .codecrafters/compile.sh

# Cache dependencies (TODO: Check if this is required, or whether build implicitly caches dependencies)
RUN swift package --build-path /tmp/codecrafters-build-redis-swift resolve
11 changes: 11 additions & 0 deletions solutions/swift/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

swift build -c release --build-path /tmp/codecrafters-build-redis-swift
11 changes: 11 additions & 0 deletions solutions/swift/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 swift run -c release --skip-build --build-path /tmp/codecrafters-build-redis-swift build-your-own-redis "$@"
1 change: 1 addition & 0 deletions solutions/swift/01-jm1/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
8 changes: 8 additions & 0 deletions solutions/swift/01-jm1/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
.build
Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
42 changes: 42 additions & 0 deletions solutions/swift/01-jm1/code/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"originHash" : "c36d8cad89fe8f5758a63a0c19924ef816667b8179783721c583fc3b73e5aff7",
"pins" : [
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "cd142fd2f64be2100422d658e7411e39489da985",
"version" : "1.2.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "671108c96644956dddcd89dd59c203dcdb36cec7",
"version" : "1.1.4"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "1b33db2dea6a64d5b619b9e888175133c6d7f410",
"version" : "2.73.0"
}
},
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-system.git",
"state" : {
"revision" : "d2ba781702a1d8285419c15ee62fd734a9437ff5",
"version" : "1.3.2"
}
}
],
"version" : 3
}
21 changes: 21 additions & 0 deletions solutions/swift/01-jm1/code/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "build-your-own-redis",
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.73.0")
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "build-your-own-redis",
dependencies: [
.product(name: "NIO", package: "swift-nio")
]
),
]
)
33 changes: 33 additions & 0 deletions solutions/swift/01-jm1/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/redis.png)

This is a starting point for Swift 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 `Sources/main.swift`. Study
and uncomment the relevant code, and push your changes to pass the first stage:

```sh
git commit -am "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 `swift (>=6.0)` installed locally
1. Run `./your_program.sh` to run your Redis server, which is implemented in
`Sources/main.swift`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
26 changes: 26 additions & 0 deletions solutions/swift/01-jm1/code/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation
import NIO

// Create an event loop group to manage network events
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)

// Shut down the event loop group
defer {
try? group.syncShutdownGracefully()
}

// Set up a channel for the server to listen on port 6379
let serverBootstrap = ServerBootstrap(group: group)
// Set SO_REUSEADDR to true
.serverChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
// Handle incoming connections
.childChannelInitializer { channel in
// You can add channel handlers here if needed
return channel.eventLoop.makeSucceededFuture(())
}

// Bind the server to port 6379 and start accepting connections
let channel = try serverBootstrap.bind(host: "localhost", port: 6379).wait()
print("Server started and listening on \(channel.localAddress!)")
try channel.closeFuture.wait()
print("Server closed")
11 changes: 11 additions & 0 deletions solutions/swift/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 Swift version used to run your code
# on Codecrafters.
#
# Available versions: swift-6.0
language_pack: swift-6.0
Loading
Loading