Skip to content

Commit

Permalink
use custom error for godot path error (#10)
Browse files Browse the repository at this point in the history
* use custom error for godot path error

* improve error handling
  • Loading branch information
EstevanBR authored Aug 10, 2024
1 parent 2d18392 commit 0879e58
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions Sources/CreateProject/CreateProject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ private struct CreateProject {
showVersionIfNeeded()

do {
let godotPath = try getGodotPath()

let projectPath = try getProjectPath()

try createProjectPathDirectoryIfNeeded(at: projectPath)
Expand Down Expand Up @@ -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())")
Expand Down Expand Up @@ -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 {
Expand All @@ -137,4 +140,18 @@ private func createProjectPathDirectoryIfNeeded(at projectPath: String) throws {
exit(1)
}
}
}
}

#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

0 comments on commit 0879e58

Please sign in to comment.