From 016f0d6c3f07f320ede482485b32e9435ccef72f Mon Sep 17 00:00:00 2001 From: Michael Housh Date: Tue, 31 Dec 2024 08:31:41 -0500 Subject: [PATCH] feat: Some parameter renaming for consistency --- Package.swift | 1 + Sources/CliClient/CliClient.swift | 2 +- .../CliClient/Internal/CliClient+run.swift | 26 +++++++++----- .../Internal/Configuration+targetUrl.swift | 10 +++--- .../Internal/FileClient+semVar.swift | 10 +++--- .../CliClient/Internal/VersionContainer.swift | 35 ++++++------------- 6 files changed, 39 insertions(+), 45 deletions(-) diff --git a/Package.swift b/Package.swift index d9788e0..b5261de 100644 --- a/Package.swift +++ b/Package.swift @@ -55,6 +55,7 @@ let package = Package( name: "ConfigurationClient", dependencies: [ "FileClient", + "LoggingExtensions", .product(name: "CustomDump", package: "swift-custom-dump"), .product(name: "Dependencies", package: "swift-dependencies"), .product(name: "DependenciesMacros", package: "swift-dependencies") diff --git a/Sources/CliClient/CliClient.swift b/Sources/CliClient/CliClient.swift index 9054a8b..9fbb92e 100644 --- a/Sources/CliClient/CliClient.swift +++ b/Sources/CliClient/CliClient.swift @@ -24,7 +24,7 @@ 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 diff --git a/Sources/CliClient/Internal/CliClient+run.swift b/Sources/CliClient/Internal/CliClient+run.swift index a63e337..14eab29 100644 --- a/Sources/CliClient/Internal/CliClient+run.swift +++ b/Sources/CliClient/Internal/CliClient+run.swift @@ -29,7 +29,7 @@ extension CliClient.SharedOptions { } // This will fail if the target url is not set properly. - let targetUrl = try configuration.targetUrl(gitDirectory: projectDirectory) + let targetUrl = try configuration.targetUrl(projectDirectory: projectDirectory) logger.debug("Target: \(targetUrl.cleanFilePath)") // Perform the operation, which generates the new version and writes it. @@ -82,19 +82,19 @@ extension CliClient.SharedOptions { @Dependency(\.logger) var logger logger.trace("Begin writing version.") - let hasChanges: Bool + // let hasChanges: Bool let targetUrl: URL let usesOptionalType: Bool let versionString: String? switch currentVersion { case let .branch(branch): - hasChanges = branch.hasChanges + // hasChanges = branch.hasChanges targetUrl = branch.targetUrl usesOptionalType = branch.usesOptionalType versionString = branch.versionString case let .semvar(semvar): - hasChanges = semvar.hasChanges + // hasChanges = semvar.hasChanges targetUrl = semvar.targetUrl usesOptionalType = semvar.usesOptionalType versionString = semvar.versionString(withPreRelease: allowPreReleaseTag) @@ -132,11 +132,7 @@ extension CliClient.SharedOptions { } } - func bump(_ type: CliClient.BumpOption?) async throws -> String { - guard let type else { - return try await generate() - } - + func bump(_ type: CliClient.BumpOption) async throws -> String { return try await run { container in @Dependency(\.logger) var logger @@ -175,3 +171,15 @@ extension CliClient.SharedOptions { } } } + +private extension CurrentVersionContainer where Version == SemVar { + func withUpdateNextVersion(_ next: SemVar) -> Self { + .init( + targetUrl: targetUrl, + usesOptionalType: usesOptionalType, + loadedVersion: loadedVersion, + precedence: .strategy, // make sure to use the next version, since it was specified, as this is called from `bump`. + strategyVersion: next + ) + } +} diff --git a/Sources/CliClient/Internal/Configuration+targetUrl.swift b/Sources/CliClient/Internal/Configuration+targetUrl.swift index 2dae3a7..a7f724b 100644 --- a/Sources/CliClient/Internal/Configuration+targetUrl.swift +++ b/Sources/CliClient/Internal/Configuration+targetUrl.swift @@ -6,16 +6,16 @@ import GitClient import ShellClient extension Configuration { - func targetUrl(gitDirectory: String?) throws -> URL { + func targetUrl(projectDirectory: String?) throws -> URL { guard let target else { throw ConfigurationParsingError.targetNotFound } - return try target.url(gitDirectory: gitDirectory) + return try target.url(projectDirectory: projectDirectory) } } private extension Configuration.Target { - func url(gitDirectory: String?) throws -> URL { + func url(projectDirectory: String?) throws -> URL { @Dependency(\.logger) var logger let filePath: String @@ -42,8 +42,8 @@ private extension Configuration.Target { filePath = "\(path)/\(module.fileNameOrDefault)" } - if let gitDirectory { - return URL(filePath: "\(gitDirectory)/\(filePath)") + if let projectDirectory { + return URL(filePath: "\(projectDirectory)/\(filePath)") } return URL(filePath: filePath) } diff --git a/Sources/CliClient/Internal/FileClient+semVar.swift b/Sources/CliClient/Internal/FileClient+semVar.swift index c22da33..2c5c4b8 100644 --- a/Sources/CliClient/Internal/FileClient+semVar.swift +++ b/Sources/CliClient/Internal/FileClient+semVar.swift @@ -7,10 +7,10 @@ import GitClient public extension FileClient { func branch( file: URL, - gitDirectory: String?, + projectDirectory: String?, requireExistingFile: Bool ) async throws -> (string: String, usesOptionalType: Bool)? { - let loaded = try? await getVersionString(fileUrl: file, gitDirectory: gitDirectory) + let loaded = try? await getVersionString(fileUrl: file, projectDirectory: projectDirectory) guard let loaded else { if requireExistingFile { throw CliClientError.fileDoesNotExist(path: file.cleanFilePath) @@ -22,10 +22,10 @@ public extension FileClient { func semvar( file: URL, - gitDirectory: String?, + projectDirectory: String?, requireExistingFile: Bool ) async throws -> (semVar: SemVar?, usesOptionalType: Bool)? { - let loaded = try? await getVersionString(fileUrl: file, gitDirectory: gitDirectory) + let loaded = try? await getVersionString(fileUrl: file, projectDirectory: projectDirectory) guard let loaded else { if requireExistingFile { throw CliClientError.fileDoesNotExist(path: file.cleanFilePath) @@ -38,7 +38,7 @@ public extension FileClient { private func getVersionString( fileUrl: URL, - gitDirectory: String? + projectDirectory: String? ) async throws -> (version: String, usesOptionalType: Bool) { @Dependency(\.gitClient) var gitClient @Dependency(\.logger) var logger diff --git a/Sources/CliClient/Internal/VersionContainer.swift b/Sources/CliClient/Internal/VersionContainer.swift index 9a312a1..86e82ae 100644 --- a/Sources/CliClient/Internal/VersionContainer.swift +++ b/Sources/CliClient/Internal/VersionContainer.swift @@ -18,13 +18,13 @@ enum VersionContainer: Sendable { case let .branch(includeCommitSha: includeCommitSha): return try await .branch(.load( branch: .init(includeCommitSha: includeCommitSha), - gitDirectory: projectDirectory, + projectDirectory: projectDirectory, url: url )) case .semvar: return try await .semvar(.load( semvar: strategy.semvar!, - gitDirectory: projectDirectory, + projectDirectory: projectDirectory, url: url )) } @@ -62,7 +62,7 @@ extension CurrentVersionContainer where Version == String { static func load( branch: Configuration.Branch, - gitDirectory: String?, + projectDirectory: String?, url: URL ) async throws -> Self { @Dependency(\.fileClient) var fileClient @@ -70,12 +70,12 @@ extension CurrentVersionContainer where Version == String { let loaded = try await fileClient.branch( file: url, - gitDirectory: gitDirectory, + projectDirectory: projectDirectory, requireExistingFile: false ) let next = try await gitClient.version(.init( - gitDirectory: gitDirectory, + gitDirectory: projectDirectory, style: .branch(commitSha: branch.includeCommitSha) )) @@ -95,13 +95,8 @@ extension CurrentVersionContainer where Version == String { extension CurrentVersionContainer where Version == SemVar { - // var preferredSemvar: SemVar? { - // switch precedence { - // } - // } - // TODO: Update to use precedence and not fetch `nextVersion` if we loaded a file version. - static func load(semvar: Configuration.SemVar, gitDirectory: String?, url: URL) async throws -> Self { + static func load(semvar: Configuration.SemVar, projectDirectory: String?, url: URL) async throws -> Self { @Dependency(\.fileClient) var fileClient @Dependency(\.logger) var logger @@ -109,10 +104,10 @@ extension CurrentVersionContainer where Version == SemVar { async let (loaded, usesOptionalType) = try await loadCurrentVersion( semvar: semvar, - gitDirectory: gitDirectory, + projectDirectory: projectDirectory, url: url ) - async let next = try await loadNextVersion(semvar: semvar, projectDirectory: gitDirectory) + async let next = try await loadNextVersion(semvar: semvar, projectDirectory: projectDirectory) return try await .init( targetUrl: url, @@ -125,7 +120,7 @@ extension CurrentVersionContainer where Version == SemVar { static func loadCurrentVersion( semvar: Configuration.SemVar, - gitDirectory: String?, + projectDirectory: String?, url: URL ) async throws -> (SemVar?, Bool) { @Dependency(\.fileClient) var fileClient @@ -135,7 +130,7 @@ extension CurrentVersionContainer where Version == SemVar { let loadedOptional = try await fileClient.semvar( file: url, - gitDirectory: gitDirectory, + projectDirectory: projectDirectory, requireExistingFile: semvar.requireExistingFile ?? false ) guard let loadedStrong = loadedOptional else { @@ -174,14 +169,4 @@ extension CurrentVersionContainer where Version == SemVar { return version?.versionString(withPreReleaseTag: withPreRelease) } - // TODO: Move to where `bump` is declared and make fileprivate. - func withUpdateNextVersion(_ next: SemVar) -> Self { - .init( - targetUrl: targetUrl, - usesOptionalType: usesOptionalType, - loadedVersion: loadedVersion, - precedence: .strategy, // make sure to use the next version, since it was specified, as this is called from `bump`. - strategyVersion: next - ) - } }