Skip to content

Commit

Permalink
Merge branch 'main' into ks1019/add-cd
Browse files Browse the repository at this point in the history
  • Loading branch information
KS1019 authored Nov 3, 2023
2 parents 0cf8d6e + bb20a3a commit 848c0df
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ExamplesTesting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ jobs:
matrix:
os:
- macOS-latest
- ubuntu-latest
# - ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
# - run: eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" || true && brew install swift-sh
- run: brew install swift-sh
- run: sh ./Scripts/ExamplesTest.sh
12 changes: 6 additions & 6 deletions .github/workflows/swift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
strategy:
matrix:
xcode-version:
- /Applications/Xcode_13.2.1.app
- /Applications/Xcode_12.5.1.app
- /Applications/Xcode_15.0.app
- /Applications/Xcode_14.3.1.app
runs-on: macOS-latest
steps:
- uses: actions/checkout@v2
- run: sudo xcode-select -s "${{ matrix.xcode-version }}"
- uses: actions/checkout@v3
- run: sudo xcode-select -s "${{ matrix.xcode-version }}" || true
- run: swift --version
- run: swift build --build-tests
- run: swift test
Expand All @@ -26,10 +26,10 @@ jobs:
name: Test on Ubuntu
strategy:
matrix:
swift-version: ["5.3", "5.4", "5.5"]
swift-version: ["5.7", "5.8"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup swiftenv
run: |
git clone https://github.com/kylef/swiftenv.git ~/.swiftenv
Expand Down
38 changes: 15 additions & 23 deletions Sources/ScriptSwift/Script.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ import struct Foundation.Data

/// Script type
///
/// By using method chain, you can express a workflow of your script in Swift.
/// By using a method chain, you can express a workflow of your script in Swift.
public struct Script<T> {
var input: Result<T, Error>

internal init(input: Result<T, Error>) {
internal init(_ input: Result<T, Error>) {
self.input = input
}

public init(success: T) {
self.init(input: .success(success))
self.init(.success(success))
}

internal init(failure: Error) {
self.init(input: .failure(failure))
self.init(.failure(failure))
}

/// This function collects inputs from stdin and returns as `String`.
/// This function collects inputs from stdin and returns them as `String`.
/// - Returns: ``Script`` object containing `String` value or failure
public func stdin() -> Script<String> {
guard let array: [String] = readLine()?.split(separator: " ").map({ s in String(s) }) else { return .init(success: "") }
return .init(success: array.joined(separator: " "))
}

/// This function accepts inputs and outputs it to stdout.
/// This function accepts inputs and outputs them to stdout.
public func stdout() {
switch input {
case .success(let input):
Expand All @@ -37,30 +37,22 @@ public struct Script<T> {
}
}

/// This function executes externtal command.
/// - Parameter command: `Array` of `String` to execute command
/// This function executes an external command.
/// - Parameter command: `Array` of `String` to execute a command
/// - Returns: ``Script`` object containing `String` value or failure
public func exec(_ command: [String]) -> Script<String> {
do {
return .init(success: try shellOut(to: command))
} catch {
return .init(failure: error)
}
Script<String>(Result { try shellOut(to: command) })
}

/// This function executes externtal command.
/// - Parameter command: `String` to execute command
/// This function executes an external command.
/// - Parameter command: `String` to execute a command
/// - Returns: ``Script`` object containing `String` value or failure
public func exec(_ command: String) -> Script<String> {
do {
return .init(success: try shellOut(to: command))
} catch {
return .init(failure: error)
}
Script<String>(Result { try shellOut(to: command) })
}

/// This function pass `self` to next function in the method chain if a file exists.
/// - Parameter filename: `String` to represent name of a file
/// This function passes `self` to the next function in the method chain if a file exists.
/// - Parameter filename: `String` to represent the name of a file
/// - Returns: ``Script`` object passed from previous function or failure
public func ifExists(_ filename: String) -> Script<T> {
do {
Expand Down Expand Up @@ -95,7 +87,7 @@ public struct Script<T> {
}

/// This function returns the contained value or error as `String`.
/// - Returns: `String` representaion of the contained value or error
/// - Returns: `String` representation of the contained value or error
public func asString() -> String {
switch input {
case .success(let input):
Expand Down
18 changes: 18 additions & 0 deletions Tests/ScriptSwiftTests/ScriptUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,41 @@ final class ScriptUnitTests: XCTestCase {
return XCTFail("Expected failure but got success")
}

#if os(macOS)
XCTAssertEqual(result1.localizedDescription, """
ShellOut encountered an error
Status code: 127
Message: "/bin/bash: makes-no-sense: command not found"
Output: ""
""")
#else
XCTAssertEqual(result1.localizedDescription, """
ShellOut encountered an error
Status code: 127
Message: "/bin/bash: line 1: makes-no-sense: command not found"
Output: ""
""")
#endif

guard case .failure(let result2) = Script().exec(command).input else {
return XCTFail("Expected failure but got success")
}

#if os(macOS)
XCTAssertEqual(result2.localizedDescription, """
ShellOut encountered an error
Status code: 127
Message: "/bin/bash: makes-no-sense: command not found"
Output: ""
""")
#else
XCTAssertEqual(result1.localizedDescription, """
ShellOut encountered an error
Status code: 127
Message: "/bin/bash: line 1: makes-no-sense: command not found"
Output: ""
""")
#endif
}

func testMap() {
Expand Down

0 comments on commit 848c0df

Please sign in to comment.