Skip to content

Commit

Permalink
Merge pull request #2 from SwiftPackageIndex/support-swift-4.2
Browse files Browse the repository at this point in the history
Support swift 4.2, 5.0, 5.1
  • Loading branch information
finestructure authored Sep 2, 2020
2 parents 5bc3ff6 + f37bd4f commit a302bf0
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.2
// swift-tools-version:4.2

import PackageDescription

Expand Down
44 changes: 37 additions & 7 deletions Sources/SemanticVersion/SemanticVersion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension SemanticVersion: LosslessStringConvertible {


extension SemanticVersion: Comparable {
public static func < (lhs: Self, rhs: Self) -> Bool {
public static func < (lhs: SemanticVersion, rhs: SemanticVersion) -> Bool {
if lhs.major != rhs.major { return lhs.major < rhs.major }
if lhs.minor != rhs.minor { return lhs.minor < rhs.minor }
if lhs.patch != rhs.patch { return lhs.patch < rhs.patch }
Expand All @@ -57,17 +57,19 @@ extension SemanticVersion: Comparable {


extension SemanticVersion {
public var isStable: Bool { preRelease.isEmpty && build.isEmpty }
public var isPreRelease: Bool { !isStable }
public var isMajorRelease: Bool { isStable && (major > 0 && minor == 0 && patch == 0) }
public var isMinorRelease: Bool { isStable && (minor > 0 && patch == 0) }
public var isPatchRelease: Bool { isStable && patch > 0 }
public var isInitialRelease: Bool { self == .init(0, 0, 0) }
public var isStable: Bool { return preRelease.isEmpty && build.isEmpty }
public var isPreRelease: Bool { return !isStable }
public var isMajorRelease: Bool { return isStable && (major > 0 && minor == 0 && patch == 0) }
public var isMinorRelease: Bool { return isStable && (minor > 0 && patch == 0) }
public var isPatchRelease: Bool { return isStable && patch > 0 }
public var isInitialRelease: Bool { return self == .init(0, 0, 0) }
}


// Source: https://regex101.com/r/Ly7O1x/3/
// Linked from https://semver.org
#if swift(>=5)

let semVerRegex = NSRegularExpression(#"""
^
v? # SPI extension: allow leading 'v'
Expand All @@ -91,3 +93,31 @@ v? # SPI extension: allow leading 'v'
)?
$
"""#, options: [.allowCommentsAndWhitespace])

#else

let semVerRegex = NSRegularExpression("""
^
v? # SPI extension: allow leading 'v'
(?<major>0|[1-9]\\d*)
\\.
(?<minor>0|[1-9]\\d*)
\\.
(?<patch>0|[1-9]\\d*)
(?:-
(?<prerelease>
(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)
(?:\\.
(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)
)
*)
)?
(?:\\+
(?<buildmetadata>[0-9a-zA-Z-]+
(?:\\.[0-9a-zA-Z-]+)
*)
)?
$
""", options: [.allowCommentsAndWhitespace])

#endif
28 changes: 28 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if swift(>=5)

#else

import XCTest

@testable import SemanticVersionTests

extension SemanticVersionTests {
static var allTests: [(String, (SemanticVersionTests) -> () throws -> Void)] = [
("test_semVerRegex_valid", test_semVerRegex_valid),
("test_allow_leading_v", test_allow_leading_v),
("test_semVerRegex_invalid", test_semVerRegex_invalid),
("test_init", test_init),
("test_description", test_description),
("test_Comparable", test_Comparable),
("test_isStable", test_isStable),
("test_isMajorRelease", test_isMajorRelease),
("test_isMinorRelease", test_isMinorRelease),
("test_isPatchRelease", test_isPatchRelease),
]
}

XCTMain([
testCase(SemanticVersionTests.allTests)
])

#endif

0 comments on commit a302bf0

Please sign in to comment.