From 84ac4a6a122ce8c665ef1ae6c6c66ebe37127ae1 Mon Sep 17 00:00:00 2001 From: Michael Housh Date: Sun, 22 Dec 2024 11:32:52 -0500 Subject: [PATCH] feat: Moving things into separate modules. --- Makefile | 4 +-- Package.swift | 19 ++++++++++-- Sources/CliVersion/CliClient.swift | 13 ++++----- Sources/CliVersion/Constants.swift | 3 ++ Sources/CliVersion/Helpers.swift | 28 +----------------- Sources/CliVersion/SemVar.swift | 1 + .../FileClient.swift | 29 ++++++++++++------- .../{CliVersion => GitClient}/GitClient.swift | 19 +++++------- Tests/CliVersionTests/CliClientTests.swift | 8 +++-- Tests/CliVersionTests/CliVersionTests.swift | 2 ++ justfile | 2 +- 11 files changed, 64 insertions(+), 64 deletions(-) create mode 100644 Sources/CliVersion/Constants.swift rename Sources/{CliVersion => FileClient}/FileClient.swift (88%) rename Sources/{CliVersion => GitClient}/GitClient.swift (95%) diff --git a/Makefile b/Makefile index c2831a3..bcf14a2 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ CONFIG := debug DOCC_TARGET ?= CliVersion DOCC_BASEPATH = $(shell basename "$(PWD)") DOCC_DIR ?= ./docs +SWIFT_VERSION ?= "5.10" clean: rm -rf .build @@ -27,7 +28,7 @@ test-linux: docker run --rm \ --volume "$(PWD):$(PWD)" \ --workdir "$(PWD)" \ - swift:5.7-focal \ + "swift:$(SWIFT_VERSION)" \ swift test test-library: @@ -39,4 +40,3 @@ update-version: --allow-writing-to-package-directory \ update-version \ git-version - diff --git a/Package.swift b/Package.swift index 55cc5ca..462f75b 100644 --- a/Package.swift +++ b/Package.swift @@ -27,15 +27,30 @@ let package = Package( .product(name: "ArgumentParser", package: "swift-argument-parser") ] ), - .target(name: "TestSupport"), .target( - name: "CliVersion", + name: "FileClient", dependencies: [ + .product(name: "Dependencies", package: "swift-dependencies"), + .product(name: "DependenciesMacros", package: "swift-dependencies") + ] + ), + .target( + name: "GitClient", + dependencies: [ + "FileClient", .product(name: "Dependencies", package: "swift-dependencies"), .product(name: "DependenciesMacros", package: "swift-dependencies"), .product(name: "ShellClient", package: "swift-shell-client") ] ), + .target( + name: "CliVersion", + dependencies: [ + "FileClient", + "GitClient" + ] + ), + .target(name: "TestSupport"), .testTarget( name: "CliVersionTests", dependencies: ["CliVersion", "TestSupport"] diff --git a/Sources/CliVersion/CliClient.swift b/Sources/CliVersion/CliClient.swift index 3a014d2..e715f52 100644 --- a/Sources/CliVersion/CliClient.swift +++ b/Sources/CliVersion/CliClient.swift @@ -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) diff --git a/Sources/CliVersion/Constants.swift b/Sources/CliVersion/Constants.swift new file mode 100644 index 0000000..6be6d11 --- /dev/null +++ b/Sources/CliVersion/Constants.swift @@ -0,0 +1,3 @@ +enum Constants { + static let defaultFileName = "Version.swift" +} diff --git a/Sources/CliVersion/Helpers.swift b/Sources/CliVersion/Helpers.swift index b8566c0..632de7c 100644 --- a/Sources/CliVersion/Helpers.swift +++ b/Sources/CliVersion/Helpers.swift @@ -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 -} diff --git a/Sources/CliVersion/SemVar.swift b/Sources/CliVersion/SemVar.swift index 1a6cb6c..d5b35e2 100644 --- a/Sources/CliVersion/SemVar.swift +++ b/Sources/CliVersion/SemVar.swift @@ -1,4 +1,5 @@ import Foundation +import GitClient // Container for sem-var version. @_spi(Internal) diff --git a/Sources/CliVersion/FileClient.swift b/Sources/FileClient/FileClient.swift similarity index 88% rename from Sources/CliVersion/FileClient.swift rename to Sources/FileClient/FileClient.swift index 5542656..bd76b42 100644 --- a/Sources/CliVersion/FileClient.swift +++ b/Sources/FileClient/FileClient.swift @@ -1,14 +1,7 @@ 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. @@ -27,7 +20,6 @@ public extension DependencyValues { /// @Dependency(\.fileClient) var fileClient /// ``` /// -@_spi(Internal) @DependencyClient public struct FileClient: Sendable { @@ -73,7 +65,6 @@ public struct FileClient: Sendable { } } -@_spi(Internal) extension FileClient: DependencyKey { /// A ``FileClient`` that does not do anything. @@ -115,7 +106,6 @@ extension FileClient: DependencyKey { } -@_spi(Internal) public actor CapturingWrite: Sendable { public private(set) var data: Data? public private(set) var url: URL? @@ -127,3 +117,22 @@ public actor CapturingWrite: Sendable { self.url = url } } + +public extension URL { + var cleanFilePath: String { + absoluteString.replacingOccurrences(of: "file://", with: "") + } +} + +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 +} diff --git a/Sources/CliVersion/GitClient.swift b/Sources/GitClient/GitClient.swift similarity index 95% rename from Sources/CliVersion/GitClient.swift rename to Sources/GitClient/GitClient.swift index b12ad9d..c2edcdd 100644 --- a/Sources/CliVersion/GitClient.swift +++ b/Sources/GitClient/GitClient.swift @@ -1,12 +1,9 @@ -import Foundation -#if canImport(FoundationNetworking) - import FoundationNetworking -#endif import Dependencies import DependenciesMacros +import FileClient +import Foundation import ShellClient -@_spi(Internal) public extension DependencyValues { /// A ``GitVersionClient`` that can retrieve the current version from a @@ -26,7 +23,6 @@ public extension DependencyValues { /// 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 { @@ -45,9 +41,12 @@ public struct GitClient: Sendable { try await currentVersion(gitDirectory, exactMatch) } - public var version: (CurrentVersionOption) async throws -> Version + public var version: @Sendable (CurrentVersionOption) async throws -> Version - public struct CurrentVersionOption: Sendable { +} + +public extension GitClient { + struct CurrentVersionOption: Sendable { let gitDirectory: String? let style: Style @@ -66,7 +65,7 @@ public struct GitClient: Sendable { } - public enum Version: Sendable, CustomStringConvertible { + enum Version: Sendable, CustomStringConvertible { case branch(String) case tag(String) @@ -79,7 +78,6 @@ public struct GitClient: Sendable { } } -@_spi(Internal) extension GitClient: TestDependencyKey { /// The ``GitVersionClient`` used in test / debug builds. @@ -253,7 +251,6 @@ extension RawRepresentable where RawValue == String, Self: CustomStringConvertib var description: String { rawValue } } -@_spi(Internal) public extension GitClient.Version { static var mocks: [Self] { [ diff --git a/Tests/CliVersionTests/CliClientTests.swift b/Tests/CliVersionTests/CliClientTests.swift index aa4daa8..f626644 100644 --- a/Tests/CliVersionTests/CliClientTests.swift +++ b/Tests/CliVersionTests/CliClientTests.swift @@ -1,6 +1,8 @@ @_spi(Internal) import CliVersion import Dependencies +import FileClient import Foundation +import GitClient import Logging import Testing import TestSupport @@ -79,11 +81,11 @@ struct CliClientTests { func gitVersionToSemVar(version: GitClient.Version) { let semVar = version.semVar if semVar != nil { - #expect(semVar!.versionString(allowPrerelease: false) == "1.0.0") - #expect(semVar!.versionString(allowPrerelease: true) == version.description) + #expect(semVar!.versionString(withPreReleaseTag: false) == "1.0.0") + #expect(semVar!.versionString(withPreReleaseTag: true) == version.description) } else { let semVar = SemVar(preRelease: version.description) - #expect(semVar.versionString(allowPrerelease: true) == "0.0.0-\(version.description)") + #expect(semVar.versionString(withPreReleaseTag: true) == "0.0.0-\(version.description)") } } diff --git a/Tests/CliVersionTests/CliVersionTests.swift b/Tests/CliVersionTests/CliVersionTests.swift index bb855aa..30c90be 100644 --- a/Tests/CliVersionTests/CliVersionTests.swift +++ b/Tests/CliVersionTests/CliVersionTests.swift @@ -1,5 +1,7 @@ @_spi(Internal) import CliVersion import Dependencies +import FileClient +import GitClient import ShellClient import TestSupport import XCTest diff --git a/justfile b/justfile index 697edf5..6cc95f2 100644 --- a/justfile +++ b/justfile @@ -1,4 +1,4 @@ -product := cli-version +product := "cli-version" [private] default: