From 0879e582f570b378dac55da59a11d1ac1de372e6 Mon Sep 17 00:00:00 2001 From: Estevan Hernandez Date: Fri, 9 Aug 2024 20:56:15 -0500 Subject: [PATCH] use custom error for godot path error (#10) * use custom error for godot path error * improve error handling --- Sources/CreateProject/CreateProject.swift | 45 ++++++++++++++++------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/Sources/CreateProject/CreateProject.swift b/Sources/CreateProject/CreateProject.swift index dd29e52..a096cda 100644 --- a/Sources/CreateProject/CreateProject.swift +++ b/Sources/CreateProject/CreateProject.swift @@ -11,6 +11,8 @@ private struct CreateProject { showVersionIfNeeded() do { + let godotPath = try getGodotPath() + let projectPath = try getProjectPath() try createProjectPathDirectoryIfNeeded(at: projectPath) @@ -40,16 +42,6 @@ private struct CreateProject { exit(0) } - let godotPath = try getGodotPath() - - #if os(macOS) - guard !godotPath.hasSuffix(".app") else { - print(color: .red, "GODOT path ends in .app, it should be the path to executable itself") - print(color: .yellow, "try \(godotPath)/Contents/MacOS/Godot") - exit(1) - } - #endif - print(color: .green, "Created \(try FileFactory.createPackageFile(projectName: projectName, executableName: executableName))") print(color: .green, "Created \(try FileFactory.copyReadmeFile())") print(color: .green, "Created \(try FileFactory.copyGitIgnoreFile())") @@ -109,10 +101,21 @@ private func getExecutableName() throws -> String { } private func getGodotPath() throws -> String { - switch ProcessInfo.processInfo.environment["GODOT"] { - case .some(let value) where value.isEmpty == false: value - default: try UserChoice.get(message: Color.yellow + "GODOT not set\n" + "Please enter the full path to the Godot 4.2 executable: ") + let godotPath: String + + godotPath = if let godotPathFromEnv = ProcessInfo.processInfo.environment["GODOT"] { + godotPathFromEnv + } else { + try UserChoice.get(message: Color.yellow + "Missing GODOT environment variable\n" + "Please enter the full path to the Godot 4.2 executable: ") + } + + #if os(macOS) + guard !godotPath.hasSuffix(".app") else { + throw GodotPathError(path: godotPath) } + #endif + + return godotPath } private func checkFor(_ argument: UserChoice.Argument, promptIfNeeded prompt: String) throws -> String { @@ -137,4 +140,18 @@ private func createProjectPathDirectoryIfNeeded(at projectPath: String) throws { exit(1) } } -} \ No newline at end of file +} + +#if os(macOS) +private struct GodotPathError: Swift.Error, LocalizedError { + let path: String + + var errorDescription: String? { + "\(path) ends in .app" + } + + var recoverySuggestion: String? { + "try \(path)/Contents/MacOS/Godot" + } +} +#endif