-
Notifications
You must be signed in to change notification settings - Fork 253
add cli and library getting started guides #192
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
Merged
tomerd
merged 9 commits into
swiftlang:content-improvements
from
tomerd:content-improvements-getting-started
Jan 12, 2023
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
54197b4
add cli and library getting started guides
tomerd 5d9d532
wip
tomerd 25d2b7d
wip
tomerd f18a3ed
fixup
tomerd d172ef9
Update getting-started/cli-tool/index.md
tomerd 26cea28
Update getting-started/cli-tool/index.md
tomerd 7192034
Update getting-started/library/index.md
tomerd 948f3d5
updates
tomerd 1d5fd19
links to guides
tomerd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 3 additions & 1 deletion
4
getting-started/_installing.md → _includes/getting-started/_installing.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| --- | ||
| layout: page | ||
| title: Build a library | ||
| --- | ||
|
|
||
| {% include getting-started/_installing.md %} | ||
|
|
||
| ## Bootstrapping | ||
|
|
||
| Let’s write a small application with our new Swift development environment. | ||
| To start, we’ll use SwiftPM to make a new project for us. In your terminal of choice run: | ||
|
|
||
| ~~~bash | ||
| ❯ mkdir swift-library | ||
| ❯ cd swift-library | ||
| ❯ swift package init --name swift-library --type library | ||
| ~~~ | ||
|
|
||
| This will generate a new directory called hello-swift with the following files: | ||
|
|
||
| ~~~no-highlight | ||
| . | ||
| ├── Package.swift | ||
| ├── README.md | ||
| ├── Sources | ||
| │ └── swift-library | ||
| │ └── swift_library.swift | ||
| └── Tests | ||
| └── swift-libraryTests | ||
| └── swift_libraryTests.swift | ||
| ~~~ | ||
|
|
||
| `Package.swift` is the manifest file for Swift. It’s where you keep metadata for your project, as well as dependencies. | ||
|
|
||
| `Sources/swift-library/swift-library.swift` is the library initial source file and where we’ll write our library code. | ||
| `Test/swift-libraryTests/swift-libraryTests.swift` is where we can write tests for our library. | ||
|
|
||
| In fact, SwiftPM generated a "Hello, world!" project for us, including some unit tests! | ||
| We can run the tests by running `swift test` in our terminal. | ||
|
|
||
| ~~~bash | ||
| ❯ swift test | ||
| Building for debugging... | ||
| [3/3] Linking swift-libraryPackageTests | ||
| Test Suite 'All tests' started at 2023-01-03 10:57:52.659 | ||
| Test Suite 'swift-libraryPackageTests.xctest' started at 2023-01-03 10:57:52.660 | ||
| Test Suite 'swift_libraryTests' started at 2023-01-03 10:57:52.660 | ||
| Test Case '-[swift_libraryTests.swift_libraryTests testExample]' started. | ||
| Test Case '-[swift_libraryTests.swift_libraryTests testExample]' passed (0.003 seconds). | ||
| Test Suite 'swift_libraryTests' passed at 2023-01-03 10:57:52.664. | ||
| Executed 1 test, with 0 failures (0 unexpected) in 0.003 (0.004) seconds | ||
| Test Suite 'swift-libraryPackageTests.xctest' passed at 2023-01-03 10:57:52.664. | ||
| Executed 1 test, with 0 failures (0 unexpected) in 0.003 (0.004) seconds | ||
| Test Suite 'All tests' passed at 2023-01-03 10:57:52.664. | ||
| Executed 1 test, with 0 failures (0 unexpected) in 0.003 (0.005) seconds | ||
| ~~~ | ||
|
|
||
| ## A small library | ||
|
|
||
| Now let’s write a small library. | ||
| Replace the example content of `swift-library.swift` with the following code: | ||
|
|
||
| ~~~swift | ||
| import Foundation | ||
|
|
||
| struct Email: CustomStringConvertible { | ||
| var description: String | ||
|
|
||
| public init(_ emailString: String) throws { | ||
| let regex = #"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,64}"# | ||
| guard let _ = emailString.range(of: regex, options: .regularExpression) else { | ||
| throw InvalidEmailError(email: emailString) | ||
| } | ||
| self.description = emailString | ||
| } | ||
| } | ||
|
|
||
| private struct InvalidEmailError: Error { | ||
| let email: String | ||
| } | ||
| ~~~ | ||
|
|
||
| Now lets add a unit test for this strongly types Email API. | ||
tomerd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Replace the example content of `swift_libraryTests.swift` with the following code: | ||
|
|
||
| ~~~swift | ||
| @testable import swift_library | ||
| import XCTest | ||
|
|
||
| final class swift_libraryTests: XCTestCase { | ||
| func testEmail() throws { | ||
| let email = try Email("[email protected]") | ||
| XCTAssertEqual(email.description, "[email protected]") | ||
|
|
||
| XCTAssertThrowsError(try Email("invalid")) | ||
| } | ||
| } | ||
| ~~~ | ||
|
|
||
| Once we save that, we can run our application with `swift run` | ||
| Assuming everything went well, we can run the tests successfully again: | ||
|
|
||
| ~~~no-highlight | ||
| ❯ swift test | ||
| Building for debugging... | ||
| [3/3] Linking swift-libraryPackageTests | ||
| Build complete! (0.84s) | ||
| Test Suite 'All tests' started at 2023-01-03 16:22:45.070 | ||
| Test Suite 'swift-libraryPackageTests.xctest' started at 2023-01-03 16:22:45.071 | ||
| Test Suite 'swift_libraryTests' started at 2023-01-03 16:22:45.071 | ||
| Test Case '-[swift_libraryTests.swift_libraryTests testEmail]' started. | ||
| Test Case '-[swift_libraryTests.swift_libraryTests testEmail]' passed (0.005 seconds). | ||
| Test Suite 'swift_libraryTests' passed at 2023-01-03 16:22:45.076. | ||
| Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.005) seconds | ||
| Test Suite 'swift-libraryPackageTests.xctest' passed at 2023-01-03 16:22:45.076. | ||
| Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.005) seconds | ||
| Test Suite 'All tests' passed at 2023-01-03 16:22:45.076. | ||
| Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.007) seconds | ||
| ~~~ | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.