diff --git a/Sources/FileBuilder/File/DataFile.swift b/Sources/FileBuilder/File/DataFile.swift index 484701a..d733860 100644 --- a/Sources/FileBuilder/File/DataFile.swift +++ b/Sources/FileBuilder/File/DataFile.swift @@ -3,17 +3,17 @@ import Foundation public struct DataFile: File { - private let name: String + private let name: FileName private let data: Data - public init(_ name: String, data: Data) { + public init(_ name: FileName, data: Data) { self.name = name self.data = data } public var file: some File { BuiltinFile { directory, _ in - let url = directory.appendingPathComponent(name) + let url = directory.appending(name) try data.write(to: url) } } diff --git a/Sources/FileBuilder/File/Directory.swift b/Sources/FileBuilder/File/Directory.swift index 1d39ce0..b408559 100644 --- a/Sources/FileBuilder/File/Directory.swift +++ b/Sources/FileBuilder/File/Directory.swift @@ -3,11 +3,11 @@ import Foundation public struct Directory: File { - private let name: String + private let name: FileName private let content: Content public init( - _ name: String, + _ name: FileName, @FileBuilder content: () -> Content ) { self.name = name @@ -16,7 +16,7 @@ public struct Directory: File { public var file: some File { BuiltinFile { directory, environment in - let url = directory.appendingPathComponent(name) + let url = directory.appending(name) try FileManager().createDirectory(at: url, withIntermediateDirectories: true) try content.write(in: url, environment: environment) } @@ -25,7 +25,7 @@ public struct Directory: File { extension Directory { - public init(_ name: String) { + public init(_ name: FileName) { self.init(name) { Empty() } diff --git a/Sources/FileBuilder/File/FileName.swift b/Sources/FileBuilder/File/FileName.swift new file mode 100644 index 0000000..0b6d365 --- /dev/null +++ b/Sources/FileBuilder/File/FileName.swift @@ -0,0 +1,30 @@ +import Foundation + +public struct FileName: Equatable, Hashable { + fileprivate let value: String + public init(_ value: String) { + self.value = value + } +} + +extension FileName: ExpressibleByStringLiteral { + public init(stringLiteral value: String) { + self.init(value) + } +} + +extension FileName: ExpressibleByStringInterpolation {} + +extension FileName: CustomStringConvertible { + public var description: String { value } +} + +extension FileName: CustomDebugStringConvertible { + public var debugDescription: String { value } +} + +extension URL { + func appending(_ name: FileName) -> URL { + appendingPathComponent(name.value) + } +} diff --git a/Sources/FileBuilder/File/TextFile.swift b/Sources/FileBuilder/File/TextFile.swift index bbc180b..431723b 100644 --- a/Sources/FileBuilder/File/TextFile.swift +++ b/Sources/FileBuilder/File/TextFile.swift @@ -4,11 +4,11 @@ import Foundation public struct TextFile: File { @Environment(\.stringEncoding) private var encoding: String.Encoding - private let name: String + private let name: FileName private let text: Content public init( - _ name: String, + _ name: FileName, @TextBuilder text: () -> Content ) { self.name = name @@ -21,12 +21,12 @@ public struct TextFile: File { guard let data = string.data(using: encoding) else { throw TextFileFailure(name: name) } - let url = directory.appendingPathComponent(name) + let url = directory.appending(name) try data.write(to: url) } } } struct TextFileFailure: Error { - let name: String + let name: FileName }