func write()

in Sources/MockoloFramework/Operations/OutputWriter.swift [20:59]


func write(candidates: [(String, Int64)],
           header: String?,
           macro: String?,
           imports: String,
           helpers: [String],
           to outputFilePath: String) throws -> String {

    let entities = candidates
        .sorted { (left: (String, Int64), right: (String, Int64)) -> Bool in
            if left.1 == right.1 {
                return left.0 < right.0
            }
            return left.1 < right.1
        }
        .map{$0.0}
    
    let headerStr = (header ?? "") + .headerDoc
    var macroStart = ""
    var macroEnd = ""
    if let macro, !macro.isEmpty {
        macroStart = .poundIf + macro
        macroEnd = .poundEndIf
    }
    let ret = [
        headerStr,
        macroStart,
        imports,
        entities.joined(separator: "\n"),
        helpers.joined(separator: "\n\n"),
        macroEnd,
    ].joined(separator: "\n\n")
    let currentFileContents = try? String(contentsOfFile: outputFilePath, encoding: .utf8)
    guard currentFileContents != ret else {
        log("Not writing the file as content is unchanged", level: .info)
        return ret
    }

    _ = try ret.write(toFile: outputFilePath, atomically: true, encoding: .utf8)
    return ret
}