forked from sbarex/SourceCodeSyntaxHighlight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
89 lines (73 loc) · 2.47 KB
/
Copy pathmain.swift
File metadata and controls
89 lines (73 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// main.swift
// relaunch
//
// Created by Sbarex on 12/05/2020.
// Copyright © 2020 sbarex. All rights reserved.
//
import AppKit
// KVO helper
class Observer: NSObject {
let _callback: () -> Void
init(callback: @escaping () -> Void) {
_callback = callback
}
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?) {
_callback()
}
}
fileprivate extension String {
func appendLine(toFileURL fileURL: URL) throws {
try (self + "\n").append(toFileURL: fileURL)
}
func append(toFileURL fileURL: URL) throws {
let data = self.data(using: String.Encoding.utf8)!
try data.append(toFileURL: fileURL)
}
}
fileprivate extension Data {
func append(toFileURL fileURL: URL) throws {
if let fileHandle = FileHandle(forWritingAtPath: fileURL.path) {
defer {
fileHandle.closeFile()
}
fileHandle.seekToEndOfFile()
fileHandle.write(self)
} else {
try write(to: fileURL, options: .atomic)
}
}
}
// main
autoreleasepool {
// the application pid
guard CommandLine.argc > 1, let parentPID = Int32(CommandLine.arguments[1]) else {
fatalError("Relaunch: parentPID == nil.")
}
// get the application instance
if let app = NSRunningApplication(processIdentifier: parentPID) {
// application URL
let bundleURL = app.bundleURL!
// try? "start listening \(parentPID)\n".write(to: url, atomically: true, encoding: .utf8)
// terminate() and wait terminated.
let listener = Observer {
// try? "loop end".appendLine(toFileURL: url)
CFRunLoopStop(CFRunLoopGetCurrent())
}
app.addObserver(listener, forKeyPath: "isTerminated", context: nil)
app.terminate() // FIXME: Don't terminate the app?!?
// app.forceTerminate()
CFRunLoopRun() // wait KVO notification
app.removeObserver(listener, forKeyPath: "isTerminated", context: nil)
// try? "end listening \(parentPID)".appendLine(toFileURL: url)
// relaunch
do {
try NSWorkspace.shared.launchApplication(at: bundleURL, configuration: [:])
} catch {
fatalError("Relaunch: NSWorkspace.shared.launchApplication failed.")
}
}
}