refactor: Update TailwindCSSCLI binary target and refactor builder

This commit is contained in:
Shibo Lyu 2025-10-05 17:25:53 +08:00
parent eafe2a2b94
commit b4f1cf9dbd
2 changed files with 4 additions and 101 deletions

View file

@ -25,8 +25,8 @@ let package = Package(
.binaryTarget( .binaryTarget(
name: "TailwindCSSCLI", name: "TailwindCSSCLI",
url: url:
"https://github.com/laosb/SwiftTailwind/releases/download/1.1.0-test.2+tw.4.1.14/tailwindcss.artifactbundleindex", "https://github.com/laosb/SwiftTailwind/releases/download/1.1.0-test.3+tw.4.1.14/tailwindcss.artifactbundleindex",
checksum: "4462492d557c16c07a3c8c07980eea54e4460d925c1b9a097ad91f1c901440ec" checksum: "3287be503b5d954d1946a110cf2a1d14d37f9941983afe55f1743ddb5b487392"
), ),
.target( .target(
name: "SwiftTailwindExample", name: "SwiftTailwindExample",

View file

@ -1,11 +1,10 @@
import Crypto
import Foundation import Foundation
class ArtifactBundleBuilder { class ArtifactBundleBuilder {
private let version: String private let version: String
private let workDir: String private let workDir: String
private let outputDir: String private let outputDir: String
private let fileManager = FileManager.default let fileManager = FileManager.default
private let binaryConfigurations: [BinaryConfiguration] = [ private let binaryConfigurations: [BinaryConfiguration] = [
BinaryConfiguration(binaryName: "tailwindcss-linux-x64", triple: "x86_64-unknown-linux-gnu"), BinaryConfiguration(binaryName: "tailwindcss-linux-x64", triple: "x86_64-unknown-linux-gnu"),
@ -106,20 +105,6 @@ class ArtifactBundleBuilder {
return BundleInfo(fileName: zipFileName, checksum: checksum, triple: config.triple) return BundleInfo(fileName: zipFileName, checksum: checksum, triple: config.triple)
} }
private 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))
}
private func makeExecutable(path: String) throws {
let attributes = [FileAttributeKey.posixPermissions: 0o755]
try fileManager.setAttributes(attributes, ofItemAtPath: path)
}
private func createInfoJSON(bundleDir: String, binaryPath: String, triple: String) throws { private func createInfoJSON(bundleDir: String, binaryPath: String, triple: String) throws {
let artifact = Artifact( let artifact = Artifact(
version: version, version: version,
@ -142,71 +127,6 @@ class ArtifactBundleBuilder {
try jsonData.write(to: URL(fileURLWithPath: infoPath)) try jsonData.write(to: URL(fileURLWithPath: infoPath))
} }
private 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
}
}
/// 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.
private 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
}
}
private func generateArtifactBundleIndex(bundleInfos: [BundleInfo]) throws { private func generateArtifactBundleIndex(bundleInfos: [BundleInfo]) throws {
print("Generating tailwindcss.artifactbundleindex...") print("Generating tailwindcss.artifactbundleindex...")
@ -219,7 +139,7 @@ class ArtifactBundleBuilder {
Bundle( Bundle(
fileName: bundleInfo.fileName, fileName: bundleInfo.fileName,
checksum: bundleInfo.checksum, checksum: bundleInfo.checksum,
supportedTriples: [bundleInfo.triple] supportedTriples: expandingTriple(bundleInfo.triple)
) )
} }
@ -236,20 +156,3 @@ class ArtifactBundleBuilder {
try jsonData.write(to: URL(fileURLWithPath: indexPath)) try jsonData.write(to: URL(fileURLWithPath: indexPath))
} }
} }
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"
}
}
}