File: Project.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 (225 lines) | stat: -rw-r--r-- 10,123 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
//===----------------------------------------------------------------------===//
//
// 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 {
    /// A PIF project, consisting of a tree of groups and file references, a list of targets, and some additional information.
    public struct Project: Sendable, Hashable {
        public let id: GUID
        public var name: String
        public var developmentRegion: String?
        public var path: String
        public var mainGroup: Group
        public private(set) var buildConfigs: [BuildConfig]
        public var projectDir: String
        public var targets: [BaseTarget]
        public var isPackage: Bool

        public init(
            id: GUID,
            path: String,
            projectDir: String,
            name: String,
            mainGroup: Group? = nil,
            buildConfigs: [BuildConfig] = [],
            targets: [BaseTarget] = [],
            developmentRegion: String? = nil,
            isPackage: Bool = true
        ) {
            precondition(!id.value.isEmpty)
            precondition(!path.isEmpty)
            precondition(!projectDir.isEmpty)
            self.id = id
            self.name = name
            self.developmentRegion = developmentRegion
            self.path = path
            self.mainGroup = mainGroup ?? Group(id: .init("\(id.value)::MAINGROUP"), path: "")
            self.buildConfigs = buildConfigs
            self.projectDir = projectDir
            self.targets = targets
            self.isPackage = isPackage
        }

        private var nextTargetId: GUID {
            return "\(self.id.value)::TARGET_\(targets.count)"
        }

        // MARK: - Test
        public mutating func withKeyPath<T, U>(_ kp: WritableKeyPath<Project, T>, _ fn: (inout T) -> WritableKeyPath<T, U>) -> WritableKeyPath<Project, U> {
            let subkp = fn(&self[keyPath: kp])
            return kp.appending(path: subkp)
        }

        // MARK: - BuildConfig

        /// Creates and adds a new empty build configuration, i.e. one that does not initially have any build settings.  The name must not be empty and must not be equal to the name of any existing build configuration in the project.
        @discardableResult public mutating func addBuildConfig(_ create: CreateFn<BuildConfig>) -> Tag<BuildConfig> {
            let id = GUID("\(self.id.value)::BUILDCONFIG_\(buildConfigs.count)")
            let buildConfig = create(id)
            buildConfigs.append(buildConfig)
            return .init(value: buildConfig.id)
        }

        public subscript(buildConfig tag: Tag<BuildConfig>) -> BuildConfig {
            get { buildConfigs[id: tag.value] }
            set { buildConfigs[id: tag.value] = newValue }
        }

        // MARK: - BaseTarget

        /// Creates and adds a new empty target, i.e. one that does not initially have any build phases.
        /// The Target ID must be non-empty and unique within the PIF workspace; an arbitrary
        /// guaranteed-to-be-unique identifier is passed in but the user may ignore it and use a
        /// different one, as long as it adheres to the rules.  The name must not be empty and must not
        /// be equal to the name of any existing target in the project.
        @discardableResult public mutating func addTarget(_ create: CreateFn<Target>) throws -> WritableKeyPath<Project, Target> {
            let target = create(nextTargetId)
            guard !target.name.isEmpty else {
                throw Error.emptyTargetName(targetIdentifier: target.id, productName: target.productName)
            }
            guard !target.id.value.isEmpty else {
                throw Error.emptyTargetIdentifier(targetName: target.name, productName: target.productName)
            }
            guard !targets.contains(where: { $0.common.id == target.id }) else {
                throw Error.duplicatedTargetIdentifier(targetIdentifier: target.id, targetName: target.name, productName: target.productName)
            }
            targets.append(.target(target))
            return \.[target: .init(value: target.id)]
        }

        /// Adds a new aggregate target returned by the `create` closure.
        /// The Target ID must be non-empty and unique within the PIF workspace; an arbitrary
        /// guaranteed-to-be-unique identifier is passed in but the user may ignore it and use a
        /// different one, as long as it adheres to the rules.  The name must not be empty and must not
        /// be equal to the name of any existing target in the project.
        @discardableResult public mutating func addAggregateTarget(_ create: CreateFn<AggregateTarget>) throws -> WritableKeyPath<Project, AggregateTarget> {
            let target = create(nextTargetId)
            guard !target.name.isEmpty else {
                throw Error.emptyTargetName(targetIdentifier: target.id, productName: "")
            }
            guard !target.id.value.isEmpty else {
                throw Error.emptyTargetIdentifier(targetName: target.name, productName: "")
            }
            guard !targets.contains(where: { $0.common.id == target.id }) else {
                throw Error.duplicatedTargetIdentifier(targetIdentifier: target.id, targetName: target.name, productName: "")
            }
            targets.append(.aggregate(target))
            let tag = Tag<AggregateTarget>(value: target.id)
            return \.[target: tag]
        }

        public subscript(target tag: Tag<Target>) -> Target {
            get {
                guard case let .target(t) = targets[id: tag.value] else { fatalError() }
                return t
            }
            set { targets[id: tag.value] = .target(newValue) }
        }

        public subscript(target tag: Tag<AggregateTarget>) -> AggregateTarget {
            get {
                guard case let .aggregate(t) = targets[id: tag.value] else { fatalError() }
                return t
            }
            set { targets[id: tag.value] = .aggregate(newValue) }
        }

        public subscript(baseTarget tag: Tag<AggregateTarget>) -> BaseTarget {
            get { targets[id: tag.value] }
            set { targets[id: tag.value] = newValue }
        }

        public subscript(baseTarget tag: Tag<Target>) -> BaseTarget {
            get { targets[id: tag.value] }
            set { targets[id: tag.value] = newValue }
        }

        public subscript(baseTarget keyPath: WritableKeyPath<Project, AggregateTarget>) -> BaseTarget {
            get { .aggregate(self[keyPath: keyPath]) }
            set {
                switch newValue {
                case .aggregate(let target): self[keyPath: keyPath] = target
                case .target(_): break
                }
            }
        }

        public subscript(baseTarget keyPath: WritableKeyPath<Project, Target>) -> BaseTarget {
            get { .target(self[keyPath: keyPath]) }
            set {
                switch newValue {
                case .target(let target): self[keyPath: keyPath] = target
                case .aggregate(_): break
                }
            }
        }

        public func findTarget(id: GUID) -> WritableKeyPath<Project, Target>? {
            return \.[target: .init(value: id)]
        }
    }

}


