feat: Renames some options, adds a require-configuration option that fails if configuration is not found.
All checks were successful
CI / Ubuntu (push) Successful in 2m19s

This commit is contained in:
2024-12-27 16:42:48 -05:00
parent 4420bd428a
commit 20f430fb8f
16 changed files with 133 additions and 154 deletions

View File

@@ -36,35 +36,46 @@ public struct CliClient: Sendable {
case major, minor, patch, preRelease
}
/// Represents options that are used by all the commands.
public struct SharedOptions: Equatable, Sendable {
/// Whether to allow pre-release suffixes.
let allowPreReleaseTag: Bool
/// Flag on if we write to files or not.
let dryRun: Bool
let gitDirectory: String?
/// Specify a path to the project directory.
let projectDirectory: String?
/// The logging options to use.
let loggingOptions: LoggingOptions
let target: Configuration.Target?
let branch: Configuration.Branch?
let semvar: Configuration.SemVar?
/// Configuration that gets merged with the loaded (or default) configuration.
let configurationToMerge: Configuration?
/// Path to the configuration file to load.
let configurationFile: String?
/// Fail if a configuration file is not found.
let requireConfigurationFile: Bool
public init(
allowPreReleaseTag: Bool = true,
dryRun: Bool = false,
gitDirectory: String? = nil,
projectDirectory: String? = nil,
loggingOptions: LoggingOptions,
target: Configuration.Target? = nil,
branch: Configuration.Branch? = nil,
semvar: Configuration.SemVar? = nil,
configurationFile: String? = nil
configurationToMerge: Configuration? = nil,
configurationFile: String? = nil,
requireConfigurationFile: Bool = false
) {
self.allowPreReleaseTag = allowPreReleaseTag
self.dryRun = dryRun
self.gitDirectory = gitDirectory
self.projectDirectory = projectDirectory
self.loggingOptions = loggingOptions
self.target = target
self.branch = branch
self.semvar = semvar
self.configurationFile = configurationFile
self.configurationToMerge = configurationToMerge
self.requireConfigurationFile = requireConfigurationFile
}
}

View File

@@ -37,7 +37,7 @@ to come after the plugin name.
| Option | Description |
| ---------- | --------------------------------------------------------------------- |
| --dry-run | Do not write to any files, but describe where values would be written |
| --print | Do not write to any files, but describe where values would be written |
| --filename | Override the file name to be written in the target directory |
| --verbose | Increase the logging output |
@@ -47,7 +47,7 @@ to come after the plugin name.
swift package \
--allow-writing-to-package-directory \
generate-version \
--dry-run \
--print \
--verbose \
<target>
```

View File

@@ -25,14 +25,14 @@ public extension CliClient.SharedOptions {
logger.trace("\nConfiguration: \(configurationString)")
// This will fail if the target url is not set properly.
let targetUrl = try configuration.targetUrl(gitDirectory: gitDirectory)
let targetUrl = try configuration.targetUrl(gitDirectory: projectDirectory)
logger.debug("Target: \(targetUrl.cleanFilePath)")
// Perform the operation, which generates the new version and writes it.
try await operation(
configuration.currentVersion(
targetUrl: targetUrl,
gitDirectory: gitDirectory
gitDirectory: projectDirectory
)
)
@@ -50,27 +50,20 @@ public extension CliClient.SharedOptions {
@Dependency(\.configurationClient) var configurationClient
@Dependency(\.logger) var logger
var strategy: Configuration.VersionStrategy?
if let branch {
if configurationToMerge?.strategy?.branch != nil {
logger.trace("Merging branch strategy.")
strategy = .branch(branch)
} else if let semvar {
// strategy = .branch(branch)
} else if let semvar = configurationToMerge?.strategy?.semvar {
logger.trace("Merging semvar strategy.")
var semvarString = ""
customDump(semvar, to: &semvarString)
logger.trace("\(semvarString)")
strategy = .semvar(semvar)
}
let configuration = Configuration(
target: target,
strategy: strategy
)
return try await configurationClient.withConfiguration(
path: configurationFile,
merging: configuration,
merging: configurationToMerge,
strict: requireConfigurationFile,
operation: operation
)
}
@@ -82,7 +75,7 @@ public extension CliClient.SharedOptions {
try await fileClient.write(string: string, to: url)
} else {
logger.debug("Skipping, due to dry-run being passed.")
logger.info("\n\(string)\n")
// logger.info("\n\(string)\n")
}
}
@@ -90,7 +83,11 @@ public extension CliClient.SharedOptions {
@Dependency(\.logger) var logger
let version = try currentVersion.version.string(allowPreReleaseTag: allowPreReleaseTag)
logger.debug("Version: \(version)")
if !dryRun {
logger.debug("Version: \(version)")
} else {
logger.info("Version: \(version)")
}
let template = currentVersion.usesOptionalType ? Template.optional(version) : Template.nonOptional(version)
logger.trace("Template string: \(template)")
@@ -160,6 +157,12 @@ extension CliClient.SharedOptions {
}
logger.debug("Bumped version: \(version)")
if dryRun {
logger.info("Version: \(version)")
return
}
let template = usesOptionalType ? Template.optional(version) : Template.build(version)
try await write(template, to: container.targetUrl)
}