|
| 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 Build |
| 15 | +import CoreCommands |
| 16 | +import Dispatch |
| 17 | +import class Foundation.NSLock |
| 18 | +import OrderedCollections |
| 19 | +import PackageGraph |
| 20 | +import PackageModel |
| 21 | +import SPMBuildCore |
| 22 | +import Workspace |
| 23 | + |
| 24 | +import struct TSCBasic.AbsolutePath |
| 25 | +import protocol TSCBasic.OutputByteStream |
| 26 | + |
| 27 | +class ToolWorkspaceDelegate: WorkspaceDelegate { |
| 28 | + private struct DownloadProgress { |
| 29 | + let bytesDownloaded: Int64 |
| 30 | + let totalBytesToDownload: Int64 |
| 31 | + } |
| 32 | + |
| 33 | + private struct FetchProgress { |
| 34 | + let progress: Int64 |
| 35 | + let total: Int64 |
| 36 | + } |
| 37 | + |
| 38 | + /// The progress of binary downloads. |
| 39 | + private var binaryDownloadProgress = OrderedCollections.OrderedDictionary<String, DownloadProgress>() |
| 40 | + private let binaryDownloadProgressLock = NSLock() |
| 41 | + |
| 42 | + /// The progress of package fetch operations. |
| 43 | + private var fetchProgress = OrderedCollections.OrderedDictionary<PackageIdentity, FetchProgress>() |
| 44 | + private let fetchProgressLock = NSLock() |
| 45 | + |
| 46 | + private let observabilityScope: ObservabilityScope |
| 47 | + |
| 48 | + private let outputHandler: (String, Bool) -> Void |
| 49 | + private let progressHandler: (Int64, Int64, String?) -> Void |
| 50 | + |
| 51 | + init( |
| 52 | + observabilityScope: ObservabilityScope, |
| 53 | + outputHandler: @escaping (String, Bool) -> Void, |
| 54 | + progressHandler: @escaping (Int64, Int64, String?) -> Void |
| 55 | + ) { |
| 56 | + self.observabilityScope = observabilityScope |
| 57 | + self.outputHandler = outputHandler |
| 58 | + self.progressHandler = progressHandler |
| 59 | + } |
| 60 | + |
| 61 | + func willFetchPackage(package: PackageIdentity, packageLocation: String?, fetchDetails: PackageFetchDetails) { |
| 62 | + self.outputHandler("Fetching \(packageLocation ?? package.description)\(fetchDetails.fromCache ? " from cache" : "")", false) |
| 63 | + } |
| 64 | + |
| 65 | + func didFetchPackage(package: PackageIdentity, packageLocation: String?, result: Result<PackageFetchDetails, Error>, duration: DispatchTimeInterval) { |
| 66 | + guard case .success = result, !self.observabilityScope.errorsReported else { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + self.fetchProgressLock.withLock { |
| 71 | + let progress = self.fetchProgress.values.reduce(0) { $0 + $1.progress } |
| 72 | + let total = self.fetchProgress.values.reduce(0) { $0 + $1.total } |
| 73 | + |
| 74 | + if progress == total && !self.fetchProgress.isEmpty { |
| 75 | + self.fetchProgress.removeAll() |
| 76 | + } else { |
| 77 | + self.fetchProgress[package] = nil |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + self.outputHandler("Fetched \(packageLocation ?? package.description) (\(duration.descriptionInSeconds))", false) |
| 82 | + } |
| 83 | + |
| 84 | + func fetchingPackage(package: PackageIdentity, packageLocation: String?, progress: Int64, total: Int64?) { |
| 85 | + let (step, total, packages) = self.fetchProgressLock.withLock { () -> (Int64, Int64, String) in |
| 86 | + self.fetchProgress[package] = FetchProgress( |
| 87 | + progress: progress, |
| 88 | + total: total ?? progress |
| 89 | + ) |
| 90 | + |
| 91 | + let progress = self.fetchProgress.values.reduce(0) { $0 + $1.progress } |
| 92 | + let total = self.fetchProgress.values.reduce(0) { $0 + $1.total } |
| 93 | + let packages = self.fetchProgress.keys.map { $0.description }.joined(separator: ", ") |
| 94 | + return (progress, total, packages) |
| 95 | + } |
| 96 | + self.progressHandler(step, total, "Fetching \(packages)") |
| 97 | + } |
| 98 | + |
| 99 | + func willUpdateRepository(package: PackageIdentity, repository url: String) { |
| 100 | + self.outputHandler("Updating \(url)", false) |
| 101 | + } |
| 102 | + |
| 103 | + func didUpdateRepository(package: PackageIdentity, repository url: String, duration: DispatchTimeInterval) { |
| 104 | + self.outputHandler("Updated \(url) (\(duration.descriptionInSeconds))", false) |
| 105 | + } |
| 106 | + |
| 107 | + func dependenciesUpToDate() { |
| 108 | + self.outputHandler("Everything is already up-to-date", false) |
| 109 | + } |
| 110 | + |
| 111 | + func willCreateWorkingCopy(package: PackageIdentity, repository url: String, at path: AbsolutePath) { |
| 112 | + self.outputHandler("Creating working copy for \(url)", false) |
| 113 | + } |
| 114 | + |
| 115 | + func didCheckOut(package: PackageIdentity, repository url: String, revision: String, at path: AbsolutePath) { |
| 116 | + self.outputHandler("Working copy of \(url) resolved at \(revision)", false) |
| 117 | + } |
| 118 | + |
| 119 | + func removing(package: PackageIdentity, packageLocation: String?) { |
| 120 | + self.outputHandler("Removing \(packageLocation ?? package.description)", false) |
| 121 | + } |
| 122 | + |
| 123 | + func willResolveDependencies(reason: WorkspaceResolveReason) { |
| 124 | + self.outputHandler(Workspace.format(workspaceResolveReason: reason), true) |
| 125 | + } |
| 126 | + |
| 127 | + func willComputeVersion(package: PackageIdentity, location: String) { |
| 128 | + self.outputHandler("Computing version for \(location)", false) |
| 129 | + } |
| 130 | + |
| 131 | + func didComputeVersion(package: PackageIdentity, location: String, version: String, duration: DispatchTimeInterval) { |
| 132 | + self.outputHandler("Computed \(location) at \(version) (\(duration.descriptionInSeconds))", false) |
| 133 | + } |
| 134 | + |
| 135 | + func willDownloadBinaryArtifact(from url: String) { |
| 136 | + self.outputHandler("Downloading binary artifact \(url)", false) |
| 137 | + } |
| 138 | + |
| 139 | + func didDownloadBinaryArtifact(from url: String, result: Result<AbsolutePath, Error>, duration: DispatchTimeInterval) { |
| 140 | + guard case .success = result, !self.observabilityScope.errorsReported else { |
| 141 | + return |
| 142 | + } |
| 143 | + |
| 144 | + self.binaryDownloadProgressLock.withLock { |
| 145 | + let progress = self.binaryDownloadProgress.values.reduce(0) { $0 + $1.bytesDownloaded } |
| 146 | + let total = self.binaryDownloadProgress.values.reduce(0) { $0 + $1.totalBytesToDownload } |
| 147 | + |
| 148 | + if progress == total && !self.binaryDownloadProgress.isEmpty { |
| 149 | + self.binaryDownloadProgress.removeAll() |
| 150 | + } else { |
| 151 | + self.binaryDownloadProgress[url] = nil |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + self.outputHandler("Downloaded \(url) (\(duration.descriptionInSeconds))", false) |
| 156 | + } |
| 157 | + |
| 158 | + func downloadingBinaryArtifact(from url: String, bytesDownloaded: Int64, totalBytesToDownload: Int64?) { |
| 159 | + let (step, total, artifacts) = self.binaryDownloadProgressLock.withLock { () -> (Int64, Int64, String) in |
| 160 | + self.binaryDownloadProgress[url] = DownloadProgress( |
| 161 | + bytesDownloaded: bytesDownloaded, |
| 162 | + totalBytesToDownload: totalBytesToDownload ?? bytesDownloaded |
| 163 | + ) |
| 164 | + |
| 165 | + let step = self.binaryDownloadProgress.values.reduce(0, { $0 + $1.bytesDownloaded }) |
| 166 | + let total = self.binaryDownloadProgress.values.reduce(0, { $0 + $1.totalBytesToDownload }) |
| 167 | + let artifacts = self.binaryDownloadProgress.keys.joined(separator: ", ") |
| 168 | + return (step, total, artifacts) |
| 169 | + } |
| 170 | + |
| 171 | + self.progressHandler(step, total, "Downloading \(artifacts)") |
| 172 | + } |
| 173 | + |
| 174 | + // noop |
| 175 | + |
| 176 | + func willLoadManifest(packagePath: AbsolutePath, url: String, version: Version?, packageKind: PackageReference.Kind) {} |
| 177 | + func didLoadManifest(packagePath: AbsolutePath, url: String, version: Version?, packageKind: PackageReference.Kind, manifest: Manifest?, diagnostics: [Basics.Diagnostic]) {} |
| 178 | + func willCheckOut(package: PackageIdentity, repository url: String, revision: String, at path: AbsolutePath) {} |
| 179 | + func didCreateWorkingCopy(package: PackageIdentity, repository url: String, at path: AbsolutePath) {} |
| 180 | + func resolvedFileChanged() {} |
| 181 | + func didDownloadAllBinaryArtifacts() {} |
| 182 | +} |
| 183 | + |
| 184 | +extension SwiftCommand { |
| 185 | + public var workspaceDelegateProvider: WorkspaceDelegateProvider { |
| 186 | + return { |
| 187 | + ToolWorkspaceDelegate(observabilityScope: $0, outputHandler: $1, progressHandler: $2) |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + public var workspaceLoaderProvider: WorkspaceLoaderProvider { |
| 192 | + return { |
| 193 | + XcodeWorkspaceLoader(fileSystem: $0, observabilityScope: $1) |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments