Skip to content

Commit

Permalink
project: handle process interruption in project long running commands (
Browse files Browse the repository at this point in the history
  • Loading branch information
kattouf authored Jan 2, 2025
1 parent 7ffd0ca commit 0508b78
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 31 deletions.
16 changes: 8 additions & 8 deletions SakeApp/MiseCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ struct MiseCommands {
skipIf: { _ in
run("mise", "which", "swiftformat").succeeded
},
run: { _ in
try runAndPrint("mise", "install", "swiftformat")
run: { context in
try interruptableRunAndPrint(bash: "mise install swiftformat", interruptionHandler: context.interruptionHandler)
}
)
}
Expand All @@ -21,8 +21,8 @@ struct MiseCommands {
skipIf: { _ in
run("mise", "which", "xcbeautify").succeeded
},
run: { _ in
try runAndPrint("mise", "install", "xcbeautify")
run: { context in
try interruptableRunAndPrint(bash: "mise install xcbeautify", interruptionHandler: context.interruptionHandler)
}
)
}
Expand All @@ -33,8 +33,8 @@ struct MiseCommands {
skipIf: { _ in
run("mise", "which", "gh").succeeded
},
run: { _ in
try runAndPrint("mise", "install", "gh")
run: { context in
try interruptableRunAndPrint(bash: "mise install gh", interruptionHandler: context.interruptionHandler)
}
)
}
Expand All @@ -45,8 +45,8 @@ struct MiseCommands {
skipIf: { _ in
run("mise", "which", "git-cliff").succeeded
},
run: { _ in
try runAndPrint("mise", "install", "git-cliff")
run: { context in
try interruptableRunAndPrint(bash: "mise install git-cliff", interruptionHandler: context.interruptionHandler)
}
)
}
Expand Down
6 changes: 3 additions & 3 deletions SakeApp/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"location" : "https://github.com/kattouf/Sake.git",
"state" : {
"branch" : "main",
"revision" : "8e04ff5437c05bc6f5d7999d5d35919b82ab58ee"
"revision" : "7ffd0ca30ddc02f13c94701d72f12f5f005e6f96"
}
},
{
Expand Down Expand Up @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax",
"state" : {
"revision" : "64889f0c732f210a935a0ad7cda38f77f876262d",
"version" : "509.1.1"
"revision" : "0687f71944021d616d34d922343dcef086855920",
"version" : "600.0.1"
}
},
{
Expand Down
11 changes: 6 additions & 5 deletions SakeApp/ReleaseCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,21 @@ struct ReleaseCommands {
}
}()

try runAndPrint(bash: swiftClean)
try runAndPrint(bash: swiftBuild)
try interruptableRunAndPrint(bash: swiftClean, interruptionHandler: context.interruptionHandler)
try interruptableRunAndPrint(bash: swiftBuild, interruptionHandler: context.interruptionHandler)

let binPath: String = run(bash: "\(swiftBuild) --show-bin-path").stdout
if binPath.isEmpty {
throw NSError(domain: "Fail to get bin path", code: -999)
}
let executablePath = binPath + "/\(Constants.executableName)"

try runAndPrint(bash: "\(strip) \(executablePath)")
try interruptableRunAndPrint(bash: "\(strip) \(executablePath)", interruptionHandler: context.interruptionHandler)

let executableArchivePath = executableArchivePath(target: target, version: version)
try runAndPrint(
bash: "\(zip) \(executableArchivePath) \(executablePath.replacingOccurrences(of: "/workdir", with: context.projectRoot))"
try interruptableRunAndPrint(
bash: "\(zip) \(executableArchivePath) \(executablePath.replacingOccurrences(of: "/workdir", with: context.projectRoot))",
interruptionHandler: context.interruptionHandler
)
}

Expand Down
8 changes: 8 additions & 0 deletions SakeApp/SwifShell+Interruption.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Sake
import SwiftShell

func interruptableRunAndPrint(bash command: String, interruptionHandler: Command.Context.InterruptionHandler) throws {
let asyncCommand = runAsyncAndPrint(bash: command)
interruptionHandler.register(asyncCommand)
try asyncCommand.finish()
}
36 changes: 21 additions & 15 deletions SakeApp/TestCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ struct TestCommands {
Command(
description: "Run tests",
dependencies: [
cleanIfNeeded,
unitTests.mapArguments { arguments in arguments.filter { $0 != "--clean" } },
integrationTests.mapArguments { arguments in arguments.filter { $0 != "--clean" } },
buildTests,
unitTests.mapArguments { arguments in arguments + ["--skip-build"] },
integrationTests.mapArguments { arguments in arguments + ["--skip-build"] },
]
)
}
Expand All @@ -27,19 +27,21 @@ struct TestCommands {
Command(
description: "Build tests",
dependencies: [cleanIfNeeded],
run: { _ in
try runAndPrint(bash: "swift build --build-tests")
skipIf: { context in
let arguments = try TestArguments.parse(context.arguments)
return arguments.skipBuild
},
run: { context in
try interruptableRunAndPrint(bash: "swift build --build-tests", interruptionHandler: context.interruptionHandler)
}
)
}

public static var unitTests: Command {
Command(
description: "Run unit tests",
dependencies: [cleanIfNeeded],
dependencies: [buildTests],
run: { context in
let arguments = try TestArguments.parse(context.arguments)
let skipBuild = arguments.skipBuild ? " --skip-build" : ""
let shouldBeautifyLog = context.environment["GITHUB_ACTIONS"] == nil
let beautifyLog = shouldBeautifyLog ? " | mise exec -- xcbeautify --disable-logging" : ""
if shouldBeautifyLog {
Expand All @@ -49,18 +51,19 @@ struct TestCommands {
)
.run()
}
try runAndPrint(bash: "swift test --filter \"^(?!.*\\bIntegrationTests\\b).*\"\(skipBuild)\(beautifyLog)")
try interruptableRunAndPrint(
bash: "swift test --skip-build --filter \"^(?!.*\\bIntegrationTests\\b).*\"\(beautifyLog)",
interruptionHandler: context.interruptionHandler
)
}
)
}

public static var integrationTests: Command {
Command(
description: "Run integration tests",
dependencies: [cleanIfNeeded],
dependencies: [buildTests],
run: { context in
let arguments = try TestArguments.parse(context.arguments)
let skipBuild = arguments.skipBuild ? " --skip-build" : ""
let shouldBeautifyLog = context.environment["GITHUB_ACTIONS"] == nil
let beautifyLog = shouldBeautifyLog ? " | mise exec -- xcbeautify --disable-logging" : ""
if shouldBeautifyLog {
Expand All @@ -70,7 +73,10 @@ struct TestCommands {
)
.run()
}
try runAndPrint(bash: "swift test --filter IntegrationTests\(skipBuild)\(beautifyLog)")
try interruptableRunAndPrint(
bash: "swift test --skip-build --filter IntegrationTests\(beautifyLog)",
interruptionHandler: context.interruptionHandler
)
}
)
}
Expand All @@ -82,8 +88,8 @@ struct TestCommands {
let arguments = try TestArguments.parse(context.arguments)
return !arguments.clean
},
run: { _ in
try runAndPrint("swift", "package", "clean")
run: { context in
try interruptableRunAndPrint(bash: "swift package clean", interruptionHandler: context.interruptionHandler)
}
)
}
Expand Down

0 comments on commit 0508b78

Please sign in to comment.