Renamed to cli version.

This commit is contained in:
2023-03-15 12:44:52 -04:00
parent 8ebfa1975a
commit 742546ec08
21 changed files with 197 additions and 56 deletions

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Michael Housh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -1,4 +1,4 @@
DOCC_TARGET ?= GitVersion DOCC_TARGET ?= CliVersion
DOCC_BASEPATH = $(shell basename "$(PWD)") DOCC_BASEPATH = $(shell basename "$(PWD)")
DOCC_DIR ?= ./docs DOCC_DIR ?= ./docs

View File

@@ -86,8 +86,8 @@
"kind" : "remoteSourceControl", "kind" : "remoteSourceControl",
"location" : "https://github.com/m-housh/swift-shell-client.git", "location" : "https://github.com/m-housh/swift-shell-client.git",
"state" : { "state" : {
"revision" : "28bcd2ecd0022aca20c520fc578a130bec7283e7", "revision" : "bcd7a06377d43bac71c086d97d82e6c18504277e",
"version" : "0.1.0" "version" : "0.1.3"
} }
}, },
{ {

View File

@@ -3,44 +3,44 @@
import PackageDescription import PackageDescription
let package = Package( let package = Package(
name: "swift-git-version", name: "swift-cli-version",
platforms: [ platforms: [
.macOS(.v12) .macOS(.v10_15)
], ],
products: [ products: [
.library(name: "GitVersion", targets: ["GitVersion"]), .library(name: "CliVersion", targets: ["CliVersion"]),
.plugin(name: "BuildWithVersionPlugin", targets: ["BuildWithVersionPlugin"]), .plugin(name: "BuildWithVersionPlugin", targets: ["BuildWithVersionPlugin"]),
.plugin(name: "GenerateVersionPlugin", targets: ["GenerateVersionPlugin"]), .plugin(name: "GenerateVersionPlugin", targets: ["GenerateVersionPlugin"]),
.plugin(name: "UpdateVersionPlugin", targets: ["UpdateVersionPlugin"]) .plugin(name: "UpdateVersionPlugin", targets: ["UpdateVersionPlugin"])
], ],
dependencies: [ dependencies: [
.package(url: "https://github.com/m-housh/swift-shell-client.git", from: "0.1.0"), .package(url: "https://github.com/m-housh/swift-shell-client.git", from: "0.1.3"),
.package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.0.0"), .package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2") .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.2.2")
], ],
targets: [ targets: [
.executableTarget( .executableTarget(
name: "git-version", name: "cli-version",
dependencies: [ dependencies: [
"GitVersion", "CliVersion",
.product(name: "ArgumentParser", package: "swift-argument-parser") .product(name: "ArgumentParser", package: "swift-argument-parser")
] ]
), ),
.target( .target(
name: "GitVersion", name: "CliVersion",
dependencies: [ dependencies: [
.product(name: "ShellClient", package: "swift-shell-client") .product(name: "ShellClient", package: "swift-shell-client")
] ]
), ),
.testTarget( .testTarget(
name: "GitVersionTests", name: "CliVersionTests",
dependencies: ["GitVersion"] dependencies: ["CliVersion"]
), ),
.plugin( .plugin(
name: "BuildWithVersionPlugin", name: "BuildWithVersionPlugin",
capability: .buildTool(), capability: .buildTool(),
dependencies: [ dependencies: [
"git-version" "cli-version"
] ]
), ),
.plugin( .plugin(
@@ -55,7 +55,7 @@ let package = Package(
] ]
), ),
dependencies: [ dependencies: [
"git-version" "cli-version"
] ]
), ),
.plugin( .plugin(
@@ -70,7 +70,7 @@ let package = Package(
] ]
), ),
dependencies: [ dependencies: [
"git-version" "cli-version"
] ]
) )
] ]

View File

