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
|
//===----------------------------------------------------------------------===//
//
// 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
extension Workspace {
/// A downloaded artifact managed by the workspace.
public struct ManagedArtifact {
/// The package reference.
public let packageRef: PackageReference
/// The name of the binary target the artifact corresponds to.
public let targetName: String
/// The source of the artifact (local or remote).
public let source: Source
/// The path of the artifact on disk
public let path: AbsolutePath
public let kind: BinaryModule.Kind
public init(
packageRef: PackageReference,
targetName: String,
source: Source,
path: AbsolutePath,
kind: BinaryModule.Kind
) {
self.packageRef = packageRef
self.targetName = targetName
self.source = source
self.path = path
self.kind = kind
}
/// Create an artifact downloaded from a remote url.
public static func remote(
packageRef: PackageReference,
targetName: String,
url: String,
checksum: String,
path: AbsolutePath,
kind: BinaryModule.Kind
) -> ManagedArtifact {
return ManagedArtifact(
packageRef: packageRef,
targetName: targetName,
source: .remote(url: url, checksum: checksum),
path: path,
kind: kind
)
}
/// Create an artifact present locally on the filesystem.
public static func local(
packageRef: PackageReference,
targetName: String,
path: AbsolutePath,
kind: BinaryModule.Kind,
checksum: String? = nil
) -> ManagedArtifact {
return ManagedArtifact(
packageRef: packageRef,
targetName: targetName,
source: .local(checksum: checksum),
path: path,
kind: kind
)
}
/// Represents the source of the artifact.
public enum Source: Equatable {
/// Represents a remote artifact, with the url it was downloaded from, its checksum, and its path relative to
/// the workspace artifacts path.
case remote(url: String, checksum: String)
/// Represents a locally available artifact, with its path relative either to its package or to the workspace artifacts
/// path, in the latter case, the checksum of the local archive the artifact was extracted from is set.
case local(checksum: String? = nil)
}
}
}
extension Workspace.ManagedArtifact: CustomStringConvertible {
public var description: String {
return "<ManagedArtifact: \(self.packageRef.identity).\(self.targetName) \(self.source) \(self.path)>"
}
}
extension Workspace.ManagedArtifact.Source: CustomStringConvertible {
public var description: String {
switch self {
case .local(let checksum):
return "local(checksum: \(checksum ?? "nil"))"
case .remote(let url, let checksum):
return "remote(url: \(url), checksum: \(checksum))"
}
}
}
// MARK: - ManagedArtifacts
extension Workspace {
/// A collection of managed artifacts which have been downloaded.
public final class ManagedArtifacts {
/// A mapping from package identity, to target name, to ManagedArtifact.
private var artifactMap: [PackageIdentity: [String: ManagedArtifact]]
internal var artifacts: AnyCollection<ManagedArtifact> {
AnyCollection(self.artifactMap.values.lazy.flatMap{ $0.values })
}
init() {
self.artifactMap = [:]
}
init(_ artifacts: [ManagedArtifact]) throws {
let artifactsByPackagePath = Dictionary(grouping: artifacts, by: { $0.packageRef.identity })
self.artifactMap = try artifactsByPackagePath.mapValues{ artifacts in
// rdar://86857825 do not use Dictionary(uniqueKeysWithValues:) as it can crash the process when input is incorrect such as in older versions of SwiftPM
var map = [String: ManagedArtifact]()
for artifact in artifacts {
if map[artifact.targetName] != nil {
throw StringError("binary artifact for '\(artifact.targetName)' already exists in managed artifacts")
}
map[artifact.targetName] = artifact
}
return map
}
}
public subscript(packageIdentity packageIdentity: PackageIdentity, targetName targetName: String) -> ManagedArtifact? {
self.artifactMap[packageIdentity]?[targetName]
}
public func add(_ artifact: ManagedArtifact) {
self.artifactMap[artifact.packageRef.identity, default: [:]][artifact.targetName] = artifact
}
public func remove(packageIdentity: PackageIdentity, targetName: String) {
self.artifactMap[packageIdentity]?[targetName] = nil
}
}
}
extension Workspace.ManagedArtifacts: Collection {
public var startIndex: AnyIndex {
self.artifacts.startIndex
}
public var endIndex: AnyIndex {
self.artifacts.endIndex
}
public subscript(index: AnyIndex) -> Workspace.ManagedArtifact {
self.artifacts[index]
}
public func index(after index: AnyIndex) -> AnyIndex {
self.artifacts.index(after: index)
}
}
extension Workspace.ManagedArtifacts: CustomStringConvertible {
public var description: String {
"<ManagedArtifacts: \(Array(self.artifacts))>"
}
}
|