Skip to content

Commit

Permalink
(fix) sorting priorities and output
Browse files Browse the repository at this point in the history
  • Loading branch information
mredig committed Dec 20, 2023
1 parent 33c23d1 commit 74de75b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# PKGAppcastGenerator Changelog:

### 0.2.1

* fixed sorting priorities and output

### 0.2.0

Expand Down
22 changes: 10 additions & 12 deletions Sources/PKGAppcastGeneratorCore/AppcastChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,25 @@ public struct AppcastChannel: Codable {
}

public static func defaultSortItems(_ first: AppcastItem, _ second: AppcastItem) -> Bool {
if let a = Int(first.version), let b = Int(second.version) {
return a > b
} else if let a = first.shortVersionString, let b = second.shortVersionString {
if let a = first.shortVersionString, let b = second.shortVersionString {
let aParts = a.split(separator: ".").compactMap { Int($0) }
let bParts = b.split(separator: ".").compactMap { Int($0) }

let zipped = zip(aParts, bParts)
for pair in zipped {
guard pair.0 > pair.1 else { continue }
return true
guard pair.0 != pair.1 else { continue }
return pair.0 > pair.1
}

if a.count > b.count {
return true
} else if b.count > a.count {
return true
} else {
return false
if a.count != b.count {
return a.count > b.count
}
}

if let a = Int(first.version), let b = Int(second.version), a != b {
return a > b
} else {
return true
return first.publishedDate > second.publishedDate
}
}
}
4 changes: 2 additions & 2 deletions Sources/PKGAppcastGeneratorCore/AppcastItem.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import XMLCoder

public struct AppcastItem: Codable {
public struct AppcastItem: Codable, Hashable {
public let title: String
public let link: URL
public let releaseNotesLink: URL?
Expand Down Expand Up @@ -72,7 +72,7 @@ public struct AppcastItem: Codable {
case phasedRolloutInterval = "sparkle:phasedRolloutInterval"
}

public struct Enclosure: Codable, DynamicNodeDecoding, DynamicNodeEncoding {
public struct Enclosure: Codable, DynamicNodeDecoding, DynamicNodeEncoding, Hashable {
public let url: URL
public let length: Int
public let mimeType: String
Expand Down
82 changes: 82 additions & 0 deletions Tests/PKGAppcastGeneratorCoreTests/GeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,88 @@ class GeneratorTests: XCTestCase {
XCTAssertEqual(xmlDoc, zipsExpectation)
}

func testSortWithMatchingBuilds() throws {
let enclosure = AppcastItem.Enclosure(
url: #URL("https://he.ho.hum/updates/myapp.pkg"),
length: 0,
mimeType: "application/octet-stream")

let now = Date.now

let base = AppcastItem(
title: "2.1.7",
link: #URL("https://he.ho.hum/myapps/downloads"),
version: "35",
shortVersionString: "2.1.7",
description: nil,
publishedDate: now,
enclosure: enclosure)

let greaterShortVersion = AppcastItem(
title: "2.1.8",
link: #URL("https://he.ho.hum/myapps/downloads"),
version: "35",
shortVersionString: "2.1.8",
description: nil,
publishedDate: now,
enclosure: enclosure)

var channel = AppcastChannel(title: "foo", items: [base, greaterShortVersion])
try channel.sortItems()
XCTAssertEqual(channel.items, [greaterShortVersion, base])
channel.items = [greaterShortVersion, base]
try channel.sortItems()
XCTAssertEqual(channel.items, [greaterShortVersion, base])

let lowerBuild = AppcastItem(
title: "2.1.7",
link: #URL("https://he.ho.hum/myapps/downloads"),
version: "31",
shortVersionString: "2.1.7",
description: nil,
publishedDate: now,
enclosure: enclosure)

channel.items = [base, lowerBuild]
try channel.sortItems()
XCTAssertEqual(channel.items, [base, lowerBuild])
channel.items = [lowerBuild, base]
try channel.sortItems()
XCTAssertEqual(channel.items, [base, lowerBuild])

let moreShortVersionComponents = AppcastItem(
title: "2.1.7.1",
link: #URL("https://he.ho.hum/myapps/downloads"),
version: "35",
shortVersionString: "2.1.7.1",
description: nil,
publishedDate: now,
enclosure: enclosure)

channel.items = [moreShortVersionComponents, base]
try channel.sortItems()
XCTAssertEqual(channel.items, [moreShortVersionComponents, base])
channel.items = [base, moreShortVersionComponents]
try channel.sortItems()
XCTAssertEqual(channel.items, [moreShortVersionComponents, base])

let older = AppcastItem(
title: "2.1.7",
link: #URL("https://he.ho.hum/myapps/downloads"),
version: "35",
shortVersionString: "2.1.7",
description: nil,
publishedDate: .distantPast,
enclosure: enclosure)

channel.items = [older, base]
try channel.sortItems()
XCTAssertEqual(channel.items, [base, older])
channel.items = [base, older]
try channel.sortItems()
XCTAssertEqual(channel.items, [base, older])
}

static func cleanXMLDates(in xmlDoc: XMLDocument) {
var theNode: XMLNode? = xmlDoc

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@
<enclosure url="https://he.ho.hum/updates/myapp_1.2.1.pkg" length="0" type="application/octet-stream" sparkle:edSignature="Secured! jk" sparkle:installationType="package"></enclosure>
</item>
</channel>
</rss>
</rss>

0 comments on commit 74de75b

Please sign in to comment.