Skip to content

Commit

Permalink
[Package] Updated dependencies and better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pvieito committed Dec 9, 2019
1 parent 0e2c7eb commit 159be91
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
29 changes: 20 additions & 9 deletions CodeSignKit/CodeSign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public enum CodeSign {
}

public static func sign(
at url: URL, identity: String? = nil, entitlements: URL? = nil, force: Bool = false) throws {
at url: URL, identity: String? = nil, entitlementsURL: URL? = nil, force: Bool = false) throws {
var arguments: [String] = []

let identity = identity ?? Self.defaultIdentity ?? Self.defaultBaseIdentity
arguments += ["-s", identity]

if let entitlements = entitlements {
arguments += ["--entitlements", entitlements.path]
if let entitlementsURL = entitlementsURL {
arguments += ["--entitlements", entitlementsURL.path]
}

if force {
Expand All @@ -35,8 +35,18 @@ public enum CodeSign {
let process = try Process(
executableName: "codesign",
arguments: arguments)
process.standardError = FileHandle.nullDevice
try process.runAndWaitUntilExit()

let standardErrorPipe = Pipe()
process.standardError = standardErrorPipe.fileHandleForWriting

do {
try process.runAndWaitUntilExit()
}
catch {
standardErrorPipe.fileHandleForWriting.closeFile()
FileHandle.standardError.write(standardErrorPipe.fileHandleForReading.readDataToEndOfFile())
throw error
}
}
}

Expand All @@ -51,16 +61,17 @@ extension CodeSign {

private static func signMainExecutableAndRun(file: String = #file) throws {
let targetDirectory = file.pathURL.deletingLastPathComponent()
let entitlements = targetDirectory
let targetEntitlementsURL = targetDirectory
.appendingPathComponent(targetDirectory.lastPathComponent)
.appendingPathExtension("entitlements")
let entitlementsURL = FileManager.default.fileExists(at: targetEntitlementsURL) ? targetEntitlementsURL: nil

var environment = ProcessInfo.processInfo.environment
environment[executableSignedEnvironmentKey] = "TRUE"

try Self.sign(
at: Bundle.main.executableURL!,
entitlements: entitlements,
entitlementsURL: entitlementsURL,
force: true)
let process = Process()
process.executableURL = Bundle.main.executableURL!
Expand Down
1 change: 1 addition & 0 deletions CodeSignTool/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import FoundationKit
import LoggerKit
import CommandLineKit
import CodeSignKit
import Security

let inputOption = StringOption(shortFlag: "i", longFlag: "input", helpMessage: "Input item.")
let simulatorOption = BoolOption(longFlag: "sim", helpMessage: "Read simulator entitlements.")
Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ let package = Package(
)
],
dependencies: [
.package(path: "../LoggerKit"),
.package(path: "../CommandLineKit"),
.package(path: "../FoundationKit")
.package(url: "https://github.com/pvieito/FoundationKit.git", .branch("master")),
.package(url: "https://github.com/pvieito/LoggerKit.git", .branch("master")),
.package(url: "https://github.com/pvieito/CommandLineKit.git", .branch("master"))
],
targets: [
.target(
Expand Down

0 comments on commit 159be91

Please sign in to comment.