Skip to content

Commit ed9fb15

Browse files
committed
✨ Trigger hooks
1 parent 504143e commit ed9fb15

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

Sources/Cli/Watch.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ struct Watch: ParsableCommand {
1111

1212
mutating func run() {
1313
let repo = EventRepo()
14-
repo.registerForEventStoreChanges()
14+
let hooks = Loader.readConfig()?.hooks ?? []
15+
repo.registerForEventStoreChanges(hooks: hooks)
1516
dispatchMain()
1617
}
1718
}

Sources/Config.swift

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
13
struct Config: Codable {
24
let iconize: [Rule]?
35
let hooks: [Hook]?
@@ -12,4 +14,17 @@ struct Rule: Codable {
1214
struct Hook: Codable {
1315
let path: String
1416
let args: [String]?
17+
18+
func trigger() {
19+
let task = Process()
20+
task.executableURL = URL(fileURLWithPath: path)
21+
task.arguments = args ?? []
22+
do {
23+
try task.run()
24+
task.waitUntilExit()
25+
print("Fired event \(path) with args \(args ?? [])")
26+
} catch {
27+
print("Failed to execute command \(path): \(error)")
28+
}
29+
}
1530
}

Sources/IO/EventRepo.swift

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import EventKit
22

33
final class EventRepo {
44
private let eventStore = EKEventStore()
5+
private var hooks: [Hook] = []
56

67
private func grantAccess() -> EKEventStore {
78
if #available(macOS 14, *) {
@@ -93,18 +94,20 @@ final class EventRepo {
9394
StdOut.print("Saved Event")
9495
}
9596

96-
/// Register for EKEventStoreChangedNotification
97-
func registerForEventStoreChanges() {
97+
func registerForEventStoreChanges(hooks: [Hook]) {
98+
self.hooks = hooks
9899
NotificationCenter.default.addObserver(
99100
self,
100101
selector: #selector(eventStoreChanged(_:)),
101102
name: .EKEventStoreChanged,
102-
object: eventStore
103+
object: nil // Listen to all EKEventStore changes
103104
)
104105
}
105106

106107
@objc private func eventStoreChanged(_: Notification) {
107-
print("Event store has changed!")
108+
for hook in hooks {
109+
hook.trigger()
110+
}
108111
}
109112

110113
deinit {

0 commit comments

Comments
 (0)