1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-08 04:20:07 +02:00

♻️ Improved code editor detection (#60)

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.
This commit is contained in:
2021-12-07 12:42:20 +01:00
parent 92a6d506dc
commit d13714c1ea
5 changed files with 129 additions and 41 deletions

View File

@ -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 = "<group>"; };
5420395E2613607600FB00FA /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = "<group>"; };
54B48B5E275F66AE006D90C5 /* Editor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Editor.swift; sourceTree = "<group>"; };
C405A4CE24B9B9130062FAFA /* InternetAccessPolicy.strings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; path = InternetAccessPolicy.strings; sourceTree = "<group>"; };
C405A4CF24B9B9140062FAFA /* InternetAccessPolicy.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = InternetAccessPolicy.plist; sourceTree = "<group>"; };
C412E5FB25700D5300A1FB67 /* HomebrewPackage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomebrewPackage.swift; sourceTree = "<group>"; };
@ -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 */,

View File

@ -230,6 +230,22 @@ PHP Monitor itself doesn't do any network requests. Feel free to check the sourc
</details>
<details>
<summary><strong>How do I get various applications to show up in the domain list's right-click menu?</strong></summary>
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: <i>PhpStorm, Visual Studio Code, Sublime Text, Sublime Merge</i>.
For <strong>Visual Studio Code</strong>, you need to have `code` available in your PATH. [More info](https://code.visualstudio.com/docs/editor/command-line) here.
For <strong>Sublime Text (and Sublime Merge)</strong>, the apps need to be in your `/Applications` directory.
For <strong>PhpStorm</strong>, 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).
</details>
<details>
<summary><strong>After running PHP Monitor, Homebrew sometimes has issues with `brew upgrade` or `brew cleanup`!</strong></summary>

View File

@ -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)
}
}
}

View File

@ -166,3 +166,7 @@ class PhpMenuItem: NSMenuItem {
class ExtensionMenuItem: NSMenuItem {
var phpExtension: PhpExtension? = nil
}
class EditorMenuItem: NSMenuItem {
var editor: Editor? = nil
}

View File

@ -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 users 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