extension ProjectModel.Project: Codable {
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.id = try container.decode(ProjectModel.GUID.self, forKey: .guid)
        self.name = try container.decode(String.self, forKey: .projectName)
        self.isPackage = try container.decode(String.self, forKey: .projectIsPackage) == "true"
        self.path = try container.decode(String.self, forKey: .path)
        self.projectDir = try container.decode(String.self, forKey: .projectDirectory)
        self.buildConfigs = try container.decode([ProjectModel.BuildConfig].self, forKey: .buildConfigurations)
        self.mainGroup = try container.decode(ProjectModel.Group.self, forKey: .groupTree)
        let signatures = try container.decode([String].self, forKey: .targets)
        self.targets = signatures.compactMap { signature in
            guard let key = CodingUserInfoKey(rawValue: signature) else {
                return nil
            }
            return decoder.userInfo[key] as? ProjectModel.BaseTarget
        }
        self.developmentRegion = try container.decodeIfPresent(String.self, forKey: .developmentRegion)
    }

    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .guid)
        try container.encode(name, forKey: .projectName)
        try container.encode(isPackage ? "true" : "false", forKey: .projectIsPackage)
        try container.encode(path, forKey: .path)
        try container.encode(projectDir, forKey: .projectDirectory)
        try container.encode(buildConfigs, forKey: .buildConfigurations)
        try container.encode("Release", forKey: .defaultConfigurationName)
        try container.encode(mainGroup, forKey: .groupTree)
        try container.encode(targets.compactMap { $0.common.signature }, forKey: .targets)
        try container.encodeIfPresent(developmentRegion, forKey: .developmentRegion)
    }

    enum CodingKeys: String, CodingKey {
        case guid
        case projectDirectory
        case projectName
        case projectIsPackage
        case path
        case buildConfigurations
        case targets
        case groupTree
        case defaultConfigurationName
        case developmentRegion
        case classPrefix
        case appPreferencesBuildSettings
    }
}