forked from Carthage/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildSettings.swift
More file actions
222 lines (189 loc) · 7.82 KB
/
BuildSettings.swift
File metadata and controls
222 lines (189 loc) · 7.82 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import Foundation
import Result
import ReactiveSwift
import XCDBLD
/// A map of build settings and their values, as generated by Xcode.
public struct BuildSettings {
/// The target to which these settings apply.
public let target: String
/// All build settings given at initialization.
public let settings: [String: String]
public init(target: String, settings: [String: String]) {
self.target = target
self.settings = settings
}
/// Matches lines of the forms:
///
/// Build settings for action build and target "ReactiveCocoaLayout Mac":
/// Build settings for action test and target CarthageKitTests:
private static let targetSettingsRegex = try! NSRegularExpression(pattern: "^Build settings for action (?:\\S+) and target \\\"?([^\":]+)\\\"?:$", options: [ .caseInsensitive, .anchorsMatchLines ])
/// Invokes `xcodebuild` to retrieve build settings for the given build
/// arguments.
///
/// Upon .success, sends one BuildSettings value for each target included in
/// the referenced scheme.
public static func loadWithArguments(_ arguments: BuildArguments) -> SignalProducer<BuildSettings, CarthageError> {
// xcodebuild (in Xcode 8) has a bug where xcodebuild -showBuildSettings
// can hang indefinitely on projects that contain core data models.
// rdar://27052195
// Including the action "clean" works around this issue, which is further
// discussed here: https://forums.developer.apple.com/thread/50372
let task = xcodebuildTask(["clean", "-showBuildSettings", "-skipUnavailableActions"], arguments)
return task.launch()
.ignoreTaskData()
.mapError(CarthageError.taskError)
// xcodebuild has a bug where xcodebuild -showBuildSettings
// can sometimes hang indefinitely on projects that don't
// share any schemes, so automatically bail out if it looks
// like that's happening.
.timeout(after: 60, raising: .xcodebuildTimeout(arguments.project), on: QueueScheduler(qos: .default))
.retry(upTo: 5)
.map { data in
return String(data: data, encoding: .utf8)!
}
.flatMap(.merge) { string -> SignalProducer<BuildSettings, CarthageError> in
return SignalProducer { observer, disposable in
var currentSettings: [String: String] = [:]
var currentTarget: String?
let flushTarget = { () -> () in
if let currentTarget = currentTarget {
let buildSettings = self.init(target: currentTarget, settings: currentSettings)
observer.send(value: buildSettings)
}
currentTarget = nil
currentSettings = [:]
}
string.enumerateLines { line, stop in
if disposable.isDisposed {
stop = true
return
}
if let result = self.targetSettingsRegex.firstMatch(in: line, range: NSMakeRange(0, line.utf16.count)) {
let targetRange = result.rangeAt(1)
flushTarget()
currentTarget = (line as NSString).substring(with: targetRange)
return
}
let trimSet = CharacterSet.whitespacesAndNewlines
let components = line.characters
.split(maxSplits: 1) { $0 == "=" }
.map { String($0).trimmingCharacters(in: trimSet) }
if components.count == 2 {
currentSettings[components[0]] = components[1]
}
}
flushTarget()
observer.sendCompleted()
}
}
}
/// Determines which SDKs the given scheme builds for, by default.
///
/// If an SDK is unrecognized or could not be determined, an error will be
/// sent on the returned signal.
public static func SDKsForScheme(_ scheme: String, inProject project: ProjectLocator) -> SignalProducer<SDK, CarthageError> {
return loadWithArguments(BuildArguments(project: project, scheme: scheme))
.take(first: 1)
.flatMap(.merge) { $0.buildSDKs }
}
/// Returns the value for the given build setting, or an error if it could
/// not be determined.
public subscript(key: String) -> Result<String, CarthageError> {
if let value = settings[key] {
return .success(value)
} else {
return .failure(.missingBuildSetting(key))
}
}
/// Attempts to determine the SDKs this scheme builds for.
public var buildSDKs: SignalProducer<SDK, CarthageError> {
let supportedPlatforms = self["SUPPORTED_PLATFORMS"]
if let supportedPlatforms = supportedPlatforms.value {
let platforms = supportedPlatforms.characters.split { $0 == " " }.map(String.init)
return SignalProducer<String, CarthageError>(platforms)
.map { platform in SignalProducer(result: SDK.from(string: platform)) }
.flatten(.merge)
}
let firstBuildSDK = self["PLATFORM_NAME"].flatMap(SDK.from(string:))
return SignalProducer(result: firstBuildSDK)
}
/// Attempts to determine the ProductType specified in these build settings.
public var productType: Result<ProductType, CarthageError> {
return self["PRODUCT_TYPE"].flatMap(ProductType.from(string:))
}
/// Attempts to determine the MachOType specified in these build settings.
public var machOType: Result<MachOType, CarthageError> {
return self["MACH_O_TYPE"].flatMap(MachOType.from(string:))
}
/// Attempts to determine the FrameworkType identified by these build settings.
internal var frameworkType: Result<FrameworkType?, CarthageError> {
return productType.fanout(machOType).map(FrameworkType.init)
}
/// Attempts to determine the URL to the built products directory.
public var builtProductsDirectoryURL: Result<URL, CarthageError> {
return self["BUILT_PRODUCTS_DIR"].map { productsDir in
return URL(fileURLWithPath: productsDir, isDirectory: true)
}
}
/// Attempts to determine the relative path (from the build folder) to the
/// built executable.
public var executablePath: Result<String, CarthageError> {
return self["EXECUTABLE_PATH"]
}
/// Attempts to determine the URL to the built executable.
public var executableURL: Result<URL, CarthageError> {
return builtProductsDirectoryURL
.fanout(executablePath)
.map { builtProductsURL, executablePath in
return builtProductsURL.appendingPathComponent(executablePath)
}
}
/// Attempts to determine the name of the built product's wrapper bundle.
public var wrapperName: Result<String, CarthageError> {
return self["WRAPPER_NAME"]
}
/// Attempts to determine the URL to the built product's wrapper.
public var wrapperURL: Result<URL, CarthageError> {
return builtProductsDirectoryURL
.fanout(wrapperName)
.map { builtProductsURL, wrapperName in
return builtProductsURL.appendingPathComponent(wrapperName)
}
}
/// Attempts to determine whether bitcode is enabled or not.
public var bitcodeEnabled: Result<Bool, CarthageError> {
return self["ENABLE_BITCODE"].map { $0 == "YES" }
}
/// Attempts to determine the relative path (from the build folder) where
/// the Swift modules for the built product will exist.
///
/// If the product does not build any modules, `nil` will be returned.
internal var relativeModulesPath: Result<String?, CarthageError> {
if let moduleName = self["PRODUCT_MODULE_NAME"].value {
return self["CONTENTS_FOLDER_PATH"].map { contentsPath in
let path1 = (contentsPath as NSString).appendingPathComponent("Modules")
let path2 = (path1 as NSString).appendingPathComponent(moduleName)
return (path2 as NSString).appendingPathExtension("swiftmodule")
}
} else {
return .success(nil)
}
}
/// Attempts to determine the code signing identity.
public var codeSigningIdentity: Result<String, CarthageError> {
return self["CODE_SIGN_IDENTITY"]
}
/// Attempts to determine if ad hoc code signing is allowed.
public var adHocCodeSigningAllowed: Result<Bool, CarthageError> {
return self["AD_HOC_CODE_SIGNING_ALLOWED"].map { $0 == "YES" }
}
/// Attempts to determine the path to the project that contains the current target
public var projectPath: Result<String, CarthageError> {
return self["PROJECT_FILE_PATH"]
}
}
extension BuildSettings: CustomStringConvertible {
public var description: String {
return "Build settings for target \"\(target)\": \(settings)"
}
}