@@ -13,7 +13,7 @@ struct GenerateVersionBuildPlugin: BuildToolPlugin {
.removingLastComponent() .removingLastComponent()
.removingLastComponent() .removingLastComponent()
let tool = try context.tool(named: "git-version") let tool = try context.tool(named: "cli-version")
let outputPath = context.pluginWorkDirectory let outputPath = context.pluginWorkDirectory
let outputFile = outputPath.appending("Version.swift") let outputFile = outputPath.appending("Version.swift")

View File

@@ -5,7 +5,7 @@ import Foundation
struct GenerateVersionPlugin: CommandPlugin { struct GenerateVersionPlugin: CommandPlugin {
func performCommand(context: PluginContext, arguments: [String]) async throws { func performCommand(context: PluginContext, arguments: [String]) async throws {
let gitVersion = try context.tool(named: "git-version") let gitVersion = try context.tool(named: "cli-version")
let arguments = ["generate"] + arguments let arguments = ["generate"] + arguments

View File

@@ -5,7 +5,7 @@ import Foundation
struct UpdateVersionPlugin: CommandPlugin { struct UpdateVersionPlugin: CommandPlugin {
func performCommand(context: PluginContext, arguments: [String]) async throws { func performCommand(context: PluginContext, arguments: [String]) async throws {
let gitVersion = try context.tool(named: "git-version") let gitVersion = try context.tool(named: "cli-version")
let arguments = ["update"] + arguments let arguments = ["update"] + arguments

View File

@@ -1,41 +1,74 @@
# swift-git-version # swift-git-version
A swift package that exposes some helpers to set the version of a command line tool to the A swift package that exposes some plugins to set the version of a command line tool to the
git tag or the git sha, if a tag is not set for the current commit. git tag or the git sha, if a tag is not set for the current commit.
## Usage [Github Repo](https://github.com/m-housh/swift-cli-version)
[Documentation](https://m-housh.github.io/swift-cli-version/documentation/cliversion)
You can use this in your command line tool via the swift package manager. ## Overview
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.
```swift ```swift
// swift-tools-version: 5.7
import PackageDescription
let package = Package( let package = Package(
... platforms:[.macOS(.v10_15)],
dependencies: [ dependencies: [
.package(url: "https://github.com/m-housh/swift-git-version.git", from: "0.1.0") ...,
.package(url: "https://github.com/m-housh/swift-cli-version.git", from: "0.1.0")
], ],
targets: [ targets: [
.executableTarget( .executableTarget(
name: "my-executable", name: "<target name>",
dependencies: [ dependencies: [...],
... plugins: [
.plugin(name: "BuildWithVersionPlugin", package: "swift-cli-version")
] ]
), )
.executableTarget(
name: "my-executable-builder",
dependencies: [
.product(name: "GitVersion", package: "swift-git-version")
]
),
] ]
) )
``` ```
Inside of your executable (`my-executable`) in the above example, you will want to create The above example uses the build tool plugin. The `BuildWithVersionPlugin` will give you access
a file that contains your version variable. to a `VERSION` variable in your project that you can use to supply the version of the tool.
### Example
```swift ```swift
// Do not set this variable, it is set by the build script. import ArgumentParser
let VERSION: String? = nil @main
struct MyCliTool: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "My awesome cli tool",
version: VERSION
)
func run() throws {
print("Version: \(VERSION)")
}
}
``` ```
After you enable the plugin, you will have access to the `VERSION` string variable even though it is
not declared in your source files.
## Documentation
You can view the latest [documentation here](https://m-housh.github.io/swift-cli-version/documentation/cliversion).
## Dependencies
This project relys on the following dependencies:
[swift-argument-parser](https://github.com/apple/swift-argument-parser)
[swift-dependencies](https://github.com/pointfreeco/swift-dependencies)
[swift-shell-client](https://github.com/m-housh/swift-shell-client)

View File

@@ -7,19 +7,26 @@ 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 Use the plugins by including as a package to your project and declaring in the `plugins` section of
your target. your target.
> Note: You must use swift-tools version 5.6 or greater for package plugins and
> target `macOS(.v10_15)` or greater.
```swift ```swift
// swift-tools-version: 5.7
import PackageDescription
let package = Package( let package = Package(
..., platforms:[.macOS(.v10_15)],
dependencies: [ dependencies: [
..., ...,
.package(url: "https://github.com/m-housh/swift-git-version.git", from: "0.1.0") .package(url: "https://github.com/m-housh/swift-cli-version.git", from: "0.1.0")
], ],
targets: [ targets: [
.executableTarget( .executableTarget(
name: "<target name>", name: "<target name>",
dependencies: [...], dependencies: [...],
plugins: [ plugins: [
.plugin(name: "BuildWithVersionPlugin", package: "swift-git-version") .plugin(name: "BuildWithVersionPlugin", package: "swift-cli-version")
] ]
) )
] ]
@@ -47,5 +54,16 @@ struct MyCliTool: ParsableCommand {
} }
``` ```
You will have access to the `VERSION` string variable even though it is not declared in your source After you enable the plugin, you will have access to the `VERSION` string variable even though it is
files. 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.
## See Also
<doc:ManualPlugins>

View File

@@ -0,0 +1,68 @@
# Manual Plugins
There are two plugins that are included that can be ran manually, if the build tool plugin does not fit
your use case.
## Generate Version
The `generate-version` plugin will create a `Version.swift` file in the given target. You can
run it by running the following command.
```bash
swift package --disable-sandbox \
--allow-writing-to-package-directory \
generate-version \
<target>
```
> Note: If using the manual version then the `VERSION` variable is an optional string that will be
> `nil`, allowing you to run the `update-version` command in your build pipeline.
## Update Version
The `update-version` plugin can be ran in your build pipeline process to set the version prior to
building for distribution.
```bash
swift package --disable-sandbox \
--allow-writing-to-package-directory \
update-version \
<target>
```
## Options
Both manual versions also allow the following options to customize the operation, the
options need to come after the plugin name.
| Option | Description |
| ------ | ----------- |
| --dry-run | 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 |
### Example with options
```bash
swift package \
--allow-writing-to-package-directory \
generate-version \
--dry-run \
--verbose \
<target>
```
## View the Executable Options
You can also run the following command to view the options in your terminal
```bash
swift run cli-version --help
```
Or
```bash
swift run cli-version <verb> --help
```
Where `verb` is one of, `build`, `generate`, or `update`.

View File

@@ -1,10 +1,10 @@
# ``GitVersion`` # ``CliVersion``
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 ## Additional Resources
[Github Repo](https://github.com/m-housh/swift-git-version) [Github Repo](https://github.com/m-housh/swift-cli-version)
## Overview ## Overview
@@ -15,8 +15,9 @@ the branch and git sha if a tag is not set for the current worktree state.
## Articles ## Articles
- <doc:GettingStarted> - <doc:GettingStarted>
- <doc:ManualPlugins>
### Api ## Api
- ``FileClient`` - ``FileClient``
- ``GitVersionClient`` - ``GitVersionClient``

Binary file not shown.

After

Width:  |  Height:  |  Size: 206 KiB

View File

@@ -1,9 +1,9 @@
import ArgumentParser import ArgumentParser
import Foundation import Foundation
import GitVersion import CliVersion
import ShellClient import ShellClient
extension GitVersionCommand { extension CliVersionCommand {
struct Build: ParsableCommand { struct Build: ParsableCommand {
static var configuration: CommandConfiguration = .init( static var configuration: CommandConfiguration = .init(
abstract: "Used for the build with version plugin.", abstract: "Used for the build with version plugin.",

View File

@@ -2,10 +2,10 @@ import ArgumentParser
import Foundation import Foundation
@main @main
struct GitVersionCommand: ParsableCommand { struct CliVersionCommand: ParsableCommand {
static var configuration: CommandConfiguration = .init( static var configuration: CommandConfiguration = .init(
commandName: "git-version", commandName: "cli-version",
version: VERSION ?? "0.0.0", version: VERSION ?? "0.0.0",
subcommands: [ subcommands: [
Build.self, Build.self,

View File

@@ -1,10 +1,10 @@
import ArgumentParser import ArgumentParser
import Dependencies import Dependencies
import Foundation import Foundation
import GitVersion import CliVersion
import ShellClient import ShellClient
extension GitVersionCommand { extension CliVersionCommand {
struct Generate: ParsableCommand { struct Generate: ParsableCommand {
static var configuration: CommandConfiguration = .init( static var configuration: CommandConfiguration = .init(

View File

@@ -1,9 +1,9 @@
import ArgumentParser import ArgumentParser
import Foundation import Foundation
import GitVersion import CliVersion
import ShellClient import ShellClient
extension GitVersionCommand { extension CliVersionCommand {
struct Update: ParsableCommand { struct Update: ParsableCommand {
static var configuration: CommandConfiguration = .init( static var configuration: CommandConfiguration = .init(

View File

@@ -1,5 +1,5 @@
import XCTest import XCTest
import GitVersion import CliVersion
import ShellClient import ShellClient
final class GitVersionTests: XCTestCase { final class GitVersionTests: XCTestCase {
@@ -22,6 +22,7 @@ final class GitVersionTests: XCTestCase {
.deletingLastPathComponent() .deletingLastPathComponent()
.deletingLastPathComponent() .deletingLastPathComponent()
.absoluteString .absoluteString
.replacingOccurrences(of: "file://", with: "")
} }
func test_overrides_work() throws { func test_overrides_work() throws {
@@ -36,8 +37,7 @@ final class GitVersionTests: XCTestCase {
} }
func test_live() throws { func test_live() throws {
@Dependency(\.gitVersionClient) var versionClient: GitVersionClient
@Dependency(\.gitVersionClient) var versionClient
let version = try versionClient.currentVersion(in: gitDir) let version = try versionClient.currentVersion(in: gitDir)
print("VERSION: \(version)") print("VERSION: \(version)")