wip
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
# Getting Started
|
||||
|
||||
Learn how to integrate the plugins into your project
|
||||
|
||||
## Overview
|
||||
|
||||
Use the plugins by including as a package to your project and declaring in the `plugins` section of
|
||||
your target.
|
||||
|
||||
```swift
|
||||
let package = Package(
|
||||
...,
|
||||
dependencies: [
|
||||
...,
|
||||
.package(url: "https://github.com/m-housh/swift-git-version.git", from: "0.1.0")
|
||||
],
|
||||
targets: [
|
||||
.executableTarget(
|
||||
name: "<target name>",
|
||||
dependencies: [...],
|
||||
plugins: [
|
||||
.plugin(name: "BuildWithVersionPlugin", package: "swift-git-version")
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
```swift
|
||||
import ArgumentParser
|
||||
|
||||
@main
|
||||
struct MyCliTool: ParsableCommand {
|
||||
static let configuration = CommandConfiguration(
|
||||
abstract: "My awesome cli tool",
|
||||
version: VERSION
|
||||
)
|
||||
|
||||
func run() throws {
|
||||
print("Version: \(VERSION)")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You will have access to the `VERSION` string variable even though it is not declared in your source
|
||||
files.
|
||||
22
Sources/GitVersion/Documentation.docc/GitVersion.md
Normal file
22
Sources/GitVersion/Documentation.docc/GitVersion.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# ``GitVersion``
|
||||
|
||||
Derive a version for a command line tool from git tags or a git sha.
|
||||
|
||||
## Additional Resources
|
||||
|
||||
[Github Repo](https://github.com/m-housh/swift-git-version)
|
||||
|
||||
## 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.
|
||||
|
||||
## Articles
|
||||
|
||||
- <doc:GettingStarted>
|
||||
|
||||
### Api
|
||||
|
||||
- ``FileClient``
|
||||
- ``GitVersionClient``
|
||||
@@ -14,10 +14,6 @@ import XCTestDynamicOverlay
|
||||
///
|
||||
public struct FileClient {
|
||||
|
||||
/// Read the file contents from the given `URL` as `Data`.
|
||||
///
|
||||
public private(set) var read: (URL) throws -> Data
|
||||
|
||||
/// Write `Data` to a file `URL`.
|
||||
public private(set) var write: (Data, URL) throws -> Void
|
||||
|
||||
@@ -30,91 +26,13 @@ public struct FileClient {
|
||||
///```
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - read: Read the file contents.
|
||||
/// - write: Write the data to a file.
|
||||
public init(
|
||||
read: @escaping (URL) throws -> Data,
|
||||
write: @escaping (Data, URL) throws -> Void
|
||||
) {
|
||||
self.read = read
|
||||
self.write = write
|
||||
}
|
||||
|
||||
/// Read a file at the given path.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - path: The path to read the file at.
|
||||
public func read(path: String) throws -> Data {
|
||||
let url = try url(for: path)
|
||||
return try self.read(url)
|
||||
}
|
||||
|
||||
/// Read the file as a string.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - url: The url for the file.
|
||||
public func readAsString(url: URL) throws -> String {
|
||||
let data = try read(url)
|
||||
return String(decoding: data, as: UTF8.self)
|
||||
}
|
||||
|
||||
/// Read the file as a string
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - path: The file path to read.
|
||||
public func readAsString(path: String) throws -> String {
|
||||
try self.readAsString(url: url(for: path))
|
||||
}
|
||||
|
||||
/// Read the contents of a file and decode as the decodable type.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - decodable: The type to decode.
|
||||
/// - url: The file url.
|
||||
/// - decoder: The decoder to use.
|
||||
public func read<D: Decodable>(
|
||||
_ decodable: D.Type,
|
||||
from url: URL,
|
||||
using decoder: JSONDecoder = .init()
|
||||
) throws -> D {
|
||||
let data = try read(url)
|
||||
return try decoder.decode(D.self, from: data)
|
||||
}
|
||||
|
||||
/// Read the contents of a file and decode as the decodable type.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - decodable: The type to decode.
|
||||
/// - path: The file path.
|
||||
/// - decoder: The decoder to use.
|
||||
public func read<D: Decodable>(
|
||||
_ decodable: D.Type,
|
||||
from path: String,
|
||||
using decoder: JSONDecoder = .init()
|
||||
) throws -> D {
|
||||
let data = try read(path: path)
|
||||
return try decoder.decode(D.self, from: data)
|
||||
}
|
||||
|
||||
/// Write the data to a file at the given path.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - data: The data to write to the file.
|
||||
/// - path: The file path.
|
||||
public func write(data: Data, to path: String) throws {
|
||||
let url = try url(for: path)
|
||||
try self.write(data, url)
|
||||
}
|
||||
|
||||
/// Write's the given string to a file.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - string: The string to write to the file.
|
||||
/// - url: The file url.
|
||||
public func write(string: String, to url: URL) throws {
|
||||
try self.write(Data(string.utf8), url)
|
||||
}
|
||||
|
||||
/// Write's the the string to a file path.
|
||||
///
|
||||
/// - Parameters:
|
||||
@@ -124,25 +42,31 @@ public struct FileClient {
|
||||
let url = try url(for: path)
|
||||
try self.write(string: string, to: url)
|
||||
}
|
||||
|
||||
/// Write's the the string to a file path.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - string: The string to write to the file.
|
||||
/// - url: The file url.
|
||||
public func write(string: String, to url: URL) throws {
|
||||
try self.write(Data(string.utf8), url)
|
||||
}
|
||||
}
|
||||
|
||||
extension FileClient: DependencyKey {
|
||||
|
||||
/// A ``FileClient`` that does not do anything.
|
||||
public static let noop = FileClient.init(
|
||||
read: { _ in Data() },
|
||||
write: { _, _ in }
|
||||
)
|
||||
|
||||
/// An `unimplemented` ``FileClient``.
|
||||
public static let testValue = FileClient(
|
||||
read: unimplemented("\(Self.self).read", placeholder: Data()),
|
||||
write: unimplemented("\(Self.self).write")
|
||||
)
|
||||
|
||||
/// The live ``FileClient``
|
||||
public static let liveValue = FileClient(
|
||||
read: { try Data(contentsOf: $0) },
|
||||
write: { try $0.write(to: $1, options: .atomic) }
|
||||
)
|
||||
|
||||
@@ -158,20 +82,6 @@ extension DependencyValues {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Overrides
|
||||
extension FileClient {
|
||||
|
||||
/// Override the data that get's returned when a `read` operation is called.
|
||||
///
|
||||
/// This is useful in a testing context.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - data: The data to return when a read operation is called.
|
||||
public mutating func overrideRead(data: Data) {
|
||||
self.read = { _ in data }
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
fileprivate func url(for path: String) throws -> URL {
|
||||
#if os(Linux)
|
||||
|
||||
@@ -10,23 +10,10 @@ import XCTestDynamicOverlay
|
||||
/// point to a commit that is tagged, it will use the `branch git-sha` as
|
||||
/// the version.
|
||||
///
|
||||
/// This is often not used directly, instead it is used with ``SwiftBuild``er
|
||||
/// This is often not used directly, instead it is used with one of the plugins
|
||||
/// that is supplied with this library. The use case is to set the version of a command line
|
||||
/// tool based on the current git tag.
|
||||
///
|
||||
/// ```swift
|
||||
/// @main
|
||||
/// public struct Build {
|
||||
/// public static func main() throws {
|
||||
/// @Dependency(\.shellClient) var shell: ShellClient
|
||||
///
|
||||
/// try shell.replacingNilWithVersionString(
|
||||
/// in: "Sources/example/Version.swift",
|
||||
/// build: SwiftBuild.release()
|
||||
/// )
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
public struct GitVersionClient {
|
||||
|
||||
/// The closure to run that returns the current version from a given
|
||||
@@ -92,6 +79,20 @@ extension DependencyValues {
|
||||
}
|
||||
}
|
||||
|
||||
extension ShellCommand {
|
||||
public static func gitCurrentSha(gitDirectory: String? = nil) -> Self {
|
||||
GitVersion(workingDirectory: gitDirectory).command(for: .commit)
|
||||
}
|
||||
|
||||
public static func gitCurrentBranch(gitDirectory: String? = nil) -> Self {
|
||||
GitVersion(workingDirectory: gitDirectory).command(for: .branch)
|
||||
}
|
||||
|
||||
public static func gitCurrentTag(gitDirectory: String? = nil) -> Self {
|
||||
GitVersion(workingDirectory: gitDirectory).command(for: .describe)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
fileprivate struct GitVersion {
|
||||
@Dependency(\.logger) var logger: Logger
|
||||
|
||||
Reference in New Issue
Block a user