Skip to content

Commit

Permalink
Merge pull request #46 from marinofelipe/fix/tag-ordering
Browse files Browse the repository at this point in the history
Fix tag ordering
  • Loading branch information
marinofelipe authored Feb 3, 2024
2 parents c7b5221 + 04defdf commit f1851f2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Basics
import PackageModel
import SourceControl
import TSCBasic
import TSCUtility

public enum ResourceState: Equatable, CustomStringConvertible {
case undefined
Expand Down Expand Up @@ -160,7 +161,7 @@ public final class SwiftPackageService {
editable: false
)

let repositoryTags = try workingCopy.getTags()
let repositoryTags = try workingCopy.getSemVerOrderedTags()

let resolvedTag: String
let tagState: ResourceState
Expand Down Expand Up @@ -217,3 +218,33 @@ public final class SwiftPackageService {
}
}
}

// MARK: - Helpers

private extension WorkingCheckout {
/// Get repository tags ordered by semantic versioning
/// it attempts to normalize the tags by removing occurrences of `v`
func getSemVerOrderedTags() throws -> [String] {
try getTags().sorted(
by: {
guard
let versionA = Version($0.removingCharacterV),
let versionB = Version($1.removingCharacterV)
else {
return $0.compare(
$1,
options: .numeric
) == .orderedAscending
}

return versionA < versionB
}
)
}
}

private extension String {
var removingCharacterV: String {
replacingOccurrences(of: "v", with: "")
}
}
4 changes: 2 additions & 2 deletions Sources/Run/SwiftPackageInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct SwiftPackageInfo: AsyncParsableCommand {
that can be used in your favor when deciding whether to
adopt or not a Swift Package as a dependency on your app.
""",
version: "1.4.0",
version: "1.4.1",
subcommands: [
BinarySize.self,
Platforms.self,
Expand Down Expand Up @@ -185,7 +185,7 @@ extension ParsableCommand {
Console.default.lineBreakAndWrite("Package version was \(tagState.description)")

if let latestTag {
Console.default.lineBreakAndWrite("Defaulting to latest found tag: \(latestTag)")
Console.default.lineBreakAndWrite("Defaulting to latest found semver tag: \(latestTag)")
swiftPackage.version = latestTag
}
case .valid:
Expand Down

0 comments on commit f1851f2

Please sign in to comment.