From f9b542ad8595ae3cb3971decaeb4db8c93d28af2 Mon Sep 17 00:00:00 2001 From: Kattouf Date: Sun, 17 Nov 2024 12:26:09 +0700 Subject: [PATCH] project: add lint/format commands. reformat project --- .swiftformat | 11 +++++++++++ SakeApp/BrewCommands.swift | 12 ++++++++++++ SakeApp/ReleaseCommands.swift | 26 +++++++++++++++----------- SakeApp/Sakefile.swift | 22 +++++++++++++++++++++- Sources/ANSI.swift | 6 ++++++ Sources/ActivityIndicator.swift | 12 ++++++------ Sources/ErrorMessage.swift | 1 + Sources/FileHandler+AsyncStream.swift | 6 +++--- Sources/Printer.swift | 6 +++--- Sources/ProgressLine.swift | 15 +++++++++------ Sources/ProgressLineController.swift | 2 +- Sources/WindowSizeObserver.swift | 8 +++++--- 12 files changed, 93 insertions(+), 34 deletions(-) create mode 100644 .swiftformat diff --git a/.swiftformat b/.swiftformat new file mode 100644 index 0000000..f5c1ef7 --- /dev/null +++ b/.swiftformat @@ -0,0 +1,11 @@ +--swiftversion 5.10 + +--exclude .build,.index-build,SakeApp/.build,SakeApp/.index-build + +--maxwidth 140 +--wraparguments before-first +--wrapparameters before-first +--wrapcollections before-first + +--enable isEmpty,wrapSwitchCases,wrapConditionalBodies,wrapEnumCases +--disable redundantRawValues,redundantSelf,redundantStaticSelf,redundantType diff --git a/SakeApp/BrewCommands.swift b/SakeApp/BrewCommands.swift index 1723742..cd0cceb 100644 --- a/SakeApp/BrewCommands.swift +++ b/SakeApp/BrewCommands.swift @@ -3,6 +3,18 @@ import SwiftShell @CommandGroup struct BrewCommands { + static var ensureSwiftFormatInstalled: Command { + Command( + description: "Ensure swiftformat is installed", + skipIf: { _ in + run("which", "swiftformat").succeeded + }, + run: { _ in + try runAndPrint("brew", "install", "swiftformat") + } + ) + } + static var ensureGhInstalled: Command { Command( description: "Ensure gh is installed", diff --git a/SakeApp/ReleaseCommands.swift b/SakeApp/ReleaseCommands.swift index db64608..ae32bda 100644 --- a/SakeApp/ReleaseCommands.swift +++ b/SakeApp/ReleaseCommands.swift @@ -1,6 +1,6 @@ import ArgumentParser -import Foundation import CryptoKit +import Foundation import Sake import SwiftShell @@ -11,6 +11,7 @@ struct ReleaseCommands { case x86 case arm } + enum OS { case macos case linux @@ -21,10 +22,10 @@ struct ReleaseCommands { var triple: String { switch (arch, os) { - case (.x86, .macos): "x86_64-apple-macosx" - case (.arm, .macos): "arm64-apple-macosx" - case (.x86, .linux): "x86_64-unknown-linux-gnu" - case (.arm, .linux): "aarch64-unknown-linux-gnu" + case (.x86, .macos): "x86_64-apple-macosx" + case (.arm, .macos): "arm64-apple-macosx" + case (.x86, .linux): "x86_64-unknown-linux-gnu" + case (.arm, .linux): "aarch64-unknown-linux-gnu" } } } @@ -167,7 +168,8 @@ struct ReleaseCommands { let buildFlags = ["--disable-sandbox", "--configuration", "release", "--triple", target.triple] if target.os == .linux { let platform = target.arch == .arm ? "linux/arm64" : "linux/amd64" - let dockerExec = "docker run --rm --volume \(context.projectRoot):/workdir --workdir /workdir --platform \(platform) swift:\(Constants.swiftVersion)" + let dockerExec = + "docker run --rm --volume \(context.projectRoot):/workdir --workdir /workdir --platform \(platform) swift:\(Constants.swiftVersion)" let buildFlags = (buildFlags + ["--static-swift-stdlib"]).joined(separator: " ") return ( "\(dockerExec) swift build \(buildFlags)", @@ -178,10 +180,10 @@ struct ReleaseCommands { } else { let buildFlags = buildFlags.joined(separator: " ") return ( - "swift build \(buildFlags)", - "swift package clean", - "strip -rSTx", - "zip -j" + "swift build \(buildFlags)", + "swift package clean", + "strip -rSTx", + "zip -j" ) } }() @@ -198,7 +200,9 @@ struct ReleaseCommands { try runAndPrint(bash: "\(strip) \(executablePath)") let executableArchivePath = executableArchivePath(target: target, version: version) - try runAndPrint(bash: "\(zip) \(executableArchivePath) \(executablePath.replacingOccurrences(of: "/workdir", with: context.projectRoot))") + try runAndPrint( + bash: "\(zip) \(executableArchivePath) \(executablePath.replacingOccurrences(of: "/workdir", with: context.projectRoot))" + ) } print("Release artifacts built successfully at '\(Constants.buildArtifactsDirectory)'") diff --git a/SakeApp/Sakefile.swift b/SakeApp/Sakefile.swift index 315ed49..f455d2d 100644 --- a/SakeApp/Sakefile.swift +++ b/SakeApp/Sakefile.swift @@ -12,6 +12,26 @@ struct Commands: SakeApp { ReleaseCommands.self, ] ) + + public static var lint: Command { + Command( + description: "Lint code", + dependencies: [BrewCommands.ensureSwiftFormatInstalled], + run: { _ in + try runAndPrint("swiftformat", "Sources", "SakeApp", "Package.swift", "--lint") + } + ) + } + + public static var format: Command { + Command( + description: "Format code", + dependencies: [BrewCommands.ensureSwiftFormatInstalled], + run: { _ in + try runAndPrint("swiftformat", "Sources", "SakeApp", "Package.swift") + } + ) + } } @CommandGroup @@ -23,7 +43,7 @@ struct TestCommands { run: { context in try runAndPrint( bash: - "\(context.projectRoot)/Tests/integration_tests.sh \(context.projectRoot)/.build/debug/progressline" + "\(context.projectRoot)/Tests/integration_tests.sh \(context.projectRoot)/.build/debug/progressline" ) } ) diff --git a/Sources/ANSI.swift b/Sources/ANSI.swift index eac0946..8229207 100644 --- a/Sources/ANSI.swift +++ b/Sources/ANSI.swift @@ -15,21 +15,27 @@ enum ANSI { static var red: String { noStyleMode ? "" : "\u{1B}[31m" } + static var green: String { noStyleMode ? "" : "\u{1B}[32m" } + static var yellow: String { noStyleMode ? "" : "\u{1B}[33m" } + static var blue: String { noStyleMode ? "" : "\u{1B}[34m" } + static var magenta: String { noStyleMode ? "" : "\u{1B}[35m" } + static var bold: String { noStyleMode ? "" : "\u{1B}[1m" } + static var reset: String { noStyleMode ? "" : "\u{1B}[0m" } diff --git a/Sources/ActivityIndicator.swift b/Sources/ActivityIndicator.swift index 39c029a..3a75a62 100644 --- a/Sources/ActivityIndicator.swift +++ b/Sources/ActivityIndicator.swift @@ -79,11 +79,11 @@ extension ActivityIndicator { } #if DEBUG -extension ActivityIndicator { - static func disabled() -> ActivityIndicator { - .init( - configuration: .init(refreshRate: 1_000_000_000, states: []) - ) + extension ActivityIndicator { + static func disabled() -> ActivityIndicator { + .init( + configuration: .init(refreshRate: 1_000_000_000, states: []) + ) + } } -} #endif diff --git a/Sources/ErrorMessage.swift b/Sources/ErrorMessage.swift index 172d382..421a127 100644 --- a/Sources/ErrorMessage.swift +++ b/Sources/ErrorMessage.swift @@ -3,6 +3,7 @@ enum ErrorMessage { static func canNotCompileRegex(_ regex: String) -> String { "\(ANSI.yellow)[!] progressline: Failed to compile regular expression: \(regex)\(ANSI.reset)" } + static func canNotOpenFile(_ path: String) -> String { "\(ANSI.yellow)[!] progressline: Failed to open file at path: \(path)\(ANSI.reset)" } diff --git a/Sources/FileHandler+AsyncStream.swift b/Sources/FileHandler+AsyncStream.swift index 1daef34..2fb06c7 100644 --- a/Sources/FileHandler+AsyncStream.swift +++ b/Sources/FileHandler+AsyncStream.swift @@ -1,8 +1,8 @@ #if os(Linux) -// Linux implementation of FileHandle not Sendable -@preconcurrency import Foundation + // Linux implementation of FileHandle not Sendable + @preconcurrency import Foundation #else -import Foundation + import Foundation #endif extension FileHandle { diff --git a/Sources/Printer.swift b/Sources/Printer.swift index 13a1fe1..37c00ac 100644 --- a/Sources/Printer.swift +++ b/Sources/Printer.swift @@ -1,9 +1,9 @@ import ConcurrencyExtras #if os(Linux) -// Linux implementation of FileHandle not Sendable -@preconcurrency import Foundation + // Linux implementation of FileHandle not Sendable + @preconcurrency import Foundation #else -import Foundation + import Foundation #endif final class Printer: Sendable { diff --git a/Sources/ProgressLine.swift b/Sources/ProgressLine.swift index 1123127..27ea90e 100644 --- a/Sources/ProgressLine.swift +++ b/Sources/ProgressLine.swift @@ -21,15 +21,18 @@ struct ProgressLine: AsyncParsableCommand { @Option(name: [.customLong("original-log-path"), .customShort("l")], help: "Save the original log to a file.") var originalLogPath: String? - @Option(name: [.customLong("log-matches"), .customShort("m")], help: "Log above progress line lines matching the given regular expressions.") + @Option( + name: [.customLong("log-matches"), .customShort("m")], + help: "Log above progress line lines matching the given regular expressions." + ) var matchesToLog: [String] = [] @Flag(name: [.customLong("log-all"), .customShort("a")], help: "Log all lines above the progress line.") var shouldLogAll: Bool = false #if DEBUG - @Flag(name: [.customLong("test-mode")], help: "Enable test mode. Activity indicator will be replaced with a static string.") - var testMode: Bool = false + @Flag(name: [.customLong("test-mode")], help: "Enable test mode. Activity indicator will be replaced with a static string.") + var testMode: Bool = false #endif mutating func run() async throws { @@ -42,10 +45,10 @@ struct ProgressLine: AsyncParsableCommand { let logger = AboveProgressLineLogger(printers: printers) #if DEBUG - let activityIndicator: ActivityIndicator = testMode ? .disabled() : .make(style: activityIndicatorStyle) + let activityIndicator: ActivityIndicator = testMode ? .disabled() : .make(style: activityIndicatorStyle) #else - let testMode = false - let activityIndicator: ActivityIndicator = .make(style: activityIndicatorStyle) + let testMode = false + let activityIndicator: ActivityIndicator = .make(style: activityIndicatorStyle) #endif let progressLineController = await ProgressLineController.buildAndStart( textMode: staticText.map { .staticText($0) } ?? .stdin, diff --git a/Sources/ProgressLineController.swift b/Sources/ProgressLineController.swift index 5463ade..53cb43c 100644 --- a/Sources/ProgressLineController.swift +++ b/Sources/ProgressLineController.swift @@ -117,7 +117,7 @@ final actor ProgressLineController { private func redrawProgressLine() async { let lineText: String? = switch textMode { - case .staticText(let text): + case let .staticText(text): text case .stdin: lastStdinLine diff --git a/Sources/WindowSizeObserver.swift b/Sources/WindowSizeObserver.swift index 86f841e..e4cf5f7 100644 --- a/Sources/WindowSizeObserver.swift +++ b/Sources/WindowSizeObserver.swift @@ -32,7 +32,9 @@ final class WindowSizeObserver: Sendable { signal(sigwinch, SIG_IGN) signalHandler.setEventHandler { [weak self] in - guard let self = self else { return } + guard let self else { + return + } self.syncWindowSize() } signalHandler.resume() @@ -48,9 +50,9 @@ final class WindowSizeObserver: Sendable { static func getTerminalSize() -> Size { var w = winsize() #if os(Linux) - _ = ioctl(STDOUT_FILENO, UInt(TIOCGWINSZ), &w) + _ = ioctl(STDOUT_FILENO, UInt(TIOCGWINSZ), &w) #else - _ = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) + _ = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) #endif return Size(width: Int(w.ws_col), height: Int(w.ws_row)) }