feat: Moving things into separate modules.
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import Foundation
|
||||
#if canImport(FoundationNetworking)
|
||||
import FoundationNetworking
|
||||
#endif
|
||||
import Dependencies
|
||||
import DependenciesMacros
|
||||
import FileClient
|
||||
import Foundation
|
||||
import GitClient
|
||||
import ShellClient
|
||||
|
||||
public extension DependencyValues {
|
||||
@@ -18,8 +17,6 @@ public extension DependencyValues {
|
||||
@DependencyClient
|
||||
public struct CliClient: Sendable {
|
||||
|
||||
static let defaultFileName = "Version.swift"
|
||||
|
||||
/// Build and update the version based on the git tag, or branch + sha.
|
||||
public var build: @Sendable (SharedOptions) async throws -> String
|
||||
|
||||
@@ -101,7 +98,7 @@ public extension CliClient.SharedOptions {
|
||||
let isDirectory = try await fileClient.isDirectory(url.cleanFilePath)
|
||||
|
||||
if isDirectory {
|
||||
url.appendPathComponent(CliClient.defaultFileName)
|
||||
url.appendPathComponent(Constants.defaultFileName)
|
||||
}
|
||||
|
||||
return url
|
||||
@@ -248,7 +245,7 @@ private extension CliClient.SharedOptions {
|
||||
|
||||
let (versionString, usesOptional) = try await getVersionString()
|
||||
let semVar = try getSemVar(versionString, type)
|
||||
let version = semVar.versionString(allowPrerelease: allowPreReleaseTag)
|
||||
let version = semVar.versionString(withPreReleaseTag: allowPreReleaseTag)
|
||||
logger.debug("Bumped version: \(version)")
|
||||
|
||||
let template = usesOptional ? Template.optional(version) : Template.build(version)
|
||||
|
||||
3
Sources/CliVersion/Constants.swift
Normal file
3
Sources/CliVersion/Constants.swift
Normal file
@@ -0,0 +1,3 @@
|
||||
enum Constants {
|
||||
static let defaultFileName = "Version.swift"
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
import Dependencies
|
||||
import DependenciesMacros
|
||||
import Foundation
|
||||
#if canImport(FoundationNetworking)
|
||||
import FoundationNetworking
|
||||
#endif
|
||||
import XCTestDynamicOverlay
|
||||
|
||||
// TODO: This can be an internal dependency.
|
||||
|
||||
@_spi(Internal)
|
||||
public extension DependencyValues {
|
||||
|
||||
/// Access a basic ``FileClient`` that can read / write data to the file system.
|
||||
///
|
||||
var fileClient: FileClient {
|
||||
get { self[FileClient.self] }
|
||||
set { self[FileClient.self] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the interactions with the file system. It is able
|
||||
/// to read from and write to files.
|
||||
///
|
||||
///
|
||||
/// ```swift
|
||||
/// @Dependency(\.fileClient) var fileClient
|
||||
/// ```
|
||||
///
|
||||
@_spi(Internal)
|
||||
@DependencyClient
|
||||
public struct FileClient: Sendable {
|
||||
|
||||
/// Return the current working directory.
|
||||
public var currentDirectory: @Sendable () async throws -> String
|
||||
|
||||
/// Check if a file exists at the given url.
|
||||
public var fileExists: @Sendable (URL) -> Bool = { _ in true }
|
||||
|
||||
/// Check if a url is a directory.
|
||||
public var isDirectory: @Sendable (String) async throws -> Bool
|
||||
|
||||
/// Read the contents of a file.
|
||||
public var read: @Sendable (URL) async throws -> String
|
||||
|
||||
/// Write `Data` to a file `URL`.
|
||||
public var write: @Sendable (Data, URL) async throws -> Void
|
||||
|
||||
/// Read the contents of a file at the given path.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - path: The file path to read from.
|
||||
public func read(_ path: String) async throws -> String {
|
||||
try await read(url(for: path))
|
||||
}
|
||||
|
||||
/// Write's the the string to a file path.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - string: The string to write to the file.
|
||||
/// - path: The file path.
|
||||
public func write(string: String, to path: String) async throws {
|
||||
try await write(string: string, to: url(for: path))
|
||||
}
|
||||
|
||||
/// 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) async throws {
|
||||
try await write(Data(string.utf8), url)
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
extension FileClient: DependencyKey {
|
||||
|
||||
/// A ``FileClient`` that does not do anything.
|
||||
public static let noop = FileClient(
|
||||
currentDirectory: { "./" },
|
||||
fileExists: { _ in true },
|
||||
isDirectory: { _ in true },
|
||||
read: { _ in "" },
|
||||
write: { _, _ in }
|
||||
)
|
||||
|
||||
/// An `unimplemented` ``FileClient``.
|
||||
public static let testValue = FileClient()
|
||||
|
||||
/// The live ``FileClient``
|
||||
public static let liveValue = FileClient(
|
||||
currentDirectory: { FileManager.default.currentDirectoryPath },
|
||||
fileExists: { FileManager.default.fileExists(atPath: $0.cleanFilePath) },
|
||||
isDirectory: { path in
|
||||
var isDirectory: ObjCBool = false
|
||||
FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
|
||||
return isDirectory.boolValue
|
||||
},
|
||||
read: { try String(contentsOf: $0, encoding: .utf8) },
|
||||
write: { try $0.write(to: $1, options: .atomic) }
|
||||
)
|
||||
|
||||
public static func capturing(
|
||||
_ captured: CapturingWrite
|
||||
) -> Self {
|
||||
.init(
|
||||
currentDirectory: { "./" },
|
||||
fileExists: { _ in true },
|
||||
isDirectory: { _ in true },
|
||||
read: { _ in "" },
|
||||
write: { await captured.set($0, $1) }
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
public actor CapturingWrite: Sendable {
|
||||
public private(set) var data: Data?
|
||||
public private(set) var url: URL?
|
||||
|
||||
public init() {}
|
||||
|
||||
func set(_ data: Data, _ url: URL) {
|
||||
self.data = data
|
||||
self.url = url
|
||||
}
|
||||
}
|
||||
@@ -1,265 +0,0 @@
|
||||
import Foundation
|
||||
#if canImport(FoundationNetworking)
|
||||
import FoundationNetworking
|
||||
#endif
|
||||
import Dependencies
|
||||
import DependenciesMacros
|
||||
import ShellClient
|
||||
|
||||
@_spi(Internal)
|
||||
public extension DependencyValues {
|
||||
|
||||
/// A ``GitVersionClient`` that can retrieve the current version from a
|
||||
/// git directory.
|
||||
var gitClient: GitClient {
|
||||
get { self[GitClient.self] }
|
||||
set { self[GitClient.self] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
/// A client that can retrieve the current version from a git directory.
|
||||
/// It will use the current `tag`, or if the current git tree does not
|
||||
/// 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 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.
|
||||
///
|
||||
@_spi(Internal)
|
||||
@DependencyClient
|
||||
public struct GitClient: Sendable {
|
||||
|
||||
/// The closure to run that returns the current version from a given
|
||||
/// git directory.
|
||||
@available(*, deprecated, message: "Use version.")
|
||||
public var currentVersion: @Sendable (String?, Bool) async throws -> String
|
||||
|
||||
/// Get the current version from the `git tag` in the given directory.
|
||||
/// If a directory is not passed in, then we will use the current working directory.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - gitDirectory: The directory to run the command in.
|
||||
@available(*, deprecated, message: "Use version.")
|
||||
public func currentVersion(in gitDirectory: String? = nil, exactMatch: Bool = true) async throws -> String {
|
||||
try await currentVersion(gitDirectory, exactMatch)
|
||||
}
|
||||
|
||||
public var version: (CurrentVersionOption) async throws -> Version
|
||||
|
||||
public struct CurrentVersionOption: Sendable {
|
||||
let gitDirectory: String?
|
||||
let style: Style
|
||||
|
||||
public init(
|
||||
gitDirectory: String? = nil,
|
||||
style: Style
|
||||
) {
|
||||
self.gitDirectory = gitDirectory
|
||||
self.style = style
|
||||
}
|
||||
|
||||
public enum Style: Sendable {
|
||||
case tag(exactMatch: Bool = false)
|
||||
case branch(commitSha: Bool = true)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum Version: Sendable, CustomStringConvertible {
|
||||
case branch(String)
|
||||
case tag(String)
|
||||
|
||||
public var description: String {
|
||||
switch self {
|
||||
case let .branch(string): return string
|
||||
case let .tag(string): return string
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
extension GitClient: TestDependencyKey {
|
||||
|
||||
/// The ``GitVersionClient`` used in test / debug builds.
|
||||
public static let testValue = GitClient()
|
||||
|
||||
/// The ``GitVersionClient`` used in release builds.
|
||||
public static var liveValue: GitClient {
|
||||
.init(
|
||||
currentVersion: { gitDirectory, exactMatch in
|
||||
try await GitVersion(workingDirectory: gitDirectory).currentVersion(exactMatch)
|
||||
},
|
||||
version: { try await $0.run() }
|
||||
)
|
||||
}
|
||||
|
||||
/// Create a mock git client, that always returns the given value.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - value: The value to return.
|
||||
public static func mock(_ value: Version) -> Self {
|
||||
.init(
|
||||
currentVersion: { _, _ in value.description },
|
||||
version: { _ in value }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private extension GitClient.CurrentVersionOption {
|
||||
|
||||
func run() async throws -> GitClient.Version {
|
||||
switch style {
|
||||
case let .tag(exactMatch: exactMatch):
|
||||
return try await .tag(runCommand(.describeTag(exactMatch: exactMatch)))
|
||||
case let .branch(commitSha: withCommit):
|
||||
async let branch = try await runCommand(.branch)
|
||||
|
||||
if withCommit {
|
||||
let commit = try await runCommand(.commit)
|
||||
return try await .branch("\(branch)-\(commit)")
|
||||
}
|
||||
return try await .branch(branch)
|
||||
}
|
||||
}
|
||||
|
||||
func runCommand(_ versionArgs: VersionArgs) async throws -> String {
|
||||
@Dependency(\.asyncShellClient) var shell
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
|
||||
var gitDirectory: String! = self.gitDirectory
|
||||
if gitDirectory == nil {
|
||||
gitDirectory = try await fileClient.currentDirectory()
|
||||
}
|
||||
|
||||
return try await shell.background(
|
||||
.init(
|
||||
shell: .env,
|
||||
environment: ProcessInfo.processInfo.environment,
|
||||
in: gitDirectory,
|
||||
versionArgs().map(\.rawValue)
|
||||
),
|
||||
trimmingCharactersIn: .whitespacesAndNewlines
|
||||
)
|
||||
}
|
||||
|
||||
enum VersionArgs {
|
||||
case branch
|
||||
case commit
|
||||
case describeTag(exactMatch: Bool)
|
||||
|
||||
func callAsFunction() -> [Args] {
|
||||
switch self {
|
||||
case .branch:
|
||||
return [.git, .symbolicRef, .quiet, .short, .head]
|
||||
case .commit:
|
||||
return [.git, .revParse, .short, .head]
|
||||
case let .describeTag(exactMatch):
|
||||
var args = [Args.git, .describe, .tags]
|
||||
if exactMatch {
|
||||
args.append(.exactMatch)
|
||||
}
|
||||
return args
|
||||
}
|
||||
}
|
||||
|
||||
enum Args: String, CustomStringConvertible {
|
||||
case git
|
||||
case describe
|
||||
case tags = "--tags"
|
||||
case exactMatch = "--exact-match"
|
||||
case quiet = "--quiet"
|
||||
case symbolicRef = "symbolic-ref"
|
||||
case revParse = "rev-parse"
|
||||
case short = "--short"
|
||||
case head = "HEAD"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private struct GitVersion {
|
||||
@Dependency(\.logger) var logger: Logger
|
||||
@Dependency(\.asyncShellClient) var shell
|
||||
|
||||
let workingDirectory: String?
|
||||
|
||||
func currentVersion(_ exactMatch: Bool) async throws -> String {
|
||||
logger.debug("\("Fetching current version".bold)")
|
||||
do {
|
||||
logger.debug("Checking for tag.")
|
||||
return try await run(command: command(for: .describe(exactMatch: exactMatch)))
|
||||
} catch {
|
||||
logger.debug("\("No tag found, deferring to branch & git sha".red)")
|
||||
let branch = try await run(command: command(for: .branch))
|
||||
let commit = try await run(command: command(for: .commit))
|
||||
return "\(branch) \(commit)"
|
||||
}
|
||||
}
|
||||
|
||||
func command(for argument: VersionArgs) -> ShellCommand {
|
||||
.init(
|
||||
shell: .env,
|
||||
environment: nil,
|
||||
in: workingDirectory ?? FileManager.default.currentDirectoryPath,
|
||||
argument.arguments.map(\.rawValue)
|
||||
)
|
||||
}
|
||||
|
||||
func run(command: ShellCommand) async throws -> String {
|
||||
try await shell.background(command, trimmingCharactersIn: .whitespacesAndNewlines)
|
||||
}
|
||||
|
||||
enum VersionArgs {
|
||||
case branch
|
||||
case commit
|
||||
case describe(exactMatch: Bool)
|
||||
|
||||
var arguments: [Args] {
|
||||
switch self {
|
||||
case .branch:
|
||||
return [.git, .symbolicRef, .quiet, .short, .head]
|
||||
case .commit:
|
||||
return [.git, .revParse, .short, .head]
|
||||
case let .describe(exactMatch):
|
||||
var args = [Args.git, .describe, .tags]
|
||||
if exactMatch {
|
||||
args.append(.exactMatch)
|
||||
}
|
||||
return args
|
||||
}
|
||||
}
|
||||
|
||||
enum Args: String, CustomStringConvertible {
|
||||
case git
|
||||
case describe
|
||||
case tags = "--tags"
|
||||
case exactMatch = "--exact-match"
|
||||
case quiet = "--quiet"
|
||||
case symbolicRef = "symbolic-ref"
|
||||
case revParse = "rev-parse"
|
||||
case short = "--short"
|
||||
case head = "HEAD"
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
extension RawRepresentable where RawValue == String, Self: CustomStringConvertible {
|
||||
var description: String { rawValue }
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
public extension GitClient.Version {
|
||||
static var mocks: [Self] {
|
||||
[
|
||||
.tag("1.0.0"),
|
||||
.tag("1.0.0-4-g59bc977"),
|
||||
.branch("dev-g59bc977")
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
import Foundation
|
||||
#if canImport(FoundationNetworking)
|
||||
import FoundationNetworking
|
||||
#endif
|
||||
import Logging
|
||||
|
||||
// TODO: Move.
|
||||
@_spi(Internal)
|
||||
public extension Logger.Level {
|
||||
|
||||
@@ -16,26 +13,3 @@ public extension Logger.Level {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@_spi(Internal)
|
||||
public extension URL {
|
||||
var cleanFilePath: String {
|
||||
absoluteString.replacingOccurrences(of: "file://", with: "")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
@_spi(Internal)
|
||||
public func url(for path: String) -> URL {
|
||||
#if os(Linux)
|
||||
return URL(fileURLWithPath: path)
|
||||
#else
|
||||
if #available(macOS 13.0, *) {
|
||||
return URL(filePath: path)
|
||||
} else {
|
||||
// Fallback on earlier versions
|
||||
return URL(fileURLWithPath: path)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import Foundation
|
||||
import GitClient
|
||||
|
||||
// Container for sem-var version.
|
||||
@_spi(Internal)
|
||||
|
||||
Reference in New Issue
Block a user