Swiftformat on githooks + New swift run tools setup command (#563)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
24
Tools/Sources/SetupProject.swift
Normal file
24
Tools/Sources/SetupProject.swift
Normal file
@@ -0,0 +1,24 @@
|
||||
import ArgumentParser
|
||||
import Foundation
|
||||
|
||||
struct SetupProject: ParsableCommand {
|
||||
static var configuration = CommandConfiguration(abstract: "A tool to setup the required components to efficiently run and contribute to Element X iOS")
|
||||
|
||||
func run() throws {
|
||||
try setupGitHooks()
|
||||
try brewBundleInstall()
|
||||
try xcodegen()
|
||||
}
|
||||
|
||||
func setupGitHooks() throws {
|
||||
try Utilities.zsh("git config core.hooksPath .githooks")
|
||||
}
|
||||
|
||||
func brewBundleInstall() throws {
|
||||
try Utilities.zsh("brew bundle install")
|
||||
}
|
||||
|
||||
func xcodegen() throws {
|
||||
try Utilities.zsh("xcodegen")
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,6 @@ import Foundation
|
||||
@main
|
||||
struct Tools: ParsableCommand {
|
||||
static var configuration = CommandConfiguration(abstract: "A collection of command line tools for ElementX",
|
||||
subcommands: [BuildSDK.self])
|
||||
subcommands: [BuildSDK.self,
|
||||
SetupProject.self])
|
||||
}
|
||||
|
||||
37
Tools/Sources/Utilities.swift
Normal file
37
Tools/Sources/Utilities.swift
Normal file
@@ -0,0 +1,37 @@
|
||||
import ArgumentParser
|
||||
import Foundation
|
||||
|
||||
enum Utilities {
|
||||
enum Error: LocalizedError {
|
||||
case scriptFailed(command: String, path: String)
|
||||
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case let .scriptFailed(command, path):
|
||||
return "command \(command) failed in path: \(path)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static var projectDirectoryURL: URL { URL(filePath: FileManager.default.currentDirectoryPath) }
|
||||
|
||||
/// Runs a command in zsh.
|
||||
@discardableResult
|
||||
static func zsh(_ command: String, workingDirectoryURL: URL = projectDirectoryURL) 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(command: command, path: workingDirectoryURL.absoluteString) }
|
||||
|
||||
guard let outputData = try outputPipe.fileHandleForReading.readToEnd() else { return nil }
|
||||
return String(data: outputData, encoding: .utf8)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user