Initial commit

This commit is contained in:
2023-03-10 11:07:27 -05:00
commit c4ac51a1c0
11 changed files with 358 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:">
</FileRef>
</Workspace>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

View File

@@ -0,0 +1,77 @@
{
"pins" : [
{
"identity" : "combine-schedulers",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/combine-schedulers",
"state" : {
"revision" : "882ac01eb7ef9e36d4467eb4b1151e74fcef85ab",
"version" : "0.9.1"
}
},
{
"identity" : "rainbow",
"kind" : "remoteSourceControl",
"location" : "https://github.com/onevcat/Rainbow",
"state" : {
"revision" : "e0dada9cd44e3fa7ec3b867e49a8ddbf543e3df3",
"version" : "4.0.1"
}
},
{
"identity" : "swift-clocks",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-clocks",
"state" : {
"revision" : "20b25ca0dd88ebfb9111ec937814ddc5a8880172",
"version" : "0.2.0"
}
},
{
"identity" : "swift-dependencies",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-dependencies.git",
"state" : {
"revision" : "6bb1034e8a1bfbf46dfb766b6c09b7b17e1cba10",
"version" : "0.2.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "32e8d724467f8fe623624570367e3d50c5638e46",
"version" : "1.5.2"
}
},
{
"identity" : "swift-log-format-and-pipe",
"kind" : "remoteSourceControl",
"location" : "https://github.com/adorkable/swift-log-format-and-pipe.git",
"state" : {
"revision" : "bd29badb9e6b18122ec10b84eed534db83cad279",
"version" : "0.1.1"
}
},
{
"identity" : "swift-shell-client",
"kind" : "remoteSourceControl",
"location" : "https://github.com/m-housh/swift-shell-client.git",
"state" : {
"revision" : "28bcd2ecd0022aca20c520fc578a130bec7283e7",
"version" : "0.1.0"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "ab8c9f45843694dd16be4297e6d44c0634fd9913",
"version" : "0.8.4"
}
}
],
"version" : 2
}

77
Package.resolved Normal file
View File

@@ -0,0 +1,77 @@
{
"pins" : [
{
"identity" : "combine-schedulers",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/combine-schedulers",
"state" : {
"revision" : "882ac01eb7ef9e36d4467eb4b1151e74fcef85ab",
"version" : "0.9.1"
}
},
{
"identity" : "rainbow",
"kind" : "remoteSourceControl",
"location" : "https://github.com/onevcat/Rainbow",
"state" : {
"revision" : "e0dada9cd44e3fa7ec3b867e49a8ddbf543e3df3",
"version" : "4.0.1"
}
},
{
"identity" : "swift-clocks",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-clocks",
"state" : {
"revision" : "20b25ca0dd88ebfb9111ec937814ddc5a8880172",
"version" : "0.2.0"
}
},
{
"identity" : "swift-dependencies",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-dependencies.git",
"state" : {
"revision" : "6bb1034e8a1bfbf46dfb766b6c09b7b17e1cba10",
"version" : "0.2.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "32e8d724467f8fe623624570367e3d50c5638e46",
"version" : "1.5.2"
}
},
{
"identity" : "swift-log-format-and-pipe",
"kind" : "remoteSourceControl",
"location" : "https://github.com/adorkable/swift-log-format-and-pipe.git",
"state" : {
"revision" : "bd29badb9e6b18122ec10b84eed534db83cad279",
"version" : "0.1.1"
}
},
{
"identity" : "swift-shell-client",
"kind" : "remoteSourceControl",
"location" : "https://github.com/m-housh/swift-shell-client.git",
"state" : {
"revision" : "28bcd2ecd0022aca20c520fc578a130bec7283e7",
"version" : "0.1.0"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "ab8c9f45843694dd16be4297e6d44c0634fd9913",
"version" : "0.8.4"
}
}
],
"version" : 2
}

31
Package.swift Normal file
View File

@@ -0,0 +1,31 @@
// swift-tools-version: 5.7
import PackageDescription
let package = Package(
name: "swift-git-version",
platforms: [
.macOS(.v10_15)
],
dependencies: [
.package(url: "https://github.com/m-housh/swift-shell-client.git", from: "0.1.0")
],
targets: [
.target(
name: "GitVersion",
dependencies: [
.product(name: "ShellClient", package: "swift-shell-client")
]
),
.executableTarget(
name: "swift-git-version",
dependencies: [
"GitVersion"
]
),
.testTarget(
name: "swift-git-versionTests",
dependencies: ["swift-git-version"]
),
]
)

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# swift-git-version
A description of this package.

View File

@@ -0,0 +1,38 @@
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import ShellClient
import XCTestDynamicOverlay
public struct GitVersionClient {
private var currentVersion: (String?) throws -> String
public init(currentVersion: @escaping (String?) throws -> String) {
self.currentVersion = currentVersion
}
public func currentVersion(in gitDirectory: String? = nil) throws -> String {
try self.currentVersion(gitDirectory)
}
public mutating func override(with version: String) {
self.currentVersion = { _ in version }
}
}
extension GitVersionClient: TestDependencyKey {
public static let testValue = GitVersionClient(
currentVersion: unimplemented("\(Self.self).currentVersion", placeholder: "")
)
}
extension DependencyValues {
public var gitVersionClient: GitVersionClient {
get { self[GitVersionClient.self] }
set { self[GitVersionClient.self] = newValue }
}
}

View File

@@ -0,0 +1,80 @@
import Foundation
import ShellClient
extension GitVersionClient: DependencyKey {
public static var liveValue: GitVersionClient {
.init(currentVersion: { gitDirectory in
try GitVersion(workingDirectory: gitDirectory).currentVersion()
})
}
}
fileprivate struct GitVersion {
@Dependency(\.logger) var logger: Logger
@Dependency(\.shellClient) var shell: ShellClient
let workingDirectory: String?
func currentVersion() throws -> String {
logger.debug("\("Fetching current version".bold)")
do {
logger.debug("Checking for tag.")
return try run(command: command(for: .describe))
} catch {
logger.debug("\("No tag found, deferring to branch & git sha".red)")
let branch = try run(command: command(for: .branch))
let commit = try run(command: command(for: .commit))
return "\(branch) \(commit)"
}
}
private func command(for argument: VersionArgs) -> ShellCommand {
.init(
shell: .env,
environment: nil,
in: workingDirectory,
arguments: argument.arguments
)
}
}
fileprivate extension GitVersion {
func run(command: ShellCommand) throws -> String {
try shell.background(command, trimmingCharactersIn: .whitespacesAndNewlines)
}
enum VersionArgs {
case branch
case commit
case describe
var arguments: [Args] {
switch self {
case .branch:
return [.git, .symbolicRef, .quiet, .short, .head]
case .commit:
return [.git, .revParse, .short, .head]
case .describe:
return [.git, .describe, .tags, .exactMatch]
}
}
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"
}
}
}
fileprivate extension RawRepresentable where RawValue == String, Self: CustomStringConvertible {
var description: String { rawValue }
}

View File

@@ -0,0 +1,17 @@
import GitVersion
import ShellClient
@main
public struct swift_git_version {
public static func main() {
@Dependency(\.logger) var logger
@Dependency(\.gitVersionClient) var client
do {
let version = try client.currentVersion()
logger.info("Version: \(version.blue)")
} catch {
logger.info("\("Oops, something went wrong".red)")
}
}
}

View File

@@ -0,0 +1,11 @@
import XCTest
@testable import swift_git_version
final class swift_git_versionTests: XCTestCase {
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(swift_git_version().text, "Hello, World!")
}
}