feat: Working on updates to sem var and git client.
This commit is contained in:
@@ -18,6 +18,8 @@ public extension DependencyValues {
|
||||
@DependencyClient
|
||||
public struct CliClient: Sendable {
|
||||
|
||||
static let defaultFileName = "Version.swift"
|
||||
|
||||
/// Build and update the version based on the git tag, or branch + sha.
|
||||
public var build: @Sendable (SharedOptions) async throws -> String
|
||||
|
||||
@@ -36,22 +38,22 @@ public struct CliClient: Sendable {
|
||||
|
||||
public struct SharedOptions: Equatable, Sendable {
|
||||
|
||||
let allowPreReleaseTag: Bool
|
||||
let dryRun: Bool
|
||||
let fileName: String
|
||||
let gitDirectory: String?
|
||||
let logLevel: Logger.Level
|
||||
let target: String
|
||||
|
||||
public init(
|
||||
allowPreReleaseTag: Bool = false,
|
||||
gitDirectory: String? = nil,
|
||||
dryRun: Bool = false,
|
||||
fileName: String = "Version.swift",
|
||||
target: String,
|
||||
logLevel: Logger.Level = .debug
|
||||
) {
|
||||
self.allowPreReleaseTag = allowPreReleaseTag
|
||||
self.gitDirectory = gitDirectory
|
||||
self.dryRun = dryRun
|
||||
self.fileName = fileName
|
||||
self.target = target
|
||||
self.logLevel = logLevel
|
||||
}
|
||||
@@ -81,18 +83,27 @@ extension CliClient: DependencyKey {
|
||||
@_spi(Internal)
|
||||
public extension CliClient.SharedOptions {
|
||||
|
||||
var fileUrl: URL {
|
||||
func fileUrl() async throws -> URL {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
|
||||
let target = self.target.hasPrefix(".") ? String(self.target.dropFirst()) : self.target
|
||||
let targetHasSources = target.hasPrefix("Sources") || target.hasPrefix("/Sources")
|
||||
|
||||
var url = url(for: gitDirectory ?? (targetHasSources ? target : "Sources"))
|
||||
|
||||
if gitDirectory != nil {
|
||||
if !targetHasSources {
|
||||
url.appendPathComponent("Sources")
|
||||
}
|
||||
url.appendPathComponent(target)
|
||||
}
|
||||
url.appendPathComponent(fileName)
|
||||
|
||||
let isDirectory = try await fileClient.isDirectory(url.cleanFilePath)
|
||||
|
||||
if isDirectory {
|
||||
url.appendPathComponent(CliClient.defaultFileName)
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
@@ -121,24 +132,62 @@ public extension CliClient.SharedOptions {
|
||||
|
||||
private extension CliClient.SharedOptions {
|
||||
|
||||
func gitVersion() async throws -> GitClient.Version {
|
||||
@Dependency(\.gitClient) var gitClient
|
||||
|
||||
if let exactMatch = try? await gitClient.version(.init(
|
||||
gitDirectory: gitDirectory,
|
||||
style: .tag(exactMatch: true)
|
||||
)) {
|
||||
return exactMatch
|
||||
} else if let partialMatch = try? await gitClient.version(.init(
|
||||
gitDirectory: gitDirectory,
|
||||
style: .tag(exactMatch: false)
|
||||
)) {
|
||||
return partialMatch
|
||||
} else {
|
||||
return try await gitClient.version(.init(
|
||||
gitDirectory: gitDirectory,
|
||||
style: .branch(commitSha: true)
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
func gitSemVar() async throws -> SemVar {
|
||||
@Dependency(\.gitClient) var gitClient
|
||||
|
||||
let version = try await gitVersion()
|
||||
|
||||
guard let semVar = version.semVar else {
|
||||
return .init(preRelease: version.description)
|
||||
}
|
||||
|
||||
if allowPreReleaseTag, semVar.preRelease == nil {
|
||||
let branchVersion = try await gitClient.version(.init(
|
||||
gitDirectory: gitDirectory,
|
||||
style: .branch(commitSha: true)
|
||||
))
|
||||
return .init(
|
||||
major: semVar.major,
|
||||
minor: semVar.minor,
|
||||
patch: semVar.patch,
|
||||
preRelease: branchVersion.description
|
||||
)
|
||||
}
|
||||
return semVar
|
||||
}
|
||||
|
||||
func build(_ environment: [String: String]) async throws -> String {
|
||||
try await run {
|
||||
@Dependency(\.gitVersionClient) var gitVersion
|
||||
@Dependency(\.gitClient) var gitVersion
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let gitDirectory = gitDirectory ?? environment["PWD"]
|
||||
|
||||
guard let gitDirectory else {
|
||||
throw CliClientError.gitDirectoryNotFound
|
||||
}
|
||||
|
||||
logger.debug("Building with git directory: \(gitDirectory)")
|
||||
|
||||
let fileUrl = self.fileUrl
|
||||
let fileUrl = try await self.fileUrl()
|
||||
logger.debug("File url: \(fileUrl.cleanFilePath)")
|
||||
|
||||
let currentVersion = try await gitVersion.currentVersion(in: gitDirectory)
|
||||
logger.debug("Git version: \(currentVersion)")
|
||||
|
||||
let fileContents = Template.build(currentVersion)
|
||||
|
||||
@@ -150,9 +199,10 @@ private extension CliClient.SharedOptions {
|
||||
|
||||
private func getVersionString() async throws -> (version: String, usesOptionalType: Bool) {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.gitVersionClient) var gitVersionClient
|
||||
@Dependency(\.gitClient) var gitVersionClient
|
||||
@Dependency(\.logger) var logger
|
||||
let targetUrl = fileUrl
|
||||
|
||||
let targetUrl = try await fileUrl()
|
||||
|
||||
guard fileClient.fileExists(targetUrl) else {
|
||||
// Get the latest tag, not requiring an exact tag set on the commit.
|
||||
@@ -163,7 +213,7 @@ private extension CliClient.SharedOptions {
|
||||
return (version, false)
|
||||
}
|
||||
|
||||
let contents = try await fileClient.read(fileUrl)
|
||||
let contents = try await fileClient.read(targetUrl)
|
||||
let versionLine = contents.split(separator: "\n")
|
||||
.first { $0.hasPrefix("let VERSION:") }
|
||||
|
||||
@@ -182,41 +232,23 @@ private extension CliClient.SharedOptions {
|
||||
return (String(versionString), isOptional)
|
||||
}
|
||||
|
||||
// swiftlint:disable large_tuple
|
||||
private func getVersionParts(_ version: String, _ bump: CliClient.BumpOption) throws -> (Int, Int, Int) {
|
||||
@Dependency(\.logger) var logger
|
||||
let parts = String(version).split(separator: ".")
|
||||
logger.debug("Version parts: \(parts)")
|
||||
|
||||
// TODO: Better error.
|
||||
guard parts.count == 3 else {
|
||||
throw CliClientError.failedToParseVersionFile
|
||||
}
|
||||
|
||||
var major = Int(String(parts[0].replacingOccurrences(of: "\"", with: ""))) ?? 0
|
||||
var minor = Int(String(parts[1])) ?? 0
|
||||
|
||||
// Handle cases where patch version has extra data / not an exact match tag.
|
||||
// Such as: 0.1.1-4-g59bc977
|
||||
var patch = Int(String(parts[2].split(separator: "-").first ?? "0")) ?? 0
|
||||
bump.bump(major: &major, minor: &minor, patch: &patch)
|
||||
return (major, minor, patch)
|
||||
private func getSemVar(_ version: String, _ bump: CliClient.BumpOption) throws -> SemVar {
|
||||
let semVar = SemVar(string: version) ?? .init()
|
||||
return semVar.bump(bump)
|
||||
}
|
||||
|
||||
// swiftlint:enable large_tuple
|
||||
|
||||
func bump(_ type: CliClient.BumpOption) async throws -> String {
|
||||
try await run {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let targetUrl = fileUrl
|
||||
let targetUrl = try await fileUrl()
|
||||
|
||||
logger.debug("Bump target url: \(targetUrl.cleanFilePath)")
|
||||
|
||||
let (versionString, usesOptional) = try await getVersionString()
|
||||
let (major, minor, patch) = try getVersionParts(versionString, type)
|
||||
let version = "\(major).\(minor).\(patch)"
|
||||
let semVar = try getSemVar(versionString, type)
|
||||
let version = semVar.versionString(allowPrerelease: allowPreReleaseTag)
|
||||
logger.debug("Bumped version: \(version)")
|
||||
|
||||
let template = usesOptional ? Template.optional(version) : Template.build(version)
|
||||
@@ -230,7 +262,7 @@ private extension CliClient.SharedOptions {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let targetUrl = fileUrl
|
||||
let targetUrl = try await fileUrl()
|
||||
|
||||
logger.debug("Generate target url: \(targetUrl.cleanFilePath)")
|
||||
|
||||
@@ -245,7 +277,7 @@ private extension CliClient.SharedOptions {
|
||||
}
|
||||
|
||||
func update() async throws -> String {
|
||||
@Dependency(\.gitVersionClient) var gitVersionClient
|
||||
@Dependency(\.gitClient) var gitVersionClient
|
||||
return try await generate(gitVersionClient.currentVersion(in: gitDirectory))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user