Skip to content

Commit

Permalink
project: add lint/format commands. reformat project
Browse files Browse the repository at this point in the history
  • Loading branch information
kattouf committed Nov 17, 2024
1 parent 23bb23a commit f9b542a
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 34 deletions.
11 changes: 11 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions SakeApp/BrewCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 15 additions & 11 deletions SakeApp/ReleaseCommands.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ArgumentParser
import Foundation
import CryptoKit
import Foundation
import Sake
import SwiftShell

Expand All @@ -11,6 +11,7 @@ struct ReleaseCommands {
case x86
case arm
}

enum OS {
case macos
case linux
Expand All @@ -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"
}
}
}
Expand Down Expand Up @@ -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)",
Expand All @@ -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"
)
}
}()
Expand All @@ -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)'")
Expand Down
22 changes: 21 additions & 1 deletion SakeApp/Sakefile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
)
}
)
Expand Down
6 changes: 6 additions & 0 deletions Sources/ANSI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/ActivityIndicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions Sources/ErrorMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/FileHandler+AsyncStream.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Printer.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
15 changes: 9 additions & 6 deletions Sources/ProgressLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Sources/ProgressLineController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions Sources/WindowSizeObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
}
Expand Down

0 comments on commit f9b542a

Please sign in to comment.