File: References.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (275 lines) | stat: -rw-r--r-- 10,277 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Foundation
import SWBProtocol

extension ProjectModel {
    // MARK: - References

    /// Base enum for all items in the group hierarchy.
    public enum Reference: Sendable, Hashable {
        /// Determines the base path for a reference's relative path.
        public enum RefPathBase: String, Sendable, Hashable, Codable {
            /// Indicates that the path is relative to the source root (i.e. the "project directory").
            case projectDir = "SOURCE_ROOT"

            /// Indicates that the path is relative to the path of the parent group.
            case groupDir = "<group>"

            /// Indicates that the path is relative to the effective build directory (which varies depending on active scheme, active run destination, or even an overridden build setting.
            case buildDir = "BUILT_PRODUCTS_DIR"

            /// Indicates that the path is an absolute path.
            case absolute = "<absolute>"

            /// The string form, suitable for use in the PIF representation.
            public var asString: String { return rawValue }
        }

        case file(FileReference)
        case group(Group)

        public var id: GUID {
            switch self {
            case let .file(fref): return fref.id
            case let .group(group): return group.common.id
            }
        }
    }

    public struct ReferenceCommon: Sendable, Hashable {
        public let id: GUID

        /// Relative path of the reference.  It is usually a literal, but may in fact contain build settings.
        public var path: String

        /// Determines the base path for the reference's relative path.
        public var pathBase: Reference.RefPathBase

        /// Name of the reference.
        public var name: String

        init(id: GUID, path: String, pathBase: Reference.RefPathBase, name: String) {
            self.id = id
            self.path = path
            self.pathBase = pathBase
            self.name = name
        }
    }

    /// A reference to a file system entity (a file, folder, etc).
    public struct FileReference: Common, Sendable, Hashable {
        public var common: ReferenceCommon
        public var fileType: String

        public init(id: GUID, path: String, pathBase: Reference.RefPathBase = .groupDir, name: String? = nil, fileType: String? = nil) {
            self.common = ReferenceCommon(id: id, path: path, pathBase: pathBase, name: name ?? path)
            self.fileType = fileType ?? Self.fileTypeIdentifier(for: path)
        }
    }

    /// A group that can contain References (FileReferences and other Groups).  The resolved path of a group is used as the base path for any child references whose source tree type is GroupRelative.
    public struct Group: Common, Sendable, Hashable {
        public var common: ReferenceCommon
        public var subitems: [Reference] = []

        public init(id: GUID, path: String, pathBase: Reference.RefPathBase = .groupDir, name: String? = nil, subitems: [Reference] = []) {
            self.common = ReferenceCommon(id: id, path: path, pathBase: pathBase, name: name ?? path)
            self.subitems = subitems
        }

        private var nextRefId: GUID {
            return "\(self.common.id.value)::REF_\(subitems.count)"
        }

        // MARK: - Reference subitems

        /// Creates and appends a new FileReference to the list of subitems.
        @discardableResult public mutating func addFileReference(_ create: CreateFn<FileReference>) -> FileReference {
            let fref = create(nextRefId)
            subitems.append(.file(fref))
            return fref
        }

        @discardableResult public mutating func addGroup(_ create: CreateFn<Group>) -> WritableKeyPath<Group, Group> {
            let group = create(nextRefId)
            subitems.append(.group(group))
            let tag = Tag<Group>(value: group.id)
            return \.[group: tag]
        }

        fileprivate subscript(group tag: Tag<Group>) -> Group {
            get {
                guard case let .group(t) = subitems.first(where: { $0.id == tag.value }) else { fatalError() }
                return t
            }
            set {
                guard let idx = subitems.firstIndex(where: { $0.id == tag.value }) else { fatalError() }
                subitems[idx] = .group(newValue)
            }
        }
    }


}


