feat: Begin working on configuration client.
This commit is contained in:
@@ -21,39 +21,73 @@ public struct CliClient: Sendable {
|
||||
public var build: @Sendable (SharedOptions) async throws -> String
|
||||
|
||||
/// Bump the existing version.
|
||||
public var bump: @Sendable (BumpOption, SharedOptions) async throws -> String
|
||||
public var bump: @Sendable (BumpOption?, SharedOptions) async throws -> String
|
||||
|
||||
/// Generate a version file with an optional version that can be set manually.
|
||||
public var generate: @Sendable (SharedOptions) async throws -> String
|
||||
|
||||
/// Update a version file manually.
|
||||
public var update: @Sendable (SharedOptions) async throws -> String
|
||||
|
||||
public enum BumpOption: Sendable, CaseIterable {
|
||||
case major, minor, patch
|
||||
case major, minor, patch, preRelease
|
||||
}
|
||||
|
||||
public enum PreReleaseStrategy: Equatable, Sendable {
|
||||
/// Use output of tag, with branch and commit sha.
|
||||
case branchAndCommit
|
||||
|
||||
/// Provide a custom pre-release tag.
|
||||
indirect case custom(String, PreReleaseStrategy? = nil)
|
||||
|
||||
/// Use the output of `git describe --tags`
|
||||
case tag
|
||||
}
|
||||
|
||||
public enum VersionStrategy: Equatable, Sendable {
|
||||
case branchAndCommit
|
||||
case semVar(SemVarOptions)
|
||||
|
||||
public struct SemVarOptions: Equatable, Sendable {
|
||||
let preReleaseStrategy: PreReleaseStrategy?
|
||||
let requireExistingFile: Bool
|
||||
let requireExistingSemVar: Bool
|
||||
|
||||
public init(
|
||||
preReleaseStrategy: PreReleaseStrategy? = nil,
|
||||
requireExistingFile: Bool = true,
|
||||
requireExistingSemVar: Bool = true
|
||||
) {
|
||||
self.preReleaseStrategy = preReleaseStrategy
|
||||
self.requireExistingFile = requireExistingFile
|
||||
self.requireExistingSemVar = requireExistingSemVar
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct SharedOptions: Equatable, Sendable {
|
||||
|
||||
let allowPreReleaseTag: Bool
|
||||
let dryRun: Bool
|
||||
let gitDirectory: String?
|
||||
let logLevel: Logger.Level
|
||||
let preReleaseStrategy: PreReleaseStrategy?
|
||||
let target: String
|
||||
let versionStrategy: VersionStrategy
|
||||
|
||||
public init(
|
||||
allowPreReleaseTag: Bool = false,
|
||||
gitDirectory: String? = nil,
|
||||
dryRun: Bool = false,
|
||||
gitDirectory: String? = nil,
|
||||
logLevel: Logger.Level = .debug,
|
||||
preReleaseStrategy: PreReleaseStrategy? = nil,
|
||||
target: String,
|
||||
logLevel: Logger.Level = .debug
|
||||
versionStrategy: VersionStrategy = .semVar(.init())
|
||||
) {
|
||||
self.allowPreReleaseTag = allowPreReleaseTag
|
||||
self.gitDirectory = gitDirectory
|
||||
self.dryRun = dryRun
|
||||
self.target = target
|
||||
self.gitDirectory = gitDirectory
|
||||
self.logLevel = logLevel
|
||||
self.preReleaseStrategy = preReleaseStrategy
|
||||
self.versionStrategy = versionStrategy
|
||||
}
|
||||
|
||||
var allowPreReleaseTag: Bool { preReleaseStrategy != nil }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -65,8 +99,7 @@ extension CliClient: DependencyKey {
|
||||
.init(
|
||||
build: { try await $0.build(environment) },
|
||||
bump: { try await $1.bump($0) },
|
||||
generate: { try await $0.generate() },
|
||||
update: { try await $0.update() }
|
||||
generate: { try await $0.generate() }
|
||||
)
|
||||
}
|
||||
|
||||
@@ -74,266 +107,3 @@ extension CliClient: DependencyKey {
|
||||
.live(environment: ProcessInfo.processInfo.environment)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Private
|
||||
|
||||
@_spi(Internal)
|
||||
public extension CliClient.SharedOptions {
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
let isDirectory = try await fileClient.isDirectory(url.cleanFilePath)
|
||||
|
||||
if isDirectory {
|
||||
url.appendPathComponent(Constants.defaultFileName)
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func run<T>(
|
||||
_ operation: () async throws -> T
|
||||
) async rethrows -> T {
|
||||
try await withDependencies {
|
||||
$0.logger.logLevel = logLevel
|
||||
} operation: {
|
||||
try await operation()
|
||||
}
|
||||
}
|
||||
|
||||
func write(_ string: String, to url: URL) async throws {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
if !dryRun {
|
||||
try await fileClient.write(string: string, to: url)
|
||||
} else {
|
||||
logger.debug("Skipping, due to dry-run being passed.")
|
||||
logger.debug("\(string)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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(\.gitClient) var gitVersion
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
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)
|
||||
|
||||
try await write(fileContents, to: fileUrl)
|
||||
|
||||
return fileUrl.cleanFilePath
|
||||
}
|
||||
}
|
||||
|
||||
private func getVersionString() async throws -> (version: String, usesOptionalType: Bool) {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.gitClient) var gitVersionClient
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let targetUrl = try await fileUrl()
|
||||
|
||||
guard fileClient.fileExists(targetUrl) else {
|
||||
// Get the latest tag, not requiring an exact tag set on the commit.
|
||||
// This will return a tag, that may have some more data on the patch
|
||||
// portion of the tag, such as: 0.1.1-4-g59bc977
|
||||
let version = try await gitVersionClient.currentVersion(in: gitDirectory, exactMatch: false)
|
||||
// TODO: Not sure what to do for the uses optional value here??
|
||||
return (version, false)
|
||||
}
|
||||
|
||||
let contents = try await fileClient.read(targetUrl)
|
||||
let versionLine = contents.split(separator: "\n")
|
||||
.first { $0.hasPrefix("let VERSION:") }
|
||||
|
||||
guard let versionLine else {
|
||||
throw CliClientError.failedToParseVersionFile
|
||||
}
|
||||
logger.debug("Version line: \(versionLine)")
|
||||
|
||||
let isOptional = versionLine.contains("String?")
|
||||
logger.debug("Uses optional: \(isOptional)")
|
||||
|
||||
let versionString = versionLine.split(separator: "let VERSION: \(isOptional ? "String?" : "String") = ").last
|
||||
guard let versionString else {
|
||||
throw CliClientError.failedToParseVersionFile
|
||||
}
|
||||
return (String(versionString), isOptional)
|
||||
}
|
||||
|
||||
private func getSemVar(_ version: String, _ bump: CliClient.BumpOption) throws -> SemVar {
|
||||
let semVar = SemVar(string: version) ?? .init()
|
||||
return semVar.bump(bump)
|
||||
}
|
||||
|
||||
func bump(_ type: CliClient.BumpOption) async throws -> String {
|
||||
try await run {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let targetUrl = try await fileUrl()
|
||||
|
||||
logger.debug("Bump target url: \(targetUrl.cleanFilePath)")
|
||||
|
||||
let (versionString, usesOptional) = try await getVersionString()
|
||||
let semVar = try getSemVar(versionString, type)
|
||||
let version = semVar.versionString(withPreReleaseTag: allowPreReleaseTag)
|
||||
logger.debug("Bumped version: \(version)")
|
||||
|
||||
let template = usesOptional ? Template.optional(version) : Template.build(version)
|
||||
try await write(template, to: targetUrl)
|
||||
return targetUrl.cleanFilePath
|
||||
}
|
||||
}
|
||||
|
||||
func generate(_ version: String? = nil) async throws -> String {
|
||||
try await run {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let targetUrl = try await fileUrl()
|
||||
|
||||
logger.debug("Generate target url: \(targetUrl.cleanFilePath)")
|
||||
|
||||
guard !fileClient.fileExists(targetUrl) else {
|
||||
throw CliClientError.fileExists(path: targetUrl.cleanFilePath)
|
||||
}
|
||||
|
||||
let template = Template.optional(version)
|
||||
try await write(template, to: targetUrl)
|
||||
return targetUrl.cleanFilePath
|
||||
}
|
||||
}
|
||||
|
||||
func update() async throws -> String {
|
||||
@Dependency(\.gitClient) var gitVersionClient
|
||||
return try await generate(gitVersionClient.currentVersion(in: gitDirectory))
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
public extension CliClient.BumpOption {
|
||||
|
||||
func bump(
|
||||
major: inout Int,
|
||||
minor: inout Int,
|
||||
patch: inout Int
|
||||
) {
|
||||
switch self {
|
||||
case .major:
|
||||
major += 1
|
||||
minor = 0
|
||||
patch = 0
|
||||
case .minor:
|
||||
minor += 1
|
||||
patch = 0
|
||||
case .patch:
|
||||
patch += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
public struct Template: Sendable {
|
||||
let type: TemplateType
|
||||
let version: String?
|
||||
|
||||
enum TemplateType: String, Sendable {
|
||||
case optionalString = "String?"
|
||||
case string = "String"
|
||||
}
|
||||
|
||||
var value: String {
|
||||
let versionString = version != nil ? "\"\(version!)\"" : "nil"
|
||||
return """
|
||||
// Do not set this variable, it is set during the build process.
|
||||
let VERSION: \(type.rawValue) = \(versionString)
|
||||
"""
|
||||
}
|
||||
|
||||
public static func build(_ version: String? = nil) -> String {
|
||||
nonOptional(version)
|
||||
}
|
||||
|
||||
public static func nonOptional(_ version: String? = nil) -> String {
|
||||
Self(type: .string, version: version).value
|
||||
}
|
||||
|
||||
public static func optional(_ version: String? = nil) -> String {
|
||||
Self(type: .optionalString, version: version).value
|
||||
}
|
||||
}
|
||||
|
||||
enum CliClientError: Error {
|
||||
case gitDirectoryNotFound
|
||||
case fileExists(path: String)
|
||||
case failedToParseVersionFile
|
||||
}
|
||||
|
||||
7
Sources/CliClient/CliClientError.swift
Normal file
7
Sources/CliClient/CliClientError.swift
Normal file
@@ -0,0 +1,7 @@
|
||||
enum CliClientError: Error {
|
||||
case gitDirectoryNotFound
|
||||
case fileExists(path: String)
|
||||
case fileDoesNotExist(path: String)
|
||||
case failedToParseVersionFile
|
||||
case semVarNotFound
|
||||
}
|
||||
48
Sources/CliClient/Internal/FileClient+semVar.swift
Normal file
48
Sources/CliClient/Internal/FileClient+semVar.swift
Normal file
@@ -0,0 +1,48 @@
|
||||
import Dependencies
|
||||
import FileClient
|
||||
import Foundation
|
||||
import GitClient
|
||||
|
||||
@_spi(Internal)
|
||||
public extension FileClient {
|
||||
private func getVersionString(
|
||||
fileUrl: URL,
|
||||
gitDirectory: String?
|
||||
) async throws -> (version: String, usesOptionalType: Bool) {
|
||||
@Dependency(\.gitClient) var gitClient
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let targetUrl = fileUrl
|
||||
|
||||
guard fileExists(targetUrl) else {
|
||||
throw CliClientError.fileDoesNotExist(path: fileUrl.cleanFilePath)
|
||||
}
|
||||
|
||||
let contents = try await read(targetUrl)
|
||||
let versionLine = contents.split(separator: "\n")
|
||||
.first { $0.hasPrefix("let VERSION:") }
|
||||
|
||||
guard let versionLine else {
|
||||
throw CliClientError.failedToParseVersionFile
|
||||
}
|
||||
logger.debug("Version line: \(versionLine)")
|
||||
|
||||
let isOptional = versionLine.contains("String?")
|
||||
logger.debug("Uses optional: \(isOptional)")
|
||||
|
||||
let versionString = versionLine.split(separator: "let VERSION: \(isOptional ? "String?" : "String") = ").last
|
||||
guard let versionString else {
|
||||
throw CliClientError.failedToParseVersionFile
|
||||
}
|
||||
return (String(versionString), isOptional)
|
||||
}
|
||||
|
||||
func semVar(
|
||||
file: URL,
|
||||
gitDirectory: String?
|
||||
) async throws -> (semVar: SemVar?, usesOptionalType: Bool) {
|
||||
let (string, usesOptionalType) = try await getVersionString(fileUrl: file, gitDirectory: gitDirectory)
|
||||
return (SemVar(string: string), usesOptionalType)
|
||||
}
|
||||
|
||||
}
|
||||
144
Sources/CliClient/Internal/Internal.swift
Normal file
144
Sources/CliClient/Internal/Internal.swift
Normal file
@@ -0,0 +1,144 @@
|
||||
import Dependencies
|
||||
import FileClient
|
||||
import Foundation
|
||||
import GitClient
|
||||
|
||||
@_spi(Internal)
|
||||
public extension CliClient.SharedOptions {
|
||||
|
||||
func parseTargetUrl() async throws -> URL {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
|
||||
let target = target.hasPrefix(".") ? String(target.dropFirst()) : 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)
|
||||
}
|
||||
|
||||
let isDirectory = try await fileClient.isDirectory(url.cleanFilePath)
|
||||
|
||||
if isDirectory {
|
||||
url.appendPathComponent(Constants.defaultFileName)
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func run(
|
||||
_ operation: (CurrentVersionContainer) async throws -> Void
|
||||
) async rethrows -> String {
|
||||
try await withDependencies {
|
||||
$0.logger.logLevel = logLevel
|
||||
} operation: {
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let targetUrl = try await parseTargetUrl()
|
||||
logger.debug("Target: \(targetUrl.cleanFilePath)")
|
||||
|
||||
try await operation(
|
||||
versionStrategy.currentVersion(
|
||||
file: targetUrl,
|
||||
gitDirectory: gitDirectory
|
||||
)
|
||||
)
|
||||
|
||||
return targetUrl.cleanFilePath
|
||||
}
|
||||
}
|
||||
|
||||
func write(_ string: String, to url: URL) async throws {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
if !dryRun {
|
||||
try await fileClient.write(string: string, to: url)
|
||||
} else {
|
||||
logger.debug("Skipping, due to dry-run being passed.")
|
||||
logger.debug("\(string)")
|
||||
}
|
||||
}
|
||||
|
||||
func write(_ currentVersion: CurrentVersionContainer) async throws {
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let version = try currentVersion.version.string(allowPreReleaseTag: allowPreReleaseTag)
|
||||
logger.debug("Version: \(version)")
|
||||
|
||||
let template = currentVersion.usesOptionalType ? Template.optional(version) : Template.nonOptional(version)
|
||||
logger.trace("Template string: \(template)")
|
||||
|
||||
try await write(template, to: currentVersion.targetUrl)
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
public struct CurrentVersionContainer: Sendable {
|
||||
|
||||
let targetUrl: URL
|
||||
let version: Version
|
||||
|
||||
var usesOptionalType: Bool {
|
||||
switch version {
|
||||
case .string: return false
|
||||
case let .semVar(_, usesOptionalType): return usesOptionalType
|
||||
}
|
||||
}
|
||||
|
||||
public enum Version: Sendable {
|
||||
case string(String)
|
||||
case semVar(SemVar, usesOptionalType: Bool = true)
|
||||
|
||||
func string(allowPreReleaseTag: Bool) throws -> String {
|
||||
switch self {
|
||||
case let .string(string):
|
||||
return string
|
||||
case let .semVar(semVar, usesOptionalType: _):
|
||||
return semVar.versionString(withPreReleaseTag: allowPreReleaseTag)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension CliClient.SharedOptions {
|
||||
|
||||
func build(_ environment: [String: String]) async throws -> String {
|
||||
try await run { currentVersion in
|
||||
try await write(currentVersion)
|
||||
}
|
||||
}
|
||||
|
||||
func bump(_ type: CliClient.BumpOption?) async throws -> String {
|
||||
guard let type else {
|
||||
return try await generate()
|
||||
}
|
||||
|
||||
return try await run { container in
|
||||
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
switch container.version {
|
||||
case .string: // When we did not parse a semVar, just write whatever we parsed for the current version.
|
||||
try await write(container)
|
||||
|
||||
case let .semVar(semVar, usesOptionalType: usesOptionalType):
|
||||
let bumped = semVar.bump(type, preRelease: nil) // preRelease is already set on semVar.
|
||||
let version = bumped.versionString(withPreReleaseTag: allowPreReleaseTag)
|
||||
logger.debug("Bumped version: \(version)")
|
||||
let template = usesOptionalType ? Template.optional(version) : Template.build(version)
|
||||
try await write(template, to: container.targetUrl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func generate() async throws -> String {
|
||||
try await run { currentVersion in
|
||||
try await write(currentVersion)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import Dependencies
|
||||
import FileClient
|
||||
import Foundation
|
||||
import GitClient
|
||||
|
||||
@@ -74,7 +76,7 @@ public struct SemVar: CustomStringConvertible, Equatable, Sendable {
|
||||
}
|
||||
|
||||
// Bumps the sem-var by the given option (major, minor, patch)
|
||||
public func bump(_ option: CliClient.BumpOption) -> Self {
|
||||
public func bump(_ option: CliClient.BumpOption, preRelease: String?) -> Self {
|
||||
switch option {
|
||||
case .major:
|
||||
return .init(
|
||||
@@ -97,8 +99,20 @@ public struct SemVar: CustomStringConvertible, Equatable, Sendable {
|
||||
patch: patch + 1,
|
||||
preRelease: preRelease
|
||||
)
|
||||
case .preRelease:
|
||||
guard let preRelease else { return self }
|
||||
return applyingPreRelease(preRelease)
|
||||
}
|
||||
}
|
||||
|
||||
public func applyingPreRelease(_ preRelease: String) -> Self {
|
||||
.init(
|
||||
major: major,
|
||||
minor: minor,
|
||||
patch: patch,
|
||||
preRelease: preRelease
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
30
Sources/CliClient/Internal/Template.swift
Normal file
30
Sources/CliClient/Internal/Template.swift
Normal file
@@ -0,0 +1,30 @@
|
||||
@_spi(Internal)
|
||||
public struct Template: Sendable {
|
||||
let type: TemplateType
|
||||
let version: String?
|
||||
|
||||
enum TemplateType: String, Sendable {
|
||||
case optionalString = "String?"
|
||||
case string = "String"
|
||||
}
|
||||
|
||||
var value: String {
|
||||
let versionString = version != nil ? "\"\(version!)\"" : "nil"
|
||||
return """
|
||||
// Do not set this variable, it is set during the build process.
|
||||
let VERSION: \(type.rawValue) = \(versionString)
|
||||
"""
|
||||
}
|
||||
|
||||
public static func build(_ version: String? = nil) -> String {
|
||||
nonOptional(version)
|
||||
}
|
||||
|
||||
public static func nonOptional(_ version: String? = nil) -> String {
|
||||
Self(type: .string, version: version).value
|
||||
}
|
||||
|
||||
public static func optional(_ version: String? = nil) -> String {
|
||||
Self(type: .optionalString, version: version).value
|
||||
}
|
||||
}
|
||||
103
Sources/CliClient/Internal/VersionStrategy+currentVersion.swift
Normal file
103
Sources/CliClient/Internal/VersionStrategy+currentVersion.swift
Normal file
@@ -0,0 +1,103 @@
|
||||
import Dependencies
|
||||
import FileClient
|
||||
import struct Foundation.URL
|
||||
import GitClient
|
||||
|
||||
@_spi(Internal)
|
||||
public extension CliClient.PreReleaseStrategy {
|
||||
|
||||
func preReleaseString(gitDirectory: String?) async throws -> String {
|
||||
@Dependency(\.gitClient) var gitClient
|
||||
switch self {
|
||||
case let .custom(string, child):
|
||||
guard let child else { return string }
|
||||
return try await "\(string)-\(child.preReleaseString(gitDirectory: gitDirectory))"
|
||||
case .tag:
|
||||
return try await gitClient.version(.init(
|
||||
gitDirectory: gitDirectory,
|
||||
style: .tag(exactMatch: false)
|
||||
)).description
|
||||
case .branchAndCommit:
|
||||
return try await gitClient.version(.init(
|
||||
gitDirectory: gitDirectory,
|
||||
style: .branch(commitSha: true)
|
||||
)).description
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
public extension CliClient.VersionStrategy.SemVarOptions {
|
||||
|
||||
private func applyingPreRelease(_ semVar: SemVar, _ gitDirectory: String?) async throws -> SemVar {
|
||||
guard let preReleaseStrategy else { return semVar }
|
||||
let preRelease = try await preReleaseStrategy.preReleaseString(gitDirectory: gitDirectory)
|
||||
return semVar.applyingPreRelease(preRelease)
|
||||
}
|
||||
|
||||
func currentVersion(file: URL, gitDirectory: String? = nil) async throws -> CurrentVersionContainer.Version {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.gitClient) var gitClient
|
||||
|
||||
let fileOutput = try? await fileClient.semVar(file: file, gitDirectory: gitDirectory)
|
||||
var semVar = fileOutput?.semVar
|
||||
let usesOptionalType = fileOutput?.usesOptionalType
|
||||
|
||||
if requireExistingFile {
|
||||
guard let semVar else {
|
||||
throw CliClientError.fileDoesNotExist(path: file.cleanFilePath)
|
||||
}
|
||||
return try await .semVar(
|
||||
applyingPreRelease(semVar, gitDirectory),
|
||||
usesOptionalType: usesOptionalType ?? false
|
||||
)
|
||||
}
|
||||
|
||||
// Didn't have existing semVar loaded from file, so check for git-tag.
|
||||
|
||||
semVar = try await gitClient.version(.init(
|
||||
gitDirectory: gitDirectory,
|
||||
style: .tag(exactMatch: false)
|
||||
)).semVar
|
||||
if requireExistingSemVar {
|
||||
guard let semVar else {
|
||||
fatalError()
|
||||
}
|
||||
return try await .semVar(
|
||||
applyingPreRelease(semVar, gitDirectory),
|
||||
usesOptionalType: usesOptionalType ?? false
|
||||
)
|
||||
}
|
||||
|
||||
return try await .semVar(
|
||||
applyingPreRelease(.init(), gitDirectory),
|
||||
usesOptionalType: usesOptionalType ?? false
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
public extension CliClient.VersionStrategy {
|
||||
|
||||
func currentVersion(file: URL, gitDirectory: String?) async throws -> CurrentVersionContainer {
|
||||
@Dependency(\.gitClient) var gitClient
|
||||
|
||||
switch self {
|
||||
case .branchAndCommit:
|
||||
return try await .init(
|
||||
targetUrl: file,
|
||||
version: .string(
|
||||
gitClient.version(.init(
|
||||
gitDirectory: gitDirectory,
|
||||
style: .branch(commitSha: true)
|
||||
)).description
|
||||
)
|
||||
)
|
||||
case let .semVar(options):
|
||||
return try await .init(
|
||||
targetUrl: file,
|
||||
version: options.currentVersion(file: file, gitDirectory: gitDirectory)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user