fix: expand triple for bundle info.json too.

This commit is contained in:
Shibo Lyu 2025-10-05 17:37:39 +08:00
parent b4f1cf9dbd
commit caa5c6651b
5 changed files with 135 additions and 1 deletions

View file

@ -0,0 +1,41 @@
import Foundation
extension ArtifactBundleBuilder {
func downloadFile(from urlString: String, to destination: String) throws {
guard let url = URL(string: urlString) else {
throw ArtifactBundleError.invalidURL(urlString)
}
let data = try Data(contentsOf: url)
try data.write(to: URL(fileURLWithPath: destination))
}
func makeExecutable(path: String) throws {
let attributes = [FileAttributeKey.posixPermissions: 0o755]
try fileManager.setAttributes(attributes, ofItemAtPath: path)
}
func createZipFile(bundleDir: String, zipPath: String) throws {
// Remove existing ZIP file if it exists
if fileManager.fileExists(atPath: zipPath) {
try fileManager.removeItem(atPath: zipPath)
}
let bundleDirURL = URL(fileURLWithPath: bundleDir)
let workDirURL = bundleDirURL.deletingLastPathComponent()
let bundleName = bundleDirURL.lastPathComponent
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/zip")
process.arguments = ["-r", zipPath, bundleName]
process.currentDirectoryURL = workDirURL
try process.run()
process.waitUntilExit()
guard process.terminationStatus == 0 else {
throw ArtifactBundleError.zipCreationFailed
}
}
}

View file

@ -0,0 +1,46 @@
import Crypto
import Foundation
extension ArtifactBundleBuilder {
/// Computes the SHA256 checksum of a file.
///
/// If `usingSHA256Directly` is true, it uses Swift Crypto's SHA256 implementation.
/// This is to workaround https://github.com/swiftlang/swift-package-manager/issues/9219.
func computeChecksum(
filePath: String,
usingSHA256Directly: Bool = false
) throws -> String {
if usingSHA256Directly {
// Use swift-crypto's SHA256 implementation
let fileURL = URL(fileURLWithPath: filePath)
let data = try Data(contentsOf: fileURL)
let hash = SHA256.hash(data: data)
return hash.compactMap { String(format: "%02x", $0) }.joined()
} else {
// Use swift package compute-checksum command
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/swift")
process.arguments = ["package", "compute-checksum", filePath]
let pipe = Pipe()
process.standardOutput = pipe
try process.run()
process.waitUntilExit()
guard process.terminationStatus == 0 else {
throw ArtifactBundleError.checksumComputationFailed
}
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)?.trimmingCharacters(
in: .whitespacesAndNewlines)
guard let checksum = output, !checksum.isEmpty else {
throw ArtifactBundleError.checksumComputationFailed
}
return checksum
}
}
}

View file

@ -0,0 +1,29 @@
extension ArtifactBundleBuilder {
/// Expands a list of triples into a stricter list of triples.
///
/// To workaround https://github.com/swiftlang/swift-package-manager/issues/7362.
func expandingTriple(_ triple: String) -> [String] {
switch triple {
case "aarch64-apple-darwin":
[
"aarch64-apple-darwin",
"arm64-apple-macosx12.0",
"arm64-apple-macosx13.0",
"arm64-apple-macosx14.0",
"arm64-apple-macosx15.0",
"arm64-apple-macosx26.0",
]
case "x86_64-apple-darwin":
[
"x86_64-apple-darwin",
"x86_64-apple-macosx12.0",
"x86_64-apple-macosx13.0",
"x86_64-apple-macosx14.0",
"x86_64-apple-macosx15.0",
"x86_64-apple-macosx26.0",
]
// TODO: Does linux need more detailed triple variants?
default: [triple]
}
}
}

View file

@ -110,7 +110,7 @@ class ArtifactBundleBuilder {
version: version,
type: "executable",
variants: [
ArtifactVariant(path: binaryPath, supportedTriples: [triple])
ArtifactVariant(path: binaryPath, supportedTriples: expandingTriple(triple))
]
)

View file

@ -0,0 +1,18 @@
import Foundation
enum ArtifactBundleError: Error, LocalizedError {
case invalidURL(String)
case zipCreationFailed
case checksumComputationFailed
var errorDescription: String? {
switch self {
case .invalidURL(let url):
return "Invalid URL: \(url)"
case .zipCreationFailed:
return "Failed to create ZIP file"
case .checksumComputationFailed:
return "Failed to compute checksum"
}
}
}