in Sources/XCRemoteCache/Commands/Prepare/Integrate/LLDBInitPatcher.swift [69:110]
func enable() throws {
var finalLines: [String]
let xcrcLLDBCommandArray = [Self.preambleString, lldbCommand]
if let content = try? fileAccessor.contents(atPath: fileLocation.path) {
let contentString = String(data: content, encoding: .utf8)!
let originalContentLines = contentString.components(separatedBy: .newlines)
var contentLines = originalContentLines
let preambleIndices = findIndices(in: contentLines, value: Self.preambleString)
if !preambleIndices.isEmpty {
let firstLLDBCommandIndex = preambleIndices[0] + 1
if firstLLDBCommandIndex >= contentLines.count {
// corrupted file, append the script line at the bottom
contentLines.append(lldbCommand)
} else {
if preambleIndices.count == 1 && contentLines[firstLLDBCommandIndex] == lldbCommand {
// the file content is already valid
return
}
contentLines[firstLLDBCommandIndex] = lldbCommand
}
// Delete excessive XCRC lldb commands
for index in preambleIndices.dropFirst().reversed() {
let rangeEnd = min(index + 1, contentLines.count - 1)
contentLines.removeSubrange(index...rangeEnd)
}
} else {
contentLines += xcrcLLDBCommandArray
}
finalLines = contentLines
} else {
finalLines = xcrcLLDBCommandArray
}
// Save to disk
if finalLines.suffix(xcrcLLDBCommandArray.count) == xcrcLLDBCommandArray {
// always end with empty line when appending a command at the bottom
finalLines.append("")
}
let finalContent = finalLines.joined(separator: "\n").data(using: .utf8)
try fileAccessor.write(toPath: fileLocation.path, contents: finalContent)
}