Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
main ▾

xtool.git / Sources / XcodeProjects / XcodeWorkspace.swift

XcodeWorkspace.swift112 lines · 3.3 KiBRaw
1import Foundation
2
3public 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 public var url: URL
67
68 public init(children: [Element], url: URL) {
69 self.children = children
70 self.url = url.standardizedFileURL.resolvingSymlinksInPath()
71 }
72
73 /// Returns a copy at a new workspace location.
74 public func withURL(_ url: URL) -> Self {
75 var copy = self
76 copy.url = url.standardizedFileURL.resolvingSymlinksInPath()
77 return copy
78 }
79}
80
81extension XcodeWorkspace.Location.Base {
82 /// Absolute path
83 public static let absolute = Self(rawValue: "absolute")
84 /// Relative to container
85 public static let container = Self(rawValue: "container")
86 /// Relative to Developer Directory
87 public static let developer = Self(rawValue: "developer")
88 /// Relative to group
89 public static let group = Self(rawValue: "group")
90 /// Single project workspace in xcodeproj directory
91 public static let current = Self(rawValue: "self")
92}
93
94extension XcodeWorkspace.Location {
95 public static var current: Self {
96 Self(base: .current, path: "")
97 }
98}
99
100extension XcodeWorkspace.Location: LosslessStringConvertible, CustomStringConvertible {
101 public init?(_ string: String) {
102 guard let colon = string.firstIndex(of: ":") else {
103 return nil
104 }
105 self.base = Base(rawValue: String(string[..<colon]))
106 self.path = String(string[colon...].dropFirst())
107 }
108
109 public var description: String {
110 "\(base.rawValue):\(path)"
111 }
112}