|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Basics |
| 14 | +import Dispatch |
| 15 | +import Foundation |
| 16 | +import PackageModel |
| 17 | + |
| 18 | +import struct TSCBasic.AbsolutePath |
| 19 | +import protocol TSCBasic.FileSystem |
| 20 | +import protocol TSCBasic.HashAlgorithm |
| 21 | + |
| 22 | +// MARK: - Registry downloads manager |
| 23 | + |
| 24 | +public struct RegistryDownloads { |
| 25 | + /// Additional information about a fetch |
| 26 | + public struct FetchDetails: Equatable { |
| 27 | + /// Indicates if the repository was fetched from the cache or from the remote. |
| 28 | + public let fromCache: Bool |
| 29 | + /// Indicates wether the wether the repository was already present in the cache and updated or if a clean fetch was performed. |
| 30 | + public let updatedCache: Bool |
| 31 | + |
| 32 | + public init(fromCache: Bool, updatedCache: Bool) { |
| 33 | + self.fromCache = fromCache |
| 34 | + self.updatedCache = updatedCache |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Delegate to notify clients about actions being performed by RegistryManager. |
| 40 | +public protocol RegistryDownloadsManagerDelegate { |
| 41 | + /// Called when a package is about to be fetched. |
| 42 | + func willFetch(package: PackageIdentity, version: Version, fetchDetails: RegistryDownloads.FetchDetails) |
| 43 | + |
| 44 | + /// Called when a package has finished fetching. |
| 45 | + func didFetch(package: PackageIdentity, version: Version, result: Result<RegistryDownloads.FetchDetails, Error>, duration: DispatchTimeInterval) |
| 46 | + |
| 47 | + /// Called every time the progress of a repository fetch operation updates. |
| 48 | + func fetching(package: PackageIdentity, version: Version, bytesDownloaded: Int64, totalBytesToDownload: Int64?) |
| 49 | +} |
| 50 | + |
| 51 | +public protocol RegistryDownloadsManagerInterface { |
| 52 | + func lookup( |
| 53 | + package: PackageIdentity, |
| 54 | + version: Version, |
| 55 | + observabilityScope: ObservabilityScope, |
| 56 | + delegateQueue: DispatchQueue, |
| 57 | + callbackQueue: DispatchQueue, |
| 58 | + completion: @escaping (Result<AbsolutePath, Error>) -> Void |
| 59 | + ) |
| 60 | + |
| 61 | + func purgeCache() throws |
| 62 | + func remove(package: PackageIdentity) throws |
| 63 | + func reset() throws |
| 64 | +} |
| 65 | + |
| 66 | +// MARK: - Registry client |
| 67 | + |
| 68 | +public struct RegistryPackageMetadata { |
| 69 | + public let versions: [Version] |
| 70 | + public let alternateLocations: [URL]? |
| 71 | + |
| 72 | + public init(versions: [Version], alternateLocations: [URL]?) { |
| 73 | + self.versions = versions |
| 74 | + self.alternateLocations = alternateLocations |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +public protocol RegistryClientInterface { |
| 79 | + var configured: Bool { get } |
| 80 | + |
| 81 | + func cancel(deadline: DispatchTime) throws |
| 82 | + |
| 83 | + func downloadSourceArchive( |
| 84 | + package: PackageIdentity, |
| 85 | + version: Version, |
| 86 | + fileSystem: FileSystem, |
| 87 | + destinationPath: AbsolutePath, |
| 88 | + checksumAlgorithm: HashAlgorithm, // the same algorithm used by `package compute-checksum` tool |
| 89 | + progressHandler: ((_ bytesReceived: Int64, _ totalBytes: Int64?) -> Void)?, |
| 90 | + timeout: DispatchTimeInterval?, |
| 91 | + observabilityScope: ObservabilityScope, |
| 92 | + callbackQueue: DispatchQueue, |
| 93 | + completion: @escaping (Result<Void, Error>) -> Void |
| 94 | + ) |
| 95 | + |
| 96 | + func getAvailableManifests( |
| 97 | + package: PackageIdentity, |
| 98 | + version: Version, |
| 99 | + timeout: DispatchTimeInterval?, |
| 100 | + observabilityScope: ObservabilityScope, |
| 101 | + callbackQueue: DispatchQueue, |
| 102 | + completion: @escaping (Result<[String: (toolsVersion: ToolsVersion, content: String?)], Error>) -> Void |
| 103 | + ) |
| 104 | + |
| 105 | + func getManifestContent( |
| 106 | + package: PackageIdentity, |
| 107 | + version: Version, |
| 108 | + customToolsVersion: ToolsVersion?, |
| 109 | + timeout: DispatchTimeInterval?, |
| 110 | + observabilityScope: ObservabilityScope, |
| 111 | + callbackQueue: DispatchQueue, |
| 112 | + completion: @escaping (Result<String, Error>) -> Void |
| 113 | + ) |
| 114 | + |
| 115 | + func getPackageMetadata( |
| 116 | + package: PackageIdentity, |
| 117 | + timeout: DispatchTimeInterval?, |
| 118 | + observabilityScope: ObservabilityScope, |
| 119 | + callbackQueue: DispatchQueue, |
| 120 | + completion: @escaping (Result<RegistryPackageMetadata, Error>) -> Void |
| 121 | + ) |
| 122 | + |
| 123 | + func lookupIdentities( |
| 124 | + url: URL, |
| 125 | + timeout: DispatchTimeInterval?, |
| 126 | + observabilityScope: ObservabilityScope, |
| 127 | + callbackQueue: DispatchQueue, |
| 128 | + completion: @escaping (Result<Set<PackageIdentity>, Error>) -> Void |
| 129 | + ) |
| 130 | +} |
| 131 | + |
| 132 | +public extension RegistryClientInterface { |
| 133 | + func downloadSourceArchive( |
| 134 | + package: PackageIdentity, |
| 135 | + version: Version, |
| 136 | + fileSystem: FileSystem, |
| 137 | + destinationPath: AbsolutePath, |
| 138 | + checksumAlgorithm: HashAlgorithm, // the same algorithm used by `package compute-checksum` tool |
| 139 | + progressHandler: ((_ bytesReceived: Int64, _ totalBytes: Int64?) -> Void)?, |
| 140 | + timeout: DispatchTimeInterval? = .none, |
| 141 | + observabilityScope: ObservabilityScope, |
| 142 | + callbackQueue: DispatchQueue, |
| 143 | + completion: @escaping (Result<Void, Error>) -> Void |
| 144 | + ) { |
| 145 | + self.downloadSourceArchive(package: package, |
| 146 | + version: version, |
| 147 | + fileSystem: fileSystem, |
| 148 | + destinationPath: destinationPath, |
| 149 | + checksumAlgorithm: checksumAlgorithm, |
| 150 | + progressHandler: progressHandler, |
| 151 | + timeout: .none, |
| 152 | + observabilityScope: observabilityScope, |
| 153 | + callbackQueue: callbackQueue, |
| 154 | + completion: completion) |
| 155 | + } |
| 156 | + |
| 157 | + func getAvailableManifests( |
| 158 | + package: PackageIdentity, |
| 159 | + version: Version, |
| 160 | + observabilityScope: ObservabilityScope, |
| 161 | + callbackQueue: DispatchQueue, |
| 162 | + completion: @escaping (Result<[String: (toolsVersion: ToolsVersion, content: String?)], Error>) -> Void |
| 163 | + ) { |
| 164 | + self.getAvailableManifests(package: package, |
| 165 | + version: version, |
| 166 | + timeout: .none, |
| 167 | + observabilityScope: observabilityScope, |
| 168 | + callbackQueue: callbackQueue, |
| 169 | + completion: completion) |
| 170 | + } |
| 171 | + |
| 172 | + func getManifestContent( |
| 173 | + package: PackageIdentity, |
| 174 | + version: Version, |
| 175 | + customToolsVersion: ToolsVersion?, |
| 176 | + observabilityScope: ObservabilityScope, |
| 177 | + callbackQueue: DispatchQueue, |
| 178 | + completion: @escaping (Result<String, Error>) -> Void |
| 179 | + ) { |
| 180 | + self.getManifestContent(package: package, |
| 181 | + version: version, |
| 182 | + customToolsVersion: customToolsVersion, |
| 183 | + timeout: .none, |
| 184 | + observabilityScope: observabilityScope, |
| 185 | + callbackQueue: callbackQueue, |
| 186 | + completion: completion) |
| 187 | + } |
| 188 | + |
| 189 | + func getPackageMetadata( |
| 190 | + package: PackageIdentity, |
| 191 | + observabilityScope: ObservabilityScope, |
| 192 | + callbackQueue: DispatchQueue, |
| 193 | + completion: @escaping (Result<RegistryPackageMetadata, Error>) -> Void |
| 194 | + ) { |
| 195 | + self.getPackageMetadata( |
| 196 | + package: package, |
| 197 | + timeout: .none, |
| 198 | + observabilityScope: observabilityScope, |
| 199 | + callbackQueue: callbackQueue, |
| 200 | + completion: completion) |
| 201 | + } |
| 202 | + |
| 203 | + func lookupIdentities( |
| 204 | + url: URL, |
| 205 | + observabilityScope: ObservabilityScope, |
| 206 | + callbackQueue: DispatchQueue, |
| 207 | + completion: @escaping (Result<Set<PackageIdentity>, Error>) -> Void |
| 208 | + ) { |
| 209 | + self.lookupIdentities(url: url, |
| 210 | + timeout: .none, |
| 211 | + observabilityScope: observabilityScope, |
| 212 | + callbackQueue: callbackQueue, |
| 213 | + completion: completion) |
| 214 | + } |
| 215 | +} |
0 commit comments