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

🏗 Use new shell when parsing apps

This commit is contained in:
2022-10-05 22:38:54 +02:00
parent fb1ca60240
commit 108ae05c1d
5 changed files with 62 additions and 28 deletions

View File

@ -33,31 +33,46 @@ class Application {
Attempt to open a specific directory in the app of choice.
(This will open the app if it isn't open yet.)
*/
@objc public func openDirectory(file: String) {
return LegacyShell.run("/usr/bin/open -a \"\(name)\" \"\(file)\"")
@objc public func openDirectory(file: String) async {
return await Shell.quiet("/usr/bin/open -a \"\(name)\" \"\(file)\"")
}
/** Checks if the app is installed. */
func isInstalled() -> Bool {
// If this script does not complain, the app exists!
return LegacyShell.user.executeSynchronously(
func isInstalled() async -> Bool {
let (process, output) = try! await Shell.attach(
"/usr/bin/open -Ra \"\(name)\"",
requiresPath: false
).task.terminationStatus == 0
didReceiveOutput: { _, _ in },
withTimeout: 2.0
)
if Shell is TestableShell {
// When testing, check the error output (must not be empty)
return !output.hasError
} else {
// If this script does not complain, the app exists!
return process.terminationStatus == 0
}
}
/**
Detect which apps are available to open a specific directory.
*/
static public func detectPresetApplications() -> [Application] {
return [
static public func detectPresetApplications() async -> [Application] {
var detected: [Application] = []
let detectable = [
Application("PhpStorm", .editor),
Application("Visual Studio Code", .editor),
Application("Sublime Text", .editor),
Application("Sublime Merge", .git_gui),
Application("iTerm", .terminal)
].filter {
return $0.isInstalled()
]
for app in detectable where await app.isInstalled() {
detected.append(app)
}
return detected
}
}

View File

@ -26,9 +26,9 @@ class PhpHelper {
let canWriteSymlinks = FileManager.default.isWritableFile(atPath: "/usr/local/bin/")
do {
LegacyShell.run("mkdir -p ~/.config/phpmon/bin")
Task { await Shell.quiet("mkdir -p ~/.config/phpmon/bin") }
if FileManager.default.fileExists(atPath: destination) {
if Filesystem.fileExists(destination) {
let contents = try String(contentsOfFile: destination)
if !contents.contains(keyPhrase) {
Log.info("The file at '\(destination)' already exists and was not generated by PHP Monitor "

View File

@ -107,9 +107,9 @@ extension DomainListVC {
LegacyShell.run("open -b com.apple.terminal '\(selectedSite!.absolutePath)'")
}
@objc func openWithEditor(sender: EditorMenuItem) {
@objc func openWithEditor(sender: EditorMenuItem) async {
guard let editor = sender.editor else { return }
editor.openDirectory(file: selectedSite!.absolutePath)
await editor.openDirectory(file: selectedSite!.absolutePath)
}
@objc func isolateSite(sender: PhpMenuItem) {

View File

@ -161,22 +161,29 @@ extension MainMenu {
Detect which applications are installed that can be used to open a domain's source directory.
*/
private func detectApplications() {
Log.info("Detecting applications...")
Task {
Log.info("Detecting applications...")
App.shared.detectedApplications = Application.detectPresetApplications()
App.shared.detectedApplications = await Application.detectPresetApplications()
let customApps = Preferences.custom.scanApps?.map { appName in
return Application(appName, .user_supplied)
}.filter { app in
return app.isInstalled()
} ?? []
let customApps = Preferences.custom.scanApps?.map { appName in
return Application(appName, .user_supplied)
} ?? []
App.shared.detectedApplications.append(contentsOf: customApps)
var detectedCustomApps: [Application] = []
let appNames = App.shared.detectedApplications.map { app in
return app.name
for app in customApps where await app.isInstalled() {
detectedCustomApps.append(app)
}
App.shared.detectedApplications
.append(contentsOf: detectedCustomApps)
let appNames = App.shared.detectedApplications.map { app in
return app.name
}
Log.info("Detected applications: \(appNames)")
}
Log.info("Detected applications: \(appNames)")
}
}

View File

@ -77,10 +77,22 @@ class Testables {
nicoverbruggen/cask
shivammathur/php
"""),
"mkdir -p ~/.config/phpmon"
: .instant(""),
"/opt/homebrew/bin/brew info php --json"
: .instant(ShellStrings.brewJson),
"brew info shivammathur/php/php --json"
: .instant("Error: No available formula with the name \"shivammathur/php/php\".")
: .instant("Error: No available formula with the name \"shivammathur/php/php\"."),
"/usr/bin/open -Ra \"PhpStorm\""
: .instant("Unable to find application named 'PhpStorm'", .stdErr),
"/usr/bin/open -Ra \"Visual Studio Code\""
: .instant("Unable to find application named 'Visual Studio Code'", .stdErr),
"/usr/bin/open -Ra \"Sublime Text\""
: .instant("Unable to find application named 'Sublime Text'", .stdErr),
"/usr/bin/open -Ra \"Sublime Merge\""
: .instant("Unable to find application named 'Sublime Merge'", .stdErr),
"/usr/bin/open -Ra \"iTerm\""
: .instant("Unable to find application named 'iTerm'", .stdErr)
]
)
}