|
11 | 11 | import Basics
|
12 | 12 | import Foundation
|
13 | 13 | import TSCBasic
|
| 14 | +import PackageRegistry |
14 | 15 |
|
15 | 16 | // MARK: - Location
|
16 | 17 |
|
@@ -62,6 +63,11 @@ extension Workspace {
|
62 | 63 | self.sharedConfigurationDirectory.map { DefaultLocations.mirrorsConfigurationFile(at: $0) }
|
63 | 64 | }
|
64 | 65 |
|
| 66 | + /// Path to the shared registries configuration. |
| 67 | + public var sharedRegistriesConfigurationFile: AbsolutePath? { |
| 68 | + self.sharedConfigurationDirectory.map { DefaultLocations.registriesConfigurationFile(at: $0) } |
| 69 | + } |
| 70 | + |
65 | 71 | /// Create a new workspace location.
|
66 | 72 | ///
|
67 | 73 | /// - Parameters:
|
@@ -129,6 +135,10 @@ extension Workspace {
|
129 | 135 | path.appending(component: "mirrors.json")
|
130 | 136 | }
|
131 | 137 |
|
| 138 | + public static func registriesConfigurationFile(at path: AbsolutePath) -> AbsolutePath { |
| 139 | + path.appending(component: "registries.json") |
| 140 | + } |
| 141 | + |
132 | 142 | public static func manifestsDirectory(at path: AbsolutePath) -> AbsolutePath {
|
133 | 143 | path.appending(component: "manifests")
|
134 | 144 | }
|
@@ -360,6 +370,141 @@ extension Workspace.Configuration {
|
360 | 370 | }
|
361 | 371 | }
|
362 | 372 |
|
| 373 | +// MARK: - Registries |
| 374 | + |
| 375 | +extension Workspace.Configuration { |
| 376 | + public class Registries { |
| 377 | + private let localRegistries: RegistriesStorage |
| 378 | + private let sharedRegistries: RegistriesStorage? |
| 379 | + private let fileSystem: FileSystem |
| 380 | + |
| 381 | + private var _configuration = RegistryConfiguration() |
| 382 | + private let lock = Lock() |
| 383 | + |
| 384 | + /// The registry configuration |
| 385 | + public var configuration: RegistryConfiguration { |
| 386 | + self.lock.withLock { |
| 387 | + return self._configuration |
| 388 | + } |
| 389 | + } |
| 390 | + |
| 391 | + /// Initialize the workspace registries configuration |
| 392 | + /// |
| 393 | + /// - Parameters: |
| 394 | + /// - localRegistriesFile: Path to the workspace registries configuration file |
| 395 | + /// - sharedRegistriesFile: Path to the shared registries configuration file, defaults to the standard location. |
| 396 | + /// - fileSystem: The file system to use. |
| 397 | + public init( |
| 398 | + localRegistriesFile: AbsolutePath, |
| 399 | + sharedRegistriesFile: AbsolutePath?, |
| 400 | + fileSystem: FileSystem |
| 401 | + ) throws { |
| 402 | + self.localRegistries = .init(path: localRegistriesFile, fileSystem: fileSystem, deleteWhenEmpty: true) |
| 403 | + self.sharedRegistries = sharedRegistriesFile.map { .init(path: $0, fileSystem: fileSystem, deleteWhenEmpty: false) } |
| 404 | + self.fileSystem = fileSystem |
| 405 | + try self.computeRegistries() |
| 406 | + } |
| 407 | + |
| 408 | + @discardableResult |
| 409 | + public func applyLocal(handler: (inout RegistryConfiguration) throws -> Void) throws -> RegistryConfiguration { |
| 410 | + try self.localRegistries.apply(handler: handler) |
| 411 | + try self.computeRegistries() |
| 412 | + return self.configuration |
| 413 | + } |
| 414 | + |
| 415 | + @discardableResult |
| 416 | + public func applyShared(handler: (inout RegistryConfiguration) throws -> Void) throws -> RegistryConfiguration { |
| 417 | + guard let sharedRegistries = self.sharedRegistries else { |
| 418 | + throw InternalError("shared registries not configured") |
| 419 | + } |
| 420 | + try sharedRegistries.apply(handler: handler) |
| 421 | + try self.computeRegistries() |
| 422 | + return self.configuration |
| 423 | + } |
| 424 | + |
| 425 | + // mutating the state we hold since we are passing it by reference to the workspace |
| 426 | + // access should be done using a lock |
| 427 | + private func computeRegistries() throws { |
| 428 | + try self.lock.withLock { |
| 429 | + var configuration = RegistryConfiguration() |
| 430 | + |
| 431 | + if let sharedConfiguration = try sharedRegistries?.get() { |
| 432 | + configuration.merge(sharedConfiguration) |
| 433 | + } |
| 434 | + |
| 435 | + let localConfiguration = try localRegistries.get() |
| 436 | + configuration.merge(localConfiguration) |
| 437 | + |
| 438 | + self._configuration = configuration |
| 439 | + } |
| 440 | + } |
| 441 | + } |
| 442 | +} |
| 443 | + |
| 444 | +extension Workspace.Configuration { |
| 445 | + private struct RegistriesStorage { |
| 446 | + private let path: AbsolutePath |
| 447 | + private let fileSystem: FileSystem |
| 448 | + private let deleteWhenEmpty: Bool |
| 449 | + |
| 450 | + public init(path: AbsolutePath, fileSystem: FileSystem, deleteWhenEmpty: Bool) { |
| 451 | + self.path = path |
| 452 | + self.fileSystem = fileSystem |
| 453 | + self.deleteWhenEmpty = deleteWhenEmpty |
| 454 | + } |
| 455 | + |
| 456 | + public func get() throws -> RegistryConfiguration { |
| 457 | + return try self.fileSystem.withLock(on: self.path.parentDirectory, type: .shared) { |
| 458 | + return try Self.load(self.path, fileSystem: self.fileSystem) |
| 459 | + } |
| 460 | + } |
| 461 | + |
| 462 | + @discardableResult |
| 463 | + public func apply(handler: (inout RegistryConfiguration) throws -> Void) throws -> RegistryConfiguration { |
| 464 | + if !self.fileSystem.exists(self.path.parentDirectory) { |
| 465 | + try self.fileSystem.createDirectory(self.path.parentDirectory, recursive: true) |
| 466 | + } |
| 467 | + return try self.fileSystem.withLock(on: self.path.parentDirectory, type: .exclusive) { |
| 468 | + let configuration = try Self.load(self.path, fileSystem: self.fileSystem) |
| 469 | + var updatedConfiguration = configuration |
| 470 | + try handler(&updatedConfiguration) |
| 471 | + if updatedConfiguration != configuration { |
| 472 | + try Self.save(updatedConfiguration, to: self.path, fileSystem: self.fileSystem, deleteWhenEmpty: self.deleteWhenEmpty) |
| 473 | + } |
| 474 | + return updatedConfiguration |
| 475 | + } |
| 476 | + } |
| 477 | + |
| 478 | + private static func load(_ path: AbsolutePath, fileSystem: FileSystem) throws -> RegistryConfiguration { |
| 479 | + guard fileSystem.exists(path) else { |
| 480 | + return RegistryConfiguration() |
| 481 | + } |
| 482 | + |
| 483 | + let data: Data = try fileSystem.readFileContents(path) |
| 484 | + let decoder = JSONDecoder.makeWithDefaults() |
| 485 | + return try decoder.decode(RegistryConfiguration.self, from: data) |
| 486 | + } |
| 487 | + |
| 488 | + private static func save(_ configuration: RegistryConfiguration, to path: AbsolutePath, fileSystem: FileSystem, deleteWhenEmpty: Bool) throws { |
| 489 | + if configuration.isEmpty { |
| 490 | + if deleteWhenEmpty && fileSystem.exists(path) { |
| 491 | + // deleteWhenEmpty is a backward compatibility mode |
| 492 | + return try fileSystem.removeFileTree(path) |
| 493 | + } else if !fileSystem.exists(path) { |
| 494 | + // nothing to do |
| 495 | + return |
| 496 | + } |
| 497 | + } |
| 498 | + |
| 499 | + let encoder = JSONEncoder.makeWithDefaults() |
| 500 | + let data = try encoder.encode(configuration) |
| 501 | + if !fileSystem.exists(path.parentDirectory) { |
| 502 | + try fileSystem.createDirectory(path.parentDirectory, recursive: true) |
| 503 | + } |
| 504 | + try fileSystem.writeFileContents(path, data: data) |
| 505 | + } |
| 506 | + } |
| 507 | +} |
363 | 508 |
|
364 | 509 | // MARK: - Deprecated 8/20201
|
365 | 510 |
|
|
0 commit comments