From 64dc95f12020f9485c5d0a894a18bbff13e16fdf Mon Sep 17 00:00:00 2001 From: Joshua Shroyer Date: Sun, 29 Sep 2024 22:23:41 -0400 Subject: [PATCH 1/2] Add support for Swift --- .../swift/.codecrafters/compile.sh | 11 +++++ compiled_starters/swift/.codecrafters/run.sh | 11 +++++ compiled_starters/swift/.gitattributes | 1 + compiled_starters/swift/.gitignore | 8 ++++ compiled_starters/swift/Package.resolved | 42 +++++++++++++++++++ compiled_starters/swift/Package.swift | 21 ++++++++++ compiled_starters/swift/README.md | 33 +++++++++++++++ compiled_starters/swift/Sources/main.swift | 30 +++++++++++++ compiled_starters/swift/codecrafters.yml | 11 +++++ compiled_starters/swift/your_program.sh | 24 +++++++++++ dockerfiles/swift-6.0.Dockerfile | 14 +++++++ .../01-jm1/code/.codecrafters/compile.sh | 11 +++++ .../swift/01-jm1/code/.codecrafters/run.sh | 11 +++++ solutions/swift/01-jm1/code/.gitattributes | 1 + solutions/swift/01-jm1/code/.gitignore | 8 ++++ solutions/swift/01-jm1/code/Package.resolved | 42 +++++++++++++++++++ solutions/swift/01-jm1/code/Package.swift | 21 ++++++++++ solutions/swift/01-jm1/code/README.md | 33 +++++++++++++++ .../swift/01-jm1/code/Sources/main.swift | 26 ++++++++++++ solutions/swift/01-jm1/code/codecrafters.yml | 11 +++++ solutions/swift/01-jm1/code/your_program.sh | 24 +++++++++++ .../swift/01-jm1/diff/Sources/main.swift.diff | 36 ++++++++++++++++ solutions/swift/01-jm1/explanation.md | 20 +++++++++ .../swift/code/.codecrafters/compile.sh | 11 +++++ .../swift/code/.codecrafters/run.sh | 11 +++++ starter_templates/swift/code/.gitignore | 8 ++++ starter_templates/swift/code/Package.resolved | 42 +++++++++++++++++++ starter_templates/swift/code/Package.swift | 21 ++++++++++ .../swift/code/Sources/main.swift | 30 +++++++++++++ starter_templates/swift/config.yml | 3 ++ 30 files changed, 576 insertions(+) create mode 100755 compiled_starters/swift/.codecrafters/compile.sh create mode 100755 compiled_starters/swift/.codecrafters/run.sh create mode 100644 compiled_starters/swift/.gitattributes create mode 100644 compiled_starters/swift/.gitignore create mode 100644 compiled_starters/swift/Package.resolved create mode 100644 compiled_starters/swift/Package.swift create mode 100644 compiled_starters/swift/README.md create mode 100644 compiled_starters/swift/Sources/main.swift create mode 100644 compiled_starters/swift/codecrafters.yml create mode 100755 compiled_starters/swift/your_program.sh create mode 100644 dockerfiles/swift-6.0.Dockerfile create mode 100755 solutions/swift/01-jm1/code/.codecrafters/compile.sh create mode 100755 solutions/swift/01-jm1/code/.codecrafters/run.sh create mode 100644 solutions/swift/01-jm1/code/.gitattributes create mode 100644 solutions/swift/01-jm1/code/.gitignore create mode 100644 solutions/swift/01-jm1/code/Package.resolved create mode 100644 solutions/swift/01-jm1/code/Package.swift create mode 100644 solutions/swift/01-jm1/code/README.md create mode 100644 solutions/swift/01-jm1/code/Sources/main.swift create mode 100644 solutions/swift/01-jm1/code/codecrafters.yml create mode 100755 solutions/swift/01-jm1/code/your_program.sh create mode 100644 solutions/swift/01-jm1/diff/Sources/main.swift.diff create mode 100644 solutions/swift/01-jm1/explanation.md create mode 100755 starter_templates/swift/code/.codecrafters/compile.sh create mode 100755 starter_templates/swift/code/.codecrafters/run.sh create mode 100644 starter_templates/swift/code/.gitignore create mode 100644 starter_templates/swift/code/Package.resolved create mode 100644 starter_templates/swift/code/Package.swift create mode 100644 starter_templates/swift/code/Sources/main.swift create mode 100644 starter_templates/swift/config.yml diff --git a/compiled_starters/swift/.codecrafters/compile.sh b/compiled_starters/swift/.codecrafters/compile.sh new file mode 100755 index 00000000..3eabd631 --- /dev/null +++ b/compiled_starters/swift/.codecrafters/compile.sh @@ -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 diff --git a/compiled_starters/swift/.codecrafters/run.sh b/compiled_starters/swift/.codecrafters/run.sh new file mode 100755 index 00000000..9f33f1c3 --- /dev/null +++ b/compiled_starters/swift/.codecrafters/run.sh @@ -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 "$@" diff --git a/compiled_starters/swift/.gitattributes b/compiled_starters/swift/.gitattributes new file mode 100644 index 00000000..176a458f --- /dev/null +++ b/compiled_starters/swift/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/compiled_starters/swift/.gitignore b/compiled_starters/swift/.gitignore new file mode 100644 index 00000000..21d4f9d2 --- /dev/null +++ b/compiled_starters/swift/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +.build +Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc \ No newline at end of file diff --git a/compiled_starters/swift/Package.resolved b/compiled_starters/swift/Package.resolved new file mode 100644 index 00000000..f50c5b30 --- /dev/null +++ b/compiled_starters/swift/Package.resolved @@ -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 +} diff --git a/compiled_starters/swift/Package.swift b/compiled_starters/swift/Package.swift new file mode 100644 index 00000000..49aebbe2 --- /dev/null +++ b/compiled_starters/swift/Package.swift @@ -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") + ] + ), + ] +) \ No newline at end of file diff --git a/compiled_starters/swift/README.md b/compiled_starters/swift/README.md new file mode 100644 index 00000000..ebe6c9ba --- /dev/null +++ b/compiled_starters/swift/README.md @@ -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. diff --git a/compiled_starters/swift/Sources/main.swift b/compiled_starters/swift/Sources/main.swift new file mode 100644 index 00000000..3b173dad --- /dev/null +++ b/compiled_starters/swift/Sources/main.swift @@ -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") diff --git a/compiled_starters/swift/codecrafters.yml b/compiled_starters/swift/codecrafters.yml new file mode 100644 index 00000000..52131074 --- /dev/null +++ b/compiled_starters/swift/codecrafters.yml @@ -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 diff --git a/compiled_starters/swift/your_program.sh b/compiled_starters/swift/your_program.sh new file mode 100755 index 00000000..1406cde5 --- /dev/null +++ b/compiled_starters/swift/your_program.sh @@ -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 "$@" diff --git a/dockerfiles/swift-6.0.Dockerfile b/dockerfiles/swift-6.0.Dockerfile new file mode 100644 index 00000000..3321ae9a --- /dev/null +++ b/dockerfiles/swift-6.0.Dockerfile @@ -0,0 +1,14 @@ +# 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 + +# Cache dependencies +RUN swift package --build-path /tmp/codecrafters-build-redis-swift resolve +RUN swift build -c release --build-path /tmp/codecrafters-build-redis-swift \ No newline at end of file diff --git a/solutions/swift/01-jm1/code/.codecrafters/compile.sh b/solutions/swift/01-jm1/code/.codecrafters/compile.sh new file mode 100755 index 00000000..3eabd631 --- /dev/null +++ b/solutions/swift/01-jm1/code/.codecrafters/compile.sh @@ -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 diff --git a/solutions/swift/01-jm1/code/.codecrafters/run.sh b/solutions/swift/01-jm1/code/.codecrafters/run.sh new file mode 100755 index 00000000..9f33f1c3 --- /dev/null +++ b/solutions/swift/01-jm1/code/.codecrafters/run.sh @@ -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 "$@" diff --git a/solutions/swift/01-jm1/code/.gitattributes b/solutions/swift/01-jm1/code/.gitattributes new file mode 100644 index 00000000..176a458f --- /dev/null +++ b/solutions/swift/01-jm1/code/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/solutions/swift/01-jm1/code/.gitignore b/solutions/swift/01-jm1/code/.gitignore new file mode 100644 index 00000000..21d4f9d2 --- /dev/null +++ b/solutions/swift/01-jm1/code/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +.build +Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc \ No newline at end of file diff --git a/solutions/swift/01-jm1/code/Package.resolved b/solutions/swift/01-jm1/code/Package.resolved new file mode 100644 index 00000000..f50c5b30 --- /dev/null +++ b/solutions/swift/01-jm1/code/Package.resolved @@ -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 +} diff --git a/solutions/swift/01-jm1/code/Package.swift b/solutions/swift/01-jm1/code/Package.swift new file mode 100644 index 00000000..49aebbe2 --- /dev/null +++ b/solutions/swift/01-jm1/code/Package.swift @@ -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") + ] + ), + ] +) \ No newline at end of file diff --git a/solutions/swift/01-jm1/code/README.md b/solutions/swift/01-jm1/code/README.md new file mode 100644 index 00000000..ebe6c9ba --- /dev/null +++ b/solutions/swift/01-jm1/code/README.md @@ -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. diff --git a/solutions/swift/01-jm1/code/Sources/main.swift b/solutions/swift/01-jm1/code/Sources/main.swift new file mode 100644 index 00000000..80272083 --- /dev/null +++ b/solutions/swift/01-jm1/code/Sources/main.swift @@ -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") diff --git a/solutions/swift/01-jm1/code/codecrafters.yml b/solutions/swift/01-jm1/code/codecrafters.yml new file mode 100644 index 00000000..52131074 --- /dev/null +++ b/solutions/swift/01-jm1/code/codecrafters.yml @@ -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 diff --git a/solutions/swift/01-jm1/code/your_program.sh b/solutions/swift/01-jm1/code/your_program.sh new file mode 100755 index 00000000..1406cde5 --- /dev/null +++ b/solutions/swift/01-jm1/code/your_program.sh @@ -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 "$@" diff --git a/solutions/swift/01-jm1/diff/Sources/main.swift.diff b/solutions/swift/01-jm1/diff/Sources/main.swift.diff new file mode 100644 index 00000000..fb033679 --- /dev/null +++ b/solutions/swift/01-jm1/diff/Sources/main.swift.diff @@ -0,0 +1,36 @@ +@@ -1,30 +1,26 @@ + 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") ++// 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") diff --git a/solutions/swift/01-jm1/explanation.md b/solutions/swift/01-jm1/explanation.md new file mode 100644 index 00000000..7f39a080 --- /dev/null +++ b/solutions/swift/01-jm1/explanation.md @@ -0,0 +1,20 @@ +The entry point for your Redis implementation is in `Sources/main.swift`. + +Study and uncomment the relevant code: + +```swift +// 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") +``` + +Push your changes to pass the first stage: + +``` +git add . +git commit -m "pass 1st stage" # any msg +git push origin master +``` diff --git a/starter_templates/swift/code/.codecrafters/compile.sh b/starter_templates/swift/code/.codecrafters/compile.sh new file mode 100755 index 00000000..3eabd631 --- /dev/null +++ b/starter_templates/swift/code/.codecrafters/compile.sh @@ -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 diff --git a/starter_templates/swift/code/.codecrafters/run.sh b/starter_templates/swift/code/.codecrafters/run.sh new file mode 100755 index 00000000..9f33f1c3 --- /dev/null +++ b/starter_templates/swift/code/.codecrafters/run.sh @@ -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 "$@" diff --git a/starter_templates/swift/code/.gitignore b/starter_templates/swift/code/.gitignore new file mode 100644 index 00000000..21d4f9d2 --- /dev/null +++ b/starter_templates/swift/code/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +.build +Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc \ No newline at end of file diff --git a/starter_templates/swift/code/Package.resolved b/starter_templates/swift/code/Package.resolved new file mode 100644 index 00000000..f50c5b30 --- /dev/null +++ b/starter_templates/swift/code/Package.resolved @@ -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 +} diff --git a/starter_templates/swift/code/Package.swift b/starter_templates/swift/code/Package.swift new file mode 100644 index 00000000..49aebbe2 --- /dev/null +++ b/starter_templates/swift/code/Package.swift @@ -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") + ] + ), + ] +) \ No newline at end of file diff --git a/starter_templates/swift/code/Sources/main.swift b/starter_templates/swift/code/Sources/main.swift new file mode 100644 index 00000000..3b173dad --- /dev/null +++ b/starter_templates/swift/code/Sources/main.swift @@ -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") diff --git a/starter_templates/swift/config.yml b/starter_templates/swift/config.yml new file mode 100644 index 00000000..393914c3 --- /dev/null +++ b/starter_templates/swift/config.yml @@ -0,0 +1,3 @@ +attributes: + required_executable: "swift (>=6.0)" + user_editable_file: Sources/main.swift From 274d6207c0ccb230ab3cfbd867e7ae26a8f318bb Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Mon, 30 Sep 2024 18:36:59 +0100 Subject: [PATCH 2/2] test re-using compile.sh --- dockerfiles/swift-6.0.Dockerfile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dockerfiles/swift-6.0.Dockerfile b/dockerfiles/swift-6.0.Dockerfile index 3321ae9a..603d7f10 100644 --- a/dockerfiles/swift-6.0.Dockerfile +++ b/dockerfiles/swift-6.0.Dockerfile @@ -9,6 +9,7 @@ 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 swift package --build-path /tmp/codecrafters-build-redis-swift resolve -RUN swift build -c release --build-path /tmp/codecrafters-build-redis-swift \ No newline at end of file +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 \ No newline at end of file