Swiftformat on githooks + New swift run tools setup command (#563)

This commit is contained in:
Mauro
2023-02-10 14:04:00 +01:00
committed by GitHub
parent f0a4cfd946
commit c8fb93e00d
11 changed files with 123 additions and 32 deletions
+7 -29
View File
@@ -11,12 +11,10 @@ struct BuildSDK: ParsableCommand {
@Option(help: "The target to build for such as aarch64-apple-ios. Omit this option to build for all targets.")
var target: String?
private var projectDirectoryURL: URL { URL(filePath: FileManager.default.currentDirectoryPath) }
private var parentDirectoryURL: URL { projectDirectoryURL.deletingLastPathComponent() }
private var parentDirectoryURL: URL { Utilities.projectDirectoryURL.deletingLastPathComponent() }
private var sdkDirectoryURL: URL { parentDirectoryURL.appending(path: "matrix-rust-sdk") }
enum Error: LocalizedError {
case scriptFailed
case rustupOutputFailure
case missingRustTargets([String])
case failureParsingProjectYAML
@@ -49,7 +47,7 @@ struct BuildSDK: ParsableCommand {
/// but only when the ``target`` option hasn't been supplied.
func checkRustupTargets() throws {
guard target == nil else { return }
guard let output = try zsh("rustup show", workingDirectoryURL: projectDirectoryURL) else { throw Error.rustupOutputFailure }
guard let output = try Utilities.zsh("rustup show") else { throw Error.rustupOutputFailure }
var requiredTargets = [
"aarch64-apple-darwin": false,
@@ -71,13 +69,13 @@ struct BuildSDK: ParsableCommand {
/// Clones the Rust SDK if a copy isn't found in the parent directory.
func cloneSDKIfNeeded() throws {
guard !FileManager.default.fileExists(atPath: sdkDirectoryURL.path) else { return }
try zsh("git clone https://github.com/matrix-org/matrix-rust-sdk", workingDirectoryURL: parentDirectoryURL)
try Utilities.zsh("git clone https://github.com/matrix-org/matrix-rust-sdk", workingDirectoryURL: parentDirectoryURL)
}
/// Checkout the specified branch of the SDK if supplied.
func checkoutBranchIfSupplied() throws {
guard let branch else { return }
try zsh("git checkout \(branch)", workingDirectoryURL: sdkDirectoryURL)
try Utilities.zsh("git checkout \(branch)", workingDirectoryURL: sdkDirectoryURL)
}
/// Build the Rust SDK as an XCFramework with the debug profile.
@@ -86,18 +84,18 @@ struct BuildSDK: ParsableCommand {
if let target {
buildCommand.append(" --only-target \(target)")
}
try zsh(buildCommand, workingDirectoryURL: sdkDirectoryURL)
try Utilities.zsh(buildCommand, workingDirectoryURL: sdkDirectoryURL)
}
/// Update the Xcode project to use the build of the SDK.
func updateXcodeProject() throws {
try updateProjectYAML()
try zsh("xcodegen", workingDirectoryURL: projectDirectoryURL)
try Utilities.zsh("xcodegen")
}
/// Update project.yml with the local path of the SDK.
func updateProjectYAML() throws {
let yamlURL = projectDirectoryURL.appending(path: "project.yml")
let yamlURL = Utilities.projectDirectoryURL.appending(path: "project.yml")
let yamlString = try String(contentsOf: yamlURL)
guard var projectConfig = try Yams.compose(yaml: yamlString) else { throw Error.failureParsingProjectYAML }
@@ -106,24 +104,4 @@ struct BuildSDK: ParsableCommand {
let updatedYAMLString = try Yams.serialize(node: projectConfig)
try updatedYAMLString.write(to: yamlURL, atomically: true, encoding: .utf8)
}
/// Runs a command in zsh.
@discardableResult
func zsh(_ command: String, workingDirectoryURL: URL) throws -> String? {
let process = Process()
process.executableURL = URL(filePath: "/bin/zsh")
process.arguments = ["-c", command]
process.currentDirectoryURL = workingDirectoryURL
let outputPipe = Pipe()
process.standardOutput = outputPipe
try process.run()
process.waitUntilExit()
guard process.terminationReason == .exit, process.terminationStatus == 0 else { throw Error.scriptFailed }
guard let outputData = try outputPipe.fileHandleForReading.readToEnd() else { return nil }
return String(data: outputData, encoding: .utf8)
}
}