diff --git a/Sources/Basics/Archiver/Archiver.swift b/Sources/Basics/Archiver/Archiver.swift index e9d416ef21d..6a2502158f3 100644 --- a/Sources/Basics/Archiver/Archiver.swift +++ b/Sources/Basics/Archiver/Archiver.swift @@ -95,4 +95,8 @@ extension Archiver { self.validate(path: path, completion: { continuation.resume(with: $0) }) } } + + package func isFileSupported(_ lastPathComponent: String) -> Bool { + self.supportedExtensions.contains(where: { lastPathComponent.hasSuffix($0) }) + } } diff --git a/Sources/Commands/PackageCommands/ComputeChecksum.swift b/Sources/Commands/PackageCommands/ComputeChecksum.swift index 9a0b92e78e0..7ea69dc2335 100644 --- a/Sources/Commands/PackageCommands/ComputeChecksum.swift +++ b/Sources/Commands/PackageCommands/ComputeChecksum.swift @@ -28,17 +28,10 @@ struct ComputeChecksum: SwiftCommand { var path: AbsolutePath func run(_ swiftCommandState: SwiftCommandState) throws { - let binaryArtifactsManager = try Workspace.BinaryArtifactsManager( - fileSystem: swiftCommandState.fileSystem, - authorizationProvider: swiftCommandState.getAuthorizationProvider(), - hostToolchain: swiftCommandState.getHostToolchain(), - checksumAlgorithm: SHA256(), - cachePath: .none, - customHTTPClient: .none, - customArchiver: .none, - delegate: .none + let checksum = try Workspace.BinaryArtifactsManager.checksum( + forBinaryArtifactAt: self.path, + fileSystem: swiftCommandState.fileSystem ) - let checksum = try binaryArtifactsManager.checksum(forBinaryArtifactAt: path) print(checksum) } } diff --git a/Sources/Workspace/Workspace+BinaryArtifacts.swift b/Sources/Workspace/Workspace+BinaryArtifacts.swift index f36ea3f1a87..c3f7e669dcb 100644 --- a/Sources/Workspace/Workspace+BinaryArtifacts.swift +++ b/Sources/Workspace/Workspace+BinaryArtifacts.swift @@ -18,7 +18,7 @@ import SPMBuildCore import struct TSCBasic.ByteString import protocol TSCBasic.HashAlgorithm - +import struct TSCBasic.SHA256 import enum TSCUtility.Diagnostics extension Workspace { @@ -537,20 +537,35 @@ extension Workspace { return result.get() } - public func checksum(forBinaryArtifactAt path: AbsolutePath) throws -> String { + package static func checksum( + forBinaryArtifactAt path: AbsolutePath, + hashAlgorithm: HashAlgorithm = SHA256(), + archiver: (any Archiver)? = nil, + fileSystem: any FileSystem + ) throws -> String { + let archiver = archiver ?? UniversalArchiver(fileSystem) // Validate the path has a supported extension. - guard let pathExtension = path.extension, self.archiver.supportedExtensions.contains(pathExtension) else { - let supportedExtensionList = self.archiver.supportedExtensions.joined(separator: ", ") + guard let lastPathComponent = path.components.last, archiver.isFileSupported(lastPathComponent) else { + let supportedExtensionList = archiver.supportedExtensions.joined(separator: ", ") throw StringError("unexpected file type; supported extensions are: \(supportedExtensionList)") } // Ensure that the path with the accepted extension is a file. - guard self.fileSystem.isFile(path) else { + guard fileSystem.isFile(path) else { throw StringError("file not found at path: \(path.pathString)") } - let contents = try self.fileSystem.readFileContents(path) - return self.checksumAlgorithm.hash(contents).hexadecimalRepresentation + let contents = try fileSystem.readFileContents(path) + return hashAlgorithm.hash(contents).hexadecimalRepresentation + } + + public func checksum(forBinaryArtifactAt path: AbsolutePath) throws -> String { + try Self.checksum( + forBinaryArtifactAt: path, + hashAlgorithm: self.checksumAlgorithm, + archiver: self.archiver, + fileSystem: self.fileSystem + ) } public func cancel(deadline: DispatchTime) throws {