xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XcodeProjects / XcodeWorkspace.swift
XcodeWorkspace.swift103 lines · 3.0 KiBRaw
| 1 | import Foundation |
| 2 | |
| 3 | public struct XcodeWorkspace: Hashable, Sendable { |
| 4 | public enum Element: Hashable, Sendable { |
| 5 | case file(Location) |
| 6 | case group(Group) |
| 7 | case fileSystemSynchronizedGroup(Group) |
| 8 | |
| 9 | public var location: Location { |
| 10 | get { |
| 11 | switch self { |
| 12 | case let .file(location): |
| 13 | location |
| 14 | case let .group(group): |
| 15 | group.location |
| 16 | case let .fileSystemSynchronizedGroup(group): |
| 17 | group.location |
| 18 | } |
| 19 | } |
| 20 | set { |
| 21 | switch self { |
| 22 | case .file: |
| 23 | self = .file(newValue) |
| 24 | case .group(var group): |
| 25 | group.location = newValue |
| 26 | self = .group(group) |
| 27 | case .fileSystemSynchronizedGroup(var group): |
| 28 | group.location = newValue |
| 29 | self = .fileSystemSynchronizedGroup(group) |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public struct Group: Hashable, Sendable { |
| 36 | public var location: Location |
| 37 | public var name: String? |
| 38 | public var children: [Element] |
| 39 | |
| 40 | public init(location: Location, name: String?, children: [Element]) { |
| 41 | self.location = location |
| 42 | self.name = name |
| 43 | self.children = children |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public struct Location: Hashable, Sendable { |
| 48 | public struct Base: Hashable, Sendable, RawRepresentable { |
| 49 | public var rawValue: String |
| 50 | |
| 51 | public init(rawValue: String) { |
| 52 | self.rawValue = rawValue |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public var base: Base |
| 57 | public var path: String |
| 58 | |
| 59 | public init(base: Base, path: String) { |
| 60 | self.base = base |
| 61 | self.path = path |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public var children: [Element] |
| 66 | |
| 67 | public init(children: [Element]) { |
| 68 | self.children = children |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | extension XcodeWorkspace.Location.Base { |
| 73 | /// Absolute path |
| 74 | public static let absolute = Self(rawValue: "absolute") |
| 75 | /// Relative to container |
| 76 | public static let container = Self(rawValue: "container") |
| 77 | /// Relative to Developer Directory |
| 78 | public static let developer = Self(rawValue: "developer") |
| 79 | /// Relative to group |
| 80 | public static let group = Self(rawValue: "group") |
| 81 | /// Single project workspace in xcodeproj directory |
| 82 | public static let current = Self(rawValue: "self") |
| 83 | } |
| 84 | |
| 85 | extension XcodeWorkspace.Location { |
| 86 | public static var current: Self { |
| 87 | Self(base: .current, path: "") |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | extension XcodeWorkspace.Location: LosslessStringConvertible, CustomStringConvertible { |
| 92 | public init?(_ string: String) { |
| 93 | guard let colon = string.firstIndex(of: ":") else { |
| 94 | return nil |
| 95 | } |
| 96 | self.base = Base(rawValue: String(string[..<colon])) |
| 97 | self.path = String(string[colon...].dropFirst()) |
| 98 | } |
| 99 | |
| 100 | public var description: String { |
| 101 | "\(base.rawValue):\(path)" |
| 102 | } |
| 103 | } |