feat: Adds git client tests, restructures files.
Some checks failed
CI / macOS (debug, 16.1) (push) Has been cancelled
CI / macOS (release, 16.1) (push) Has been cancelled
CI / Ubuntu (push) Failing after 5s

This commit is contained in:
2024-12-24 14:42:28 -05:00
parent 8aa4b73cab
commit 1972260317
26 changed files with 454 additions and 397 deletions

View File

@@ -27,6 +27,9 @@ public struct CliClient: Sendable {
/// Generate a version file with an optional version that can be set manually.
public var generate: @Sendable (SharedOptions) async throws -> String
/// Parse the configuration options.
public var parsedConfiguration: @Sendable (SharedOptions) async throws -> Configuration
public enum BumpOption: Sendable, CaseIterable {
case major, minor, patch, preRelease
}
@@ -94,7 +97,10 @@ extension CliClient: DependencyKey {
.init(
build: { try await $0.build(environment) },
bump: { try await $1.bump($0) },
generate: { try await $0.generate() }
generate: { try await $0.generate() },
parsedConfiguration: { options in
try await options.withMergedConfiguration { $0 }
}
)
}

View File

@@ -7,25 +7,25 @@ Learn how to integrate the plugins into your project
Use the plugins by including as a package to your project and declaring in the `plugins` section of
your target.
> Note: You must use swift-tools version 5.6 or greater for package plugins and
> target `macOS(.v10_15)` or greater.
> Note: You must use swift-tools version 5.6 or greater for package plugins and target `macOS(.v13)`
> or greater.
```swift
// swift-tools-version: 5.7
// swift-tools-version: 5.10
import PackageDescription
let package = Package(
platforms:[.macOS(.v10_15)],
platforms:[.macOS(.v13)],
dependencies: [
...,
.package(url: "https://github.com/m-housh/swift-cli-version.git", from: "0.1.0")
.package(url: "https://github.com/m-housh/swift-cli-version.git", from: "0.2.0")
],
targets: [
.executableTarget(
name: "<target name>",
dependencies: [...],
plugins: [
plugins: [
.plugin(name: "BuildWithVersionPlugin", package: "swift-cli-version")
]
)
@@ -33,8 +33,8 @@ let package = Package(
)
```
The above example uses the build tool plugin. The `BuildWithVersionPlugin` will give you access
to a `VERSION` variable in your project that you can use to supply the version of the tool.
The above example uses the build tool plugin. The `BuildWithVersionPlugin` will give you access to a
`VERSION` variable in your project that you can use to supply the version of the tool.
### Example
@@ -42,27 +42,27 @@ to a `VERSION` variable in your project that you can use to supply the version o
import ArgumentParser
@main
struct MyCliTool: ParsableCommand {
struct MyCliTool: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "My awesome cli tool",
version: VERSION
)
func run() throws {
func run() throws {
print("Version: \(VERSION)")
}
}
```
After you enable the plugin, you will have access to the `VERSION` string variable even though it is
After you enable the plugin, you will have access to the `VERSION` string variable even though it is
not declared in your source files.
![Trust & Enable](trust)
> Note: If your `DerivedData` folder lives in a directory that is a mounted volume / or somewhere
> that is not under your home folder then you may get build failures using the build tool
> plugin, it will work if you build from the command line and pass the `--disable-sandbox` flag to the
> build command or use one of the manual methods.
> that is not under your home folder then you may get build failures using the build tool plugin, it
> will work if you build from the command line and pass the `--disable-sandbox` flag to the build
> command or use one of the manual methods.
## See Also

View File

@@ -1,6 +1,6 @@
# ``CliVersion``
# CliClient
Derive a version for a command line tool from git tags or a git sha.
Derive a version for a command-line tool from git tags or a git sha.
## Additional Resources
@@ -9,15 +9,10 @@ Derive a version for a command line tool from git tags or a git sha.
## Overview
This tool exposes several plugins that can be used to derive a version for a command line program at
build time or by manually running the plugin. The version is derived from git tags and falling back to
the branch and git sha if a tag is not set for the current worktree state.
build time or by manually running the plugin. The version is derived from git tags and falling back
to the branch and git sha if a tag is not set for the current worktree state.
## Articles
- <doc:GettingStarted>
- <doc:ManualPlugins>
## Api
- ``FileClient``
- ``GitVersionClient``

View File

@@ -7,6 +7,25 @@ import GitClient
@_spi(Internal)
public extension CliClient.SharedOptions {
// Merges any configuration set via the passed in options.
@discardableResult
func withMergedConfiguration<T>(
operation: (Configuration) async throws -> T
) async throws -> T {
try await withConfiguration(path: configurationFile) { configuration in
var configuration = configuration
configuration = configuration.mergingTarget(target)
if configuration.strategy?.branch != nil, let branch {
configuration = configuration.mergingStrategy(.branch(branch))
} else if let semvar {
configuration = configuration.mergingStrategy(.semvar(semvar))
}
return try await operation(configuration)
}
}
@discardableResult
func run(
_ operation: (CurrentVersionContainer) async throws -> Void
@@ -15,19 +34,9 @@ public extension CliClient.SharedOptions {
$0.logger.logLevel = logLevel
} operation: {
// Load the default configuration, if it exists.
try await withConfiguration(path: configurationFile) { configuration in
try await withMergedConfiguration { configuration in
@Dependency(\.logger) var logger
// Merge any configuration set from caller into default configuration.
var configuration = configuration
configuration = configuration.mergingTarget(target)
if configuration.strategy?.branch != nil, let branch {
configuration = configuration.mergingStrategy(.branch(branch))
} else if let semvar {
configuration = configuration.mergingStrategy(.semvar(semvar))
}
logger.debug("Configuration: \(configuration)")
// This will fail if the target url is not set properly.