Root folder creation (#2)

This PR contains the work done to create the root folder of a new project when executing the _colibri_ executable. In addition, some other work has been done:

* added the `ArgumentParser` package dependency to the package;
* implemented the `FileService` service in the _library_ target;
* implemented the `CreateRootFolderTask` task in the _library_ target;
* removed some unnecessary comments and boilerplate code;

Reviewed-on: #2
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #2.
This commit is contained in:
2025-01-27 23:54:50 +00:00
committed by Javier Cicchelli
parent d0d47d280d
commit b8c354e614
12 changed files with 619 additions and 5 deletions
+6 -5
View File
@@ -4,10 +4,11 @@ import ColibriLibrary
@main
struct Colibri: AsyncParsableCommand {
// MARK: Functions
func run() async throws {
// ...
}
// MARK: Properties
static let configuration = CommandConfiguration(
abstract: "The utility to manage your Hummingbird apps",
subcommands: [Create.self]
)
}
+56
View File
@@ -0,0 +1,56 @@
import ArgumentParser
import ColibriLibrary
import Foundation
extension Colibri {
struct Create: AsyncParsableCommand {
// MARK: Properties
static let configuration = CommandConfiguration(
commandName: "create-project",
abstract: "Create a new, tailored Hummingbird app",
helpNames: .shortAndLong,
aliases: ["create"]
)
@OptionGroup var options: Options
// MARK: Functions
mutating func run() async throws {
let fileService = FileService()
let createRootFolder = CreateRootFolderTask(fileService: fileService)
let rootFolder = try await createRootFolder(
name: options.name,
at: options.locationURL
)
print(rootFolder)
}
}
}
// MARK: - Options
extension Colibri.Create {
struct Options: ParsableArguments {
// MARK: Properties
@Option(name: .shortAndLong)
var name: String
@Option(name: .shortAndLong)
var location: String?
// MARK: Computed
var locationURL: URL? {
location.flatMap { URL(fileURLWithPath: $0) }
}
}
}
@@ -0,0 +1,15 @@
import Foundation
extension URL {
// MARK: Initialisers
init(at filePath: String) {
if #available(macOS 13.0, *) {
self = URL(filePath: filePath)
} else {
self = URL(fileURLWithPath: filePath)
}
}
}
@@ -0,0 +1,25 @@
import Foundation
public protocol FileServicing {
// MARK: Computed
var currentFolder: URL { get async }
// MARK: Functions
func createFolder(at url: URL) async throws (FileServiceError)
func delete(at url: URL) async throws (FileServiceError)
func exists(at url: URL) async throws (FileServiceError) -> Bool
}
// MARK: - Errors
public enum FileServiceError: Error, Equatable {
case folderNotCreated
case urlAlreadyExists
case urlNotDeleted
case urlNotExists
case urlNotFileURL
}
@@ -0,0 +1,78 @@
import Foundation
public struct FileService: FileServicing {
// MARK: Properties
private let fileManager: FileManager
// MARK: Initialisers
public init(fileManager: FileManager = .default) {
self.fileManager = fileManager
}
// MARK: Computed
public var currentFolder: URL {
get async {
.init(at: fileManager.currentDirectoryPath)
}
}
// MARK: Functions
public func createFolder(at url: URL) async throws (FileServiceError) {
guard try await !exists(at: url) else {
throw FileServiceError.urlAlreadyExists
}
do {
try fileManager.createDirectory(
at: url,
withIntermediateDirectories: true
)
} catch {
throw FileServiceError.folderNotCreated
}
}
public func delete(at url: URL) async throws (FileServiceError) {
guard try await exists(at: url) else {
throw FileServiceError.urlNotExists
}
do {
try fileManager.removeItem(at: url)
} catch {
throw FileServiceError.urlNotDeleted
}
}
public func exists(at url: URL) async throws (FileServiceError) -> Bool {
guard url.isFileURL else {
throw FileServiceError.urlNotFileURL
}
let filePath = getPath(for: url)
return fileManager.fileExists(atPath: filePath)
}
}
// MARK: - Helpers
private extension FileService {
// MARK: Functions
func getPath(for url: URL) -> String {
if #available(macOS 13.0, *) {
return url.path()
} else {
return url.path
}
}
}
@@ -0,0 +1,48 @@
import Foundation
public struct CreateRootFolderTask {
// MARK: Properties
private let fileService: FileServicing
// MARK: Initialisers
public init(fileService: FileServicing) {
self.fileService = fileService
}
// MARK: Functions
public func callAsFunction(
name: String,
at location: URL? = nil
) async throws -> URL {
guard !name.isEmpty else {
throw CreateRootFolderError.nameIsEmpty
}
let rootFolder = if let location {
location
} else {
await fileService.currentFolder
}
let newFolder = if #available(macOS 13.0, *) {
rootFolder.appending(path: name)
} else {
rootFolder.appendingPathComponent(name)
}
try await fileService.createFolder(at: newFolder)
return newFolder
}
}
// MARK: - Errors
public enum CreateRootFolderError: Error {
case nameIsEmpty
}