From 89e20a490d633dfd350fa908f0135bd683a7af7b Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 22 Jan 2022 20:58:53 -0800 Subject: [PATCH] Add hidden option to generate_appcast to set max CDATA threshold (#2075) --- generate_appcast/ArchiveItem.swift | 19 ++++++++----------- generate_appcast/FeedXML.swift | 6 +++--- generate_appcast/main.swift | 10 ++++++++-- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/generate_appcast/ArchiveItem.swift b/generate_appcast/ArchiveItem.swift index 40f58e0c..405ab7c5 100644 --- a/generate_appcast/ArchiveItem.swift +++ b/generate_appcast/ArchiveItem.swift @@ -5,9 +5,6 @@ import Foundation -// CDATA text must contain less characters than this threshold -let CDATA_HTML_FRAGMENT_THRESHOLD = 1000 - class DeltaUpdate { let fromVersion: String let archivePath: URL @@ -199,9 +196,9 @@ class ArchiveItem: CustomStringConvertible { return releaseNotes } - private func getReleaseNotesAsHTMLFragment(_ path: URL) -> String? { + private func getReleaseNotesAsHTMLFragment(_ path: URL, _ maxCDATAThreshold: Int) -> String? { if let html = try? String(contentsOf: path) { - if html.utf8.count < CDATA_HTML_FRAGMENT_THRESHOLD && + if html.utf8.count <= maxCDATAThreshold && !html.localizedCaseInsensitiveContains(" String? { if let path = self.releaseNotesPath { - return self.getReleaseNotesAsHTMLFragment(path) + return self.getReleaseNotesAsHTMLFragment(path, maxCDATAThreshold) } return nil } - - var releaseNotesURL: URL? { + + func releaseNotesURL(maxCDATAThreshold: Int) -> URL? { guard let path = self.releaseNotesPath else { return nil } // The file is already used as inline description - if self.getReleaseNotesAsHTMLFragment(path) != nil { + if self.getReleaseNotesAsHTMLFragment(path, maxCDATAThreshold) != nil { return nil } return self.releaseNoteURL(for: path.lastPathComponent) diff --git a/generate_appcast/FeedXML.swift b/generate_appcast/FeedXML.swift index 1d45c601..4785977d 100644 --- a/generate_appcast/FeedXML.swift +++ b/generate_appcast/FeedXML.swift @@ -49,7 +49,7 @@ func extractVersion(parent: XMLNode) -> String? { return nil } -func writeAppcast(appcastDestPath: URL, updates: [ArchiveItem], newVersions: Set?, maxNewVersionsInFeed: Int, fullReleaseNotesLink: String?, link: String?, newChannel: String?, majorVersion: String?, phasedRolloutInterval: Int?, criticalUpdateVersion: String?, informationalUpdateVersions: [String]?) throws -> (numNewUpdates: Int, numExistingUpdates: Int) { +func writeAppcast(appcastDestPath: URL, updates: [ArchiveItem], newVersions: Set?, maxNewVersionsInFeed: Int, fullReleaseNotesLink: String?, maxCDATAThreshold: Int, link: String?, newChannel: String?, majorVersion: String?, phasedRolloutInterval: Int?, criticalUpdateVersion: String?, informationalUpdateVersions: [String]?) throws -> (numNewUpdates: Int, numExistingUpdates: Int) { let appBaseName = updates[0].appPath.deletingPathExtension().lastPathComponent let sparkleNS = "http://www.andymatuschak.org/xml-namespaces/sparkle" @@ -232,7 +232,7 @@ func writeAppcast(appcastDestPath: URL, updates: [ArchiveItem], newVersions: Set } shortVersionElement?.setChildren([text(update.shortVersion)]) - if let html = update.releaseNotesHTML { + if let html = update.releaseNotesHTML(maxCDATAThreshold: maxCDATAThreshold) { let descElement = findOrCreateElement(name: "description", parent: item) let cdata = XMLNode(kind: .text, options: .nodeIsCDATA) cdata.stringValue = html @@ -260,7 +260,7 @@ func writeAppcast(appcastDestPath: URL, updates: [ArchiveItem], newVersions: Set .contains(where: { $0.name == SUXMLLanguage }) } let relElement = results?.first - if let url = update.releaseNotesURL { + if let url = update.releaseNotesURL(maxCDATAThreshold: maxCDATAThreshold) { // The update includes a valid release notes URL if let existingReleaseNotesElement = relElement { // The existing item includes a release notes element. Update it. diff --git a/generate_appcast/main.swift b/generate_appcast/main.swift index afd6d5f5..59b14ece 100644 --- a/generate_appcast/main.swift +++ b/generate_appcast/main.swift @@ -46,6 +46,8 @@ func loadPrivateKeys(_ account: String, _ privateDSAKey: SecKey?, _ privateEdStr return PrivateKeys(privateDSAKey: privateDSAKey, privateEdKey: privateEdKey, publicEdKey: publicEdKey) } +let DEFAULT_MAX_CDATA_THRESHOLD = 1000 + struct GenerateAppcast: ParsableCommand { static let programName = "generate_appcast" static let programNamePath: String = CommandLine.arguments.first ?? "./\(programName)" @@ -123,6 +125,10 @@ struct GenerateAppcast: ParsableCommand { @Flag(help: .hidden) var verbose: Bool = false + // CDATA text must contain <= this number of characters + @Option(name: .customLong("max-cdata-threshold"), help: .hidden) + var maxCDATAThreshold: Int = DEFAULT_MAX_CDATA_THRESHOLD + static var configuration = CommandConfiguration( abstract: "Generate appcast from a directory of Sparkle update archives.", discussion: """ @@ -132,7 +138,7 @@ struct GenerateAppcast: ParsableCommand { Old entries in the appcast are kept intact. Otherwise, a new appcast file will be generated and written. .html files that have the same filename as an archive (except for the file extension) will be used for release notes for that item. - If the contents of these files are short (< \(CDATA_HTML_FRAGMENT_THRESHOLD) characters) and do not include a DOCTYPE or body tags, they will be treated as embedded CDATA release notes. + If the contents of these files are short (< \(DEFAULT_MAX_CDATA_THRESHOLD) characters) and do not include a DOCTYPE or body tags, they will be treated as embedded CDATA release notes. For new update entries, Sparkle infers the minimum system OS requirement based on your update's LSMinimumSystemVersion provided by your application's Info.plist. If none is found, \(programName) defaults to Sparkle's own minimum system requirement (macOS 10.11). @@ -242,7 +248,7 @@ struct GenerateAppcast: ParsableCommand { relativeTo: archivesSourceDir) // Write the appcast - let (numNewUpdates, numExistingUpdates) = try writeAppcast(appcastDestPath: appcastDestPath, updates: updates, newVersions: versions, maxNewVersionsInFeed: maxNewVersionsInFeed, fullReleaseNotesLink: fullReleaseNotesURL, link: link, newChannel: channel, majorVersion: majorVersion, phasedRolloutInterval: phasedRolloutInterval, criticalUpdateVersion: criticalUpdateVersion, informationalUpdateVersions: informationalUpdateVersions) + let (numNewUpdates, numExistingUpdates) = try writeAppcast(appcastDestPath: appcastDestPath, updates: updates, newVersions: versions, maxNewVersionsInFeed: maxNewVersionsInFeed, fullReleaseNotesLink: fullReleaseNotesURL, maxCDATAThreshold: maxCDATAThreshold, link: link, newChannel: channel, majorVersion: majorVersion, phasedRolloutInterval: phasedRolloutInterval, criticalUpdateVersion: criticalUpdateVersion, informationalUpdateVersions: informationalUpdateVersions) // Inform the user, pluralizing "update" if necessary let pluralizeUpdates = { $0 == 1 ? "update" : "updates" }