feat: Moving things into separate modules.

This commit is contained in:
2024-12-22 11:32:52 -05:00
parent 0484f45d57
commit 84ac4a6a12
11 changed files with 64 additions and 64 deletions

View File

@@ -3,6 +3,7 @@ CONFIG := debug
DOCC_TARGET ?= CliVersion DOCC_TARGET ?= CliVersion
DOCC_BASEPATH = $(shell basename "$(PWD)") DOCC_BASEPATH = $(shell basename "$(PWD)")
DOCC_DIR ?= ./docs DOCC_DIR ?= ./docs
SWIFT_VERSION ?= "5.10"
clean: clean:
rm -rf .build rm -rf .build
@@ -27,7 +28,7 @@ test-linux:
docker run --rm \ docker run --rm \
--volume "$(PWD):$(PWD)" \ --volume "$(PWD):$(PWD)" \
--workdir "$(PWD)" \ --workdir "$(PWD)" \
swift:5.7-focal \ "swift:$(SWIFT_VERSION)" \
swift test swift test
test-library: test-library:
@@ -39,4 +40,3 @@ update-version:
--allow-writing-to-package-directory \ --allow-writing-to-package-directory \
update-version \ update-version \
git-version git-version

View File

@@ -27,15 +27,30 @@ let package = Package(
.product(name: "ArgumentParser", package: "swift-argument-parser") .product(name: "ArgumentParser", package: "swift-argument-parser")
] ]
), ),
.target(name: "TestSupport"),
.target( .target(
name: "CliVersion", name: "FileClient",
dependencies: [ 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: "Dependencies", package: "swift-dependencies"),
.product(name: "DependenciesMacros", package: "swift-dependencies"), .product(name: "DependenciesMacros", package: "swift-dependencies"),
.product(name: "ShellClient", package: "swift-shell-client") .product(name: "ShellClient", package: "swift-shell-client")
] ]
), ),
.target(
name: "CliVersion",
dependencies: [
"FileClient",
"GitClient"
]
),
.target(name: "TestSupport"),
.testTarget( .testTarget(
name: "CliVersionTests", name: "CliVersionTests",
dependencies: ["CliVersion", "TestSupport"] dependencies: ["CliVersion", "TestSupport"]

View File

@@ -1,9 +1,8 @@
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import Dependencies import Dependencies
import DependenciesMacros import DependenciesMacros
import FileClient
import Foundation
import GitClient
import ShellClient import ShellClient
public extension DependencyValues { public extension DependencyValues {
@@ -18,8 +17,6 @@ public extension DependencyValues {
@DependencyClient @DependencyClient
public struct CliClient: Sendable { public struct CliClient: Sendable {
static let defaultFileName = "Version.swift"
/// Build and update the version based on the git tag, or branch + sha. /// Build and update the version based on the git tag, or branch + sha.
public var build: @Sendable (SharedOptions) async throws -> String public var build: @Sendable (SharedOptions) async throws -> String
@@ -101,7 +98,7 @@ public extension CliClient.SharedOptions {
let isDirectory = try await fileClient.isDirectory(url.cleanFilePath) let isDirectory = try await fileClient.isDirectory(url.cleanFilePath)
if isDirectory { if isDirectory {
url.appendPathComponent(CliClient.defaultFileName) url.appendPathComponent(Constants.defaultFileName)
} }
return url return url
@@ -248,7 +245,7 @@ private extension CliClient.SharedOptions {
let (versionString, usesOptional) = try await getVersionString() let (versionString, usesOptional) = try await getVersionString()
let semVar = try getSemVar(versionString, type) let semVar = try getSemVar(versionString, type)
let version = semVar.versionString(allowPrerelease: allowPreReleaseTag) let version = semVar.versionString(withPreReleaseTag: allowPreReleaseTag)
logger.debug("Bumped version: \(version)") logger.debug("Bumped version: \(version)")
let template = usesOptional ? Template.optional(version) : Template.build(version) let template = usesOptional ? Template.optional(version) : Template.build(version)

View File

@@ -0,0 +1,3 @@
enum Constants {
static let defaultFileName = "Version.swift"
}

View File

@@ -1,9 +1,6 @@
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import Logging import Logging
// TODO: Move.
@_spi(Internal) @_spi(Internal)
public extension Logger.Level { 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
}

View File

@@ -1,4 +1,5 @@
import Foundation import Foundation
import GitClient
// Container for sem-var version. // Container for sem-var version.
@_spi(Internal) @_spi(Internal)

View File

@@ -1,14 +1,7 @@
import Dependencies import Dependencies
import DependenciesMacros import DependenciesMacros
import Foundation import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import XCTestDynamicOverlay
// TODO: This can be an internal dependency.
@_spi(Internal)
public extension DependencyValues { public extension DependencyValues {
/// Access a basic ``FileClient`` that can read / write data to the file system. /// Access a basic ``FileClient`` that can read / write data to the file system.
@@ -27,7 +20,6 @@ public extension DependencyValues {
/// @Dependency(\.fileClient) var fileClient /// @Dependency(\.fileClient) var fileClient
/// ``` /// ```
/// ///
@_spi(Internal)
@DependencyClient @DependencyClient
public struct FileClient: Sendable { public struct FileClient: Sendable {
@@ -73,7 +65,6 @@ public struct FileClient: Sendable {
} }
} }
@_spi(Internal)
extension FileClient: DependencyKey { extension FileClient: DependencyKey {
/// A ``FileClient`` that does not do anything. /// A ``FileClient`` that does not do anything.
@@ -115,7 +106,6 @@ extension FileClient: DependencyKey {
} }
@_spi(Internal)
public actor CapturingWrite: Sendable { public actor CapturingWrite: Sendable {
public private(set) var data: Data? public private(set) var data: Data?
public private(set) var url: URL? public private(set) var url: URL?
@@ -127,3 +117,22 @@ public actor CapturingWrite: Sendable {
self.url = url 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
}

View File

@@ -1,12 +1,9 @@
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import Dependencies import Dependencies
import DependenciesMacros import DependenciesMacros
import FileClient
import Foundation
import ShellClient import ShellClient
@_spi(Internal)
public extension DependencyValues { public extension DependencyValues {
/// A ``GitVersionClient`` that can retrieve the current version from a /// 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 /// 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. /// tool based on the current git tag.
/// ///
@_spi(Internal)
@DependencyClient @DependencyClient
public struct GitClient: Sendable { public struct GitClient: Sendable {
@@ -45,9 +41,12 @@ public struct GitClient: Sendable {
try await currentVersion(gitDirectory, exactMatch) 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 gitDirectory: String?
let style: Style let style: Style
@@ -66,7 +65,7 @@ public struct GitClient: Sendable {
} }
public enum Version: Sendable, CustomStringConvertible { enum Version: Sendable, CustomStringConvertible {
case branch(String) case branch(String)
case tag(String) case tag(String)
@@ -79,7 +78,6 @@ public struct GitClient: Sendable {
} }
} }
@_spi(Internal)
extension GitClient: TestDependencyKey { extension GitClient: TestDependencyKey {
/// The ``GitVersionClient`` used in test / debug builds. /// The ``GitVersionClient`` used in test / debug builds.
@@ -253,7 +251,6 @@ extension RawRepresentable where RawValue == String, Self: CustomStringConvertib
var description: String { rawValue } var description: String { rawValue }
} }
@_spi(Internal)
public extension GitClient.Version { public extension GitClient.Version {
static var mocks: [Self] { static var mocks: [Self] {
[ [

View File

@@ -1,6 +1,8 @@
@_spi(Internal) import CliVersion @_spi(Internal) import CliVersion
import Dependencies import Dependencies
import FileClient
import Foundation import Foundation
import GitClient
import Logging import Logging
import Testing import Testing
import TestSupport import TestSupport
@@ -79,11 +81,11 @@ struct CliClientTests {
func gitVersionToSemVar(version: GitClient.Version) { func gitVersionToSemVar(version: GitClient.Version) {
let semVar = version.semVar let semVar = version.semVar
if semVar != nil { if semVar != nil {
#expect(semVar!.versionString(allowPrerelease: false) == "1.0.0") #expect(semVar!.versionString(withPreReleaseTag: false) == "1.0.0")
#expect(semVar!.versionString(allowPrerelease: true) == version.description) #expect(semVar!.versionString(withPreReleaseTag: true) == version.description)
} else { } else {
let semVar = SemVar(preRelease: version.description) 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)")
} }
} }

View File

@@ -1,5 +1,7 @@
@_spi(Internal) import CliVersion @_spi(Internal) import CliVersion
import Dependencies import Dependencies
import FileClient
import GitClient
import ShellClient import ShellClient
import TestSupport import TestSupport
import XCTest import XCTest

View File

@@ -1,4 +1,4 @@
product := cli-version product := "cli-version"
[private] [private]
default: default: