Skip to content

Commit

Permalink
reformatted code
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Aug 26, 2024
1 parent 9e6fa38 commit 5873296
Show file tree
Hide file tree
Showing 26 changed files with 1,354 additions and 1,276 deletions.
104 changes: 52 additions & 52 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,56 @@
import PackageDescription

let package = Package(
name: "ReleaseTools",
platforms: [
.macOS(.v10_15)
],
products: [
.executable(name: "rt", targets: ["ReleaseTools"]),
.library(name: "Resources", targets: ["Resources"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.3.2"),
.package(url: "https://github.com/elegantchaos/Coercion.git", from: "1.1.2"),
.package(url: "https://github.com/elegantchaos/Files.git", from: "1.2.0"),
.package(url: "https://github.com/elegantchaos/Logger.git", from: "1.6.0"),
.package(url: "https://github.com/elegantchaos/Runner.git", from: "1.3.3"),
.package(url: "https://github.com/elegantchaos/XCTestExtensions.git", from: "1.3.0"),
.package(url: "https://github.com/elegantchaos/Versionator.git", from: "1.0.3"),
],
targets: [
.executableTarget(
name: "ReleaseTools",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"Coercion",
"Files",
"Logger",
"Runner",
"Resources"
],
plugins: [
.plugin(name: "VersionatorPlugin", package: "Versionator")
]
),
.target(
name: "Resources",
dependencies: [
"Files"
],
resources: [
.copy("Scripts")
]
),
.testTarget(
name: "ReleaseToolsTests",
dependencies: ["ReleaseTools", "XCTestExtensions"]
),
]
name: "ReleaseTools",

platforms: [
.macOS(.v10_15)
],

products: [
.executable(name: "rt", targets: ["ReleaseTools"]),
.library(name: "Resources", targets: ["Resources"]),
],

dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.3.2"),
.package(url: "https://github.com/elegantchaos/Coercion.git", from: "1.1.2"),
.package(url: "https://github.com/elegantchaos/Files.git", from: "1.2.0"),
.package(url: "https://github.com/elegantchaos/Logger.git", from: "1.6.0"),
.package(url: "https://github.com/elegantchaos/Runner.git", from: "1.3.3"),
.package(url: "https://github.com/elegantchaos/XCTestExtensions.git", from: "1.3.0"),
.package(url: "https://github.com/elegantchaos/Versionator.git", from: "1.0.3"),
],

targets: [
.executableTarget(
name: "ReleaseTools",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"Coercion",
"Files",
"Logger",
"Runner",
"Resources",
],
plugins: [
.plugin(name: "VersionatorPlugin", package: "Versionator")
]
),

.target(
name: "Resources",
dependencies: [
"Files"
],
resources: [
.copy("Scripts")
]
),

.testTarget(
name: "ReleaseToolsTests",
dependencies: ["ReleaseTools", "XCTestExtensions"]
),
]
)
10 changes: 5 additions & 5 deletions Sources/ReleaseTools/CommandLine+Name.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import Foundation

