From d13714c1ea9e51b3f149162557b16a78027ba26f Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 7 Dec 2021 12:42:20 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Improved=20code=20editor?= =?UTF-8?q?=20detection=20(#60)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now correctly detects the following apps that can open a directory: - PhpStorm (installed via Toolbox) - Sublime Text - Sublime Merge This in addition to: - PhpStorm (manual installation) - Visual Studio Code These need to be installed in the default location. For VS Code to work, you need to have added `code` to your PATH. --- PHP Monitor.xcodeproj/project.pbxproj | 8 ++- README.md | 16 +++++ phpmon/Domain/Helpers/Editor.swift | 89 +++++++++++++++++++++++++ phpmon/Domain/Menu/StatusMenu.swift | 4 ++ phpmon/Domain/SiteList/SiteListVC.swift | 53 ++++----------- 5 files changed, 129 insertions(+), 41 deletions(-) create mode 100644 phpmon/Domain/Helpers/Editor.swift diff --git a/PHP Monitor.xcodeproj/project.pbxproj b/PHP Monitor.xcodeproj/project.pbxproj index 4244974..9481b8d 100644 --- a/PHP Monitor.xcodeproj/project.pbxproj +++ b/PHP Monitor.xcodeproj/project.pbxproj @@ -9,6 +9,8 @@ /* Begin PBXBuildFile section */ 5420395926135DC100FB00FA /* PrefsVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5420395826135DC100FB00FA /* PrefsVC.swift */; }; 5420395F2613607600FB00FA /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5420395E2613607600FB00FA /* Preferences.swift */; }; + 54B48B5F275F66AE006D90C5 /* Editor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B48B5E275F66AE006D90C5 /* Editor.swift */; }; + 54B48B60275F66AE006D90C5 /* Editor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54B48B5E275F66AE006D90C5 /* Editor.swift */; }; 54EAC806262F212B0092D14E /* GlobalKeybindPreference.swift in Sources */ = {isa = PBXBuildFile; fileRef = C41CD0282628D8EE0065BBED /* GlobalKeybindPreference.swift */; }; C405A4D024B9B9140062FAFA /* InternetAccessPolicy.strings in Resources */ = {isa = PBXBuildFile; fileRef = C405A4CE24B9B9130062FAFA /* InternetAccessPolicy.strings */; }; C405A4D124B9B9140062FAFA /* InternetAccessPolicy.plist in Resources */ = {isa = PBXBuildFile; fileRef = C405A4CF24B9B9140062FAFA /* InternetAccessPolicy.plist */; }; @@ -116,6 +118,7 @@ /* Begin PBXFileReference section */ 5420395826135DC100FB00FA /* PrefsVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrefsVC.swift; sourceTree = ""; }; 5420395E2613607600FB00FA /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = ""; }; + 54B48B5E275F66AE006D90C5 /* Editor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Editor.swift; sourceTree = ""; }; C405A4CE24B9B9130062FAFA /* InternetAccessPolicy.strings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; path = InternetAccessPolicy.strings; sourceTree = ""; }; C405A4CF24B9B9140062FAFA /* InternetAccessPolicy.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = InternetAccessPolicy.plist; sourceTree = ""; }; C412E5FB25700D5300A1FB67 /* HomebrewPackage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomebrewPackage.swift; sourceTree = ""; }; @@ -317,8 +320,9 @@ isa = PBXGroup; children = ( C476FF9722B0DD830098105B /* Alert.swift */, - C41C1B4822B00A9800E7CF16 /* MenuBarImageGenerator.swift */, + 54B48B5E275F66AE006D90C5 /* Editor.swift */, C474B00524C0E98C00066A22 /* LocalNotification.swift */, + C41C1B4822B00A9800E7CF16 /* MenuBarImageGenerator.swift */, C4CCBA6B275C567B008C7055 /* PMWindowController.swift */, ); path = Helpers; @@ -536,6 +540,7 @@ C4CCBA6C275C567B008C7055 /* PMWindowController.swift in Sources */, C41CD0292628D8EE0065BBED /* GlobalKeybindPreference.swift in Sources */, C42295DD2358D02000E263B2 /* Command.swift in Sources */, + 54B48B5F275F66AE006D90C5 /* Editor.swift in Sources */, C4B97B78275CF1B5003F3378 /* App+ActivationPolicy.swift in Sources */, C4811D2422D70A4700B5F6B3 /* App.swift in Sources */, C41C1B4922B00A9800E7CF16 /* MenuBarImageGenerator.swift in Sources */, @@ -567,6 +572,7 @@ files = ( 54EAC806262F212B0092D14E /* GlobalKeybindPreference.swift in Sources */, C4F780C425D80B75000DBC97 /* MainMenu.swift in Sources */, + 54B48B60275F66AE006D90C5 /* Editor.swift in Sources */, C4F780C825D80B75000DBC97 /* DateExtension.swift in Sources */, C4F780CC25D80B75000DBC97 /* ActivePhpInstallation.swift in Sources */, C4F780B125D80B4D000DBC97 /* PhpExtension.swift in Sources */, diff --git a/README.md b/README.md index 2c84e63..d9410d9 100644 --- a/README.md +++ b/README.md @@ -230,6 +230,22 @@ PHP Monitor itself doesn't do any network requests. Feel free to check the sourc +
+How do I get various applications to show up in the domain list's right-click menu? + +When you select and right-click on a domain, you can open these directories with various applications. This can help speed up your workflow. However, for these apps to show up, they must be detected first. + +The supported apps are: PhpStorm, Visual Studio Code, Sublime Text, Sublime Merge. + +For Visual Studio Code, you need to have `code` available in your PATH. [More info](https://code.visualstudio.com/docs/editor/command-line) here. + +For Sublime Text (and Sublime Merge), the apps need to be in your `/Applications` directory. + +For PhpStorm, both a standalone installation (in `/Applications`) and an installation via JetBrains Toolbox are can be detected. If you happen to have both installed, they are listed separately. + +To see which files are checked to determine availability, see [this file](./phpmon/Domain/Helpers/Editor.swift). +
+
After running PHP Monitor, Homebrew sometimes has issues with `brew upgrade` or `brew cleanup`! diff --git a/phpmon/Domain/Helpers/Editor.swift b/phpmon/Domain/Helpers/Editor.swift new file mode 100644 index 0000000..e067962 --- /dev/null +++ b/phpmon/Domain/Helpers/Editor.swift @@ -0,0 +1,89 @@ +// +// Editor.swift +// PHP Monitor +// +// Created by Nico Verbruggen on 07/12/2021. +// Copyright © 2021 Nico Verbruggen. All rights reserved. +// + +import Foundation + +/// An application that is capable of opening a particular directory (usually of a PHP project). +/// In most cases this is going to be a code editor, but it could also be another application +/// that supports opening those directories, like a visual Git client. +class Editor { + + /// Name of the editor. Used for display purposes. + let name: String + + /// Path to check whether the application is actually installed. + /// This was previously called `path` but the new variable name is a bit more clear. + /// To be clear, this is *not* the path to the actual binary! + let pathToVerifyInstalled: String + + /// Callback that is executed to open a particular folder. Can be different from the installation path or the procedure required to determine whether the application is installed. + @objc let openCallback: (String) -> Void + + /** + - Parameter name: Name of the editor. + - Parameter path: File to verify, if this file exists here the app is considered present on the system. + - Parameter open: Callback used to open a specific directory in the editor in question. + */ + init(name: String, path: String, open: @escaping ((String) -> Void)) { + self.name = name + self.pathToVerifyInstalled = path.replacingOccurrences(of: " ", with: "\\ ") + self.openCallback = open + } + + /** + Attempt to open a specific directory in the editor of choice. This will open the editor if it isn't open yet. + */ + @objc public func openDirectory(file: String) { + self.openCallback(file) + } + + /** + Detect which "editors" are available to open a specific directory. + */ + static public func detect() -> [Editor] { + return [ + Editor( + name: "PhpStorm", + path: "/Applications/PhpStorm.app/Contents/Info.plist", + open: { path in + Shell.run("open -a /Applications/PhpStorm.app \(path)") + } + ), + Editor( + name: "PhpStorm (via Toolbox)", + path: "~/Applications/JetBrains Toolbox/PhpStorm.app/Contents/Info.plist", + open: { path in + Shell.run("open -a ~/Applications/JetBrains\\ Toolbox/PhpStorm.app \(path)") + } + ), + Editor( + name: "Visual Studio Code", + path: "/usr/local/bin/code", + open: { path in + Shell.run("/usr/local/bin/code \(path)") + } + ), + Editor( + name: "Sublime Text", + path: "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl", + open: { path in + Shell.run("/Applications/Sublime\\ Text.app/Contents/SharedSupport/bin/subl \(path)") + } + ), + Editor( + name: "Sublime Merge", + path: "/Applications/Sublime Merge.app/Contents/SharedSupport/bin/smerge", + open: { path in + Shell.run("/Applications/Sublime\\ Merge.app/Contents/SharedSupport/bin/smerge \(path)") + } + ) + ].filter { editor in + Shell.fileExists(editor.pathToVerifyInstalled) + } + } +} diff --git a/phpmon/Domain/Menu/StatusMenu.swift b/phpmon/Domain/Menu/StatusMenu.swift index 84b2f9e..0d6dfc8 100644 --- a/phpmon/Domain/Menu/StatusMenu.swift +++ b/phpmon/Domain/Menu/StatusMenu.swift @@ -166,3 +166,7 @@ class PhpMenuItem: NSMenuItem { class ExtensionMenuItem: NSMenuItem { var phpExtension: PhpExtension? = nil } + +class EditorMenuItem: NSMenuItem { + var editor: Editor? = nil +} diff --git a/phpmon/Domain/SiteList/SiteListVC.swift b/phpmon/Domain/SiteList/SiteListVC.swift index 83af04d..35ebd05 100644 --- a/phpmon/Domain/SiteList/SiteListVC.swift +++ b/phpmon/Domain/SiteList/SiteListVC.swift @@ -23,7 +23,7 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { var sites: [Valet.Site] = [] /// Array that contains various editors that might open a particular site directory. - var editorAvailability: [String] = [] + var editors: [Editor] = Editor.detect() /// String that was last searched for. Empty by default. var lastSearchedFor = "" @@ -70,7 +70,6 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { // MARK: - Lifecycle override func viewDidLoad() { - determineEditorAvailability() sites = Valet.shared.sites setUINotBusy() } @@ -182,30 +181,6 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { } } - // MARK: Open with IDE / Editor - - /** - Find out which editors are available on the user’s system. - Currently only PHPStorm and Visual Studio Code are detected. - */ - private func determineEditorAvailability() { - if (Shell.fileExists("/usr/local/bin/code")) { - editorAvailability.append("vscode") - } - - if (Shell.fileExists("/Applications/PhpStorm.app/Contents/Info.plist")) { - editorAvailability.append("phpstorm") - } - } - - @objc public func openWithPhpStorm() { - Shell.run("open -a /Applications/PhpStorm.app \(selectedSite!.absolutePath!)") - } - - @objc public func openWithVSCode() { - Shell.run("/usr/local/bin/code \(selectedSite!.absolutePath!)") - } - // MARK: Open in Browser & Finder @objc public func openInBrowser() { @@ -256,23 +231,17 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { keyEquivalent: "L" ) - if (editorAvailability.count > 0) { + if (editors.count > 0) { menu.addItem(NSMenuItem.separator()) - if editorAvailability.contains("vscode") { - menu.addItem( - withTitle: "site_list.open_with_vs_code".localized, - action: #selector(self.openWithVSCode), - keyEquivalent: "" - ) - } - - if editorAvailability.contains("phpstorm") { - menu.addItem( - withTitle: "site_list.open_with_pstorm".localized, - action: #selector(self.openWithPhpStorm), - keyEquivalent: "" + for (index, editor) in editors.enumerated() { + let editorMenuItem = EditorMenuItem( + title: "Open with \(editor.name)", + action: #selector(self.openWithEditor(sender:)), + keyEquivalent: "\(index + 1)" ) + editorMenuItem.editor = editor + menu.addItem(editorMenuItem) } menu.addItem(NSMenuItem.separator()) @@ -291,6 +260,10 @@ class SiteListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource { tableView.menu = menu } + + @objc func openWithEditor(sender: EditorMenuItem) { + sender.editor?.openDirectory(file: selectedSite!.absolutePath!) + } // MARK: - Deinitialization