feat: Integrates configuration-client.
This commit is contained in:
@@ -2,14 +2,6 @@
|
||||
requireExistingFile = true
|
||||
requireExistingSemVar = true
|
||||
|
||||
[strategy.semvar.preRelease]
|
||||
prefix = 'rc'
|
||||
style = 'custom'
|
||||
usesGitTag = false
|
||||
|
||||
[strategy.semvar.preRelease.branch]
|
||||
includeCommitSha = true
|
||||
|
||||
[target.module]
|
||||
fileName = 'Version.swift'
|
||||
name = 'cli-version'
|
||||
name = 'cli-version'
|
||||
|
||||
@@ -33,66 +33,29 @@ public struct CliClient: Sendable {
|
||||
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 typealias SemVarOptions = Configuration.SemVar
|
||||
|
||||
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 dryRun: Bool
|
||||
let gitDirectory: String?
|
||||
let logLevel: Logger.Level
|
||||
let preReleaseStrategy: PreReleaseStrategy?
|
||||
let target: String
|
||||
let versionStrategy: VersionStrategy
|
||||
let configuration: Configuration
|
||||
|
||||
public init(
|
||||
dryRun: Bool = false,
|
||||
gitDirectory: String? = nil,
|
||||
logLevel: Logger.Level = .debug,
|
||||
preReleaseStrategy: PreReleaseStrategy? = nil,
|
||||
target: String,
|
||||
versionStrategy: VersionStrategy = .semVar(.init())
|
||||
configuration: Configuration
|
||||
) {
|
||||
self.dryRun = dryRun
|
||||
self.target = target
|
||||
self.gitDirectory = gitDirectory
|
||||
self.logLevel = logLevel
|
||||
self.preReleaseStrategy = preReleaseStrategy
|
||||
self.versionStrategy = versionStrategy
|
||||
self.configuration = configuration
|
||||
}
|
||||
|
||||
var allowPreReleaseTag: Bool { preReleaseStrategy != nil }
|
||||
var allowPreReleaseTag: Bool {
|
||||
guard let semvar = configuration.strategy?.semvar else { return false }
|
||||
return semvar.preRelease != nil
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,8 +3,29 @@ import Dependencies
|
||||
import Foundation
|
||||
import GitClient
|
||||
|
||||
extension Configuration {
|
||||
func targetUrl(gitDirectory: String?) throws -> URL {
|
||||
guard let target else {
|
||||
throw ConfigurationParsingError.targetNotFound
|
||||
}
|
||||
return try target.url(gitDirectory: gitDirectory)
|
||||
}
|
||||
|
||||
func currentVersion(targetUrl: URL, gitDirectory: String?) async throws -> CurrentVersionContainer {
|
||||
guard let strategy else {
|
||||
throw ConfigurationParsingError.versionNotFound
|
||||
}
|
||||
return try await strategy.currentVersion(
|
||||
targetUrl: targetUrl,
|
||||
gitDirectory: gitDirectory
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension Configuration.Target {
|
||||
func url(gitDirectory: String?) throws -> URL {
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let filePath: String
|
||||
|
||||
if let path {
|
||||
@@ -15,13 +36,17 @@ extension Configuration.Target {
|
||||
}
|
||||
|
||||
var path = module.name
|
||||
if !path.hasPrefix("Sources") || !path.hasPrefix("./Sources") {
|
||||
path = "Sources/\(path)"
|
||||
}
|
||||
logger.debug("module.name: \(path)")
|
||||
|
||||
if path.hasPrefix("./") {
|
||||
path = String(path.dropFirst(2))
|
||||
}
|
||||
|
||||
if !path.hasPrefix("Sources") {
|
||||
logger.debug("no prefix")
|
||||
path = "Sources/\(path)"
|
||||
}
|
||||
|
||||
filePath = "\(path)/\(module.fileName)"
|
||||
}
|
||||
|
||||
@@ -70,46 +95,66 @@ extension Configuration.PreReleaseStrategy {
|
||||
public extension Configuration.SemVar {
|
||||
|
||||
private func applyingPreRelease(_ semVar: SemVar, _ gitDirectory: String?) async throws -> SemVar {
|
||||
guard let preReleaseStrategy = self.preRelease else { return semVar }
|
||||
@Dependency(\.logger) var logger
|
||||
logger.trace("Start apply pre-release to: \(semVar)")
|
||||
guard let preReleaseStrategy = self.preRelease else {
|
||||
logger.trace("No pre-release strategy, returning original semvar.")
|
||||
return semVar
|
||||
}
|
||||
|
||||
let preRelease = try await preReleaseStrategy.preReleaseString(gitDirectory: gitDirectory)
|
||||
logger.trace("Pre-release string: \(preRelease)")
|
||||
|
||||
return semVar.applyingPreRelease(preRelease)
|
||||
}
|
||||
|
||||
func currentVersion(file: URL, gitDirectory: String? = nil) async throws -> CurrentVersionContainer.Version {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.gitClient) var gitClient
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let fileOutput = try? await fileClient.semVar(file: file, gitDirectory: gitDirectory)
|
||||
var semVar = fileOutput?.semVar
|
||||
|
||||
logger.trace("file output semvar: \(String(describing: semVar))")
|
||||
|
||||
let usesOptionalType = fileOutput?.usesOptionalType
|
||||
|
||||
if requireExistingFile {
|
||||
guard let semVar else {
|
||||
throw CliClientError.fileDoesNotExist(path: file.cleanFilePath)
|
||||
}
|
||||
// We parsed a semvar from the existing file, use it.
|
||||
if semVar != nil {
|
||||
return try await .semVar(
|
||||
applyingPreRelease(semVar, gitDirectory),
|
||||
applyingPreRelease(semVar!, gitDirectory),
|
||||
usesOptionalType: usesOptionalType ?? false
|
||||
)
|
||||
}
|
||||
|
||||
// Didn't have existing semVar loaded from file, so check for git-tag.
|
||||
if requireExistingFile {
|
||||
logger.debug("Failed to parse existing file, and caller requires it.")
|
||||
throw CliClientError.fileDoesNotExist(path: file.cleanFilePath)
|
||||
}
|
||||
|
||||
logger.trace("Does not require existing file, checking git-tag.")
|
||||
|
||||
// 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()
|
||||
}
|
||||
if semVar != nil {
|
||||
return try await .semVar(
|
||||
applyingPreRelease(semVar, gitDirectory),
|
||||
applyingPreRelease(semVar!, gitDirectory),
|
||||
usesOptionalType: usesOptionalType ?? false
|
||||
)
|
||||
}
|
||||
|
||||
if requireExistingSemVar {
|
||||
logger.trace("Caller requires existing semvar and it was not found in file or git-tag.")
|
||||
throw CliClientError.semVarNotFound
|
||||
}
|
||||
|
||||
// Semvar doesn't exist, so create a new one.
|
||||
logger.trace("Generating new semvar.")
|
||||
return try await .semVar(
|
||||
applyingPreRelease(.init(), gitDirectory),
|
||||
usesOptionalType: usesOptionalType ?? false
|
||||
@@ -143,6 +188,8 @@ extension Configuration.VersionStrategy {
|
||||
}
|
||||
|
||||
enum ConfigurationParsingError: Error {
|
||||
case targetNotFound
|
||||
case pathOrModuleNotSet
|
||||
case versionStrategyError(message: String)
|
||||
case versionNotFound
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ public extension FileClient {
|
||||
guard let versionString else {
|
||||
throw CliClientError.failedToParseVersionFile
|
||||
}
|
||||
logger.debug("Parsed version string: \(versionString)")
|
||||
return (String(versionString), isOptional)
|
||||
}
|
||||
|
||||
@@ -41,8 +42,12 @@ public extension FileClient {
|
||||
file: URL,
|
||||
gitDirectory: String?
|
||||
) async throws -> (semVar: SemVar?, usesOptionalType: Bool) {
|
||||
@Dependency(\.logger) var logger
|
||||
let (string, usesOptionalType) = try await getVersionString(fileUrl: file, gitDirectory: gitDirectory)
|
||||
return (SemVar(string: string), usesOptionalType)
|
||||
let semvar = SemVar(string: string)
|
||||
logger.debug("Semvar: \(String(describing: semvar))")
|
||||
|
||||
return (semvar, usesOptionalType)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,30 +6,6 @@ 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
|
||||
@@ -39,12 +15,12 @@ public extension CliClient.SharedOptions {
|
||||
} operation: {
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
let targetUrl = try await parseTargetUrl()
|
||||
let targetUrl = try configuration.targetUrl(gitDirectory: gitDirectory)
|
||||
logger.debug("Target: \(targetUrl.cleanFilePath)")
|
||||
|
||||
try await operation(
|
||||
versionStrategy.currentVersion(
|
||||
file: targetUrl,
|
||||
configuration.currentVersion(
|
||||
targetUrl: targetUrl,
|
||||
gitDirectory: gitDirectory
|
||||
)
|
||||
)
|
||||
@@ -124,9 +100,11 @@ extension CliClient.SharedOptions {
|
||||
|
||||
switch container.version {
|
||||
case .string: // When we did not parse a semVar, just write whatever we parsed for the current version.
|
||||
logger.debug("Failed to parse semvar, but got current version string.")
|
||||
try await write(container)
|
||||
|
||||
case let .semVar(semVar, usesOptionalType: usesOptionalType):
|
||||
logger.debug("Semvar prior to bumping: \(semVar)")
|
||||
let bumped = semVar.bump(type, preRelease: nil) // preRelease is already set on semVar.
|
||||
let version = bumped.versionString(withPreReleaseTag: allowPreReleaseTag)
|
||||
logger.debug("Bumped version: \(version)")
|
||||
|
||||
@@ -37,16 +37,30 @@ public struct SemVar: CustomStringConvertible, Equatable, Sendable {
|
||||
}
|
||||
|
||||
public init?(string: String) {
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
logger.trace("Parsing semvar from: \(string)")
|
||||
|
||||
let parts = string.split(separator: ".")
|
||||
logger.trace("parts: \(parts)")
|
||||
guard parts.count >= 3 else {
|
||||
return nil
|
||||
}
|
||||
let major = Int(String(parts[0].replacingOccurrences(of: "\"", with: "")))
|
||||
let minor = Int(String(parts[1]))
|
||||
|
||||
let patchParts = parts[2].split(separator: "-")
|
||||
let major = Int(String(parts[0].replacingOccurrences(of: "\"", with: "")))
|
||||
logger.trace("major: \(String(describing: major))")
|
||||
|
||||
let minor = Int(String(parts[1]))
|
||||
logger.trace("minor: \(String(describing: minor))")
|
||||
|
||||
let patchParts = parts[2].replacingOccurrences(of: "\"", with: "").split(separator: "-")
|
||||
logger.trace("patchParts: \(String(describing: patchParts))")
|
||||
|
||||
let patch = Int(patchParts.first ?? "0")
|
||||
logger.trace("patch: \(String(describing: patch))")
|
||||
|
||||
let preRelease = String(patchParts.dropFirst().joined(separator: "-"))
|
||||
logger.trace("preRelease: \(String(describing: preRelease))")
|
||||
|
||||
self.init(
|
||||
major: major ?? 0,
|
||||
|
||||
@@ -1,104 +1,104 @@
|
||||
import ConfigurationClient
|
||||
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)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
// import ConfigurationClient
|
||||
// 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)
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
61
Sources/cli-version/ConfigurationExtensions.swift
Normal file
61
Sources/cli-version/ConfigurationExtensions.swift
Normal file
@@ -0,0 +1,61 @@
|
||||
import ConfigurationClient
|
||||
import Dependencies
|
||||
import FileClient
|
||||
import Foundation
|
||||
|
||||
extension Configuration {
|
||||
|
||||
func mergingTarget(_ otherTarget: Configuration.Target?) -> Self {
|
||||
.init(
|
||||
target: otherTarget ?? target,
|
||||
strategy: strategy
|
||||
)
|
||||
}
|
||||
|
||||
func mergingStrategy(_ otherStrategy: Configuration.VersionStrategy?) -> Self {
|
||||
.init(
|
||||
target: target,
|
||||
strategy: strategy?.merging(otherStrategy)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension Configuration.Branch {
|
||||
func merging(_ other: Self?) -> Self {
|
||||
return .init(includeCommitSha: other?.includeCommitSha ?? includeCommitSha)
|
||||
}
|
||||
}
|
||||
|
||||
extension Configuration.SemVar {
|
||||
func merging(_ other: Self?) -> Self {
|
||||
.init(
|
||||
preRelease: other?.preRelease ?? preRelease,
|
||||
requireExistingFile: other?.requireExistingFile ?? requireExistingFile,
|
||||
requireExistingSemVar: other?.requireExistingSemVar ?? requireExistingSemVar
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension Configuration.VersionStrategy {
|
||||
func merging(_ other: Self?) -> Self {
|
||||
guard let branch else {
|
||||
guard let semvar else { return self }
|
||||
return .init(semvar: semvar.merging(other?.semvar))
|
||||
}
|
||||
return .init(branch: branch.merging(other?.branch))
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func withConfiguration<T>(
|
||||
path: String?,
|
||||
_ operation: (Configuration) async throws -> T
|
||||
) async throws -> T {
|
||||
@Dependency(\.configurationClient) var configurationClient
|
||||
|
||||
let configuration = try await configurationClient.findAndLoad(
|
||||
path != nil ? URL(filePath: path!) : nil
|
||||
)
|
||||
|
||||
return try await operation(configuration)
|
||||
}
|
||||
@@ -7,6 +7,12 @@ import Rainbow
|
||||
|
||||
struct GlobalOptions<Child: ParsableArguments>: ParsableArguments {
|
||||
|
||||
@Option(
|
||||
name: .shortAndLong,
|
||||
help: "Specify the path to a configuration file."
|
||||
)
|
||||
var configurationFile: String?
|
||||
|
||||
@OptionGroup var targetOptions: TargetOptions
|
||||
@OptionGroup var child: Child
|
||||
|
||||
@@ -71,7 +77,7 @@ struct PreReleaseOptions: ParsableArguments {
|
||||
var useTagAsPreRelease: Bool = false
|
||||
|
||||
@Option(
|
||||
name: .shortAndLong,
|
||||
name: .long,
|
||||
help: """
|
||||
Apply custom pre-release suffix, can also use branch or tag along with this
|
||||
option as a prefix, used if branch is not set. (example: \"rc\")
|
||||
@@ -103,14 +109,18 @@ typealias GlobalSemVarOptions = GlobalOptions<SemVarOptions>
|
||||
typealias GlobalBranchOptions = GlobalOptions<Empty>
|
||||
|
||||
extension GlobalSemVarOptions {
|
||||
func shared() throws -> CliClient.SharedOptions {
|
||||
try shared(.semVar(child.semVarOptions()))
|
||||
func shared() async throws -> CliClient.SharedOptions {
|
||||
try await withConfiguration(path: configurationFile) { configuration in
|
||||
try shared(configuration.mergingStrategy(.init(semvar: child.configSemVarOptions())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension GlobalBranchOptions {
|
||||
func shared() throws -> CliClient.SharedOptions {
|
||||
try shared(.branchAndCommit)
|
||||
func shared() async throws -> CliClient.SharedOptions {
|
||||
try await withConfiguration(path: configurationFile) { configuration in
|
||||
try shared(configuration.mergingStrategy(.init(branch: .init())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,13 +156,12 @@ extension CliClient.SharedOptions {
|
||||
|
||||
extension GlobalOptions {
|
||||
|
||||
func shared(_ versionStrategy: CliClient.VersionStrategy) throws -> CliClient.SharedOptions {
|
||||
try .init(
|
||||
func shared(_ configuration: Configuration) throws -> CliClient.SharedOptions {
|
||||
.init(
|
||||
dryRun: dryRun,
|
||||
gitDirectory: gitDirectory,
|
||||
logLevel: .init(verbose: verbose),
|
||||
target: targetOptions.target(),
|
||||
versionStrategy: versionStrategy
|
||||
configuration: configuration
|
||||
)
|
||||
}
|
||||
|
||||
@@ -188,26 +197,6 @@ private extension TargetOptions {
|
||||
|
||||
extension PreReleaseOptions {
|
||||
|
||||
func preReleaseStrategy() throws -> CliClient.PreReleaseStrategy? {
|
||||
guard let custom else {
|
||||
if useBranchAsPreRelease {
|
||||
return .branchAndCommit
|
||||
} else if useTagAsPreRelease {
|
||||
return .tag
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if useBranchAsPreRelease {
|
||||
return .custom(custom, .branchAndCommit)
|
||||
} else if useTagAsPreRelease {
|
||||
return .custom(custom, .tag)
|
||||
} else {
|
||||
return .custom(custom, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func configPreReleaseStrategy() throws -> Configuration.PreReleaseStrategy? {
|
||||
guard let custom else {
|
||||
if useBranchAsPreRelease {
|
||||
@@ -230,13 +219,6 @@ extension PreReleaseOptions {
|
||||
}
|
||||
|
||||
extension SemVarOptions {
|
||||
func semVarOptions() throws -> CliClient.VersionStrategy.SemVarOptions {
|
||||
try .init(
|
||||
preReleaseStrategy: preRelease.preReleaseStrategy(),
|
||||
requireExistingFile: requireExistingFile,
|
||||
requireExistingSemVar: requireExistingSemvar
|
||||
)
|
||||
}
|
||||
|
||||
func configSemVarOptions() throws -> Configuration.SemVar {
|
||||
try .init(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
@_spi(Internal) import CliClient
|
||||
import ConfigurationClient
|
||||
import Dependencies
|
||||
import FileClient
|
||||
import Foundation
|
||||
@@ -20,7 +21,7 @@ struct CliClientTests {
|
||||
@Dependency(\.cliClient) var client
|
||||
let output = try await client.build(.testOptions(
|
||||
target: target,
|
||||
versionStrategy: .semVar(.init(requireExistingFile: false))
|
||||
versionStrategy: .init(semvar: .init(requireExistingFile: false))
|
||||
))
|
||||
#expect(output == "/baz/Sources/bar/Version.swift")
|
||||
}
|
||||
@@ -65,7 +66,7 @@ struct CliClientTests {
|
||||
@Dependency(\.cliClient) var client
|
||||
let output = try await client.build(.testOptions(
|
||||
target: target,
|
||||
versionStrategy: .semVar(.init(requireExistingFile: false))
|
||||
versionStrategy: .init(semvar: .init(requireExistingFile: false))
|
||||
))
|
||||
#expect(output == "/baz/Sources/bar/Version.swift")
|
||||
}
|
||||
@@ -113,7 +114,7 @@ struct CliClientTests {
|
||||
}
|
||||
|
||||
enum TestArguments {
|
||||
static let testCases = ["bar", "Sources/bar", "/Sources/bar", "./Sources/bar"]
|
||||
static let testCases = ["bar", "Sources/bar", "./Sources/bar"]
|
||||
static let bumpCases = CliClient.BumpOption.allCases.reduce(into: [(CliClient.BumpOption, Bool)]()) {
|
||||
$0.append(($1, true))
|
||||
$0.append(($1, false))
|
||||
@@ -130,14 +131,16 @@ extension CliClient.SharedOptions {
|
||||
dryRun: Bool = false,
|
||||
target: String = "bar",
|
||||
logLevel: Logger.Level = .trace,
|
||||
versionStrategy: CliClient.VersionStrategy = .semVar(.init())
|
||||
versionStrategy: Configuration.VersionStrategy = .init(semvar: .init())
|
||||
) -> Self {
|
||||
.init(
|
||||
dryRun: dryRun,
|
||||
gitDirectory: gitDirectory,
|
||||
logLevel: logLevel,
|
||||
target: target,
|
||||
versionStrategy: versionStrategy
|
||||
configuration: .init(
|
||||
target: .init(module: .init(target)),
|
||||
strategy: versionStrategy
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user