xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XcodeProjects / XcodeWorkspace+XML.swift
XcodeWorkspace+XML.swift85 lines · 2.7 KiBRaw
| 1 | import Foundation |
| 2 | private import XcodeProj |
| 3 | |
| 4 | extension XcodeWorkspace { |
| 5 | public init(url: URL) throws { |
| 6 | let workspace = try XCWorkspace(pathString: url.path) |
| 7 | self.init(workspace.data, url: url) |
| 8 | } |
| 9 | |
| 10 | public func dataRepresentation() throws -> Data { |
| 11 | try xcWorkspaceData.dataRepresentation() ?? Data() |
| 12 | } |
| 13 | |
| 14 | /// Writes the workspace to its current URL. |
| 15 | public func write() throws { |
| 16 | try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true) |
| 17 | try self.dataRepresentation() |
| 18 | .write(to: url.appendingPathComponent("contents.xcworkspacedata")) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // MARK: - Conversion |
| 23 | |
| 24 | extension XcodeWorkspace { |
| 25 | fileprivate init(_ xcWorkspaceData: XCWorkspaceData, url: URL) { |
| 26 | self.init(children: xcWorkspaceData.children.map { .init(from: $0) }, url: url) |
| 27 | } |
| 28 | |
| 29 | fileprivate var xcWorkspaceData: XCWorkspaceData { |
| 30 | XCWorkspaceData(children: children.map(\.xcWorkspaceDataElement)) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | extension XcodeWorkspace.Element { |
| 35 | fileprivate init(from xcWorkspaceDataElement: XCWorkspaceDataElement) { |
| 36 | switch xcWorkspaceDataElement { |
| 37 | case .file(let file): |
| 38 | self = .file(.init(file.location)) |
| 39 | case .group(let group): |
| 40 | self = .group(.init(group)) |
| 41 | case .fileSystemSynchronizedGroup(let group): |
| 42 | self = .fileSystemSynchronizedGroup(.init(group)) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fileprivate var xcWorkspaceDataElement: XCWorkspaceDataElement { |
| 47 | switch self { |
| 48 | case .file(let location): |
| 49 | .file(.init(location: location.xcWorkspaceLocation)) |
| 50 | case .group(let group): |
| 51 | .group(group.xcWorkspaceDataElement) |
| 52 | case .fileSystemSynchronizedGroup(let group): |
| 53 | .fileSystemSynchronizedGroup(group.xcWorkspaceDataElement) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | extension XcodeWorkspace.Location { |
| 59 | fileprivate init(_ xcWorkspaceDataElementLocation: XCWorkspaceDataElementLocationType) { |
| 60 | self.base = .init(rawValue: xcWorkspaceDataElementLocation.schema) |
| 61 | self.path = xcWorkspaceDataElementLocation.path |
| 62 | } |
| 63 | |
| 64 | fileprivate var xcWorkspaceLocation: XCWorkspaceDataElementLocationType { |
| 65 | .other(base.rawValue, path) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | extension XcodeWorkspace.Group { |
| 70 | fileprivate init(_ xcWorkspaceDataGroup: XCWorkspaceDataGroup) { |
| 71 | self.init( |
| 72 | location: .init(xcWorkspaceDataGroup.location), |
| 73 | name: xcWorkspaceDataGroup.name, |
| 74 | children: xcWorkspaceDataGroup.children.map { .init(from: $0) } |
| 75 | ) |
| 76 | } |
| 77 | |
| 78 | fileprivate var xcWorkspaceDataElement: XCWorkspaceDataGroup { |
| 79 | .init( |
| 80 | location: self.location.xcWorkspaceLocation, |
| 81 | name: name, |
| 82 | children: children.map(\.xcWorkspaceDataElement) |
| 83 | ) |
| 84 | } |
| 85 | } |