Skip to content

Commit f9b542a

Browse files
committed
project: add lint/format commands. reformat project
1 parent 23bb23a commit f9b542a

12 files changed

+93
-34
lines changed

.swiftformat

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--swiftversion 5.10
2+
3+
--exclude .build,.index-build,SakeApp/.build,SakeApp/.index-build
4+
5+
--maxwidth 140
6+
--wraparguments before-first
7+
--wrapparameters before-first
8+
--wrapcollections before-first
9+
10+
--enable isEmpty,wrapSwitchCases,wrapConditionalBodies,wrapEnumCases
11+
--disable redundantRawValues,redundantSelf,redundantStaticSelf,redundantType

SakeApp/BrewCommands.swift

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import SwiftShell
33

44
@CommandGroup
55
struct BrewCommands {
6+
static var ensureSwiftFormatInstalled: Command {
7+
Command(
8+
description: "Ensure swiftformat is installed",
9+
skipIf: { _ in
10+
run("which", "swiftformat").succeeded
11+
},
12+
run: { _ in
13+
try runAndPrint("brew", "install", "swiftformat")
14+
}
15+
)
16+
}
17+
618
static var ensureGhInstalled: Command {
719
Command(
820
description: "Ensure gh is installed",

SakeApp/ReleaseCommands.swift

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ArgumentParser
2-
import Foundation
32
import CryptoKit
3+
import Foundation
44
import Sake
55
import SwiftShell
66

@@ -11,6 +11,7 @@ struct ReleaseCommands {
1111
case x86
1212
case arm
1313
}
14+
1415
enum OS {
1516
case macos
1617
case linux
@@ -21,10 +22,10 @@ struct ReleaseCommands {
2122

2223
var triple: String {
2324
switch (arch, os) {
24-
case (.x86, .macos): "x86_64-apple-macosx"
25-
case (.arm, .macos): "arm64-apple-macosx"
26-
case (.x86, .linux): "x86_64-unknown-linux-gnu"
27-
case (.arm, .linux): "aarch64-unknown-linux-gnu"
25+
case (.x86, .macos): "x86_64-apple-macosx"
26+
case (.arm, .macos): "arm64-apple-macosx"
27+
case (.x86, .linux): "x86_64-unknown-linux-gnu"
28+
case (.arm, .linux): "aarch64-unknown-linux-gnu"
2829
}
2930
}
3031
}
@@ -167,7 +168,8 @@ struct ReleaseCommands {
167168
let buildFlags = ["--disable-sandbox", "--configuration", "release", "--triple", target.triple]
168169
if target.os == .linux {
169170
let platform = target.arch == .arm ? "linux/arm64" : "linux/amd64"
170-
let dockerExec = "docker run --rm --volume \(context.projectRoot):/workdir --workdir /workdir --platform \(platform) swift:\(Constants.swiftVersion)"
171+
let dockerExec =
172+
"docker run --rm --volume \(context.projectRoot):/workdir --workdir /workdir --platform \(platform) swift:\(Constants.swiftVersion)"
171173
let buildFlags = (buildFlags + ["--static-swift-stdlib"]).joined(separator: " ")
172174
return (
173175
"\(dockerExec) swift build \(buildFlags)",
@@ -178,10 +180,10 @@ struct ReleaseCommands {
178180
} else {
179181
let buildFlags = buildFlags.joined(separator: " ")
180182
return (
181-
"swift build \(buildFlags)",
182-
"swift package clean",
183-
"strip -rSTx",
184-
"zip -j"
183+
"swift build \(buildFlags)",
184+
"swift package clean",
185+
"strip -rSTx",
186+
"zip -j"
185187
)
186188
}
187189
}()
@@ -198,7 +200,9 @@ struct ReleaseCommands {
198200
try runAndPrint(bash: "\(strip) \(executablePath)")
199201

200202
let executableArchivePath = executableArchivePath(target: target, version: version)
201-
try runAndPrint(bash: "\(zip) \(executableArchivePath) \(executablePath.replacingOccurrences(of: "/workdir", with: context.projectRoot))")
203+
try runAndPrint(
204+
bash: "\(zip) \(executableArchivePath) \(executablePath.replacingOccurrences(of: "/workdir", with: context.projectRoot))"
205+
)
202206
}
203207

204208
print("Release artifacts built successfully at '\(Constants.buildArtifactsDirectory)'")

SakeApp/Sakefile.swift

+21-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ struct Commands: SakeApp {
1212
ReleaseCommands.self,
1313
]
1414
)
15+
16+
public static var lint: Command {
17+
Command(
18+
description: "Lint code",
19+
dependencies: [BrewCommands.ensureSwiftFormatInstalled],
20+
run: { _ in
21+
try runAndPrint("swiftformat", "Sources", "SakeApp", "Package.swift", "--lint")
22+
}
23+
)
24+
}
25+
26+
public static var format: Command {
27+
Command(
28+
description: "Format code",
29+
dependencies: [BrewCommands.ensureSwiftFormatInstalled],
30+
run: { _ in
31+
try runAndPrint("swiftformat", "Sources", "SakeApp", "Package.swift")
32+
}
33+
)
34+
}
1535
}
1636

1737
@CommandGroup
@@ -23,7 +43,7 @@ struct TestCommands {
2343
run: { context in
2444
try runAndPrint(
2545
bash:
26-
"\(context.projectRoot)/Tests/integration_tests.sh \(context.projectRoot)/.build/debug/progressline"
46+
"\(context.projectRoot)/Tests/integration_tests.sh \(context.projectRoot)/.build/debug/progressline"
2747
)
2848
}
2949
)

Sources/ANSI.swift

+6
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,27 @@ enum ANSI {
1515
static var red: String {
1616
noStyleMode ? "" : "\u{1B}[31m"
1717
}
18+
1819
static var green: String {
1920
noStyleMode ? "" : "\u{1B}[32m"
2021
}
22+
2123
static var yellow: String {
2224
noStyleMode ? "" : "\u{1B}[33m"
2325
}
26+
2427
static var blue: String {
2528
noStyleMode ? "" : "\u{1B}[34m"
2629
}
30+
2731
static var magenta: String {
2832
noStyleMode ? "" : "\u{1B}[35m"
2933
}
34+
3035
static var bold: String {
3136
noStyleMode ? "" : "\u{1B}[1m"
3237
}
38+
3339
static var reset: String {
3440
noStyleMode ? "" : "\u{1B}[0m"
3541
}

Sources/ActivityIndicator.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ extension ActivityIndicator {
7979
}
8080

8181
#if DEBUG
82-
extension ActivityIndicator {
83-
static func disabled() -> ActivityIndicator {
84-
.init(
85-
configuration: .init(refreshRate: 1_000_000_000, states: [])
86-
)
82+
extension ActivityIndicator {
83+
static func disabled() -> ActivityIndicator {
84+
.init(
85+
configuration: .init(refreshRate: 1_000_000_000, states: [])
86+
)
87+
}
8788
}
88-
}
8989
#endif

Sources/ErrorMessage.swift

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ enum ErrorMessage {
33
static func canNotCompileRegex(_ regex: String) -> String {
44
"\(ANSI.yellow)[!] progressline: Failed to compile regular expression: \(regex)\(ANSI.reset)"
55
}
6+
67
static func canNotOpenFile(_ path: String) -> String {
78
"\(ANSI.yellow)[!] progressline: Failed to open file at path: \(path)\(ANSI.reset)"
89
}

Sources/FileHandler+AsyncStream.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#if os(Linux)
2-
// Linux implementation of FileHandle not Sendable
3-
@preconcurrency import Foundation
2+
// Linux implementation of FileHandle not Sendable
3+
@preconcurrency import Foundation
44
#else
5-
import Foundation
5+
import Foundation
66
#endif
77

88
extension FileHandle {

Sources/Printer.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import ConcurrencyExtras
22
#if os(Linux)
3-
// Linux implementation of FileHandle not Sendable
4-
@preconcurrency import Foundation
3+
// Linux implementation of FileHandle not Sendable
4+
@preconcurrency import Foundation
55
#else
6-
import Foundation
6+
import Foundation
77
#endif
88

99
final class Printer: Sendable {

Sources/ProgressLine.swift

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ struct ProgressLine: AsyncParsableCommand {
2121
@Option(name: [.customLong("original-log-path"), .customShort("l")], help: "Save the original log to a file.")
2222
var originalLogPath: String?
2323

24-
@Option(name: [.customLong("log-matches"), .customShort("m")], help: "Log above progress line lines matching the given regular expressions.")
24+
@Option(
25+
name: [.customLong("log-matches"), .customShort("m")],
26+
help: "Log above progress line lines matching the given regular expressions."
27+
)
2528
var matchesToLog: [String] = []
2629

2730
@Flag(name: [.customLong("log-all"), .customShort("a")], help: "Log all lines above the progress line.")
2831
var shouldLogAll: Bool = false
2932

3033
#if DEBUG
31-
@Flag(name: [.customLong("test-mode")], help: "Enable test mode. Activity indicator will be replaced with a static string.")
32-
var testMode: Bool = false
34+
@Flag(name: [.customLong("test-mode")], help: "Enable test mode. Activity indicator will be replaced with a static string.")
35+
var testMode: Bool = false
3336
#endif
3437

3538
mutating func run() async throws {
@@ -42,10 +45,10 @@ struct ProgressLine: AsyncParsableCommand {
4245
let logger = AboveProgressLineLogger(printers: printers)
4346

4447
#if DEBUG
45-
let activityIndicator: ActivityIndicator = testMode ? .disabled() : .make(style: activityIndicatorStyle)
48+
let activityIndicator: ActivityIndicator = testMode ? .disabled() : .make(style: activityIndicatorStyle)
4649
#else
47-
let testMode = false
48-
let activityIndicator: ActivityIndicator = .make(style: activityIndicatorStyle)
50+
let testMode = false
51+
let activityIndicator: ActivityIndicator = .make(style: activityIndicatorStyle)
4952
#endif
5053
let progressLineController = await ProgressLineController.buildAndStart(
5154
textMode: staticText.map { .staticText($0) } ?? .stdin,

Sources/ProgressLineController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ final actor ProgressLineController {
117117

118118
private func redrawProgressLine() async {
119119
let lineText: String? = switch textMode {
120-
case .staticText(let text):
120+
case let .staticText(text):
121121
text
122122
case .stdin:
123123
lastStdinLine

Sources/WindowSizeObserver.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ final class WindowSizeObserver: Sendable {
3232
signal(sigwinch, SIG_IGN)
3333

3434
signalHandler.setEventHandler { [weak self] in
35-
guard let self = self else { return }
35+
guard let self else {
36+
return
37+
}
3638
self.syncWindowSize()
3739
}
3840
signalHandler.resume()
@@ -48,9 +50,9 @@ final class WindowSizeObserver: Sendable {
4850
static func getTerminalSize() -> Size {
4951
var w = winsize()
5052
#if os(Linux)
51-
_ = ioctl(STDOUT_FILENO, UInt(TIOCGWINSZ), &w)
53+
_ = ioctl(STDOUT_FILENO, UInt(TIOCGWINSZ), &w)
5254
#else
53-
_ = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w)
55+
_ = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w)
5456
#endif
5557
return Size(width: Int(w.ws_col), height: Int(w.ws_row))
5658
}

0 commit comments

Comments
 (0)