Skip to content

Commit a7d3682

Browse files
committed
Remove throws when app is not activated
1 parent a14dcbd commit a7d3682

2 files changed

Lines changed: 51 additions & 20 deletions

File tree

Pro

Submodule Pro updated from 53c37b4 to bdca40b

Tool/Sources/XcodeInspector/XcodeInspector+TriggerCommand.swift

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,56 @@ public extension XcodeAppInstanceInspector {
77
func triggerCopilotCommand(name: String, activateXcode: Bool = true) async throws {
88
let bundleName = Bundle.main
99
.object(forInfoDictionaryKey: "EXTENSION_BUNDLE_NAME") as! String
10-
try await triggerMenuItem(path: ["Editor", bundleName, name], activateXcode: activateXcode)
10+
try await triggerMenuItem(path: ["Editor", bundleName, name], activateApp: activateXcode)
1111
}
1212
}
1313

1414
public extension AppInstanceInspector {
15-
@MainActor
16-
func triggerMenuItem(path: [String], activateXcode: Bool) async throws {
17-
guard !path.isEmpty else { return }
15+
struct CantRunCommand: Error, LocalizedError {
16+
let path: String
17+
let reason: String
18+
public var errorDescription: String? {
19+
"Can't run command \(path): \(reason)"
20+
}
21+
}
1822

19-
struct CantRunCommand: Error, LocalizedError {
20-
let path: [String]
21-
var errorDescription: String? {
22-
"Can't run command \(path.joined(separator: "/"))."
23-
}
23+
@MainActor
24+
func triggerMenuItem(path: [String], activateApp: Bool) async throws {
25+
let sourcePath = path.joined(separator: "/")
26+
func cantRunCommand(_ reason: String) -> CantRunCommand {
27+
return CantRunCommand(path: sourcePath, reason: reason)
2428
}
2529

26-
if activateXcode {
30+
guard path.count >= 2 else { throw cantRunCommand("Path too short.") }
31+
32+
if activateApp {
2733
if !runningApplication.activate() {
28-
throw CantRunCommand(path: path)
34+
Logger.service.error("""
35+
Trigger menu item \(sourcePath) failed: \
36+
Xcode not activated.
37+
""")
2938
}
3039
} else {
3140
if !runningApplication.isActive {
32-
throw CantRunCommand(path: path)
41+
Logger.service.error("""
42+
Trigger menu item \(sourcePath) failed: \
43+
Xcode not activated.
44+
""")
3345
}
3446
}
3547

3648
await Task.yield()
3749

3850
if UserDefaults.shared.value(for: \.triggerActionWithAccessibilityAPI) {
3951
let app = AXUIElementCreateApplication(runningApplication.processIdentifier)
40-
guard let menuBar = app.menuBar else { throw CantRunCommand(path: path) }
52+
53+
guard let menuBar = app.menuBar else {
54+
Logger.service.error("""
55+
Trigger menu item \(sourcePath) failed: \
56+
Menu not found.
57+
""")
58+
throw cantRunCommand("Menu not found.")
59+
}
4160
var path = path
4261
var currentMenu = menuBar
4362
while !path.isEmpty {
@@ -47,22 +66,34 @@ public extension AppInstanceInspector {
4766
let error = AXUIElementPerformAction(button, kAXPressAction as CFString)
4867
if error != AXError.success {
4968
Logger.service.error("""
50-
Trigger menu item \(path.joined(separator: "/")) failed: \
69+
Trigger menu item \(sourcePath) failed: \
5170
\(error.localizedDescription)
5271
""")
53-
throw error
72+
throw cantRunCommand(error.localizedDescription)
5473
} else {
74+
#if DEBUG
75+
Logger.service.info("""
76+
Trigger menu item \(sourcePath) succeeded.
77+
""")
78+
#endif
5579
return
5680
}
5781
} else if let menu = currentMenu.child(title: item) {
82+
#if DEBUG
83+
Logger.service.info("""
84+
Trigger menu item \(sourcePath): Move to \(item).
85+
""")
86+
#endif
5887
currentMenu = menu
5988
} else {
60-
throw CantRunCommand(path: path)
89+
Logger.service.error("""
90+
Trigger menu item \(sourcePath) failed: \
91+
\(item) is not found.
92+
""")
93+
throw cantRunCommand("\(item) is not found.")
6194
}
6295
}
6396
} else {
64-
guard path.count >= 2 else { throw CantRunCommand(path: path) }
65-
6697
let clickTask = {
6798
var path = path
6899
let button = path.removeLast()
@@ -103,7 +134,7 @@ public extension AppInstanceInspector {
103134
Trigger menu item \(path.joined(separator: "/")) failed: \
104135
\(error.localizedDescription)
105136
""")
106-
throw error
137+
throw cantRunCommand(error.localizedDescription)
107138
}
108139
}
109140
}

0 commit comments

Comments
 (0)