xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XcodeProjects / XcodeProject.swift
XcodeProject.swift78 lines · 2.6 KiBRaw
| 1 | import Foundation |
| 2 | @_exported import XcodeProjectFormat |
| 3 | |
| 4 | // This library is a lightweight layer on top of the XcodeProjectFormat |
| 5 | // interface, providing other facilities for working with the Xcode |
| 6 | // project formats. |
| 7 | // |
| 8 | // We currently use XcodeProj under the hood for some functionality, but this |
| 9 | // should be considered an implementation detail. |
| 10 | |
| 11 | public struct XcodeProject: Equatable, Sendable { |
| 12 | public enum Format: Sendable { |
| 13 | case pbxproj |
| 14 | case xcproj |
| 15 | } |
| 16 | |
| 17 | public var project: XCSchema.Project |
| 18 | public var format: Format |
| 19 | |
| 20 | public init( |
| 21 | project: XCSchema.Project, |
| 22 | format: Format = .pbxproj, |
| 23 | ) { |
| 24 | self.project = project |
| 25 | self.format = format |
| 26 | } |
| 27 | |
| 28 | /// Reads the project from the given `.xcodeproj` path. |
| 29 | public init(url: URL) throws { |
| 30 | guard url.pathExtension == "xcodeproj" else { |
| 31 | throw Errors.notAnXcodeproj(url.path) |
| 32 | } |
| 33 | let xcprojURL = url.appending(path: "project.xcproj") |
| 34 | lazy var pbxprojURL = url.appending(path: "project.pbxproj") |
| 35 | if FileManager.default.fileExists(atPath: xcprojURL.path) { |
| 36 | let data = try Data(contentsOf: xcprojURL) |
| 37 | self.project = try XCSchema.Project(jsonRepresentation: data) |
| 38 | self.format = .xcproj |
| 39 | } else if FileManager.default.fileExists(atPath: pbxprojURL.path) { |
| 40 | let data = try Data(contentsOf: pbxprojURL) |
| 41 | self.project = try XCSchema.Project(pbxprojRepresentation: data) |
| 42 | self.format = .pbxproj |
| 43 | } else { |
| 44 | throw Errors.missingProjectFile(url.path) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// Sets the format in which the project should be serialized. |
| 49 | public func withFormat(_ format: Format) -> XcodeProject { |
| 50 | var copy = self |
| 51 | copy.format = format |
| 52 | return copy |
| 53 | } |
| 54 | |
| 55 | /// Writes the project to the given `.xcodeproj` path. |
| 56 | public func write(to url: URL) throws { |
| 57 | try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) |
| 58 | |
| 59 | switch format { |
| 60 | case .pbxproj: |
| 61 | try project |
| 62 | .pbxprojRepresentation(name: url.deletingPathExtension().lastPathComponent) |
| 63 | .write(to: url.appendingPathComponent("project.pbxproj")) |
| 64 | case .xcproj: |
| 65 | try project |
| 66 | .jsonRepresentation() |
| 67 | .write(to: url.appendingPathComponent("project.xcproj")) |
| 68 | } |
| 69 | |
| 70 | try XcodeWorkspace(children: [.file(.current)]) |
| 71 | .write(to: url.appendingPathComponent("project.xcworkspace")) |
| 72 | } |
| 73 | |
| 74 | public enum Errors: Error { |
| 75 | case notAnXcodeproj(String) |
| 76 | case missingProjectFile(String) |
| 77 | } |
| 78 | } |