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. Attempt to open a specific directory in the app of choice.
(This will open the app if it isn't open yet.) (This will open the app if it isn't open yet.)
*/ */
@objc public func openDirectory(file: String) { @objc public func openDirectory(file: String) async {
return LegacyShell.run("/usr/bin/open -a \"\(name)\" \"\(file)\"") return await Shell.quiet("/usr/bin/open -a \"\(name)\" \"\(file)\"")
} }
/** Checks if the app is installed. */ /** Checks if the app is installed. */
func isInstalled() -> Bool { func isInstalled() async -> Bool {
// If this script does not complain, the app exists!
return LegacyShell.user.executeSynchronously( let (process, output) = try! await Shell.attach(
"/usr/bin/open -Ra \"\(name)\"", "/usr/bin/open -Ra \"\(name)\"",
requiresPath: false didReceiveOutput: { _, _ in },
).task.terminationStatus == 0 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. Detect which apps are available to open a specific directory.
*/ */
static public func detectPresetApplications() -> [Application] { static public func detectPresetApplications() async -> [Application] {
return [ var detected: [Application] = []
let detectable = [
Application("PhpStorm", .editor), Application("PhpStorm", .editor),
Application("Visual Studio Code", .editor), Application("Visual Studio Code", .editor),
Application("Sublime Text", .editor), Application("Sublime Text", .editor),
Application("Sublime Merge", .git_gui), Application("Sublime Merge", .git_gui),
Application("iTerm", .terminal) 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/") let canWriteSymlinks = FileManager.default.isWritableFile(atPath: "/usr/local/bin/")
do { 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) let contents = try String(contentsOfFile: destination)
if !contents.contains(keyPhrase) { if !contents.contains(keyPhrase) {
Log.info("The file at '\(destination)' already exists and was not generated by PHP Monitor " 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)'") 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 } guard let editor = sender.editor else { return }
editor.openDirectory(file: selectedSite!.absolutePath) await editor.openDirectory(file: selectedSite!.absolutePath)
} }
@objc func isolateSite(sender: PhpMenuItem) { @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. Detect which applications are installed that can be used to open a domain's source directory.
*/ */
private func detectApplications() { 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 let customApps = Preferences.custom.scanApps?.map { appName in
return Application(appName, .user_supplied) return Application(appName, .user_supplied)
}.filter { app in } ?? []
return app.isInstalled()
} ?? []
App.shared.detectedApplications.append(contentsOf: customApps) var detectedCustomApps: [Application] = []
let appNames = App.shared.detectedApplications.map { app in for app in customApps where await app.isInstalled() {
return app.name 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 nicoverbruggen/cask
shivammathur/php shivammathur/php
"""), """),
"mkdir -p ~/.config/phpmon"
: .instant(""),
"/opt/homebrew/bin/brew info php --json" "/opt/homebrew/bin/brew info php --json"
: .instant(ShellStrings.brewJson), : .instant(ShellStrings.brewJson),
"brew info shivammathur/php/php --json" "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)
] ]
) )
} }