public extension CommandLine {
static var name: String {
let url = URL(fileURLWithPath: arguments[0])
return url.lastPathComponent
}
extension CommandLine {
public static var name: String {
let url = URL(fileURLWithPath: arguments[0])
return url.lastPathComponent
}
}
188 changes: 101 additions & 87 deletions Sources/ReleaseTools/Commands/AppcastCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,102 +3,116 @@
// All code (c) 2019 - present day, Elegant Chaos Limited.
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

import ArgumentParser
import Foundation
import Runner
import ArgumentParser

enum AppcastError: Error, CustomStringConvertible {
case buildAppcastGeneratorFailed(_ output: String)
case appcastGeneratorFailed(_ result: Runner.Result)
case keyGenerationFailed(_ result: Runner.Result)
case keyImportFailed(_ result: Runner.Result)
case generatedKeys(_ name: String)

public var description: String {
switch self {
case .buildAppcastGeneratorFailed(let output): return "Failed to build the generate_appcast tool.\n\(output)"
case .appcastGeneratorFailed(let result): return "Failed to generate the appcast.\n\(result)"
case .keyGenerationFailed(let result): return "Failed to generate appcast keys.\n\(result)"
case .keyImportFailed(let result): return "Failed to import appcast keys.\n\(result)"
case .generatedKeys(let name): return """
The appcast private key was missing, so we've generated one.
Open the keychain, rename the key `Imported Private Key` as `\(name)`, then try running this command again.
"""
}
case buildAppcastGeneratorFailed(_ output: String)
case appcastGeneratorFailed(_ result: Runner.Result)
case keyGenerationFailed(_ result: Runner.Result)
case keyImportFailed(_ result: Runner.Result)
case generatedKeys(_ name: String)

public var description: String {
switch self {
case .buildAppcastGeneratorFailed(let output):
return "Failed to build the generate_appcast tool.\n\(output)"
case .appcastGeneratorFailed(let result): return "Failed to generate the appcast.\n\(result)"
case .keyGenerationFailed(let result): return "Failed to generate appcast keys.\n\(result)"
case .keyImportFailed(let result): return "Failed to import appcast keys.\n\(result)"
case .generatedKeys(let name):
return """
The appcast private key was missing, so we've generated one.
Open the keychain, rename the key `Imported Private Key` as `\(name)`, then try running this command again.
"""
}
}
}


struct AppcastCommand: ParsableCommand {
static var configuration = CommandConfiguration(
commandName: "appcast",
abstract: "Update the Sparkle appcast to include the zip created by the compress command."
static var configuration = CommandConfiguration(
commandName: "appcast",
abstract: "Update the Sparkle appcast to include the zip created by the compress command."
)

@Option(
help: "Path the to the keychain to get the appcast key from. Defaults to the login keychain.")
var keychain: String?
@OptionGroup() var scheme: SchemeOption
@OptionGroup() var platform: PlatformOption
@OptionGroup() var updates: UpdatesOption
@OptionGroup() var options: CommonOptions

func run() throws {
let parsed = try OptionParser(
options: options,
command: Self.configuration,
scheme: scheme,
platform: platform
)

@Option(help: "Path the to the keychain to get the appcast key from. Defaults to the login keychain.") var keychain: String?
@OptionGroup() var scheme: SchemeOption
@OptionGroup() var platform: PlatformOption
@OptionGroup() var updates: UpdatesOption
@OptionGroup() var options: CommonOptions

func run() throws {
let parsed = try OptionParser(
options: options,
command: Self.configuration,
scheme: scheme,
platform: platform
)

let xcode = XCodeBuildRunner(parsed: parsed)

let keyChainPath = keychain ?? parsed.getDefault(for: "keychain") ?? ("~/Library/Keychains/login.keychain" as NSString).expandingTildeInPath

parsed.log("Rebuilding appcast.")
let fm = FileManager.default
let rootURL = URL(fileURLWithPath: fm.currentDirectoryPath)
let buildURL = rootURL.appendingPathComponent(".build")
let result = try xcode.run(arguments: ["build", "-workspace", parsed.workspace, "-scheme", "generate_appcast", "BUILD_DIR=\(buildURL.path)"])
if result.status != 0 {
throw AppcastError.buildAppcastGeneratorFailed(result.stderr)
}

let workspaceName = URL(fileURLWithPath: parsed.workspace).deletingPathExtension().lastPathComponent
let keyName = "\(workspaceName) Sparkle Key"

let generator = Runner(for: URL(fileURLWithPath: ".build/Release/generate_appcast"))
let genResult = try generator.sync(arguments: ["-n", keyName, "-k", keyChainPath, updates.path])
if genResult.status != 0 {
if !genResult.stdout.contains("Unable to load DSA private key") {
throw AppcastError.appcastGeneratorFailed(genResult)
}

parsed.log("Could not find Sparkle key - generating one.")

let keygen = Runner(for: URL(fileURLWithPath: "Dependencies/Sparkle/bin/generate_keys"))
let keygenResult = try keygen.sync(arguments: [])
if keygenResult.status != 0 {
throw AppcastError.keyGenerationFailed(keygenResult)
}

parsed.log("Importing Key.")

let security = Runner(for: URL(fileURLWithPath: "/usr/bin/security"))
let importResult = try security.sync(arguments: ["import", "dsa_priv.pem", "-a", "labl", "\(parsed.scheme) Sparkle Key"])
if importResult.status != 0 {
throw AppcastError.keyImportFailed(importResult)
}

parsed.log("Moving Public Key.")

try? fm.moveItem(at: rootURL.appendingPathComponent("dsa_pub.pem"), to: rootURL.appendingPathComponent("Sources").appendingPathComponent(parsed.scheme).appendingPathComponent("Resources").appendingPathComponent("dsa_pub.pem"))

parsed.log("Deleting Private Key.")

try? fm.removeItem(at: rootURL.appendingPathComponent("dsa_priv.pem"))

throw AppcastError.generatedKeys(keyName)
}

try? fm.removeItem(at: updates.url.appendingPathComponent(".tmp"))
let xcode = XCodeBuildRunner(parsed: parsed)

let keyChainPath =
keychain ?? parsed.getDefault(for: "keychain")
?? ("~/Library/Keychains/login.keychain" as NSString).expandingTildeInPath

parsed.log("Rebuilding appcast.")
let fm = FileManager.default
let rootURL = URL(fileURLWithPath: fm.currentDirectoryPath)
let buildURL = rootURL.appendingPathComponent(".build")
let result = try xcode.run(arguments: [
"build", "-workspace", parsed.workspace, "-scheme", "generate_appcast",
"BUILD_DIR=\(buildURL.path)",
])
if result.status != 0 {
throw AppcastError.buildAppcastGeneratorFailed(result.stderr)
}

let workspaceName = URL(fileURLWithPath: parsed.workspace).deletingPathExtension()
.lastPathComponent
let keyName = "\(workspaceName) Sparkle Key"

let generator = Runner(for: URL(fileURLWithPath: ".build/Release/generate_appcast"))
let genResult = try generator.sync(arguments: ["-n", keyName, "-k", keyChainPath, updates.path])
if genResult.status != 0 {
if !genResult.stdout.contains("Unable to load DSA private key") {
throw AppcastError.appcastGeneratorFailed(genResult)
}

parsed.log("Could not find Sparkle key - generating one.")

let keygen = Runner(for: URL(fileURLWithPath: "Dependencies/Sparkle/bin/generate_keys"))
let keygenResult = try keygen.sync(arguments: [])
if keygenResult.status != 0 {
throw AppcastError.keyGenerationFailed(keygenResult)
}

parsed.log("Importing Key.")

let security = Runner(for: URL(fileURLWithPath: "/usr/bin/security"))
let importResult = try security.sync(arguments: [
"import", "dsa_priv.pem", "-a", "labl", "\(parsed.scheme) Sparkle Key",
])
if importResult.status != 0 {
throw AppcastError.keyImportFailed(importResult)
}

parsed.log("Moving Public Key.")

try? fm.moveItem(
at: rootURL.appendingPathComponent("dsa_pub.pem"),
to: rootURL.appendingPathComponent("Sources").appendingPathComponent(parsed.scheme)
.appendingPathComponent("Resources").appendingPathComponent("dsa_pub.pem"))

parsed.log("Deleting Private Key.")

try? fm.removeItem(at: rootURL.appendingPathComponent("dsa_priv.pem"))

throw AppcastError.generatedKeys(keyName)
}

try? fm.removeItem(at: updates.url.appendingPathComponent(".tmp"))
}
}
Loading

0 comments on commit 5873296

Please sign in to comment.