/
GlobalSetupScript.swift
61 lines (45 loc) · 2.12 KB
/
GlobalSetupScript.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// GlobalSetupScript.swift
// CSAuthSample Example App
//
// Created by Charles Srstka on 4/12/20.
//
import Foundation
let env = ProcessInfo.processInfo.environment
let versionVarName = "CURRENT_PROJECT_VERSION"
let srcURL = URL(fileURLWithPath: env["SRCROOT"]!)
let derivedFileDir = URL(fileURLWithPath: env["DERIVED_FILE_DIR"]!)
let configURL = srcURL.appendingPathComponent("Common/Config/CSAuthSample-Example.xcconfig")
let version = Int(env[versionVarName]!)!
let newConfig = """
\(versionVarName) = \(version + 1)
APP_BUNDLE_ID = \(Identifiers.appID)
XPC_SERVICE_ID = \(Identifiers.xpcServiceID)
HELPER_ID = \(Identifiers.helperID)
CS_REQUIREMENT=\(getRequirement())
"""
try! newConfig.write(to: configURL, atomically: true, encoding: .utf8)
func getRequirement() -> String {
// Is there any way to generate the designated requirement string without invoking the codesign tool?
// This is a bit of a kludge, but it's the only way I've been find to do this so far other than hard-coding the
// format that `codesign` uses
let tempFileName = UUID().uuidString
let tempFileURL = `derivedFileDir`.appendingPathComponent(tempFileName)
try! Data().write(to: tempFileURL)
defer { try! FileManager.default.removeItem(at: tempFileURL) }
let codesign = Process()
codesign.executableURL = URL(fileURLWithPath: "/usr/bin/codesign")
codesign.arguments = ["-s", env["CODE_SIGN_IDENTITY"]!, "-i", "", tempFileURL.path]
try! codesign.run()
codesign.waitUntilExit()
var code: SecStaticCode?
assert(SecStaticCodeCreateWithPath(tempFileURL as CFURL, [], &code) == errSecSuccess)
var requirement: SecRequirement?
assert(SecCodeCopyDesignatedRequirement(code!, [], &requirement) == errSecSuccess)
var cfRequirementString: CFString?
assert(SecRequirementCopyString(requirement!, [], &cfRequirementString) == errSecSuccess)
var requirementString = cfRequirementString! as String
let identifierRange = requirementString.range(of: "identifier \"\(tempFileName)\" and ")!
requirementString.replaceSubrange(identifierRange, with: "")
return requirementString
}