Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors are now properly handled in build script #450

Merged
merged 7 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
DEST := /usr/local/bin/vapor

build:
swift build -c release
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? Seems a bit redundant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that if I just built it without the release flag, it defaulted to debug.

Which ultimately changed the output folder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, sorry I mean why are we doing a build here at all? The build.swift script is the one doing the build

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Duh. Apologies, should be fixed up now.

swiftc ./scripts/build.swift
./build
rm ./build
Expand Down
124 changes: 81 additions & 43 deletions scripts/build.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
#!/usr/bin/env swift
import Foundation

try build()
struct ShellError: Error {
var terminationStatus: Int32
}

func build() throws {
try withVersion(in: "Sources/VaporToolbox/Version.swift", as: currentVersion()) {
try foregroundShell(
func main() async {
do {
try await build()
} catch {
print("Error: \(error)")
exit(1)
}
}

func build() async throws {
let version = try await currentVersion()
try await withVersion(in: "Sources/VaporToolbox/Version.swift", as: version) {
try await foregroundShell(
"swift", "build",
"--disable-sandbox",
"--configuration", "release",
Expand All @@ -14,68 +26,94 @@ func build() throws {
}
}

func withVersion(in file: String, as version: String, _ closure: () throws -> ()) throws {
func withVersion(in file: String, as version: String, _ operation: () async throws -> Void) async throws {
let fileURL = URL(fileURLWithPath: file)
let originalFileContents = try String(contentsOf: fileURL, encoding: .utf8)
// set version

try originalFileContents
.replacingOccurrences(of: "nil", with: "\"\(version)\"")
.write(to: fileURL, atomically: true, encoding: .utf8)
defer {
// undo set version
try! originalFileContents

var operationError: Error?
do {
try await operation()
} catch {
operationError = error
}

// If an error occurred, revert the file change
if operationError != nil {
try? originalFileContents
.write(to: fileURL, atomically: true, encoding: .utf8)
if let error = operationError {
throw error
}
}
// run closure
try closure()
}

func currentVersion() throws -> String {

func currentVersion() async throws -> String {
do {
let tag = try backgroundShell("git", "describe", "--tags", "--exact-match")
let tag = try await backgroundShell("git", "describe", "--tags", "--exact-match")
return tag
} catch {
let branch = try backgroundShell("git", "symbolic-ref", "-q", "--short", "HEAD")
let commit = try backgroundShell("git", "rev-parse", "--short", "HEAD")
let branch = try await backgroundShell("git", "symbolic-ref", "-q", "--short", "HEAD")
let commit = try await backgroundShell("git", "rev-parse", "--short", "HEAD")
return "\(branch) (\(commit))"
}
}

func foregroundShell(_ args: String...) throws {
print("$", args.joined(separator: " "))
let task = Process()
task.executableURL = URL(fileURLWithPath: "/usr/bin/env")
task.arguments = args
try task.run()
task.waitUntilExit()
func foregroundShell(_ args: String...) async throws {
try await withCheckedThrowingContinuation { continuation in
let task = Process()
task.executableURL = URL(fileURLWithPath: "/usr/bin/env")
task.arguments = args

task.terminationHandler = { process in
if process.terminationStatus == 0 {
continuation.resume()
} else {
continuation.resume(throwing: ShellError(terminationStatus: process.terminationStatus))
}
}

guard task.terminationStatus == 0 else {
throw ShellError(terminationStatus: task.terminationStatus)
do {
try task.run()
} catch {
continuation.resume(throwing: error)
}
}
}

@discardableResult
func backgroundShell(_ args: String...) throws -> String {
let task = Process()
task.executableURL = URL(fileURLWithPath: "/usr/bin/env")
task.arguments = args
// grab stdout
let output = Pipe()
task.standardOutput = output
// ignore stderr
let error = Pipe()
task.standardError = error
try task.run()
task.waitUntilExit()
func backgroundShell(_ args: String...) async throws -> String {
try await withCheckedThrowingContinuation { continuation in
let task = Process()
task.executableURL = URL(fileURLWithPath: "/usr/bin/env")
task.arguments = args
let output = Pipe()
task.standardOutput = output
task.standardError = Pipe()

guard task.terminationStatus == 0 else {
throw ShellError(terminationStatus: task.terminationStatus)
}
task.terminationHandler = { process in
guard process.terminationStatus == 0 else {
continuation.resume(throwing: ShellError(terminationStatus: process.terminationStatus))
return
}

let data = output.fileHandleForReading.readDataToEndOfFile()
let outputString = String(decoding: data, as: UTF8.self).trimmingCharacters(in: .whitespacesAndNewlines)
continuation.resume(returning: outputString)
}

return String(decoding: output.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self)
.trimmingCharacters(in: .whitespacesAndNewlines)
do {
try task.run()
} catch {
continuation.resume(throwing: error)
}
}
}

struct ShellError: Swift.Error {
var terminationStatus: Int32
Task {
await main()
}