XcodeGen/Sources/XcodeGenKit/FileWriter.swift

57 lines
1.9 KiB
Swift
Raw Normal View History

2018-10-29 16:50:53 +03:00
import Foundation
2018-11-04 14:30:34 +03:00
import PathKit
2018-10-29 16:50:53 +03:00
import ProjectSpec
2019-06-07 23:36:48 +03:00
import XcodeProj
2018-10-29 16:50:53 +03:00
2018-11-03 12:27:59 +03:00
public class FileWriter {
2018-10-29 16:50:53 +03:00
let project: Project
public init(project: Project) {
self.project = project
}
2018-11-03 12:27:59 +03:00
public func writeXcodeProject(_ xcodeProject: XcodeProj, to projectPath: Path? = nil) throws {
2019-01-09 02:41:28 +03:00
let projectPath = projectPath ?? project.defaultProjectPath
let tempPath = try Path.processUniqueTemporary() + "XcodeGen"
2018-10-29 16:50:53 +03:00
try? tempPath.delete()
if projectPath.exists {
try projectPath.copy(tempPath)
}
try xcodeProject.write(path: tempPath, override: true)
try? projectPath.delete()
try tempPath.copy(projectPath)
try? tempPath.delete()
}
public func writePlists() throws {
let infoPlistGenerator = InfoPlistGenerator()
for target in project.targets {
// write Info.plist
if let plist = target.info {
2018-10-30 13:47:16 +03:00
let properties = infoPlistGenerator.generateProperties(target: target).merged(plist.properties)
try writePlist(properties, path: plist.path)
2018-10-29 16:50:53 +03:00
}
// write entitlements
if let plist = target.entitlements {
2018-10-30 13:47:16 +03:00
try writePlist(plist.properties, path: plist.path)
2018-10-29 16:50:53 +03:00
}
}
}
2018-10-30 13:47:16 +03:00
private func writePlist(_ plist: [String: Any], path: String) throws {
let path = project.basePath + path
2018-10-30 14:18:30 +03:00
if path.exists, let data: Data = try? path.read(),
let existingPlist = (try? PropertyListSerialization.propertyList(from: data, format: nil)) as? [String: Any], NSDictionary(dictionary: plist).isEqual(to: existingPlist) {
// file is the same
return
}
2018-10-30 13:47:16 +03:00
let data = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)
try? path.delete()
try path.parent().mkpath()
try path.write(data)
}
2018-10-29 16:50:53 +03:00
}