Skip to content

Commit 492158d

Browse files
authored
Ks1019/add documentation (#3)
* Add plugin * Add docs to Script.swift * Add docs to Script+Comparable.swift * Add docs to SystemCall.swift * Fix docc * Fix * Fix * Add docs * Add docs to Script+Files.swift * Add docs to Script+String.swift * Run `swiftlint --fix .`
1 parent b7b4af7 commit 492158d

File tree

7 files changed

+65
-11
lines changed

7 files changed

+65
-11
lines changed

Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/ScriptSwift/Script+Comparable.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/// Scripting methods whose values are comparable
22
extension Script where T: Comparable {
3-
/// Return Bool value by comparing the piped value and the parameter `number`.
4-
///
3+
54
/// This method return true when the piped value is more than `number` and false otherwise.
5+
/// - Parameter number: `Comparable` value
6+
/// - Returns: Bool value by comparing the piped value and the parameter `number`
67
public func more(than number: T) -> Script<Bool> {
78
switch input {
89
case .success(let input):
@@ -12,9 +13,9 @@ extension Script where T: Comparable {
1213
}
1314
}
1415

15-
/// Return Bool value by comparing the piped value and the parameter `number`.
16-
///
1716
/// This method return true when the piped value is less than `number` and false otherwise.
17+
/// - Parameter number: `Comparable` value
18+
/// - Returns: Bool value by comparing the piped value and the parameter `number`
1819
public func less(than number: T) -> Script<Bool> {
1920
switch input {
2021
case .success(let input):
@@ -24,9 +25,9 @@ extension Script where T: Comparable {
2425
}
2526
}
2627

27-
/// Return Bool value by comparing the piped value and the parameter `number`.
28-
///
2928
/// This method return true when the piped value is equal to `number` and false otherwise.
29+
/// - Parameter number: `Comparable` value
30+
/// - Returns: Bool value by comparing the piped value and the parameter `number`
3031
public func equal(to number: T) -> Script<Bool> {
3132
switch input {
3233
case .success(let input):

Sources/ScriptSwift/Script+Data.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import struct Foundation.Data
22
import Files
33

44
extension Script where T == Data {
5+
/// This function writes `Data` value from previous ``Script`` method to a file specified.
6+
/// - Parameter path: `String` representation of path of the file
7+
/// - Returns: ``Script`` object with `File` value or failure
58
public func write(path: String) -> Script<File> {
69
switch input {
710
case .success(let input):
@@ -18,6 +21,9 @@ extension Script where T == Data {
1821
}
1922
}
2023

24+
/// This function writes `Data` value from previous ``Script`` method to a file specified.
25+
/// - Parameter filename: `String` representation of the name of the file
26+
/// - Returns: ``Script`` object with `File` value or failure
2127
public func write(to filename: String) -> Script<File> {
2228
switch input {
2329
case .success(let input):

Sources/ScriptSwift/Script+Files.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import struct Foundation.Data
22
import Files
33

44
extension Script where T == File {
5+
/// This function reads file from the value of the previous ``Script`` method.
6+
/// - Returns: ``Script`` object with `Data` representation of the file or failure
57
public func read() -> Script<Data> {
68
switch input {
79
case .success(let input):
@@ -16,6 +18,9 @@ extension Script where T == File {
1618
}
1719
}
1820

21+
/// This function reads file from the value of the previous ``Script`` method.
22+
/// - Parameter encoded: This indicates how the file is encoded.
23+
/// - Returns: ``Script`` object with `String` representation of the file or failure
1924
public func read(encoded: String.Encoding = .utf8) -> Script<String> {
2025
switch input {
2126
case .success(let input):

Sources/ScriptSwift/Script+String.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ extension Script where T == String {
66
self.init(success: "")
77
}
88

9+
/// This function executes externtal command using the input `String` value.
10+
/// - Returns: ``Script`` object containing `String` value or failure
911
public func exec() -> Script<String> {
1012
switch input {
1113
case .success(let input):
@@ -20,6 +22,8 @@ extension Script where T == String {
2022
}
2123
}
2224

25+
/// This function pass `self` to next function in the method chain if a file exists using the input `String` value.
26+
/// - Returns: ``Script`` object passed from previous function or failure
2327
public func ifExists() -> Script<String> {
2428
switch input {
2529
case .success(let input):
@@ -34,6 +38,9 @@ extension Script where T == String {
3438
}
3539
}
3640

41+
/// This function passes `String` value only when matched.
42+
/// - Parameter string: `String` value to match
43+
/// - Returns: ``Script`` object with only matched `String` value or failure
3744
public func match(_ string: String) -> Script<String> {
3845
switch input {
3946
case .success(let input):
@@ -48,6 +55,8 @@ extension Script where T == String {
4855
}
4956
}
5057

58+
/// This function returns the number of lines of `String` input value.
59+
/// - Returns: ``Script`` object with `Int` value of the number of lines
5160
public func countLines() -> Script<Int> {
5261
switch input {
5362
case .success(let input):
@@ -57,6 +66,8 @@ extension Script where T == String {
5766
}
5867
}
5968

69+
/// This function combines files using input `String` value as file names, and outputs the combined files as `Array` of `String`.
70+
/// - Returns: ``Script`` object with `Array` of `String`
6071
public func concat() -> Script<[String]> {
6172
switch input {
6273
case .success(let input):
@@ -72,6 +83,8 @@ extension Script where T == String {
7283
}
7384
}
7485

86+
/// This function passes multi-line `String` value as `Array` to next ``Script`` method.
87+
/// - Returns: ``Script`` object with `Array` of `String` value
7588
public func asArray() -> Script<[String]> {
7689
switch input {
7790
case .success(let input):

Sources/ScriptSwift/Script.swift

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import Files
22
import ShellOut
33
import struct Foundation.Data
44

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

@@ -17,16 +20,14 @@ public struct Script<T> {
1720
self.init(input: .failure(failure))
1821
}
1922

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

25-
// public func stdin() -> Script<[String]> {
26-
// guard let array: [String] = readLine()?.split(separator: " ").map({ s in String(s) }) else { return .init(success: []) }
27-
// return .init(success: array)
28-
// }
29-
30+
/// This function accepts inputs and outputs it to stdout.
3031
public func stdout() {
3132
switch input {
3233
case .success(let input):
@@ -36,6 +37,9 @@ public struct Script<T> {
3637
}
3738
}
3839

40+
/// This function executes externtal command.
41+
/// - Parameter command: `Array` of `String` to execute command
42+
/// - Returns: ``Script`` object containing `String` value or failure
3943
public func exec(_ command: [String]) -> Script<String> {
4044
do {
4145
return .init(success: try shellOut(to: command))
@@ -44,6 +48,9 @@ public struct Script<T> {
4448
}
4549
}
4650

51+
/// This function executes externtal command.
52+
/// - Parameter command: `String` to execute command
53+
/// - Returns: ``Script`` object containing `String` value or failure
4754
public func exec(_ command: String) -> Script<String> {
4855
do {
4956
return .init(success: try shellOut(to: command))
@@ -52,6 +59,9 @@ public struct Script<T> {
5259
}
5360
}
5461

62+
/// This function pass `self` to next function in the method chain if a file exists.
63+
/// - Parameter filename: `String` to represent name of a file
64+
/// - Returns: ``Script`` object passed from previous function or failure
5565
public func ifExists(_ filename: String) -> Script<T> {
5666
do {
5767
_ = try File(path: filename)
@@ -61,6 +71,9 @@ public struct Script<T> {
6171
}
6272
}
6373

74+
/// This function lets user modify the contained value in the method chain.
75+
/// - Parameter transform: A closure to modify the contained value
76+
/// - Returns: ``Script`` object with modified value or failure
6477
public func map<N>(_ transform: (T) -> N) -> Script<N> {
6578
switch input {
6679
case .success(let input):
@@ -70,6 +83,8 @@ public struct Script<T> {
7083
}
7184
}
7285

86+
/// This function returns the contained value, ending the method chain.
87+
/// - Returns: The contained value or exit with failure
7388
public func raw() -> T {
7489
switch input {
7590
case .success(let input):
@@ -79,6 +94,8 @@ public struct Script<T> {
7994
}
8095
}
8196

97+
/// This function returns the contained value or error as `String`.
98+
/// - Returns: `String` representaion of the contained value or error
8299
public func asString() -> String {
83100
switch input {
84101
case .success(let input):

Sources/ScriptSwift/SystemCall.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import Glibc
66
let _exit = Glibc.exit
77
#endif
88

9+
/// This function exit current process with error if given.
10+
/// - Parameter error: `Error` object
11+
/// - Returns: `Never`
912
func exit(withError error: Error? = nil) -> Never {
1013
guard let error = error else {
1114
_exit(0)

0 commit comments

Comments
 (0)