feat: Begin cleaning up cli-client for better separation for current and next versions.
All checks were successful
CI / Ubuntu (push) Successful in 2m33s

This commit is contained in:
2024-12-28 00:12:49 -05:00
parent f9710e5992
commit b9cf913528
7 changed files with 184 additions and 107 deletions

View File

@@ -96,10 +96,14 @@ public extension CliClient.SharedOptions {
}
}
// TODO: Add optional property for currentVersion (loaded version from file)
// and rename version to nextVersion.
@_spi(Internal)
public struct CurrentVersionContainer: Sendable {
let targetUrl: URL
let currentVersion: CurrentVersion?
let version: Version
var usesOptionalType: Bool {
@@ -109,8 +113,15 @@ public struct CurrentVersionContainer: Sendable {
}
}
public enum CurrentVersion: Sendable {
case branch(String, usesOptionalType: Bool)
case semvar(SemVar, usesOptionalType: Bool)
}
public enum Version: Sendable {
// TODO: Call this branch for consistency.
case string(String)
// TODO: Remove has changes when currentVersion/nextVersion is implemented.
case semvar(SemVar, usesOptionalType: Bool = true, hasChanges: Bool)
func string(allowPreReleaseTag: Bool) throws -> String {

View File

@@ -23,6 +23,35 @@ extension Configuration {
}
}
private extension Configuration.SemVar.Strategy {
func getSemvar(gitDirectory: String? = nil) async throws -> SemVar {
@Dependency(\.asyncShellClient) var asyncShellClient
@Dependency(\.gitClient) var gitClient
@Dependency(\.logger) var logger
let semvar: SemVar?
switch self {
case let .command(arguments: arguments):
logger.trace("Using custom command strategy with: \(arguments)")
semvar = try await SemVar(string: asyncShellClient.background(.init(arguments)))
case let .gitTag(exactMatch: exactMatch):
logger.trace("Using gitTag strategy.")
semvar = try await gitClient.version(.init(
gitDirectory: gitDirectory,
style: .tag(exactMatch: exactMatch ?? false)
)).semVar
}
guard let semvar else {
throw CliClientError.semVarNotFound
}
return semvar
}
}
@_spi(Internal)
public extension Configuration.SemVar {
@@ -92,6 +121,9 @@ public extension Configuration.SemVar {
private extension Configuration.VersionStrategy {
// TODO: This should just load the `nextVersion`, and should probably live on CurrentVersionContainer.
// FIX: Fix what's passed to current verions here.
func currentVersion(targetUrl: URL, gitDirectory: String?) async throws -> CurrentVersionContainer {
@Dependency(\.gitClient) var gitClient
@@ -103,11 +135,13 @@ private extension Configuration.VersionStrategy {
}
return try await .init(
targetUrl: targetUrl,
currentVersion: nil,
version: semvar.currentVersion(file: targetUrl, gitDirectory: gitDirectory)
)
}
return try await .init(
targetUrl: targetUrl,
currentVersion: nil,
version: .string(
gitClient.version(includeCommitSha: branch.includeCommitSha, gitDirectory: gitDirectory)
)

View File

@@ -5,6 +5,46 @@ import GitClient
@_spi(Internal)
public extension FileClient {
func loadCurrentVersion(
url: URL,
gitDirectory: String?,
expectsBranch: Bool
) async throws -> CurrentVersionContainer.CurrentVersion? {
@Dependency(\.logger) var logger
switch expectsBranch {
case true:
let (string, usesOptionalType) = try await branch(file: url, gitDirectory: gitDirectory)
logger.debug("Loaded branch: \(string)")
return .branch(string, usesOptionalType: usesOptionalType)
case false:
let (semvar, usesOptionalType) = try await semvar(file: url, gitDirectory: gitDirectory)
guard let semvar else { return nil }
logger.debug("Semvar: \(semvar)")
return .semvar(semvar, usesOptionalType: usesOptionalType)
}
}
// TODO: Make private.
func branch(
file: URL,
gitDirectory: String?
) async throws -> (string: String, usesOptionalType: Bool) {
let (string, usesOptionalType) = try await getVersionString(fileUrl: file, gitDirectory: gitDirectory)
return (string, usesOptionalType)
}
// TODO: Make private.
func semvar(
file: URL,
gitDirectory: String?
) async throws -> (semVar: SemVar?, usesOptionalType: Bool) {
let (string, usesOptionalType) = try await getVersionString(fileUrl: file, gitDirectory: gitDirectory)
let semvar = SemVar(string: string)
return (semvar, usesOptionalType)
}
private func getVersionString(
fileUrl: URL,
gitDirectory: String?
@@ -38,15 +78,4 @@ public extension FileClient {
return (String(versionString), isOptional)
}
func semvar(
file: URL,
gitDirectory: String?
) async throws -> (semVar: SemVar?, usesOptionalType: Bool) {
@Dependency(\.logger) var logger
let (string, usesOptionalType) = try await getVersionString(fileUrl: file, gitDirectory: gitDirectory)
let semvar = SemVar(string: string)
logger.debug("Semvar: \(String(describing: semvar))")
return (semvar, usesOptionalType)
}
}