Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146import Foundation
struct ReleaseOrder { static func git(_ repository: String, _ args: [String]) throws -> String { String( decoding: try DevProcess.checked("git", ["-C", repository, "-c", "core.hooksPath=/dev/null"] + args), as: UTF8.self ).trimmingCharacters(in: .whitespacesAndNewlines) }
static func commit(_ repository: String, _ tag: String) throws -> String { guard tag.hasPrefix("v") else { throw DevError("Release tags must be vMAJOR.MINOR.PATCH") } try DevVersion.validate(String(tag.dropFirst()), root: ReleaseTools.root, kind: "ordered-version") let sha = try git(repository, ["rev-parse", "--verify", "refs/tags/\(tag)^{commit}"]) guard sha.range(of: "^[a-fA-F0-9]{40}$", options: .regularExpression) != nil else { throw DevError("Expected a full release commit SHA") } return sha }
static func numberCompare(_ left: String, _ right: String) -> ComparisonResult { let a = String(left.drop(while: { $0 == "0" })) let b = String(right.drop(while: { $0 == "0" })) if a.count != b.count { return a.count > b.count ? .orderedDescending : .orderedAscending } return a.compare(b, options: .literal) }
static func versionGreater(_ value: String, _ previous: String) -> Bool { for (a, b) in zip(value.split(separator: "."), previous.split(separator: ".")) { let order = numberCompare(String(a), String(b)) if order != .orderedSame { return order == .orderedDescending } } return false }
static func compare(_ args: [String]) throws -> String { guard args.count == 7 else { throw DevError( "Usage: release-order compare REPO TAG PREVIOUS_TAG VERSION PREVIOUS_VERSION BUILD PREVIOUS_BUILD") } let repository = args[0], tag = args[1], previousTag = args[2], version = args[3], previousVersion = args[4], number = args[5], previousNumber = args[6] guard tag == "v" + version, previousTag == "v" + previousVersion else { throw DevError("Release tag and metadata version disagree") } for value in [version, previousVersion] { try DevVersion.validate(value, root: ReleaseTools.root, kind: "ordered-version") } guard versionGreater(version, previousVersion) else { throw DevError("Release version must be strictly greater than the previous release") } for value in [number, previousNumber] { try DevVersion.validate(value, root: ReleaseTools.root, kind: "positive-build") } guard numberCompare(number, previousNumber) == .orderedDescending else { throw DevError("Build number must be strictly greater than the previous release") } let current = try commit(repository, tag) let previous = try commit(repository, previousTag) guard current != previous else { throw DevError("Release commit must be newer than the previous release commit") } do { _ = try git(repository, ["merge-base", "--is-ancestor", previous, current]) } catch { throw DevError("Release commit must descend from the previous release commit") } return current }
static func preflight(_ args: [String]) throws -> String { guard args.count == 5 else { throw DevError("Usage: release-order preflight REPO TAG BUILD GITHUB_REPO MAIN") } let repository = args[0], tag = args[1], number = args[2], github = args[3], main = args[4] try DevVersion.validate( String(tag.dropFirst(tag.hasPrefix("v") ? 1 : 0)), root: ReleaseTools.root, kind: "ordered-version") try DevVersion.validate(number, root: ReleaseTools.root, kind: "positive-build") let current = try commit(repository, tag) _ = try git(repository, ["merge-base", "--is-ancestor", current, main]) let pages = try DevJSON.decode( DevProcess.checked("gh", ["api", "repos/\(github)/releases", "--paginate", "--slurp"]) ).requireArray() var releases: [(tag: String, date: Date)] = [] let dates = ISO8601DateFormatter() for page in pages { for release in try page.requireArray() { guard let draft = release["draft"]?.bool else { throw DevError("Release has no draft state") } if draft { continue } guard let name = release["tag_name"]?.string, let published = release["published_at"]?.string, let date = dates.date(from: published) else { throw DevError("Release has invalid publication metadata") } if name == tag { throw DevError("Published releases are immutable; choose a new tag") } releases.append((name, date)) } } guard let previous = releases.max(by: { $0.date < $1.date }) else { return current } _ = try commit(repository, previous.tag) let directory = FileManager.default.temporaryDirectory.appendingPathComponent("release-predecessor-\(UUID())") try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: false) defer { do { try FileManager.default.removeItem(at: directory) } catch { FileHandle.standardError.write(Data("Could not remove previous release metadata: \(error)\n".utf8)) } } do { _ = try DevProcess.checked( "gh", [ "release", "download", previous.tag, "--repo", github, "--pattern", "appcast.xml", "--dir", directory.path, ]) } catch { throw DevError( "Could not download \(previous.tag)'s appcast.xml; restore its original release metadata asset before proceeding" ) } let path = directory.appendingPathComponent("appcast.xml") guard try ReleaseFiles.regular(path) else { throw DevError("Previous release must include its appcast.xml metadata asset") } let document = try LaunchFeed.parse(path) let channel = try LaunchFeed.channel(document) let items = LaunchFeed.elements(channel, "item") guard items.count == 1 else { throw DevError("Expected one previous release metadata entry") } return try compare([ repository, tag, previous.tag, String(tag.dropFirst()), LaunchFeed.text(items[0], "shortVersionString"), number, LaunchFeed.text(items[0], "version"), ]) }
static func provenance(_ repository: String, _ tag: String, _ main: String) throws -> String { let sha = try commit(repository, tag) _ = try git(repository, ["merge-base", "--is-ancestor", sha, main]) return sha }
static func run(_ args: [String]) throws { do { let rest = Array(args.dropFirst()) switch args.first { case "preflight": print(try preflight(rest)) case "compare": print(try compare(rest)) case "provenance" where rest.count == 3: print(try provenance(rest[0], rest[1], rest[2])) default: throw DevError("Usage: release-order preflight|compare|provenance ARGS") } } catch { throw DevError("Release ordering: \(error)") } }}