File: ManagedDependency.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (242 lines) | stat: -rw-r--r-- 9,102 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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2020 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 Basics
import PackageGraph
import PackageModel
import SourceControl

import struct TSCUtility.Version

extension Workspace {
    /// An individual managed dependency.
    ///
    /// Each dependency will have a checkout containing the sources at a
    /// particular revision, and may have an associated version.
    public struct ManagedDependency: Equatable {
        /// Represents the state of the managed dependency.
        public indirect enum State: Equatable, CustomStringConvertible {
            /// The dependency is a local package on the file system.
            case fileSystem(AbsolutePath)

            /// The dependency is a managed source control checkout.
            case sourceControlCheckout(CheckoutState)

            /// The dependency is downloaded from a registry.
            case registryDownload(version: Version)

            /// The dependency is in edited state.
            ///
            /// If the path is non-nil, the dependency is managed by a user and is
            /// located at the path. In other words, this dependency is being used
            /// for top of the tree style development.
            case edited(basedOn: ManagedDependency?, unmanagedPath: AbsolutePath?)

            case custom(version: Version, path: AbsolutePath)

            public var description: String {
                switch self {
                case .fileSystem(let path):
                    return "fileSystem (\(path))"
                case .sourceControlCheckout(let checkoutState):
                    return "sourceControlCheckout (\(checkoutState))"
                case .registryDownload(let version):
                    return "registryDownload (\(version))"
                case .edited:
                    return "edited"
                case .custom:
                    return "custom"
                }
            }
        }

        /// The package reference.
        public let packageRef: PackageReference

        /// The state of the managed dependency.
        public let state: State

        /// The checked out path of the dependency on disk, relative to the workspace checkouts path.
        public let subpath: RelativePath

        internal init(
            packageRef: PackageReference,
            state: State,
            subpath: RelativePath
        ) {
            self.packageRef = packageRef
            self.subpath = subpath
            self.state = state
        }

        /// Create an editable managed dependency based on a dependency which
        /// was *not* in edit state.
        ///
        /// - Parameters:
        ///     - subpath: The subpath inside the editable directory.
        ///     - unmanagedPath: A custom absolute path instead of the subpath.
        public func edited(subpath: RelativePath, unmanagedPath: AbsolutePath?) throws -> ManagedDependency {
            guard case .sourceControlCheckout =  self.state else {
                throw InternalError("invalid dependency state: \(self.state)")
            }
            return ManagedDependency(
                packageRef: self.packageRef,
                state: .edited(basedOn: self, unmanagedPath: unmanagedPath),
                subpath: subpath
            )
        }

        /// Create a dependency present locally on the filesystem.
        public static func fileSystem(
            packageRef: PackageReference
        ) throws -> ManagedDependency {
            switch packageRef.kind {
            case .root(let path), .fileSystem(let path), .localSourceControl(let path):
                return try ManagedDependency(
                    packageRef: packageRef,
                    state: .fileSystem(path),
                    // FIXME: This is just a fake entry, we should fix it.
                    subpath: RelativePath(validating: packageRef.identity.description)
                )
            default:
                throw InternalError("invalid package type: \(packageRef.kind)")
            }
        }

        /// Create a source control dependency checked out
        public static func sourceControlCheckout(
            packageRef: PackageReference,
            state: CheckoutState,
            subpath: RelativePath
        ) throws -> ManagedDependency {
            switch packageRef.kind {
            case .localSourceControl, .remoteSourceControl:
                return ManagedDependency(
                    packageRef: packageRef,
                    state: .sourceControlCheckout(state),
                    subpath: subpath
                )
            default:
                throw InternalError("invalid package type: \(packageRef.kind)")
            }
        }

        /// Create a registry dependency downloaded
        public static func registryDownload(
            packageRef: PackageReference,
            version: Version,
            subpath: RelativePath
        ) throws -> ManagedDependency {
            guard case .registry = packageRef.kind else {
                throw InternalError("invalid package type: \(packageRef.kind)")
            }
            return ManagedDependency(
                packageRef: packageRef,
                state: .registryDownload(version: version),
                subpath: subpath
            )
        }

        /// Create an edited dependency
        public static func edited(
            packageRef: PackageReference,
            subpath: RelativePath,
            basedOn: ManagedDependency?,
            unmanagedPath: AbsolutePath?
        ) -> ManagedDependency {
            return ManagedDependency(
                packageRef: packageRef,
                state: .edited(basedOn: basedOn, unmanagedPath: unmanagedPath),
                subpath: subpath
            )
        }
    }
}

extension Workspace.ManagedDependency: CustomStringConvertible {
    public var description: String {
        return "<ManagedDependency: \(self.packageRef.identity) \(self.state)>"
    }
}

// MARK: - ManagedDependencies

extension Workspace {
    /// A collection of managed dependencies.
    final public class ManagedDependencies {
        private var dependencies: [PackageIdentity: ManagedDependency]

        init() {
            self.dependencies = [:]
        }

        init(_ dependencies: [ManagedDependency]) throws {
            // rdar://86857825 do not use Dictionary(uniqueKeysWithValues:) as it can crash the process when input is incorrect such as in older versions of SwiftPM
            self.dependencies = [:]
            for dependency in dependencies {
                if self.dependencies[dependency.packageRef.identity] != nil {
                    throw StringError("\(dependency.packageRef.identity) already exists in managed dependencies")
                }
                self.dependencies[dependency.packageRef.identity] = dependency
            }
        }

        public subscript(identity: PackageIdentity) -> ManagedDependency? {
            return self.dependencies[identity]
        }

        // When loading manifests in Workspace, there are cases where we must also compare the location
        // as it may attempt to load manifests for dependencies that have the same identity but from a different location
        // (e.g. dependency is changed to a fork with the same identity)
        public subscript(comparingLocation package: PackageReference) -> ManagedDependency? {
            if let dependency = self.dependencies[package.identity], dependency.packageRef.equalsIncludingLocation(package) {
                return dependency
            }
            return .none
        }

        public func add(_ dependency: ManagedDependency) {
            self.dependencies[dependency.packageRef.identity] = dependency
        }

        public func remove(_ identity: PackageIdentity) {
            self.dependencies[identity] = nil
        }
    }
}

extension Workspace.ManagedDependencies: Collection {
    public typealias Index = Dictionary<PackageIdentity, Workspace.ManagedDependency>.Index
    public typealias Element = Workspace.ManagedDependency

    public var startIndex: Index {
        self.dependencies.startIndex
    }

    public var endIndex: Index {
        self.dependencies.endIndex
    }

    public subscript(index: Index) -> Element {
        self.dependencies[index].value
    }

    public func index(after index: Index) -> Index {
        self.dependencies.index(after: index)
    }
}

extension Workspace.ManagedDependencies: CustomStringConvertible {
    public var description: String {
        "<ManagedDependencies: \(Array(self.dependencies.values))>"
    }
}