Skip to content

Revert "Replace lenient version parsing implementation with the one in TSC" #805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2155,7 +2155,7 @@ extension Driver {
diagnosticsEngine.emit(.warning_no_sdksettings_json(sdkPath.pathString))
return false
}
guard let sdkVersion = try? Version(versionString: sdkInfo.versionString, usesLenientParsing: true) else {
guard let sdkVersion = Version(potentiallyIncompleteVersionString: sdkInfo.versionString) else {
diagnosticsEngine.emit(.warning_fail_parse_sdk_ver(sdkInfo.versionString, sdkPath.pathString))
return false
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftDriver/Toolchains/DarwinToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,12 @@ public final class DarwinToolchain: Toolchain {
let mappingDict = try keyedContainer.decode([String: String].self, forKey: .macOSToCatalystMapping)
self.macOSToCatalystMapping = [:]
try mappingDict.forEach { key, value in
guard let newKey = try? Version(versionString: key, usesLenientParsing: true) else {
guard let newKey = Version(potentiallyIncompleteVersionString: key) else {
throw DecodingError.dataCorruptedError(forKey: .macOSToCatalystMapping,
in: keyedContainer,
debugDescription: "Malformed version string")
}
guard let newValue = try? Version(versionString: value, usesLenientParsing: true) else {
guard let newValue = Version(potentiallyIncompleteVersionString: value) else {
throw DecodingError.dataCorruptedError(forKey: .macOSToCatalystMapping,
in: keyedContainer,
debugDescription: "Malformed version string")
Expand All @@ -277,7 +277,7 @@ public final class DarwinToolchain: Toolchain {

self.versionString = try keyedContainer.decode(String.self, forKey: .version)
self.canonicalName = try keyedContainer.decode(String.self, forKey: .canonicalName)
guard let version = try? Version(versionString: versionString, usesLenientParsing: true) else {
guard let version = Version(potentiallyIncompleteVersionString: versionString) else {
throw DecodingError.dataCorruptedError(forKey: .version,
in: keyedContainer,
debugDescription: "Malformed version string")
Expand Down
39 changes: 39 additions & 0 deletions Sources/SwiftDriver/Utilities/VersionExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,45 @@ import TSCUtility

// TODO: maybe move this to TSC.
extension Version {
/// Create a version from a string, replacing unknown trailing components with '0'.
init?(potentiallyIncompleteVersionString string: String) {
// This is a copied version of TSC's version parsing, modified to fill
// in missing components if needed.
let prereleaseStartIndex = string.firstIndex(of: "-")
let metadataStartIndex = string.firstIndex(of: "+")

let requiredEndIndex = prereleaseStartIndex ?? metadataStartIndex ?? string.endIndex
let requiredCharacters = string.prefix(upTo: requiredEndIndex)
var requiredComponents = requiredCharacters
.split(separator: ".", maxSplits: 2, omittingEmptySubsequences: false)
.map(String.init).compactMap({ Int($0) }).filter({ $0 >= 0 })

requiredComponents.append(contentsOf:
Array(repeating: 0,
count: max(0, 3 - requiredComponents.count)))

let major = requiredComponents[0]
let minor = requiredComponents[1]
let patch = requiredComponents[2]

func identifiers(start: String.Index?, end: String.Index) -> [String] {
guard let start = start else { return [] }
let identifiers = string[string.index(after: start)..<end]
return identifiers.split(separator: ".").map(String.init)
}

let prereleaseIdentifiers = identifiers(
start: prereleaseStartIndex,
end: metadataStartIndex ?? string.endIndex)
let buildMetadataIdentifiers = identifiers(
start: metadataStartIndex,
end: string.endIndex)

self.init(major, minor, patch,
prereleaseIdentifiers: prereleaseIdentifiers,
buildMetadataIdentifiers: buildMetadataIdentifiers)
}

/// Returns the version with out any build/release metadata numbers.
var withoutBuildNumbers: Version {
return Version(self.major, self.minor, self.patch)
Expand Down