feat: Begins update for more modern swift-dependencies implementation.

This commit is contained in:
2024-12-20 12:54:10 -05:00
parent 1885a90f62
commit 847ddbc7b5
19 changed files with 439 additions and 130 deletions

View File

@@ -1,10 +1,21 @@
import Dependencies
import DependenciesMacros
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
import XCTestDynamicOverlay
public extension DependencyValues {
/// Access a basic ``FileClient`` that can read / write data to the file system.
///
var fileClient: FileClient {
get { self[FileClient.self] }
set { self[FileClient.self] = newValue }
}
}
/// Represents the interactions with the file system. It is able
/// to read from and write to files.
///
@@ -13,25 +24,23 @@ import XCTestDynamicOverlay
/// @Dependency(\.fileClient) var fileClient
/// ```
///
public struct FileClient {
@DependencyClient
public struct FileClient: Sendable {
public var fileExists: @Sendable (URL) -> Bool = { _ in true }
/// Read the contents of a file.
public var read: @Sendable (URL) throws -> String
/// Write `Data` to a file `URL`.
public private(set) var write: (Data, URL) throws -> Void
public private(set) var write: @Sendable (Data, URL) throws -> Void
/// Create a new ``GitVersion/FileClient`` instance.
///
/// This is generally not interacted with directly, instead access as a dependency.
///
///```swift
/// @Dependency(\.fileClient) var fileClient
///```
/// Read the contents of a file at the given path.
///
/// - Parameters:
/// - write: Write the data to a file.
public init(
write: @escaping (Data, URL) throws -> Void
) {
self.write = write
/// - path: The file path to read from.
public func read(_ path: String) throws -> String {
try read(url(for: path))
}
/// Write's the the string to a file path.
@@ -40,8 +49,8 @@ public struct FileClient {
/// - string: The string to write to the file.
/// - path: The file path.
public func write(string: String, to path: String) throws {
let url = try url(for: path)
try self.write(string: string, to: url)
let url = url(for: path)
try write(string: string, to: url)
}
/// Write's the the string to a file path.
@@ -50,49 +59,27 @@ public struct FileClient {
/// - string: The string to write to the file.
/// - url: The file url.
public func write(string: String, to url: URL) throws {
try self.write(Data(string.utf8), url)
try write(Data(string.utf8), url)
}
}
extension FileClient: DependencyKey {
/// A ``FileClient`` that does not do anything.
public static let noop = FileClient.init(
public static let noop = FileClient(
fileExists: { _ in true },
read: { _ in "" },
write: { _, _ in }
)
/// An `unimplemented` ``FileClient``.
public static let testValue = FileClient(
write: unimplemented("\(Self.self).write")
)
public static let testValue = FileClient()
/// The live ``FileClient``
public static let liveValue = FileClient(
fileExists: { FileManager.default.fileExists(atPath: $0.cleanFilePath) },
read: { try String(contentsOf: $0, encoding: .utf8) },
write: { try $0.write(to: $1, options: .atomic) }
)
}
extension DependencyValues {
/// Access a basic ``FileClient`` that can read / write data to the file system.
///
public var fileClient: FileClient {
get { self[FileClient.self] }
set { self[FileClient.self] = newValue }
}
}
// MARK: - Private
fileprivate func url(for path: String) throws -> 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
}