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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2017 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 Foundation
import PackageModel
import enum TSCBasic.JSON
import struct TSCUtility.Version
public final class PinsStore {
public typealias Pins = [PackageIdentity: PinsStore.Pin]
public struct Pin: Equatable {
/// The package reference of the pinned dependency.
public let packageRef: PackageReference
/// The pinned state.
public let state: PinState
public init(packageRef: PackageReference, state: PinState) {
self.packageRef = packageRef
self.state = state
}
}
public enum PinState: Equatable, CustomStringConvertible {
case branch(name: String, revision: String)
case version(_ version: Version, revision: String?)
case revision(_ revision: String)
public var description: String {
switch self {
case .version(let version, _):
return version.description
case .branch(let name, _):
return name
case .revision(let revision):
return revision
}
}
}
private let mirrors: DependencyMirrors
/// storage
private let storage: PinsStorage
private let _pins: ThreadSafeKeyValueStore<PackageIdentity, PinsStore.Pin>
public let originHash: String?
/// The current pins.
public var pins: Pins {
self._pins.get()
}
/// Create a new pins store.
///
/// - Parameters:
/// - pinsFile: Path to the pins file.
/// - fileSystem: The filesystem to manage the pin file on.
public init(
pinsFile: AbsolutePath,
workingDirectory: AbsolutePath,
fileSystem: FileSystem,
mirrors: DependencyMirrors
) throws {
self.storage = .init(path: pinsFile, workingDirectory: workingDirectory, fileSystem: fileSystem)
self.mirrors = mirrors
do {
let (pins, originHash) = try self.storage.load(mirrors: mirrors)
self._pins = .init(pins)
self.originHash = originHash
} catch {
self._pins = .init()
throw StringError(
"\(pinsFile) file is corrupted or malformed; fix or delete the file to continue: \(error.interpolationDescription)"
)
}
}
/// Pin a repository at a version.
///
/// This method does not automatically write to state file.
///
/// - Parameters:
/// - packageRef: The package reference to pin.
/// - state: The state to pin at.
public func pin(packageRef: PackageReference, state: PinState) {
self.add(.init(
packageRef: packageRef,
state: state
))
}
/// Add a pin.
///
/// This will replace any previous pin with same package name.
public func add(_ pin: Pin) {
self._pins[pin.packageRef.identity] = pin
}
/// Remove a pin.
///
/// This will replace any previous pin with same package name.
public func remove(_ pin: Pin) {
self._pins[pin.packageRef.identity] = nil
}
/// Unpin all of the currently pinned dependencies.
///
/// This method does not automatically write to state file.
public func unpinAll() {
// Reset the pins map.
self._pins.clear()
}
public func saveState(
toolsVersion: ToolsVersion,
originHash: String?
) throws {
try self.storage.save(
toolsVersion: toolsVersion,
pins: self._pins.get(),
mirrors: self.mirrors,
originHash: originHash,
removeIfEmpty: true
)
}
// for testing
public func schemeVersion() throws -> Int {
try self.storage.schemeVersion()
}
}
// MARK: - Serialization
private struct PinsStorage {
private let path: AbsolutePath
private let lockFilePath: AbsolutePath
private let fileSystem: FileSystem
private let encoder = JSONEncoder.makeWithDefaults()
private let decoder = JSONDecoder.makeWithDefaults()
init(path: AbsolutePath, workingDirectory: AbsolutePath, fileSystem: FileSystem) {
self.path = path
self.lockFilePath = workingDirectory.appending(component: path.basename)
self.fileSystem = fileSystem
}
func load(mirrors: DependencyMirrors) throws -> (pins: PinsStore.Pins, originHash: String?) {
if !self.fileSystem.exists(self.path) {
return (pins: [:], originHash: .none)
}
return try self.fileSystem.withLock(on: self.lockFilePath, type: .shared) {
let version = try self.decoder.decode(path: self.path, fileSystem: self.fileSystem, as: Version.self)
switch version.version {
case V1.version:
let v1 = try decoder.decode(path: self.path, fileSystem: self.fileSystem, as: V1.self)
return (
pins: try v1.object.pins
.map { try PinsStore.Pin($0, mirrors: mirrors) }
.reduce(into: [PackageIdentity: PinsStore.Pin]()) { partial, iterator in
if partial.keys.contains(iterator.packageRef.identity) {
throw StringError("duplicated entry for package \"\(iterator.packageRef.identity)\"")
}
partial[iterator.packageRef.identity] = iterator
},
originHash: .none
)
case V2.version:
let v2 = try decoder.decode(path: self.path, fileSystem: self.fileSystem, as: V2.self)
return (
pins: try v2.pins
.map { try PinsStore.Pin($0, mirrors: mirrors) }
.reduce(into: [PackageIdentity: PinsStore.Pin]()) { partial, iterator in
if partial.keys.contains(iterator.packageRef.identity) {
throw StringError("duplicated entry for package \"\(iterator.packageRef.identity)\"")
}
partial[iterator.packageRef.identity] = iterator
},
originHash: .none
)
case V3.version:
let v3 = try decoder.decode(path: self.path, fileSystem: self.fileSystem, as: V3.self)
return (
pins: try v3.pins
.map { try PinsStore.Pin($0, mirrors: mirrors) }
.reduce(into: [PackageIdentity: PinsStore.Pin]()) { partial, iterator in
if partial.keys.contains(iterator.packageRef.identity) {
throw StringError("duplicated entry for package \"\(iterator.packageRef.identity)\"")
}
partial[iterator.packageRef.identity] = iterator
},
originHash: v3.originHash
)
default:
throw StringError("unknown 'PinsStorage' version '\(version.version)' at '\(self.path)'.")
}
}
}
func save(
toolsVersion: ToolsVersion,
pins: PinsStore.Pins,
mirrors: DependencyMirrors,
originHash: String?,
removeIfEmpty: Bool
) throws {
if !self.fileSystem.exists(self.path.parentDirectory) {
try self.fileSystem.createDirectory(self.path.parentDirectory)
}
try self.fileSystem.withLock(on: self.lockFilePath, type: .exclusive) {
// Remove the pins file if there are zero pins to save.
//
// This can happen if all dependencies are path-based or edited
// dependencies.
if removeIfEmpty && pins.isEmpty {
try self.fileSystem.removeFileTree(self.path)
return
}
var data: Data
if toolsVersion > .v5_9 {
let container = try V3(
pins: pins,
mirrors: mirrors,
originHash: originHash
)
data = try self.encoder.encode(container)
} else if toolsVersion >= .v5_6 {
let container = try V2(
pins: pins,
mirrors: mirrors
)
data = try self.encoder.encode(container)
} else {
let container = try V1(pins: pins, mirrors: mirrors)
let json = container.toLegacyJSON()
let bytes = json.toBytes(prettyPrint: true)
data = Data(bytes.contents)
}
#if !os(Windows)
// rdar://83646952: add newline for POSIXy systems
if data.last != 0x0A {
data.append(0x0A)
}
#endif
try self.fileSystem.writeFileContents(self.path, data: data)
}
}
func reset() throws {
if !self.fileSystem.exists(self.path.parentDirectory) {
return
}
try self.fileSystem.withLock(on: self.lockFilePath, type: .exclusive) {
try self.fileSystem.removeFileTree(self.path)
}
}
// for testing
func schemeVersion() throws -> Int {
try self.decoder.decode(path: self.path, fileSystem: self.fileSystem, as: Version.self).version
}
// version reader
struct Version: Codable {
let version: Int
}
// v1 storage format
struct V1: Codable {
static let version = 1
let version: Int
let object: Container
init(pins: PinsStore.Pins, mirrors: DependencyMirrors) throws {
self.version = Self.version
self.object = try .init(
pins: pins.values
.sorted(by: { $0.packageRef.identity < $1.packageRef.identity })
.map { try Pin($0, mirrors: mirrors) }
)
}
// backwards compatibility of JSON format
func toLegacyJSON() -> JSON {
.init([
"version": self.version.toJSON(),
"object": self.object.toLegacyJSON(),
])
}
struct Container: Codable {
var pins: [Pin]
// backwards compatibility of JSON format
func toLegacyJSON() -> JSON {
.init([
"pins": self.pins.map { $0.toLegacyJSON() },
])
}
}
struct Pin: Codable {
let package: String?
let repositoryURL: String
let state: State
init(_ pin: PinsStore.Pin, mirrors: DependencyMirrors) throws {
let location: String
switch pin.packageRef.kind {
case .localSourceControl(let path):
location = path.pathString
case .remoteSourceControl(let url):
location = url.absoluteString
default:
throw StringError("invalid package type \(pin.packageRef.kind)")
}
self.package = pin.packageRef.deprecatedName
// rdar://52529014, rdar://52529011: pin file should store the original location but remap when loading
self.repositoryURL = mirrors.original(for: location) ?? location
self.state = try .init(pin.state)
}
// backwards compatibility of JSON format
func toLegacyJSON() -> JSON {
.init([
"package": self.package.toJSON(),
"repositoryURL": self.repositoryURL.toJSON(),
"state": self.state.toLegacyJSON(),
])
}
}
struct State: Codable {
let revision: String
let branch: String?
let version: String?
init(_ state: PinsStore.PinState) throws {
switch state {
case .version(let version, let revision) where revision != nil:
self.version = version.description
self.branch = nil
self.revision = revision! // nil guarded above in case
case .branch(let branch, let revision):
self.version = nil
self.branch = branch
self.revision = revision
case .revision(let revision):
self.version = nil
self.branch = nil
self.revision = revision
default:
throw StringError("invalid pin state: \(state)")
}
}
// backwards compatibility of JSON format
func toLegacyJSON() -> JSON {
.init([
"revision": self.revision.toJSON(),
"version": self.version.toJSON(),
"branch": self.branch.toJSON(),
])
}
}
}
// v2 storage format
struct V2: Codable {
static let version = 2
let version: Int
let pins: [Pin]
init(
pins: PinsStore.Pins,
mirrors: DependencyMirrors
) throws {
self.version = Self.version
self.pins = try pins.values
.sorted(by: { $0.packageRef.identity < $1.packageRef.identity })
.map { try Pin($0, mirrors: mirrors) }
}
struct Pin: Codable {
let identity: PackageIdentity
let kind: Kind
let location: String
let state: State
init(_ pin: PinsStore.Pin, mirrors: DependencyMirrors) throws {
let kind: Kind
let location: String
switch pin.packageRef.kind {
case .localSourceControl(let path):
kind = .localSourceControl
location = path.pathString
case .remoteSourceControl(let url):
kind = .remoteSourceControl
location = url.absoluteString
case .registry:
kind = .registry
location = "" // FIXME: this is likely not correct
default:
throw StringError("invalid package type \(pin.packageRef.kind)")
}
self.identity = pin.packageRef.identity
self.kind = kind
// rdar://52529014, rdar://52529011: pin file should store the original location but remap when loading
self.location = mirrors.original(for: location) ?? location
self.state = .init(pin.state)
}
}
enum Kind: String, Codable {
case localSourceControl
case remoteSourceControl
case registry
}
struct State: Codable {
let version: String?
let branch: String?
let revision: String?
init(_ state: PinsStore.PinState) {
switch state {
case .version(let version, let revision):
self.version = version.description
self.branch = nil
self.revision = revision
case .branch(let branch, let revision):
self.version = nil
self.branch = branch
self.revision = revision
case .revision(let revision):
self.version = nil
self.branch = nil
self.revision = revision
}
}
}
}
// v3 storage format
struct V3: Codable {
static let version = 3
let version: Int
let originHash: String?
let pins: [V2.Pin]
init(
pins: PinsStore.Pins,
mirrors: DependencyMirrors,
originHash: String?
) throws {
self.version = Self.version
self.pins = try pins.values
.sorted(by: { $0.packageRef.identity < $1.packageRef.identity })
.map { try V2.Pin($0, mirrors: mirrors) }
self.originHash = originHash
}
}
}
extension PinsStore.Pin {
fileprivate init(_ pin: PinsStorage.V1.Pin, mirrors: DependencyMirrors) throws {
// rdar://52529014, rdar://52529011: pin file should store the original location but remap when loading
let location = mirrors.effective(for: pin.repositoryURL)
let identity = PackageIdentity(urlString: location) // FIXME: pin store should also encode identity
var packageRef: PackageReference
if let path = try? AbsolutePath(validating: location) {
packageRef = .localSourceControl(identity: identity, path: path)
} else {
packageRef = .remoteSourceControl(identity: identity, url: SourceControlURL(location))
}
if let newName = pin.package {
packageRef = packageRef.withName(newName)
}
self.init(
packageRef: packageRef,
state: try .init(pin.state)
)
}
}
extension PinsStore.PinState {
fileprivate init(_ state: PinsStorage.V1.State) throws {
let revision = state.revision
if let version = state.version {
self = try .version(Version(versionString: version), revision: revision)
} else if let branch = state.branch {
self = .branch(name: branch, revision: revision)
} else {
self = .revision(revision)
}
}
}
extension PinsStore.Pin {
fileprivate init(_ pin: PinsStorage.V2.Pin, mirrors: DependencyMirrors) throws {
let packageRef: PackageReference
let identity = pin.identity
// rdar://52529014, rdar://52529011: pin file should store the original location but remap when loading
let location = mirrors.effective(for: pin.location)
switch pin.kind {
case .localSourceControl:
packageRef = try .localSourceControl(identity: identity, path: AbsolutePath(validating: location))
case .remoteSourceControl:
packageRef = .remoteSourceControl(identity: identity, url: SourceControlURL(location))
case .registry:
packageRef = .registry(identity: identity)
}
self.init(
packageRef: packageRef,
state: try .init(pin.state)
)
}
}
extension PinsStore.PinState {
fileprivate init(_ state: PinsStorage.V2.State) throws {
if let version = state.version {
self = try .version(Version(versionString: version), revision: state.revision)
} else if let branch = state.branch, let revision = state.revision {
self = .branch(name: branch, revision: revision)
} else if let revision = state.revision {
self = .revision(revision)
} else {
throw StringError("invalid pin state: \(state)")
}
}
}
|