Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156import Foundation
struct UpdateMetadata { let product: DevProduct let environment: [String: String]
init(root: URL = ReleaseTools.root, environment: [String: String] = ProcessInfo.processInfo.environment) throws { product = try DevProduct.read(root: root) self.environment = environment }
init(product: DevProduct, environment: [String: String]) { self.product = product self.environment = environment }
func setting(_ name: String) throws -> String { environment[try product.require("PRODUCT_ENV_PREFIX") + "_" + name] ?? "" }
static func https(_ value: String) throws -> URLComponents { guard let url = URLComponents(string: value), url.scheme == "https", let host = url.host, !host.isEmpty, url.user == nil, url.password == nil, (url.fragment ?? "").isEmpty, !value.unicodeScalars.contains(where: CharacterSet.whitespacesAndNewlines.contains) else { throw DevError("A secure HTTPS URL without credentials or a fragment is required") } return url }
static func publicKey(_ value: String) throws -> String { guard let decoded = Data(base64Encoded: value), decoded.count == 32, decoded.base64EncodedString() == value else { throw DevError("The public key must be a canonical base64 Ed25519 public key") } return value }
func configure(_ path: String, _ configuration: String) throws { guard ["debug", "release"].contains(configuration) else { throw DevError("Expected debug or release") } var info = try UpdatePlist.read(path) let name = try product.require("PRODUCT_NAME") let support = try SupportMetadata(product: product) info[name + "SupportEmail"] = .string(support.email) info[name + "IssuesURL"] = .string(support.issuesURL) info[name + "UpdatesEnabled"] = .bool(configuration == "release") if configuration == "release" { let feed = try setting("APPCAST_URL") _ = try Self.https(feed) info["SUFeedURL"] = .string(feed) info["SUPublicEDKey"] = .string(try Self.publicKey(setting("UPDATE_PUBLIC_KEY"))) } else { info.removeValue(forKey: "SUFeedURL") info.removeValue(forKey: "SUPublicEDKey") info["SUEnableAutomaticChecks"] = .bool(false) } try UpdatePlist.write(info, to: path) }
func check(_ path: String) throws { let info = try UpdatePlist.read(path) let enabled = info[try product.require("PRODUCT_NAME") + "UpdatesEnabled"] if enabled == .bool(true) { _ = try Self.https(info["SUFeedURL"]?.string ?? "") _ = try Self.publicKey(info["SUPublicEDKey"]?.string ?? "") } else if enabled != .bool(false) || info["SUEnableAutomaticChecks"] != .bool(false) { throw DevError("The bundle has no explicit update policy") } }
static func rubyLiteral(_ value: String) -> String { value.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "'", with: "\\'") }
func downloadURL(for archive: URL) throws -> String { let exact = try setting("DOWNLOAD_URL") let prefix = try setting("DOWNLOAD_URL_PREFIX") guard exact.isEmpty || prefix.isEmpty else { throw DevError("Set only one download URL or download URL prefix") } if !exact.isEmpty { _ = try Self.https(exact) return exact } let parsed = try Self.https(prefix) guard (parsed.query ?? "").isEmpty else { throw DevError("The download URL prefix must not contain a query") } var trimmed = prefix while trimmed.hasSuffix("/") { trimmed.removeLast() } return trimmed + "/" + archive.lastPathComponent }
func release(_ dmg: String, _ signature: String, _ output: String, _ template: String) throws { let version = try setting("VERSION") let build = try setting("BUILD_NUMBER") try DevVersion.validate(version, root: product.root) try DevVersion.validate(build, root: product.root, kind: "build") guard let bytes = Data(base64Encoded: signature), bytes.count == 64 else { throw DevError("Invalid Ed25519 signature") } let archive = URL(fileURLWithPath: dmg) let url = try downloadURL(for: archive) let name = try product.require("PRODUCT_NAME") let channel = XMLElement(name: "channel") channel.addChild(LaunchFeed.element("title", name + " Updates")) let item = XMLElement(name: "item") item.addChild(LaunchFeed.element("title", name + " " + version)) item.addChild(LaunchFeed.element("sparkle:version", build)) item.addChild(LaunchFeed.element("sparkle:shortVersionString", version)) item.addChild(LaunchFeed.element("sparkle:minimumSystemVersion", "15.0")) let enclosure = XMLElement(name: "enclosure") for (key, value) in [ "url": url, "length": String(try archive.resourceValues(forKeys: [.fileSizeKey]).fileSize ?? 0), "type": "application/octet-stream", "sparkle:edSignature": signature, ] { enclosure.addAttribute(LaunchFeed.attribute(key, value)) } item.addChild(enclosure) channel.addChild(item) let rss = XMLElement(name: "rss") rss.addAttribute(LaunchFeed.attribute("version", "2.0")) let namespace = XMLNode(kind: .namespace) namespace.name = "sparkle" namespace.stringValue = LaunchFeed.namespace rss.addNamespace(namespace) rss.addChild(channel) let document = XMLDocument(rootElement: rss) document.version = "1.0" document.characterEncoding = "utf-8" let destination = URL(fileURLWithPath: output) try document.xmlData(options: .nodePrettyPrint).write(to: destination.appendingPathComponent("appcast.xml")) let sha = try UpdateCrypto.digest(archive) var cask = try String(contentsOfFile: template, encoding: .utf8) cask = cask.replacingOccurrences(of: "@VERSION@", with: Self.rubyLiteral(version)) .replacingOccurrences(of: "@SHA256@", with: sha) .replacingOccurrences(of: "@DOWNLOAD_URL@", with: Self.rubyLiteral(url)) try cask.write( to: destination.appendingPathComponent(try product.require("PRODUCT_CASK") + ".rb"), atomically: false, encoding: .utf8) try "\(sha) \(archive.lastPathComponent)\n".write( to: destination.appendingPathComponent("SHA256SUMS"), atomically: false, encoding: .utf8) }
static func run(_ args: [String]) throws { do { let metadata = try Self() switch args.first { case "configure" where args.count == 3: try metadata.configure(args[1], args[2]) case "check" where args.count == 2: try metadata.check(args[1]) case "release" where args.count == 5: try metadata.release(args[1], args[2], args[3], args[4]) default: throw DevError( "Usage: update-metadata configure PLIST CONFIG | check PLIST | release DMG SIGNATURE OUTPUT TEMPLATE" ) } } catch { throw DevError("Update metadata: \(error)") } }}