Skip to content

Commit 10ba39d

Browse files
committed
Commands: make some of the private API more public
This extends the visibility of some of the private implementation to the package level to allow use for testing without `@testable` imports.
1 parent ca97038 commit 10ba39d

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

Sources/Commands/CommandWorkspaceDelegate.swift

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import Workspace
2424
import protocol TSCBasic.OutputByteStream
2525
import struct TSCUtility.Version
2626

27-
final class CommandWorkspaceDelegate: WorkspaceDelegate {
27+
package final class CommandWorkspaceDelegate: WorkspaceDelegate {
2828
private struct DownloadProgress {
2929
let bytesDownloaded: Int64
3030
let totalBytesToDownload: Int64
@@ -49,7 +49,7 @@ final class CommandWorkspaceDelegate: WorkspaceDelegate {
4949
private let progressHandler: (Int64, Int64, String?) -> Void
5050
private let inputHandler: (String, (String?) -> Void) -> Void
5151

52-
init(
52+
package init(
5353
observabilityScope: ObservabilityScope,
5454
outputHandler: @escaping (String, Bool) -> Void,
5555
progressHandler: @escaping (Int64, Int64, String?) -> Void,
@@ -61,11 +61,11 @@ final class CommandWorkspaceDelegate: WorkspaceDelegate {
6161
self.inputHandler = inputHandler
6262
}
6363

64-
func willFetchPackage(package: PackageIdentity, packageLocation: String?, fetchDetails: PackageFetchDetails) {
64+
package func willFetchPackage(package: PackageIdentity, packageLocation: String?, fetchDetails: PackageFetchDetails) {
6565
self.outputHandler("Fetching \(packageLocation ?? package.description)\(fetchDetails.fromCache ? " from cache" : "")", false)
6666
}
6767

68-
func didFetchPackage(package: PackageIdentity, packageLocation: String?, result: Result<PackageFetchDetails, Error>, duration: DispatchTimeInterval) {
68+
package func didFetchPackage(package: PackageIdentity, packageLocation: String?, result: Result<PackageFetchDetails, Error>, duration: DispatchTimeInterval) {
6969
guard case .success = result, !self.observabilityScope.errorsReported else {
7070
return
7171
}
@@ -84,7 +84,7 @@ final class CommandWorkspaceDelegate: WorkspaceDelegate {
8484
self.outputHandler("Fetched \(packageLocation ?? package.description) from cache (\(duration.descriptionInSeconds))", false)
8585
}
8686

87-
func fetchingPackage(package: PackageIdentity, packageLocation: String?, progress: Int64, total: Int64?) {
87+
package func fetchingPackage(package: PackageIdentity, packageLocation: String?, progress: Int64, total: Int64?) {
8888
let (step, total, packages) = self.fetchProgressLock.withLock { () -> (Int64, Int64, String) in
8989
self.fetchProgress[package] = FetchProgress(
9090
progress: progress,
@@ -99,51 +99,51 @@ final class CommandWorkspaceDelegate: WorkspaceDelegate {
9999
self.progressHandler(step, total, "Fetching \(packages)")
100100
}
101101

102-
func willUpdateRepository(package: PackageIdentity, repository url: String) {
102+
package func willUpdateRepository(package: PackageIdentity, repository url: String) {
103103
self.outputHandler("Updating \(url)", false)
104104
}
105105

106-
func didUpdateRepository(package: PackageIdentity, repository url: String, duration: DispatchTimeInterval) {
106+
package func didUpdateRepository(package: PackageIdentity, repository url: String, duration: DispatchTimeInterval) {
107107
self.outputHandler("Updated \(url) (\(duration.descriptionInSeconds))", false)
108108
}
109109

110-
func dependenciesUpToDate() {
110+
package func dependenciesUpToDate() {
111111
self.outputHandler("Everything is already up-to-date", false)
112112
}
113113

114-
func willCreateWorkingCopy(package: PackageIdentity, repository url: String, at path: AbsolutePath) {
114+
package func willCreateWorkingCopy(package: PackageIdentity, repository url: String, at path: AbsolutePath) {
115115
self.outputHandler("Creating working copy for \(url)", false)
116116
}
117117

118-
func didCheckOut(package: PackageIdentity, repository url: String, revision: String, at path: AbsolutePath, duration: DispatchTimeInterval) {
118+
package func didCheckOut(package: PackageIdentity, repository url: String, revision: String, at path: AbsolutePath, duration: DispatchTimeInterval) {
119119
self.outputHandler("Working copy of \(url) resolved at \(revision)", false)
120120
}
121121

122-
func removing(package: PackageIdentity, packageLocation: String?) {
122+
package func removing(package: PackageIdentity, packageLocation: String?) {
123123
self.outputHandler("Removing \(packageLocation ?? package.description)", false)
124124
}
125125

126-
func willResolveDependencies(reason: WorkspaceResolveReason) {
126+
package func willResolveDependencies(reason: WorkspaceResolveReason) {
127127
self.outputHandler(Workspace.format(workspaceResolveReason: reason), true)
128128
}
129129

130-
func willComputeVersion(package: PackageIdentity, location: String) {
130+
package func willComputeVersion(package: PackageIdentity, location: String) {
131131
self.outputHandler("Computing version for \(location)", false)
132132
}
133133

134-
func didComputeVersion(package: PackageIdentity, location: String, version: String, duration: DispatchTimeInterval) {
134+
package func didComputeVersion(package: PackageIdentity, location: String, version: String, duration: DispatchTimeInterval) {
135135
self.outputHandler("Computed \(location) at \(version) (\(duration.descriptionInSeconds))", false)
136136
}
137137

138-
func willDownloadBinaryArtifact(from url: String, fromCache: Bool) {
138+
package func willDownloadBinaryArtifact(from url: String, fromCache: Bool) {
139139
if fromCache {
140140
self.outputHandler("Fetching binary artifact \(url) from cache", false)
141141
} else {
142142
self.outputHandler("Downloading binary artifact \(url)", false)
143143
}
144144
}
145145

146-
func didDownloadBinaryArtifact(from url: String, result: Result<(path: AbsolutePath, fromCache: Bool), Error>, duration: DispatchTimeInterval) {
146+
package func didDownloadBinaryArtifact(from url: String, result: Result<(path: AbsolutePath, fromCache: Bool), Error>, duration: DispatchTimeInterval) {
147147
guard case .success(let fetchDetails) = result, !self.observabilityScope.errorsReported else {
148148
return
149149
}
@@ -166,7 +166,7 @@ final class CommandWorkspaceDelegate: WorkspaceDelegate {
166166
}
167167
}
168168

169-
func downloadingBinaryArtifact(from url: String, bytesDownloaded: Int64, totalBytesToDownload: Int64?) {
169+
package func downloadingBinaryArtifact(from url: String, bytesDownloaded: Int64, totalBytesToDownload: Int64?) {
170170
let (step, total, artifacts) = self.binaryDownloadProgressLock.withLock { () -> (Int64, Int64, String) in
171171
self.binaryDownloadProgress[url] = DownloadProgress(
172172
bytesDownloaded: bytesDownloaded,
@@ -183,7 +183,7 @@ final class CommandWorkspaceDelegate: WorkspaceDelegate {
183183
}
184184

185185
/// The workspace has started downloading a binary artifact.
186-
func willDownloadPrebuilt(package: PackageIdentity, from url: String, fromCache: Bool) {
186+
package func willDownloadPrebuilt(package: PackageIdentity, from url: String, fromCache: Bool) {
187187
if fromCache {
188188
self.outputHandler("Fetching package prebuilt \(url) from cache", false)
189189
} else {
@@ -192,7 +192,7 @@ final class CommandWorkspaceDelegate: WorkspaceDelegate {
192192
}
193193

194194
/// The workspace has finished downloading a binary artifact.
195-
func didDownloadPrebuilt(
195+
package func didDownloadPrebuilt(
196196
package: PackageIdentity,
197197
from url: String,
198198
result: Result<(path: AbsolutePath, fromCache: Bool), Error>,
@@ -210,18 +210,18 @@ final class CommandWorkspaceDelegate: WorkspaceDelegate {
210210
}
211211

212212
/// The workspace is downloading a binary artifact.
213-
func downloadingPrebuilt(package: PackageIdentity, from url: String, bytesDownloaded: Int64, totalBytesToDownload: Int64?) {
213+
package func downloadingPrebuilt(package: PackageIdentity, from url: String, bytesDownloaded: Int64, totalBytesToDownload: Int64?) {
214214

215215
}
216216

217217
/// The workspace finished downloading all binary artifacts.
218-
func didDownloadAllPrebuilts() {
218+
package func didDownloadAllPrebuilts() {
219219

220220
}
221221

222222
// registry signature handlers
223223

224-
func onUnsignedRegistryPackage(registryURL: URL, package: PackageModel.PackageIdentity, version: TSCUtility.Version, completion: (Bool) -> Void) {
224+
package func onUnsignedRegistryPackage(registryURL: URL, package: PackageModel.PackageIdentity, version: TSCUtility.Version, completion: (Bool) -> Void) {
225225
self.inputHandler("\(package) \(version) from \(registryURL) is unsigned. okay to proceed? (yes/no) ") { response in
226226
switch response?.lowercased() {
227227
case "yes":
@@ -235,7 +235,7 @@ final class CommandWorkspaceDelegate: WorkspaceDelegate {
235235
}
236236
}
237237

238-
func onUntrustedRegistryPackage(registryURL: URL, package: PackageModel.PackageIdentity, version: TSCUtility.Version, completion: (Bool) -> Void) {
238+
package func onUntrustedRegistryPackage(registryURL: URL, package: PackageModel.PackageIdentity, version: TSCUtility.Version, completion: (Bool) -> Void) {
239239
self.inputHandler("\(package) \(version) from \(registryURL) is signed with an untrusted certificate. okay to proceed? (yes/no) ") { response in
240240
switch response?.lowercased() {
241241
case "yes":
@@ -269,36 +269,36 @@ final class CommandWorkspaceDelegate: WorkspaceDelegate {
269269
os_signpost(.end, name: SignpostName.resolvingDependencies)
270270
}
271271

272-
func willLoadGraph() {
272+
package func willLoadGraph() {
273273
self.observabilityScope.emit(debug: "Loading and validating graph")
274274
os_signpost(.begin, name: SignpostName.loadingGraph)
275275
}
276276

277-
func didLoadGraph(duration: DispatchTimeInterval) {
277+
package func didLoadGraph(duration: DispatchTimeInterval) {
278278
self.observabilityScope.emit(debug: "Graph loaded in (\(duration.descriptionInSeconds))")
279279
os_signpost(.end, name: SignpostName.loadingGraph)
280280
}
281281

282-
func didCompileManifest(packageIdentity: PackageIdentity, packageLocation: String, duration: DispatchTimeInterval) {
282+
package func didCompileManifest(packageIdentity: PackageIdentity, packageLocation: String, duration: DispatchTimeInterval) {
283283
self.observabilityScope.emit(debug: "Compiled manifest for '\(packageIdentity)' (from '\(packageLocation)') in \(duration.descriptionInSeconds)")
284284
}
285285

286-
func didEvaluateManifest(packageIdentity: PackageIdentity, packageLocation: String, duration: DispatchTimeInterval) {
286+
package func didEvaluateManifest(packageIdentity: PackageIdentity, packageLocation: String, duration: DispatchTimeInterval) {
287287
self.observabilityScope.emit(debug: "Evaluated manifest for '\(packageIdentity)' (from '\(packageLocation)') in \(duration.descriptionInSeconds)")
288288
}
289289

290-
func didLoadManifest(packageIdentity: PackageIdentity, packagePath: AbsolutePath, url: String, version: Version?, packageKind: PackageReference.Kind, manifest: Manifest?, diagnostics: [Basics.Diagnostic], duration: DispatchTimeInterval) {
290+
package func didLoadManifest(packageIdentity: PackageIdentity, packagePath: AbsolutePath, url: String, version: Version?, packageKind: PackageReference.Kind, manifest: Manifest?, diagnostics: [Basics.Diagnostic], duration: DispatchTimeInterval) {
291291
self.observabilityScope.emit(debug: "Loaded manifest for '\(packageIdentity)' (from '\(url)') in \(duration.descriptionInSeconds)")
292292
}
293293

294294
// noop
295-
func willCheckOut(package: PackageIdentity, repository url: String, revision: String, at path: AbsolutePath) {}
296-
func didCreateWorkingCopy(package: PackageIdentity, repository url: String, at path: AbsolutePath, duration: DispatchTimeInterval) {}
297-
func resolvedFileChanged() {}
298-
func didDownloadAllBinaryArtifacts() {}
299-
func willCompileManifest(packageIdentity: PackageIdentity, packageLocation: String) {}
300-
func willEvaluateManifest(packageIdentity: PackageIdentity, packageLocation: String) {}
301-
func willLoadManifest(packageIdentity: PackageIdentity, packagePath: AbsolutePath, url: String, version: Version?, packageKind: PackageReference.Kind) {}
295+
package func willCheckOut(package: PackageIdentity, repository url: String, revision: String, at path: AbsolutePath) {}
296+
package func didCreateWorkingCopy(package: PackageIdentity, repository url: String, at path: AbsolutePath, duration: DispatchTimeInterval) {}
297+
package func resolvedFileChanged() {}
298+
package func didDownloadAllBinaryArtifacts() {}
299+
package func willCompileManifest(packageIdentity: PackageIdentity, packageLocation: String) {}
300+
package func willEvaluateManifest(packageIdentity: PackageIdentity, packageLocation: String) {}
301+
package func willLoadManifest(packageIdentity: PackageIdentity, packagePath: AbsolutePath, url: String, version: Version?, packageKind: PackageReference.Kind) {}
302302
}
303303

304304
public extension _SwiftCommand {

Sources/Commands/PackageCommands/ShowDependencies.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import protocol TSCBasic.OutputByteStream
2121
import var TSCBasic.stdoutStream
2222

2323
extension SwiftPackageCommand {
24-
struct ShowDependencies: AsyncSwiftCommand {
25-
static let configuration = CommandConfiguration(
24+
package struct ShowDependencies: AsyncSwiftCommand {
25+
package static let configuration = CommandConfiguration(
2626
abstract: "Print the resolved dependency graph.")
2727

2828
@OptionGroup(visibility: .hidden)
29-
var globalOptions: GlobalOptions
29+
package var globalOptions: GlobalOptions
3030

3131
@Option(help: "Set the output format.")
3232
var format: ShowDependenciesMode = .text
@@ -35,7 +35,9 @@ extension SwiftPackageCommand {
3535
help: "The absolute or relative path to output the resolved dependency graph.")
3636
var outputPath: AbsolutePath?
3737

38-
func run(_ swiftCommandState: SwiftCommandState) async throws {
38+
package init() {}
39+
40+
package func run(_ swiftCommandState: SwiftCommandState) async throws {
3941
let graph = try await swiftCommandState.loadPackageGraph()
4042
// command's result output goes on stdout
4143
// ie "swift package show-dependencies" should output to stdout
@@ -48,7 +50,7 @@ extension SwiftPackageCommand {
4850
)
4951
}
5052

51-
static func dumpDependenciesOf(
53+
package static func dumpDependenciesOf(
5254
graph: ModulesGraph,
5355
rootPackage: ResolvedPackage,
5456
mode: ShowDependenciesMode,
@@ -69,7 +71,7 @@ extension SwiftPackageCommand {
6971
stream.flush()
7072
}
7173

72-
enum ShowDependenciesMode: String, RawRepresentable, CustomStringConvertible, ExpressibleByArgument, CaseIterable {
74+
package enum ShowDependenciesMode: String, RawRepresentable, CustomStringConvertible, ExpressibleByArgument, CaseIterable {
7375
case text, dot, json, flatlist
7476

7577
public init?(rawValue: String) {

Sources/Commands/Utilities/MermaidPackageSerializer.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ import class PackageModel.Package
1515
import class PackageModel.Product
1616
import class PackageModel.Module
1717

18-
struct MermaidPackageSerializer {
18+
package struct MermaidPackageSerializer {
1919
let package: Package
2020
var shouldIncludeLegend = false
2121

22-
var renderedMarkdown: String {
22+
package init(package: Package) {
23+
self.package = package
24+
}
25+
26+
package var renderedMarkdown: String {
2327
var subgraphs = OrderedDictionary<String, [Edge]>()
2428
subgraphs[package.identity.description] = package.products.productTargetEdges
2529

0 commit comments

Comments
 (0)