commit c4ac51a1c09a96bd311153b98ea153d3c54cc6e8 Author: Michael Housh Date: Fri Mar 10 11:07:27 2023 -0500 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b29812 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ +DerivedData/ +.swiftpm/config/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/GitVersion.xcworkspace/contents.xcworkspacedata b/GitVersion.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..ca3329e --- /dev/null +++ b/GitVersion.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/GitVersion.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/GitVersion.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/GitVersion.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/GitVersion.xcworkspace/xcshareddata/swiftpm/Package.resolved b/GitVersion.xcworkspace/xcshareddata/swiftpm/Package.resolved new file mode 100644 index 0000000..90864bc --- /dev/null +++ b/GitVersion.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -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 +} diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..90864bc --- /dev/null +++ b/Package.resolved @@ -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 +} diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..65a3d3b --- /dev/null +++ b/Package.swift @@ -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"] + ), + ] +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..fe54f65 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# swift-git-version + +A description of this package. diff --git a/Sources/GitVersion/Client.swift b/Sources/GitVersion/Client.swift new file mode 100644 index 0000000..b3393f9 --- /dev/null +++ b/Sources/GitVersion/Client.swift @@ -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 } + } +} diff --git a/Sources/GitVersion/LiveKey.swift b/Sources/GitVersion/LiveKey.swift new file mode 100644 index 0000000..a3ef61e --- /dev/null +++ b/Sources/GitVersion/LiveKey.swift @@ -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 } +} diff --git a/Sources/swift-git-version/swift_git_version.swift b/Sources/swift-git-version/swift_git_version.swift new file mode 100644 index 0000000..dec585b --- /dev/null +++ b/Sources/swift-git-version/swift_git_version.swift @@ -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)") + } + } +} diff --git a/Tests/swift-git-versionTests/swift_git_versionTests.swift b/Tests/swift-git-versionTests/swift_git_versionTests.swift new file mode 100644 index 0000000..1f97460 --- /dev/null +++ b/Tests/swift-git-versionTests/swift_git_versionTests.swift @@ -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!") + } +}