mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-11-09 05:40:07 +01: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:
89
phpmon/Domain/Helpers/Editor.swift
Normal file
89
phpmon/Domain/Helpers/Editor.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -166,3 +166,7 @@ class PhpMenuItem: NSMenuItem {
|
||||
class ExtensionMenuItem: NSMenuItem {
|
||||
var phpExtension: PhpExtension? = nil
|
||||
}
|
||||
|
||||
class EditorMenuItem: NSMenuItem {
|
||||
var editor: Editor? = nil
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user