Implemented the CopyFilesTask task in the library module.

This commit is contained in:
2025-01-18 03:06:39 +01:00
parent 647c5bd32a
commit 1466bff250
6 changed files with 78 additions and 67 deletions
@@ -16,7 +16,7 @@ extension URL {
var pathString: String {
if #available(macOS 13.0, *) {
path()
path(percentEncoded: true)
} else {
path
}
@@ -23,9 +23,6 @@ public struct FileService: FileServicing {
// MARK: Functions
public func copyFile(from source: URL, to destination: URL) async throws (FileServiceError) {
guard try await isItemExists(at: source) else {
throw FileServiceError.itemNotExists
}
guard try await !isItemExists(at: destination) else {
throw FileServiceError.itemAlreadyExists
}
+20 -34
View File
@@ -4,50 +4,36 @@ public struct CopyFilesTask {
// MARK: Properties
private let bundleService: BundleServicing
private let fileService: FileServicing
// MARK: Initialisers
public init(fileService: FileServicing) {
public init(
bundleService: BundleServicing? = nil,
fileService: FileServicing
) {
self.bundleService = bundleService ?? Bundle.module
self.fileService = fileService
}
// MARK: Functions
public func callAsFunction(to rootFolder: URL) async throws {
let filesFolder = URL(at: .folderFiles)
for fileToCopy in Self.filesToCopy {
try await fileService.copyItem(
from: filesFolder.appendingPath(fileToCopy),
to: rootFolder.appendingPath(fileToCopy)
)
public func callAsFunction(to rootFolder: URL) async throws (FileServiceError) {
for resource in ResourceFile.allCases {
guard let source = bundleService.url(
forResource: resource.rawValue,
withExtension: nil,
subdirectory: "Resources/Files"
) else {
assertionFailure("URL should have been initialized.")
return
}
let destination = rootFolder.appendingPath(resource.fileName)
try await fileService.copyFile(from: source, to: destination)
}
}
}
// MARK: - Helpers
extension CopyFilesTask {
// MARK: Constants
static let filesToCopy: [String] = [
.fileDockerIgnore,
.fileGitIgnore,
.fileLicense,
.fileReadme
]
}
// MARK: - URL+Constants
private extension String {
static let folderFiles = "./Resources/Files"
static let fileDockerIgnore = ".dockerignore"
static let fileGitIgnore = ".gitignore"
static let fileLicense = "LICENSE"
static let fileReadme = "README.md"
}
+4 -13
View File
@@ -31,19 +31,10 @@ extension CreateFoldersTask {
// MARK: Constants
static let foldersToCreate: [String] = [
.folderApp,
.folderAppInfrastructure,
.folderAppTestCases,
.folderAppTestSources
"Sources/App",
"Sources/AppInfrastructure",
"Tests/App/Cases",
"Tests/App/Sources"
]
}
// MARK: - String+Constants
private extension String {
static let folderApp = "Sources/App"
static let folderAppInfrastructure = "Sources/AppInfrastructure"
static let folderAppTestCases = "Tests/App/Cases"
static let folderAppTestSources = "Tests/App/Sources"
}