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_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

View File

@@ -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"]

View File

@@ -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)

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
// 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
}

View File

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

View File

@@ -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
}

View File

@@ -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] {
[

View File

@@ -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)")
}
}

View File

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

View File

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