|
| 1 | +import ArgumentParser |
| 2 | +import Foundation |
| 3 | +import CryptoKit |
| 4 | +import Sake |
| 5 | +import SwiftShell |
| 6 | + |
| 7 | +@CommandGroup |
| 8 | +struct ReleaseCommands { |
| 9 | + private struct BuildTarget { |
| 10 | + enum Arch { |
| 11 | + case x86 |
| 12 | + case arm |
| 13 | + } |
| 14 | + enum OS { |
| 15 | + case macos |
| 16 | + case linux |
| 17 | + } |
| 18 | + |
| 19 | + let arch: Arch |
| 20 | + let os: OS |
| 21 | + |
| 22 | + var triple: String { |
| 23 | + switch (arch, os) { |
| 24 | + case (.x86, .macos): "x86_64-apple-macosx" |
| 25 | + case (.arm, .macos): "arm64-apple-macosx" |
| 26 | + case (.x86, .linux): "x86_64-unknown-linux-gnu" |
| 27 | + case (.arm, .linux): "aarch64-unknown-linux-gnu" |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private enum Constants { |
| 33 | + static let buildArtifactsDirectory = ".build/artifacts" |
| 34 | + static let swiftVersion = "6.0" |
| 35 | + static let buildTargets: [BuildTarget] = [ |
| 36 | + .init(arch: .arm, os: .macos), |
| 37 | + .init(arch: .x86, os: .macos), |
| 38 | + .init(arch: .x86, os: .linux), |
| 39 | + .init(arch: .arm, os: .linux), |
| 40 | + ] |
| 41 | + static let executableName = "progressline" |
| 42 | + } |
| 43 | + |
| 44 | + private struct ReleaseArguments: ParsableArguments { |
| 45 | + @Argument(help: "Version number") |
| 46 | + var version: String |
| 47 | + |
| 48 | + func validate() throws { |
| 49 | + guard version.range(of: #"^\d+\.\d+\.\d+$"#, options: .regularExpression) != nil else { |
| 50 | + throw ValidationError("Invalid version number. Should be in the format 'x.y.z'") |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static var brewRelease: Command { |
| 56 | + Command( |
| 57 | + description: "Brew to Homebrew", |
| 58 | + run: { context in |
| 59 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 60 | + try arguments.validate() |
| 61 | + |
| 62 | + let version = arguments.version |
| 63 | + try runAndPrint("brew", "bump-formula-pr", "--version=\(version)", "progressline") |
| 64 | + } |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + public static var githubRelease: Command { |
| 69 | + Command( |
| 70 | + description: "Release to GitHub", |
| 71 | + dependencies: [ |
| 72 | + bumpVersion, |
| 73 | + cleanReleaseArtifacts, |
| 74 | + buildReleaseArtifacts, |
| 75 | + calculateBuildArtifactsSha256, |
| 76 | + createAndPushTag, |
| 77 | + generateReleaseNotes, |
| 78 | + draftReleaseWithArtifacts, |
| 79 | + ] |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + static var bumpVersion: Command { |
| 84 | + Command( |
| 85 | + description: "Bump version", |
| 86 | + skipIf: { context in |
| 87 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 88 | + try arguments.validate() |
| 89 | + |
| 90 | + let version = arguments.version |
| 91 | + let versionFilePath = "Sources/Version.swift" |
| 92 | + let currentVersion = try String(contentsOfFile: versionFilePath) |
| 93 | + .split(separator: "\"")[1] |
| 94 | + if currentVersion == version { |
| 95 | + print("Version is already \(version). Skipping...") |
| 96 | + return true |
| 97 | + } else { |
| 98 | + return false |
| 99 | + } |
| 100 | + }, |
| 101 | + run: { context in |
| 102 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 103 | + try arguments.validate() |
| 104 | + |
| 105 | + let version = arguments.version |
| 106 | + let versionFilePath = "Sources/Version.swift" |
| 107 | + let versionFileContent = """ |
| 108 | + // This file is autogenerated. Do not edit. |
| 109 | + let progressLineVersion = "\(version)" |
| 110 | +
|
| 111 | + """ |
| 112 | + try versionFileContent.write(toFile: versionFilePath, atomically: true, encoding: .utf8) |
| 113 | + |
| 114 | + try runAndPrint("git", "add", versionFilePath) |
| 115 | + try runAndPrint("git", "commit", "-m", "chore(release): Bump version to \(version)") |
| 116 | + print("Version bumped to \(version)") |
| 117 | + } |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + static var cleanReleaseArtifacts: Command { |
| 122 | + Command( |
| 123 | + description: "Clean release artifacts", |
| 124 | + run: { _ in |
| 125 | + try? runAndPrint("rm", "-rf", Constants.buildArtifactsDirectory) |
| 126 | + } |
| 127 | + ) |
| 128 | + } |
| 129 | + |
| 130 | + static var buildReleaseArtifacts: Command { |
| 131 | + Command( |
| 132 | + description: "Build release artifacts", |
| 133 | + skipIf: { context in |
| 134 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 135 | + try arguments.validate() |
| 136 | + let version = arguments.version |
| 137 | + |
| 138 | + let targetsWithExistingArtifacts = Constants.buildTargets.filter { target in |
| 139 | + let archivePath = executableArchivePath(target: target, version: version) |
| 140 | + return FileManager.default.fileExists(atPath: archivePath) |
| 141 | + } |
| 142 | + if targetsWithExistingArtifacts.count == Constants.buildTargets.count { |
| 143 | + print("Release artifacts already exist. Skipping...") |
| 144 | + return true |
| 145 | + } else { |
| 146 | + context.storage["existing-artifacts-triples"] = targetsWithExistingArtifacts.map(\.triple) |
| 147 | + return false |
| 148 | + } |
| 149 | + }, |
| 150 | + run: { context in |
| 151 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 152 | + try arguments.validate() |
| 153 | + let version = arguments.version |
| 154 | + |
| 155 | + try FileManager.default.createDirectory( |
| 156 | + atPath: Constants.buildArtifactsDirectory, |
| 157 | + withIntermediateDirectories: true, |
| 158 | + attributes: nil |
| 159 | + ) |
| 160 | + let existingArtifactsTriples = context.storage["existing-artifacts-triples"] as? [String] ?? [] |
| 161 | + for target in Constants.buildTargets { |
| 162 | + if existingArtifactsTriples.contains(target.triple) { |
| 163 | + print("Skipping \(target.triple) as artifacts already exist") |
| 164 | + continue |
| 165 | + } |
| 166 | + let (swiftBuild, swiftClean, strip, zip) = { |
| 167 | + let buildFlags = ["--disable-sandbox", "--configuration", "release", "--triple", target.triple] |
| 168 | + if target.os == .linux { |
| 169 | + let platform = target.arch == .arm ? "linux/arm64" : "linux/amd64" |
| 170 | + let dockerExec = "docker run --rm --volume \(context.projectRoot):/workdir --workdir /workdir --platform \(platform) swift:\(Constants.swiftVersion)" |
| 171 | + let buildFlags = (buildFlags + ["--static-swift-stdlib"]).joined(separator: " ") |
| 172 | + return ( |
| 173 | + "\(dockerExec) swift build \(buildFlags)", |
| 174 | + "\(dockerExec) swift package clean", |
| 175 | + "\(dockerExec) strip -s", |
| 176 | + "zip -j" |
| 177 | + ) |
| 178 | + } else { |
| 179 | + let buildFlags = buildFlags.joined(separator: " ") |
| 180 | + return ( |
| 181 | + "swift build \(buildFlags)", |
| 182 | + "swift package clean", |
| 183 | + "strip -rSTx", |
| 184 | + "zip -j" |
| 185 | + ) |
| 186 | + } |
| 187 | + }() |
| 188 | + |
| 189 | + try runAndPrint(bash: swiftClean) |
| 190 | + try runAndPrint(bash: swiftBuild) |
| 191 | + |
| 192 | + let binPath: String = run(bash: "\(swiftBuild) --show-bin-path").stdout |
| 193 | + if binPath.isEmpty { |
| 194 | + throw NSError(domain: "Fail to get bin path", code: -999) |
| 195 | + } |
| 196 | + let executablePath = binPath + "/\(Constants.executableName)" |
| 197 | + |
| 198 | + try runAndPrint(bash: "\(strip) \(executablePath)") |
| 199 | + |
| 200 | + let executableArchivePath = executableArchivePath(target: target, version: version) |
| 201 | + try runAndPrint(bash: "\(zip) \(executableArchivePath) \(executablePath.replacingOccurrences(of: "/workdir", with: context.projectRoot))") |
| 202 | + } |
| 203 | + |
| 204 | + print("Release artifacts built successfully at '\(Constants.buildArtifactsDirectory)'") |
| 205 | + } |
| 206 | + ) |
| 207 | + } |
| 208 | + |
| 209 | + static var calculateBuildArtifactsSha256: Command { |
| 210 | + @Sendable |
| 211 | + func shasumFilePath(version: String) -> String { |
| 212 | + ".build/artifacts/shasum-\(version)" |
| 213 | + } |
| 214 | + |
| 215 | + return Command( |
| 216 | + description: "Calculate SHA-256 checksums for build artifacts", |
| 217 | + skipIf: { context in |
| 218 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 219 | + try arguments.validate() |
| 220 | + let version = arguments.version |
| 221 | + |
| 222 | + let shasumFilePath = shasumFilePath(version: version) |
| 223 | + |
| 224 | + return FileManager.default.fileExists(atPath: shasumFilePath) |
| 225 | + }, |
| 226 | + run: { context in |
| 227 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 228 | + try arguments.validate() |
| 229 | + let version = arguments.version |
| 230 | + |
| 231 | + var shasumResults = [String]() |
| 232 | + for target in Constants.buildTargets { |
| 233 | + let archivePath = executableArchivePath(target: target, version: version) |
| 234 | + let file = FileHandle(forReadingAtPath: archivePath)! |
| 235 | + let shasum = SHA256.hash(data: file.readDataToEndOfFile()) |
| 236 | + let shasumString = shasum.compactMap { String(format: "%02x", $0) }.joined() |
| 237 | + shasumResults.append("\(shasumString) \(archivePath)") |
| 238 | + } |
| 239 | + FileManager.default.createFile( |
| 240 | + atPath: shasumFilePath(version: version), |
| 241 | + contents: shasumResults.joined(separator: "\n").data(using: .utf8) |
| 242 | + ) |
| 243 | + } |
| 244 | + ) |
| 245 | + } |
| 246 | + |
| 247 | + static var createAndPushTag: Command { |
| 248 | + Command( |
| 249 | + description: "Create and push a tag", |
| 250 | + skipIf: { context in |
| 251 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 252 | + try arguments.validate() |
| 253 | + |
| 254 | + let version = arguments.version |
| 255 | + |
| 256 | + let grepResult = run(bash: "git tag | grep \(arguments.version)") |
| 257 | + if grepResult.succeeded { |
| 258 | + print("Tag \(version) already exists. Skipping...") |
| 259 | + return true |
| 260 | + } else { |
| 261 | + return false |
| 262 | + } |
| 263 | + }, |
| 264 | + run: { context in |
| 265 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 266 | + try arguments.validate() |
| 267 | + |
| 268 | + let version = arguments.version |
| 269 | + |
| 270 | + print("Creating and pushing tag \(version)") |
| 271 | + try runAndPrint("git", "tag", version) |
| 272 | + try runAndPrint("git", "push", "origin", "tag", version) |
| 273 | + try runAndPrint("git", "push") // push local changes like version bump |
| 274 | + } |
| 275 | + ) |
| 276 | + } |
| 277 | + |
| 278 | + static var generateReleaseNotes: Command { |
| 279 | + Command( |
| 280 | + description: "Generate release notes", |
| 281 | + dependencies: [BrewCommands.ensureGitCliffInstalled], |
| 282 | + skipIf: { context in |
| 283 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 284 | + try arguments.validate() |
| 285 | + |
| 286 | + let version = arguments.version |
| 287 | + let releaseNotesPath = releaseNotesPath(version: version) |
| 288 | + if FileManager.default.fileExists(atPath: releaseNotesPath) { |
| 289 | + print("Release notes for \(version) already exist at \(releaseNotesPath). Skipping...") |
| 290 | + return true |
| 291 | + } else { |
| 292 | + return false |
| 293 | + } |
| 294 | + }, |
| 295 | + run: { context in |
| 296 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 297 | + try arguments.validate() |
| 298 | + |
| 299 | + let version = arguments.version |
| 300 | + let releaseNotesPath = releaseNotesPath(version: version) |
| 301 | + try runAndPrint("git", "cliff", "--latest", "--strip=all", "--tag", version, "--output", releaseNotesPath) |
| 302 | + print("Release notes generated at \(releaseNotesPath)") |
| 303 | + } |
| 304 | + ) |
| 305 | + } |
| 306 | + |
| 307 | + static var draftReleaseWithArtifacts: Command { |
| 308 | + Command( |
| 309 | + description: "Draft a release on GitHub", |
| 310 | + dependencies: [BrewCommands.ensureGhInstalled], |
| 311 | + skipIf: { context in |
| 312 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 313 | + try arguments.validate() |
| 314 | + |
| 315 | + let tagName = arguments.version |
| 316 | + let ghViewResult = run(bash: "gh release view \(tagName)") |
| 317 | + if ghViewResult.succeeded { |
| 318 | + print("Release \(tagName) already exists. Skipping...") |
| 319 | + return true |
| 320 | + } else { |
| 321 | + return false |
| 322 | + } |
| 323 | + }, |
| 324 | + run: { context in |
| 325 | + let arguments = try ReleaseArguments.parse(context.arguments) |
| 326 | + try arguments.validate() |
| 327 | + |
| 328 | + print("Drafting release \(arguments.version) on GitHub") |
| 329 | + let tagName = arguments.version |
| 330 | + let releaseTitle = arguments.version |
| 331 | + let draftReleaseCommand = |
| 332 | + "gh release create \(tagName) \(Constants.buildArtifactsDirectory)/*.zip --title '\(releaseTitle)' --draft --verify-tag --notes-file \(releaseNotesPath(version: tagName))" |
| 333 | + try runAndPrint(bash: draftReleaseCommand) |
| 334 | + } |
| 335 | + ) |
| 336 | + } |
| 337 | + |
| 338 | + private static func executableArchivePath(target: BuildTarget, version: String) -> String { |
| 339 | + "\(Constants.buildArtifactsDirectory)/\(Constants.executableName)-\(version)-\(target.triple).zip" |
| 340 | + } |
| 341 | + |
| 342 | + private static func releaseNotesPath(version: String) -> String { |
| 343 | + ".build/artifacts/release-notes-\(version).md" |
| 344 | + } |
| 345 | +} |
0 commit comments