diff --git a/phpmon/Common/Helpers/Filesystem.swift b/phpmon/Common/Helpers/Filesystem.swift index 129763c..a54f665 100644 --- a/phpmon/Common/Helpers/Filesystem.swift +++ b/phpmon/Common/Helpers/Filesystem.swift @@ -1,5 +1,5 @@ // -// FileSystem.swift +// Filesystem.swift // PHP Monitor // // Created by Nico Verbruggen on 07/12/2021. @@ -7,17 +7,43 @@ // import Cocoa +import Foundation class Filesystem { /** - Checks if a file exists at the provided path. - Uses `FileManager`. + Checks if a file or directory exists at the provided path. */ - public static func fileExists(_ path: String) -> Bool { + public static func exists(_ path: String) -> Bool { return FileManager.default.fileExists( atPath: path.replacingOccurrences(of: "~", with: "/Users/\(Paths.whoami)") ) } + /** + Checks if a file exists at the provided path. + */ + public static func fileExists(_ path: String) -> Bool { + var isDirectory: ObjCBool = true + let exists = FileManager.default.fileExists( + atPath: path.replacingOccurrences(of: "~", with: "/Users/\(Paths.whoami)"), + isDirectory: &isDirectory + ) + + return exists && !isDirectory.boolValue + } + + /** + Checks if a directory exists at the provided path. + */ + public static func directoryExists(_ path: String) -> Bool { + var isDirectory: ObjCBool = true + let exists = FileManager.default.fileExists( + atPath: path.replacingOccurrences(of: "~", with: "/Users/\(Paths.whoami)"), + isDirectory: &isDirectory + ) + + return exists && isDirectory.boolValue + } + } diff --git a/phpmon/Domain/App/Startup.swift b/phpmon/Domain/App/Startup.swift index 38ebf9b..08245cc 100644 --- a/phpmon/Domain/App/Startup.swift +++ b/phpmon/Domain/App/Startup.swift @@ -167,6 +167,18 @@ class Startup { descriptionText: "startup.errors.services_json_error.desc".localized ), // ================================================================================= + // Determine that Valet is installed + // ================================================================================= + EnvironmentCheck( + command: { + return !Filesystem.directoryExists("~/.config/valet") + }, + name: "`.config/valet` not empty (Valet installed)", + titleText: "startup.errors.valet_not_installed.title".localized, + subtitleText: "startup.errors.valet_not_installed.subtitle".localized, + descriptionText: "startup.errors.valet_not_installed.desc".localized + ), + // ================================================================================= // Determine that the Valet configuration JSON file is valid. // ================================================================================= EnvironmentCheck( @@ -224,7 +236,7 @@ class Startup { titleText: "startup.errors.valet_version_unknown.title".localized, subtitleText: "startup.errors.valet_version_unknown.subtitle".localized, descriptionText: "startup.errors.valet_version_unknown.desc".localized - ) + ), ] // MARK: - EnvironmentCheck struct diff --git a/phpmon/Domain/DomainList/AddSiteVC.swift b/phpmon/Domain/DomainList/AddSiteVC.swift index 43c2603..7a0193e 100644 --- a/phpmon/Domain/DomainList/AddSiteVC.swift +++ b/phpmon/Domain/DomainList/AddSiteVC.swift @@ -55,7 +55,7 @@ class AddSiteVC: NSViewController, NSTextFieldDelegate { let path = pathControl.url!.path let name = inputDomainName.stringValue - if !FileManager.default.fileExists(atPath: path) { + if !Filesystem.exists(path) { Alert.confirm( onWindow: view.window!, messageText: "domain_list.alert.folder_missing.title".localized, diff --git a/phpmon/Domain/Integrations/Composer/PhpFrameworks.swift b/phpmon/Domain/Integrations/Composer/PhpFrameworks.swift index fa72d92..95fc0d0 100644 --- a/phpmon/Domain/Integrations/Composer/PhpFrameworks.swift +++ b/phpmon/Domain/Integrations/Composer/PhpFrameworks.swift @@ -71,7 +71,7 @@ struct PhpFrameworks { public static func detectFallbackDependency(_ basePath: String) -> String? { for entry in Self.FileMapping { let found = entry.value - .map { path in return Filesystem.fileExists(basePath + path) } + .map { path in return Filesystem.exists(basePath + path) } .contains(true) if found { diff --git a/phpmon/Domain/Watcher/PhpConfigWatcher.swift b/phpmon/Domain/Watcher/PhpConfigWatcher.swift index b3ad98d..b1b1e45 100644 --- a/phpmon/Domain/Watcher/PhpConfigWatcher.swift +++ b/phpmon/Domain/Watcher/PhpConfigWatcher.swift @@ -51,7 +51,7 @@ class PhpConfigWatcher { eventMask: DispatchSource.FileSystemEvent, behaviour: FSWatcherBehaviour = .reloadsMenu ) { - if !Filesystem.fileExists(url.path) { + if !Filesystem.exists(url.path) { Log.warn("No watcher was created for \(url.path) because the requested file does not exist.") return } diff --git a/phpmon/Localizable.strings b/phpmon/Localizable.strings index 794eb52..85f8594 100644 --- a/phpmon/Localizable.strings +++ b/phpmon/Localizable.strings @@ -435,6 +435,12 @@ You can do this by running `composer global update` in your terminal. After that "startup.errors.valet_version_unknown.subtitle" = "Parsing the output of `valet --version` failed. Make sure your Valet installation works and is up-to-date."; "startup.errors.valet_version_unknown.desc" = "Try running `valet --version` in a terminal to find out what's going on."; +"startup.errors.valet_not_installed.title" = "Your Valet configuration directory is missing"; +"startup.errors.valet_not_installed.subtitle" = "The required directory `~/.config/valet` is missing. This usually means that you forgot to run `valet install`."; +"startup.errors.valet_not_installed.desc" = "Assuming you already installed Valet via Composer, please run `valet install` to finish setting up Laravel Valet. + +If you are seeing this message but are confused why this folder has gone missing, then you may want to investigate why it is gone—it shouldn't just disappear and it means your Valet installation is broken."; + /// Brew & sudoers "startup.errors.sudoers_brew.title" = "Brew has not been added to sudoers.d"; "startup.errors.sudoers_brew.subtitle" = "You must run `sudo valet trust` to ensure Valet can start and stop services without having to use sudo every time. The app will not work correctly until you resolve this issue.";