From d8babdbf31b450ac5a2a03bab8c9d48ed62e5289 Mon Sep 17 00:00:00 2001 From: Tom Ludwig Date: Mon, 12 Feb 2024 17:08:30 +0100 Subject: [PATCH 1/2] Ensure Folder Existence; Create Missing Parent Folders as Needed Signed-off-by: Tom Ludwig --- Sources/CodeEditCLI/Open.swift | 37 +++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/Sources/CodeEditCLI/Open.swift b/Sources/CodeEditCLI/Open.swift index fd988e4..7b7d680 100644 --- a/Sources/CodeEditCLI/Open.swift +++ b/Sources/CodeEditCLI/Open.swift @@ -26,19 +26,42 @@ extension CodeEditCLI { func run() throws { let task = Process() + let fileManager = FileManager.default // use the `open` cli as the executable task.launchPath = "/usr/bin/open" - if let path { - let (path, line, column) = try extractLineColumn(path) - let openURL = try absolutePath(path, for: task) + if let path = path { + let (filePath, line, column) = try extractLineColumn(path) + let openURL = try absolutePath(filePath, for: task) + + // Create directories if they don't exist + let directoryURL = openURL.deletingLastPathComponent() + do { + try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil) + } catch { + print("Failed to create directory at \(directoryURL.path): \(error)") + return + } - // open CodeEdit using the url scheme - if let line, !openURL.hasDirectoryPath { - task.arguments = ["-u", "codeedit://\(openURL.path):\(line):\(column ?? 1)"] + if fileManager.fileExists(atPath: openURL.path) { + // File exists, proceed to open it + if let line = line, !openURL.hasDirectoryPath { + task.arguments = ["-u", "codeedit://\(openURL.path):\(line):\(column ?? 1)"] + } else { + task.arguments = ["-u", "codeedit://\(openURL.path)"] + } } else { - task.arguments = ["-u", "codeedit://\(openURL.path)"] + // File doesn't exist, create one + let success = fileManager.createFile(atPath: openURL.path, contents: nil, attributes: nil) + if success { + // Proceed to open the newly created file + task.arguments = ["-u", "codeedit://\(openURL.path)"] + } else { + // Handle error if file creation fails + print("Failed to create file at \(openURL.path)") + return + } } } else { task.arguments = ["-a", "CodeEdit.app"] From 95e34893aca5c35299e106a61fba14934ead68a9 Mon Sep 17 00:00:00 2001 From: Tom Ludwig Date: Mon, 12 Feb 2024 17:21:03 +0100 Subject: [PATCH 2/2] Fixed linter issues --- Sources/CodeEditCLI/Open.swift | 6 +++++- Sources/CodeEditCLI/main.swift | 2 -- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/CodeEditCLI/Open.swift b/Sources/CodeEditCLI/Open.swift index 7b7d680..81f4e71 100644 --- a/Sources/CodeEditCLI/Open.swift +++ b/Sources/CodeEditCLI/Open.swift @@ -38,7 +38,11 @@ extension CodeEditCLI { // Create directories if they don't exist let directoryURL = openURL.deletingLastPathComponent() do { - try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil) + try fileManager.createDirectory( + at: directoryURL, + withIntermediateDirectories: true, + attributes: nil + ) } catch { print("Failed to create directory at \(directoryURL.path): \(error)") return diff --git a/Sources/CodeEditCLI/main.swift b/Sources/CodeEditCLI/main.swift index 0919243..336cf24 100644 --- a/Sources/CodeEditCLI/main.swift +++ b/Sources/CodeEditCLI/main.swift @@ -26,8 +26,6 @@ struct CodeEditCLI: ParsableCommand { defaultSubcommand: Open.self ) - init() {} - enum CLIError: Error { case invalidWorkingDirectory case invalidFileURL