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