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 swift language support #27

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions compiled_starters/git-starter-swift/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
19 changes: 19 additions & 0 deletions compiled_starters/git-starter-swift/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "swift-git-challenge",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.executableTarget(
name: "swift-git-challenge",
dependencies: []),
]
)
61 changes: 61 additions & 0 deletions compiled_starters/git-starter-swift/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/git.png)

This is a starting point for Swift solutions to the
["Build Your Own Git" Challenge](https://codecrafters.io/challenges/git).

In this challenge, you'll build a small Git implementation that's capable of
initializing a repository, creating commits and cloning a public repository.
Along the way we'll learn about the `.git` directory, Git objects (blobs,
commits, trees etc.), Git's transfer protocols 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 Git implementation is in
`Sources/swift-git-challenge/Main.swift`. 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 `swift (5.7)` installed locally
1. Run `./your_git.sh` to run your Git implementation, which is implemented in
`Sources/swift-git-challenge/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.

# Testing locally

The `your_git.sh` script is expected to operate on the `.git` folder inside the
current working directory. If you're running this inside the root of this
repository, you might end up accidentally damaging your repository's `.git`
folder.

We suggest executing `your_git.sh` in a different folder when testing locally.
For example:

```sh
mkdir -p /tmp/testing && cd /tmp/testing
/path/to/your/repo/your_git.sh init
```

To make this easier to type out, you could add a
[shell alias](https://shapeshed.com/unix-alias/):

```sh
alias mygit=/path/to/your/repo/your_git.sh

mkdir -p /tmp/testing && cd /tmp/testing
mygit init
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Foundation

@main
public struct Main {
public static func main() throws {
// You can use print statements as follows for debugging, they'll be visible when running tests.
print("Logs from your program will appear here!")

// Uncomment this block to pass the first stage
//
// guard CommandLine.argc >= 2 else {
// print("usage: mygit <command> [<args>...]")
// return
// }
//
// let command = CommandLine.arguments[1]
// switch command {
// case "init":
// let fileManager = FileManager.default
//
// try fileManager.createDirectory(atPath: ".git", withIntermediateDirectories: false)
// try fileManager.createDirectory(atPath: ".git/objects", withIntermediateDirectories: false)
// try fileManager.createDirectory(atPath: ".git/refs", withIntermediateDirectories: false)
// fileManager.createFile(atPath: ".git/HEAD", contents: "ref: refs/heads/master\n".data(using: .utf8))
//
// print("Initialized git directory")
// default:
// print("Unknown command \(command)")
// return
// }
}
}
11 changes: 11 additions & 0 deletions compiled_starters/git-starter-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-5.7
language_pack: swift-5.7
15 changes: 15 additions & 0 deletions compiled_starters/git-starter-swift/your_git.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
set -e
set -o pipefail

packagePath=$(dirname "$0")
# Swift build writes errors to stdout instead of stderr. Let's collect all output in a file and only print if the exit code is zero.
buildOutputFile=$(mktemp)
swift build -c release --package-path "$packagePath" > "$buildOutputFile" || (cat "$buildOutputFile" && exit 1)
exec swift run -c release --skip-build --package-path "$packagePath" swift-git-challenge "$@"
2 changes: 1 addition & 1 deletion course-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ short_description_md: |

completion_percentage: 10

early_access_languages: []
early_access_languages: ["swift"]
supported_languages: ["python", "ruby", "go", "rust"] # TODO: Add kotlin back once our builds are faster

starter_repos:
Expand Down
5 changes: 5 additions & 0 deletions dockerfiles/swift-5.7.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM swift:5.7.3-focal

RUN apt update && \
apt install --no-install-recommends --yes git && \
rm -r /var/lib/apt/lists/
9 changes: 9 additions & 0 deletions solutions/swift/01-init/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
19 changes: 19 additions & 0 deletions solutions/swift/01-init/code/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "swift-git-challenge",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.executableTarget(
name: "swift-git-challenge",
dependencies: []),
]
)
61 changes: 61 additions & 0 deletions solutions/swift/01-init/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/git.png)

