diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 62fb4e4..dda7cb9 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -1687,7 +1687,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 951; + CURRENT_PROJECT_VERSION = 955; DEBUG = YES; DEVELOPMENT_TEAM = 8M54J5J787; ENABLE_HARDENED_RUNTIME = YES; @@ -1697,7 +1697,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 11.0; - MARKETING_VERSION = 5.5.0; + MARKETING_VERSION = 5.5.1; PRODUCT_BUNDLE_IDENTIFIER = com.nicoverbruggen.phpmon; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -1714,7 +1714,7 @@ CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 951; + CURRENT_PROJECT_VERSION = 955; DEBUG = NO; DEVELOPMENT_TEAM = 8M54J5J787; ENABLE_HARDENED_RUNTIME = YES; @@ -1724,7 +1724,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 11.0; - MARKETING_VERSION = 5.5.0; + MARKETING_VERSION = 5.5.1; PRODUCT_BUNDLE_IDENTIFIER = com.nicoverbruggen.phpmon; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/phpmon/Common/Core/Paths.swift b/phpmon/Common/Core/Paths.swift index 465951b..89b5d43 100644 --- a/phpmon/Common/Core/Paths.swift +++ b/phpmon/Common/Core/Paths.swift @@ -21,7 +21,7 @@ public class Paths { init() { baseDir = App.architecture != "x86_64" ? .opt : .usr - userName = String(Shell.pipe("whoami").split(separator: "\n")[0]) + userName = String(Shell.pipe("id -un").split(separator: "\n")[0]) } public func detectBinaryPaths() { @@ -57,6 +57,10 @@ public class Paths { return shared.userName } + public static var homePath: String { + return NSHomeDirectory() + } + public static var cellarPath: String { return "\(shared.baseDir.rawValue)/Cellar" } diff --git a/phpmon/Common/Helpers/Filesystem.swift b/phpmon/Common/Helpers/Filesystem.swift index 652ae44..a6d4152 100644 --- a/phpmon/Common/Helpers/Filesystem.swift +++ b/phpmon/Common/Helpers/Filesystem.swift @@ -16,7 +16,7 @@ class Filesystem { */ public static func exists(_ path: String) -> Bool { return FileManager.default.fileExists( - atPath: path.replacingOccurrences(of: "~", with: "/Users/\(Paths.whoami)") + atPath: path.replacingOccurrences(of: "~", with: Paths.homePath) ) } @@ -26,13 +26,29 @@ class Filesystem { public static func fileExists(_ path: String) -> Bool { var isDirectory: ObjCBool = true let exists = FileManager.default.fileExists( - atPath: path.replacingOccurrences(of: "~", with: "/Users/\(Paths.whoami)"), + atPath: path.replacingOccurrences(of: "~", with: Paths.homePath), 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: Paths.homePath), + isDirectory: &isDirectory + ) + + return exists && isDirectory.boolValue + } + + /** + Checks if a given file is a symbolic link. + */ public static func fileIsSymlink(_ path: String) -> Bool { do { let attribs = try FileManager.default.attributesOfItem(atPath: path) @@ -42,17 +58,4 @@ class Filesystem { } } - /** - 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/Common/PHP/PHP Version/PhpHelper.swift b/phpmon/Common/PHP/PHP Version/PhpHelper.swift index 9224635..9775edd 100644 --- a/phpmon/Common/PHP/PHP Version/PhpHelper.swift +++ b/phpmon/Common/PHP/PHP Version/PhpHelper.swift @@ -17,10 +17,10 @@ class PhpHelper { let dotless = version.replacingOccurrences(of: ".", with: "") // Determine the dotless name for this PHP version - let destination = "/Users/\(Paths.whoami)/.config/phpmon/bin/pm\(dotless)" + let destination = "\(Paths.homePath)/.config/phpmon/bin/pm\(dotless)" // Check if the ~/.config/phpmon/bin directory is in the PATH - let inPath = Shell.user.PATH.contains("/Users/\(Paths.whoami)/.config/phpmon/bin") + let inPath = Shell.user.PATH.contains("\(Paths.homePath)/.config/phpmon/bin") // Check if we can create symlinks (`/usr/local/bin` must be writable) let canWriteSymlinks = FileManager.default.isWritableFile(atPath: "/usr/local/bin/") @@ -81,7 +81,7 @@ class PhpHelper { } private static func createSymlink(_ dotless: String) { - let source = "/Users/\(Paths.whoami)/.config/phpmon/bin/pm\(dotless)" + let source = "\(Paths.homePath)/.config/phpmon/bin/pm\(dotless)" let destination = "/usr/local/bin/pm\(dotless)" if !Filesystem.fileExists(destination) { diff --git a/phpmon/Common/PHP/PhpConfigurationFile.swift b/phpmon/Common/PHP/PhpConfigurationFile.swift index bd01dd3..b8b72ca 100644 --- a/phpmon/Common/PHP/PhpConfigurationFile.swift +++ b/phpmon/Common/PHP/PhpConfigurationFile.swift @@ -32,18 +32,11 @@ class PhpConfigurationFile: CreatedFromFile { /** Resolves a PHP configuration file (.ini) */ static func from(filePath: String) -> Self? { - let path = filePath.replacingOccurrences( - of: "~", - with: "/Users/\(Paths.whoami)" - ) + let path = filePath.replacingOccurrences(of: "~", with: Paths.homePath) do { let fileContents = try String(contentsOfFile: path) - - return Self.init( - path: path, - contents: fileContents - ) + return Self.init(path: path, contents: fileContents) } catch { Log.warn("Could not read the PHP configuration file at: `\(filePath)`") return nil diff --git a/phpmon/Domain/Integrations/Nginx/NginxConfigurationFile.swift b/phpmon/Domain/Integrations/Nginx/NginxConfigurationFile.swift index c8dade6..3ea9eee 100644 --- a/phpmon/Domain/Integrations/Nginx/NginxConfigurationFile.swift +++ b/phpmon/Domain/Integrations/Nginx/NginxConfigurationFile.swift @@ -21,18 +21,12 @@ class NginxConfigurationFile: CreatedFromFile { /** Resolves an nginx configuration file (.conf) */ static func from(filePath: String) -> Self? { - let path = filePath.replacingOccurrences( - of: "~", - with: "/Users/\(Paths.whoami)" - ) + let path = filePath.replacingOccurrences(of: "~", with: Paths.homePath) do { let fileContents = try String(contentsOfFile: path) - return Self.init( - path: path, - contents: fileContents - ) + return Self.init(path: path, contents: fileContents) } catch { Log.warn("Could not read the nginx configuration file at: `\(filePath)`") return nil diff --git a/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift b/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift index b5c2f43..e59282c 100644 --- a/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift +++ b/phpmon/Domain/Integrations/Valet/Sites/ValetSite.swift @@ -20,7 +20,7 @@ class ValetSite: DomainListable { /// replacing the user's home folder with ~. lazy var absolutePathRelative: String = { return self.absolutePath - .replacingOccurrences(of: "/Users/\(Paths.whoami)", with: "~") + .replacingOccurrences(of: Paths.homePath, with: "~") }() /// The TLD used to locate this site. diff --git a/phpmon/Domain/Preferences/Preferences.swift b/phpmon/Domain/Preferences/Preferences.swift index ea7f564..1b3e4fd 100644 --- a/phpmon/Domain/Preferences/Preferences.swift +++ b/phpmon/Domain/Preferences/Preferences.swift @@ -222,7 +222,7 @@ class Preferences { moveOutdatedConfigurationFile() // Attempt to load the file if it exists - let url = URL(fileURLWithPath: "/Users/\(Paths.whoami)/.config/phpmon/config.json") + let url = URL(fileURLWithPath: "\(Paths.homePath)/.config/phpmon/config.json") if Filesystem.fileExists(url.path) { Log.info("A custom ~/.config/phpmon/config.json file was found. Attempting to parse...") diff --git a/phpmon/Domain/Presets/Preset.swift b/phpmon/Domain/Presets/Preset.swift index e2d9794..07c6e57 100644 --- a/phpmon/Domain/Presets/Preset.swift +++ b/phpmon/Domain/Presets/Preset.swift @@ -263,7 +263,7 @@ struct Preset: Codable, Equatable { try! String(data: data, encoding: .utf8)! .write( - toFile: "/Users/\(Paths.whoami)/.config/phpmon/preset_revert.json", + toFile: "\(Paths.homePath)/.config/phpmon/preset_revert.json", atomically: true, encoding: .utf8 ) diff --git a/phpmon/Domain/Presets/PresetHelper.swift b/phpmon/Domain/Presets/PresetHelper.swift index 86bc0ae..6ff1242 100644 --- a/phpmon/Domain/Presets/PresetHelper.swift +++ b/phpmon/Domain/Presets/PresetHelper.swift @@ -16,7 +16,7 @@ class PresetHelper { public static func loadRollbackPresetFromFile() { guard let revert = try? String( - contentsOfFile: "/Users/\(Paths.whoami)/.config/phpmon/preset_revert.json", + contentsOfFile: "\(Paths.homePath)/.config/phpmon/preset_revert.json", encoding: .utf8 ) else { PresetHelper.rollbackPreset = nil diff --git a/phpmon/Domain/Warnings/WarningManager.swift b/phpmon/Domain/Warnings/WarningManager.swift index e56d977..079996a 100644 --- a/phpmon/Domain/Warnings/WarningManager.swift +++ b/phpmon/Domain/Warnings/WarningManager.swift @@ -32,7 +32,7 @@ class WarningManager { ), Warning( command: { - return !Shell.user.PATH.contains("/Users/\(Paths.whoami)/.config/phpmon/bin") && + return !Shell.user.PATH.contains("\(Paths.homePath)/.config/phpmon/bin") && !FileManager.default.isWritableFile(atPath: "/usr/local/bin/") }, name: "Helpers cannot be symlinked and not in PATH",