From 418068e902ed8b0dc34d4e266e2e4e652979d938 Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Fri, 24 Jul 2020 20:33:35 -0500 Subject: [PATCH] Forward-port PR #1499 (localized release notes in appcast) to 2.x --- Sparkle.xcodeproj/project.pbxproj | 4 +++ Sparkle/SUConstants.h | 2 ++ Sparkle/SUConstants.m | 2 ++ .../testlocalizedreleasenotesappcast.xml | 17 +++++++++++++ Tests/SUAppcastTest.swift | 16 ++++++++++++ generate_appcast/ArchiveItem.swift | 18 +++++++++++++ generate_appcast/Bridging-Header.h | 5 ++-- generate_appcast/FeedXML.swift | 25 +++++++++++++++++-- 8 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 Tests/Resources/testlocalizedreleasenotesappcast.xml diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 1f5354e0..bb89504b 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -465,6 +465,7 @@ EA1E287E22B66705004AA304 /* libed25519.a in Frameworks */ = {isa = PBXBuildFile; fileRef = EA1E282D22B660BE004AA304 /* libed25519.a */; }; F8761EB11ADC5068000C9034 /* SUCodeSigningVerifierTest.m in Sources */ = {isa = PBXBuildFile; fileRef = F8761EB01ADC5068000C9034 /* SUCodeSigningVerifierTest.m */; }; F8761EB31ADC50EB000C9034 /* SparkleTestCodeSignApp.zip in Resources */ = {isa = PBXBuildFile; fileRef = F8761EB21ADC50EB000C9034 /* SparkleTestCodeSignApp.zip */; }; + FA30773D24CBC295007BA37D /* testlocalizedreleasenotesappcast.xml in Resources */ = {isa = PBXBuildFile; fileRef = FA30773C24CBC295007BA37D /* testlocalizedreleasenotesappcast.xml */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -1373,6 +1374,7 @@ FA1941CF0D94A70100DD942E /* ConfigCommonDebug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = ConfigCommonDebug.xcconfig; sourceTree = ""; }; FA1941D00D94A70100DD942E /* ConfigCommon.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = ConfigCommon.xcconfig; sourceTree = ""; }; FA1941D10D94A70100DD942E /* ConfigFramework.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = ConfigFramework.xcconfig; sourceTree = ""; }; + FA30773C24CBC295007BA37D /* testlocalizedreleasenotesappcast.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = testlocalizedreleasenotesappcast.xml; sourceTree = ""; }; FA3AAF3B1050B273004B3130 /* ConfigUnitTest.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = ConfigUnitTest.xcconfig; sourceTree = ""; }; FE5536F517A2C6A7007CB333 /* ko */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ko; path = ko.lproj/Sparkle.strings; sourceTree = ""; }; FE5536F617A2C6AB007CB333 /* sk */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sk; path = sk.lproj/Sparkle.strings; sourceTree = ""; }; @@ -1728,6 +1730,7 @@ 5AF6C74E1AEA46D10014A3AB /* test.pkg */, 5AD0FA7E1C73F2E2004BCEFF /* testappcast.xml */, 5F1510A11C96E591006E1629 /* testnamespaces.xml */, + FA30773C24CBC295007BA37D /* testlocalizedreleasenotesappcast.xml */, 5A5DD41B249F0F4B0045EB3E /* test-relative-urls.xml */, 5A5DD40324958AFF0045EB3E /* SUUpdateValidatorTest */, ); @@ -2999,6 +3002,7 @@ 14958C6F19AEBC980061B14F /* test-pubkey.pem in Resources */, 5AF6C74F1AEA46D10014A3AB /* test.pkg in Resources */, 5AD0FA7F1C73F2E2004BCEFF /* testappcast.xml in Resources */, + FA30773D24CBC295007BA37D /* testlocalizedreleasenotesappcast.xml in Resources */, 5F1510A21C96E591006E1629 /* testnamespaces.xml in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Sparkle/SUConstants.h b/Sparkle/SUConstants.h index abf13893..61973bc5 100644 --- a/Sparkle/SUConstants.h +++ b/Sparkle/SUConstants.h @@ -90,4 +90,6 @@ extern NSString *const SURSSElementLink; extern NSString *const SURSSElementPubDate; extern NSString *const SURSSElementTitle; +extern NSString *const SUXMLLanguage; + #endif diff --git a/Sparkle/SUConstants.m b/Sparkle/SUConstants.m index b74537ca..23a5f136 100644 --- a/Sparkle/SUConstants.m +++ b/Sparkle/SUConstants.m @@ -82,3 +82,5 @@ NSString *const SURSSElementEnclosure = @"enclosure"; NSString *const SURSSElementLink = @"link"; NSString *const SURSSElementPubDate = @"pubDate"; NSString *const SURSSElementTitle = @"title"; + +NSString *const SUXMLLanguage = @"xml:lang"; diff --git a/Tests/Resources/testlocalizedreleasenotesappcast.xml b/Tests/Resources/testlocalizedreleasenotesappcast.xml new file mode 100644 index 00000000..ee21cd0a --- /dev/null +++ b/Tests/Resources/testlocalizedreleasenotesappcast.xml @@ -0,0 +1,17 @@ + + + + For unit test only + + + Version 6.0 + Sat, 26 Jul 2019 15:20:13 +0000 + + https://sparkle-project.org/#works + + https://sparkle-project.org/#localized_notes_link_works + + 2.0.0 + + + diff --git a/Tests/SUAppcastTest.swift b/Tests/SUAppcastTest.swift index bc518440..387cb96d 100644 --- a/Tests/SUAppcastTest.swift +++ b/Tests/SUAppcastTest.swift @@ -66,6 +66,22 @@ class SUAppcastTest: XCTestCase { } } + func testParseAppcastWithLocalizedReleaseNotes() { + let appcast = SUAppcast() + let testFile = Bundle(for: SUAppcastTest.self).path(forResource: "testlocalizedreleasenotesappcast", + ofType: "xml")! + let testFileUrl = URL(fileURLWithPath: testFile) + XCTAssertNotNil(testFileUrl) + + do { + let items = try appcast.parseAppcastItems(fromXMLFile: testFileUrl) as! [SUAppcastItem]; + XCTAssertEqual("https://sparkle-project.org/#localized_notes_link_works", items[0].releaseNotesURL.absoluteString) + } catch let err as NSError { + NSLog("%@", err) + XCTFail(err.localizedDescription) + } + } + func testNamespaces() { let appcast = SUAppcast() let testFile = Bundle(for: SUAppcastTest.self).path(forResource: "testnamespaces", ofType: "xml")! diff --git a/generate_appcast/ArchiveItem.swift b/generate_appcast/ArchiveItem.swift index 2993aec8..5a97e32e 100644 --- a/generate_appcast/ArchiveItem.swift +++ b/generate_appcast/ArchiveItem.swift @@ -186,5 +186,23 @@ class ArchiveItem: CustomStringConvertible { return URL(string: escapedFilename) } + func localizedReleaseNotes() -> [(String, URL)] { + let fileManager = FileManager.default + var basename = archivePath.deletingPathExtension() + if basename.pathExtension == "tar" { + basename = basename.deletingPathExtension() + } + var localizedReleaseNotes = [(String, URL)]() + for languageCode in Locale.isoLanguageCodes { + let localizedReleaseNoteURL = basename + .appendingPathExtension(languageCode) + .appendingPathExtension("html") + if fileManager.fileExists(atPath: localizedReleaseNoteURL.path) { + localizedReleaseNotes.append((languageCode, localizedReleaseNoteURL)) + } + } + return localizedReleaseNotes + } + let mimeType = "application/octet-stream" } diff --git a/generate_appcast/Bridging-Header.h b/generate_appcast/Bridging-Header.h index 4b8b160e..ffb6aec8 100644 --- a/generate_appcast/Bridging-Header.h +++ b/generate_appcast/Bridging-Header.h @@ -1,9 +1,10 @@ #import -#import +#import "SUStandardVersionComparator.h" #import "SUConstants.h" -#import +#import "SUErrors.h" #import "SUUnarchiver.h" #import "SUBinaryDeltaUnarchiver.h" #import "SUBinaryDeltaCreate.h" +#import "SUSignatures.h" #import "ed25519.h" // Run `git submodule update --init` if you get an error here diff --git a/generate_appcast/FeedXML.swift b/generate_appcast/FeedXML.swift index 581dce6e..1834a577 100644 --- a/generate_appcast/FeedXML.swift +++ b/generate_appcast/FeedXML.swift @@ -108,7 +108,11 @@ func writeAppcast(appcastDestPath: URL, updates: [ArchiveItem]) throws { } minVer?.setChildren([text(update.minimumSystemVersion)]) - let relElement = findElement(name: SUAppcastElementReleaseNotesLink, parent: item) + let releaseNotesXpath = "\(SUAppcastElementReleaseNotesLink)" + let results = ((try? item.nodes(forXPath: releaseNotesXpath)) as? [XMLElement])? + .filter { !($0.attributes ?? []) + .contains(where: { $0.name == SUXMLLanguage }) } + let relElement = results?.first if let url = update.releaseNotesURL { if nil == relElement { item.addChild(XMLElement.element(withName: SUAppcastElementReleaseNotesLink, stringValue: url.absoluteString) as! XMLElement) @@ -117,6 +121,23 @@ func writeAppcast(appcastDestPath: URL, updates: [ArchiveItem]) throws { item.removeChild(at: childIndex) } + let languageNotesNodes = ((try? item.nodes(forXPath: releaseNotesXpath)) as? [XMLElement])? + .map { ($0, $0.attribute(forName: SUXMLLanguage)?.stringValue )} + .filter { $0.1 != nil } ?? [] + for (node, language) in languageNotesNodes.reversed() + where !update.localizedReleaseNotes().contains(where: { $0.0 == language }) { + item.removeChild(at: node.index) + } + for (language, url) in update.localizedReleaseNotes() { + if !languageNotesNodes.contains(where: { $0.1 == language }) { + let localizedNode = XMLNode.element( + withName: SUAppcastElementReleaseNotesLink, + children: [XMLNode.text(withStringValue: url.lastPathComponent) as! XMLNode], + attributes: [XMLNode.attribute(withName: SUXMLLanguage, stringValue: language) as! XMLNode]) + item.addChild(localizedNode as! XMLNode) + } + } + var enclosure = findElement(name: "enclosure", parent: item) if nil == enclosure { enclosure = XMLElement.element(withName: "enclosure") as? XMLElement @@ -157,7 +178,7 @@ func writeAppcast(appcastDestPath: URL, updates: [ArchiveItem]) throws { XMLNode.attribute(withName: SUAppcastAttributeDeltaFrom, uri: sparkleNS, stringValue: delta.fromVersion) as! XMLNode, XMLNode.attribute(withName: "length", stringValue: String(delta.fileSize)) as! XMLNode, XMLNode.attribute(withName: "type", stringValue: "application/octet-stream") as! XMLNode, - ] + ] if let sig = delta.edSignature { attributes.append(XMLNode.attribute(withName: SUAppcastAttributeEDSignature, uri: sparkleNS, stringValue: sig) as! XMLNode) }