extension ProjectModel.Reference: Codable {
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        switch try container.decode(String.self, forKey: .type) {
        case "file":
            self = .file(try .init(from: decoder))
        case "group":
            self = .group(try .init(from: decoder))
        default:
            throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Unknown value for key")
        }
    }
    
    public func encode(to encoder: any Encoder) throws {
        switch self {
        case let .file(fref):
            try fref.encode(to: encoder)
        case let .group(group):
            try group.encode(to: encoder)
        }
    }

    enum CodingKeys: String, CodingKey {
        case type
    }
}

extension ProjectModel.FileReference: Codable {
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let id = try container.decode(ProjectModel.GUID.self, forKey: .guid)
        let pathBase = try container.decode(ProjectModel.Reference.RefPathBase.self, forKey: .sourceTree)
        let path = try container.decode(String.self, forKey: .path)
        let name = try container.decode(String.self, forKey: .name)
        self.common = .init(id: id, path: path, pathBase: pathBase, name: name)
        self.fileType = try container.decode(String.self, forKey: .fileType)
    }

    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode("file", forKey: .type)
        try container.encode(self.id, forKey: .guid)
        try container.encode(self.name, forKey: .name)
        try container.encode(self.pathBase, forKey: .sourceTree)
        try container.encode(self.path, forKey: .path)
        try container.encode(self.fileType, forKey: .fileType)
    }

    enum CodingKeys: String, CodingKey {
        case type
        case guid
        case sourceTree
        case path
        case fileType
        case name
    }

    private static func fileTypeIdentifier(for path: String) -> String {
        // FIXME: We need real logic here.  <rdar://problem/33634352> [SwiftPM] When generating PIF, we need a standard way to determine the file type
        let pathExtension = (path as NSString).pathExtension as String
        switch pathExtension {
        case "a":
            return "archive.ar"
        case "s", "S":
            return "sourcecode.asm"
        case "c":
            return "sourcecode.c.c"
        case "cl":
            return "sourcecode.opencl"
        case "cpp", "cp", "cxx", "cc", "c++", "C", "tcc":
            return "sourcecode.cpp.cpp"
        case "d":
            return "sourcecode.dtrace"
        case "defs", "mig":
            return "sourcecode.mig"
        case "m":
            return "sourcecode.c.objc"
        case "mm", "M":
            return "sourcecode.cpp.objcpp"
        case "metal":
            return "sourcecode.metal"
        case "l", "lm", "lmm", "lpp", "lp", "lxx":
            return "sourcecode.lex"
        case "swift":
            return "sourcecode.swift"
        case "y", "ym", "ymm", "ypp", "yp", "yxx":
            return "sourcecode.yacc"


        // FIXME: This is probably now more important because of resources support.
        case "xcassets":
            return "folder.assetcatalog"
        case "icon":
            return "folder.iconcomposer.icon"
        case "xcstrings":
            return "text.json.xcstrings"
        case "storyboard":
            return "file.storyboard"
        case "xib":
            return "file.xib"

        case "xcframework":
            return "wrapper.xcframework"

        case "docc":
            return "folder.documentationcatalog"

        case "rkassets":
            return "folder.rkassets"

        default:
            return XCBuildFileType.all.first{ $0.fileTypes.contains(pathExtension) }?.fileTypeIdentifier ?? "file"
        }
    }

}

extension ProjectModel.Group: Codable {
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let id = try container.decode(ProjectModel.GUID.self, forKey: .guid)
        let pathBase = try container.decode(ProjectModel.Reference.RefPathBase.self, forKey: .sourceTree)
        let path = try container.decode(String.self, forKey: .path)
        let name = try container.decode(String.self, forKey: .name)
        self.common = .init(id: id, path: path, pathBase: pathBase, name: name)
        self.subitems = try container.decode([ProjectModel.Reference].self, forKey: .children)
    }

    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode("group", forKey: .type)
        try container.encode(self.id, forKey: .guid)
        try container.encode(self.pathBase, forKey: .sourceTree)
        try container.encode(self.path, forKey: .path)
        try container.encode(self.name, forKey: .name)
        try container.encode(subitems, forKey: .children)
    }

    enum CodingKeys: String, CodingKey {
        case type
        case guid
        case sourceTree
        case path
        case name
        case children
    }

}