Skip to content

Commit

Permalink
Update the tools to use swift-command-line-tools.
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave committed May 15, 2024
1 parent 5fe84f9 commit 8da1752
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 64 deletions.
8 changes: 8 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
"version" : "1.2.2"
}
},
{
"identity" : "swift-command-line-tools",
"kind" : "remoteSourceControl",
"location" : "https://github.com/element-hq/swift-command-line-tools.git",
"state" : {
"revision" : "a6ad90808f4f6cac615ab8496c6ff1bc5f9fa192"
}
},
{
"identity" : "yams",
"kind" : "remoteSourceControl",
Expand Down
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import PackageDescription
let package = Package(
name: "Element Swift",
platforms: [
.macOS(.v12)
.macOS(.v13)
],
products: [
.executable(name: "tools", targets: ["Tools"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "1.2.0")),
.package(url: "https://github.com/element-hq/swift-command-line-tools.git", revision: "a6ad90808f4f6cac615ab8496c6ff1bc5f9fa192"),
// .package(path: "../../../swift-command-line-tools"),
.package(url: "https://github.com/jpsim/Yams", .upToNextMinor(from: "5.0.0"))
],
targets: [
.executableTarget(name: "Tools",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "CommandLineTools", package: "swift-command-line-tools"),
.product(name: "Yams", package: "Yams")
],
path: "Tools/Sources")
Expand Down
17 changes: 9 additions & 8 deletions Tools/Sources/BuildSDK.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ArgumentParser
import CommandLineTools
import Foundation
import Yams

Expand Down Expand Up @@ -67,7 +68,7 @@ struct BuildSDK: ParsableCommand {
/// but only when the ``target`` option hasn't been supplied.
func checkRustupTargets() throws {
guard target.isEmpty, device == 0, simulator == 0 else { return }
guard let output = try Utilities.zsh("rustup show") else { throw Error.rustupOutputFailure }
guard let output = try Zsh.run(command: "rustup show") else { throw Error.rustupOutputFailure }

var requiredTargets = Target.allCases.reduce(into: [String: Bool]()) { partialResult, target in
partialResult[target.rawValue] = false
Expand All @@ -85,14 +86,14 @@ 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: Utilities.sdkDirectoryURL.path) else { return }
try Utilities.zsh("git clone https://github.com/matrix-org/matrix-rust-sdk", workingDirectoryURL: Utilities.parentDirectoryURL)
guard !FileManager.default.fileExists(atPath: URL.sdkDirectory.path) else { return }
try Zsh.run(command: "git clone https://github.com/matrix-org/matrix-rust-sdk", directory: .parentDirectory)
}

/// Checkout the specified branch of the SDK if supplied.
func checkoutBranchIfSupplied() throws {
guard let branch else { return }
try Utilities.zsh("git checkout \(branch)", workingDirectoryURL: Utilities.sdkDirectoryURL)
try Zsh.run(command: "git checkout \(branch)", directory: .sdkDirectory)
}

/// Build the Rust SDK as an XCFramework with the debug profile.
Expand All @@ -103,7 +104,7 @@ struct BuildSDK: ParsableCommand {
if device > 0 {
buildCommand.append(" --target \(Target.iOS.rawValue)")
} else if simulator > 0 {
let hostArchitecture = try Utilities.zsh("arch")
let hostArchitecture = try Zsh.run(command: "arch")
if hostArchitecture?.trimmingCharacters(in: .whitespacesAndNewlines) == "arm64" {
buildCommand.append(" --target \(Target.simulatorARM64.rawValue)")
} else {
Expand All @@ -112,18 +113,18 @@ struct BuildSDK: ParsableCommand {
} else if !target.isEmpty {
target.forEach { buildCommand.append(" --target \($0.rawValue)") }
}
try Utilities.zsh(buildCommand, workingDirectoryURL: Utilities.sdkDirectoryURL)
try Zsh.run(command: buildCommand, directory: .sdkDirectory)
}

/// Update the Xcode project to use the build of the SDK.
func updateXcodeProject() throws {
try updateProjectYAML()
try Utilities.zsh("xcodegen")
try Zsh.run(command: "xcodegen")
}

/// Update project.yml with the local path of the SDK.
func updateProjectYAML() throws {
let yamlURL = Utilities.projectDirectoryURL.appendingPathComponent("project.yml")
let yamlURL = URL.projectDirectory.appendingPathComponent("project.yml")
let yamlString = try String(contentsOf: yamlURL)
guard var projectConfig = try Yams.compose(yaml: yamlString) else { throw Error.failureParsingProjectYAML }

Expand Down
5 changes: 3 additions & 2 deletions Tools/Sources/DownloadStrings.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ArgumentParser
import CommandLineTools
import Foundation

struct DownloadStrings: ParsableCommand {
Expand All @@ -14,10 +15,10 @@ struct DownloadStrings: ParsableCommand {

private func localazyDownload() throws {
let arguments = allLanguages ? " all" : ""
try Utilities.zsh("localazy download\(arguments)")
try Zsh.run(command: "localazy download\(arguments)")
}

private func swiftgen() throws {
try Utilities.zsh("swiftgen config run --config Tools/SwiftGen/swiftgen-config.yml")
try Zsh.run(command: "swiftgen config run --config Tools/SwiftGen/swiftgen-config.yml")
}
}
3 changes: 1 addition & 2 deletions Tools/Sources/GenerateSAS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ struct GenerateSAS: ParsableCommand {
}

private func writeToFile(file: String, dict: [String: String], subdirectory: String) {
let projectDirectory = Utilities.projectDirectoryURL
let fileDirectory = projectDirectory.appendingPathComponent("ElementX/Resources/Localizations").appendingPathComponent(subdirectory)
let fileDirectory = URL.projectDirectory.appendingPathComponent("ElementX/Resources/Localizations").appendingPathComponent(subdirectory)

let filePath = fileDirectory.appendingPathComponent(file)

Expand Down
5 changes: 3 additions & 2 deletions Tools/Sources/GenerateSDKMocks.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ArgumentParser
import CommandLineTools
import Foundation

struct GenerateSDKMocks: ParsableCommand {
Expand All @@ -15,7 +16,7 @@ struct GenerateSDKMocks: ParsableCommand {

func run() throws {
if version == "local" {
try generateSDKMocks(ffiPath: "\(Utilities.sdkDirectoryURL.path)/bindings/apple/generated/swift")
try generateSDKMocks(ffiPath: "\(URL.sdkDirectory.path)/bindings/apple/generated/swift")
} else {
try downloadSDK(version: version) { path in
try generateSDKMocks(ffiPath: path)
Expand All @@ -26,7 +27,7 @@ struct GenerateSDKMocks: ParsableCommand {

/// Generates the SDK mocks using Sourcery.
func generateSDKMocks(ffiPath: String) throws {
try Utilities.zsh("sourcery --sources \(ffiPath) --sources ElementX/Sources/Mocks/SDK --templates Tools/Sourcery --output ElementX/Sources/Mocks/Generated/SDKGeneratedMocks.swift --args autoMockableImports=\"Foundation\",autoMockableImports=\"MatrixRustSDK\"")
try Zsh.run(command: "sourcery --sources \(ffiPath) --sources ElementX/Sources/Mocks/SDK --templates Tools/Sourcery --output ElementX/Sources/Mocks/Generated/SDKGeneratedMocks.swift --args autoMockableImports=\"Foundation\",autoMockableImports=\"MatrixRustSDK\"")
}

/// Downloads the specified version of the `matrix_sdk_ffi.swift` file and returns the path to the downloaded file.
Expand Down
7 changes: 4 additions & 3 deletions Tools/Sources/Locheck.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ArgumentParser
import CommandLineTools
import Foundation

struct Locheck: ParsableCommand {
Expand All @@ -19,7 +20,7 @@ struct Locheck: ParsableCommand {
static var configuration = CommandConfiguration(abstract: "A tool that verifies bad strings contained in localization files")

private var stringsDirectoryURL: URL {
Utilities.projectDirectoryURL.appendingPathComponent("ElementX/Resources/Localizations")
.projectDirectory.appendingPathComponent("ElementX/Resources/Localizations")
}

func run() throws {
Expand All @@ -28,14 +29,14 @@ struct Locheck: ParsableCommand {
}

func checkStrings() throws {
guard let output = try Utilities.zsh("mint run locheck discoverlproj --ignore-missing --ignore lproj_file_missing_from_translation --treat-warnings-as-errors \(stringsDirectoryURL.path)") else {
guard let output = try Zsh.run(command: "mint run locheck discoverlproj --ignore-missing --ignore lproj_file_missing_from_translation --treat-warnings-as-errors \(stringsDirectoryURL.path)") else {
throw LocheckError.missingMint
}
print(output)
}

private func checkMint() throws {
let result = try Utilities.zsh("which mint")
let result = try Zsh.run(command: "which mint")

if result?.contains("not found") == true {
throw LocheckError.missingMint
Expand Down
7 changes: 4 additions & 3 deletions Tools/Sources/OutdatedPackages.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import ArgumentParser
import CommandLineTools
import Foundation

struct OutdatedPackages: ParsableCommand {
static var configuration = CommandConfiguration(abstract: "A tool to check outdated package dependencies. Please make sure you have already run setup-project before using this tool.")

private var projectSwiftPMDirectoryURL: URL { Utilities.projectDirectoryURL.appendingPathComponent("ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm") }
private var projectSwiftPMDirectoryURL: URL { .projectDirectory.appendingPathComponent("ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm") }

func run() throws {
try checkToolsDependencies()
try checkProjectDependencies()
}

func checkToolsDependencies() throws {
guard let output = try Utilities.zsh("swift outdated"), !output.isEmpty else { return }
guard let output = try Zsh.run(command: "swift outdated"), !output.isEmpty else { return }
print("outdated tools Swift packages:\n\(output)")
}

func checkProjectDependencies() throws {
guard let output = try Utilities.zsh("swift outdated", workingDirectoryURL: projectSwiftPMDirectoryURL), !output.isEmpty else { return }
guard let output = try Zsh.run(command: "swift outdated", directory: projectSwiftPMDirectoryURL), !output.isEmpty else { return }
print("outdated project Swift packages:\n\(output)")
}
}
9 changes: 5 additions & 4 deletions Tools/Sources/SetupProject.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ArgumentParser
import CommandLineTools
import Foundation

struct SetupProject: ParsableCommand {
Expand All @@ -12,18 +13,18 @@ struct SetupProject: ParsableCommand {
}

func setupGitHooks() throws {
try Utilities.zsh("git config core.hooksPath .githooks")
try Zsh.run(command: "git config core.hooksPath .githooks")
}

func brewBundleInstall() throws {
try Utilities.zsh("brew install xcodegen swiftgen swiftformat git-lfs sourcery mint kiliankoe/formulae/swift-outdated localazy/tools/localazy peripheryapp/periphery/periphery")
try Zsh.run(command: "brew install xcodegen swiftgen swiftformat git-lfs sourcery mint kiliankoe/formulae/swift-outdated localazy/tools/localazy peripheryapp/periphery/periphery")
}

func mintPackagesInstall() throws {
try Utilities.zsh("mint install Asana/locheck")
try Zsh.run(command: "mint install Asana/locheck")
}

func xcodegen() throws {
try Utilities.zsh("xcodegen")
try Zsh.run(command: "xcodegen")
}
}
8 changes: 8 additions & 0 deletions Tools/Sources/URL.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ArgumentParser
import Foundation

extension URL {
static var projectDirectory: URL { URL(fileURLWithPath: FileManager.default.currentDirectoryPath) }
static var parentDirectory: URL { .projectDirectory.deletingLastPathComponent() }
static var sdkDirectory: URL { .parentDirectory.appendingPathComponent("matrix-rust-sdk") }
}
39 changes: 0 additions & 39 deletions Tools/Sources/Utilities.swift

This file was deleted.

0 comments on commit 8da1752

Please sign in to comment.