This is a starting point for Swift solutions to the
["Build Your Own Git" Challenge](https://codecrafters.io/challenges/git).

In this challenge, you'll build a small Git implementation that's capable of
initializing a repository, creating commits and cloning a public repository.
Along the way we'll learn about the `.git` directory, Git objects (blobs,
commits, trees etc.), Git's transfer protocols 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 Git implementation is in
`Sources/swift-git-challenge/Main.swift`. 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 `swift (5.7)` installed locally
1. Run `./your_git.sh` to run your Git implementation, which is implemented in
`Sources/swift-git-challenge/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.

# Testing locally

The `your_git.sh` script is expected to operate on the `.git` folder inside the
current working directory. If you're running this inside the root of this
repository, you might end up accidentally damaging your repository's `.git`
folder.

We suggest executing `your_git.sh` in a different folder when testing locally.
For example:

```sh
mkdir -p /tmp/testing && cd /tmp/testing
/path/to/your/repo/your_git.sh init
```

To make this easier to type out, you could add a
[shell alias](https://shapeshed.com/unix-alias/):

```sh
alias mygit=/path/to/your/repo/your_git.sh

mkdir -p /tmp/testing && cd /tmp/testing
mygit init
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Foundation

@main
public struct Main {
public static func main() throws {
guard CommandLine.argc >= 2 else {
print("usage: mygit <command> [<args>...]")
return
}

let command = CommandLine.arguments[1]
switch command {
case "init":
let fileManager = FileManager.default

try fileManager.createDirectory(atPath: ".git", withIntermediateDirectories: false)
try fileManager.createDirectory(atPath: ".git/objects", withIntermediateDirectories: false)
try fileManager.createDirectory(atPath: ".git/refs", withIntermediateDirectories: false)
fileManager.createFile(atPath: ".git/HEAD", contents: "ref: refs/heads/master\n".data(using: .utf8))

print("Initialized git directory")
default:
print("Unknown command \(command)")
return
}
}
}
11 changes: 11 additions & 0 deletions solutions/swift/01-init/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-5.7
language_pack: swift-5.7
15 changes: 15 additions & 0 deletions solutions/swift/01-init/code/your_git.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
set -e
set -o pipefail

packagePath=$(dirname "$0")
# Swift build writes errors to stdout instead of stderr. Let's collect all output in a file and only print if the exit code is zero.
buildOutputFile=$(mktemp)
swift build -c release --package-path "$packagePath" > "$buildOutputFile" || (cat "$buildOutputFile" && exit 1)
exec swift run -c release --skip-build --package-path "$packagePath" swift-git-challenge "$@"
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@@ -1,32 +1,27 @@
import Foundation

@main
public struct Main {
public static func main() throws {
- // You can use print statements as follows for debugging, they'll be visible when running tests.
- print("Logs from your program will appear here!")
+ guard CommandLine.argc >= 2 else {
+ print("usage: mygit <command> [<args>...]")
+ return
+ }

- // Uncomment this block to pass the first stage
- //
- // guard CommandLine.argc >= 2 else {
- // print("usage: mygit <command> [<args>...]")
- // return
- // }
- //
- // let command = CommandLine.arguments[1]
- // switch command {
- // case "init":
- // let fileManager = FileManager.default
- //
- // try fileManager.createDirectory(atPath: ".git", withIntermediateDirectories: false)
- // try fileManager.createDirectory(atPath: ".git/objects", withIntermediateDirectories: false)
- // try fileManager.createDirectory(atPath: ".git/refs", withIntermediateDirectories: false)
- // fileManager.createFile(atPath: ".git/HEAD", contents: "ref: refs/heads/master\n".data(using: .utf8))
- //
- // print("Initialized git directory")
- // default:
- // print("Unknown command \(command)")
- // return
- // }
+ let command = CommandLine.arguments[1]
+ switch command {
+ case "init":
+ let fileManager = FileManager.default
+
+ try fileManager.createDirectory(atPath: ".git", withIntermediateDirectories: false)
+ try fileManager.createDirectory(atPath: ".git/objects", withIntermediateDirectories: false)
+ try fileManager.createDirectory(atPath: ".git/refs", withIntermediateDirectories: false)
+ fileManager.createFile(atPath: ".git/HEAD", contents: "ref: refs/heads/master\n".data(using: .utf8))
+
+ print("Initialized git directory")
+ default:
+ print("Unknown command \(command)")
+ return
+ }
}
}
Loading