-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathAppDelegate.swift
205 lines (160 loc) · 6.58 KB
/
AppDelegate.swift
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// AppDelegate.swift
// mongodb
//
// Created by Giovanni Collazo on 1/15/15.
// Copyright (c) 2015 Giovanni Collazo. All rights reserved.
//
import Foundation
import Cocoa
import Sparkle
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var updater: SPUStandardUpdaterController!
static let userApplicationSupportDirectory =
try! FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
var dataPath: String
var logPath: String
var task: Process = Process()
var pipe: Pipe = Pipe()
var file: FileHandle
var statusBar = NSStatusBar.system
var statusBarItem: NSStatusItem = NSStatusItem()
var menu: NSMenu = NSMenu()
var statusMenuItem: NSMenuItem = NSMenuItem()
var openMongoMenuItem: NSMenuItem = NSMenuItem()
var openLogsMenuItem: NSMenuItem = NSMenuItem()
var docsMenuItem: NSMenuItem = NSMenuItem()
var aboutMenuItem: NSMenuItem = NSMenuItem()
var versionMenuItem: NSMenuItem = NSMenuItem()
var quitMenuItem: NSMenuItem = NSMenuItem()
var updatesMenuItem: NSMenuItem = NSMenuItem()
override init() {
let appSupport = AppDelegate.userApplicationSupportDirectory
guard
let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String
else {
fatalError("Unable to determine application name & version from Info.plist")
}
// Add name to Application Support directory, then add the version (which follows mongoDB version).
// A versioned directory allows users to use separate versions of the app without worrying about
// incompatible data or log file formats.
let dataDirectory = appSupport
.appendingPathComponent(appName)
self.dataPath = dataDirectory.appendingPathComponent("Data").path
self.logPath = dataDirectory.appendingPathComponent("Logs").path
self.file = self.pipe.fileHandleForReading
super.init()
}
func startServer() {
self.task = Process()
self.pipe = Pipe()
self.file = self.pipe.fileHandleForReading
if let path = Bundle.main.path(forResource: "mongod", ofType: "", inDirectory: "Vendor/mongodb/bin") {
self.task.launchPath = path
}
self.task.arguments = [
"--dbpath", "\(self.dataPath)",
"--nounixsocket",
"--bind_ip",
"127.0.0.1",
"--logpath", "\(self.logPath)/mongo.log"
]
self.task.standardOutput = self.pipe
print("Run mongod")
self.task.launch()
}
func stopServer() {
print("Terminate mongod")
task.terminate()
let data: Data = self.file.readDataToEndOfFile()
self.file.closeFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
print(output)
}
@objc func openDocumentationPage(_ send: AnyObject) {
if let url: URL = URL(string: "https://github.com/gcollazo/mongodbapp") {
NSWorkspace.shared.open(url)
}
}
@objc func openLogsDirectory(_ send: AnyObject) {
let logURL = URL(fileURLWithPath: self.logPath)
NSWorkspace.shared.open(logURL)
}
func createDirectories() {
if (!FileManager.default.fileExists(atPath: self.dataPath)) {
do {
try FileManager.default
.createDirectory(atPath: self.dataPath, withIntermediateDirectories: true, attributes: nil)
} catch {
print("Something went wrong creating dataPath: \(error)")
}
}
if (!FileManager.default.fileExists(atPath: self.logPath)) {
do {
try FileManager.default
.createDirectory(atPath: self.logPath, withIntermediateDirectories: true, attributes: nil)
} catch {
print("Something went wrong creating logPath: \(error)")
}
}
print("Mongo data directory: \(self.dataPath)")
print("Mongo logs directory: \(self.logPath)")
}
@objc func checkForUpdates(_ sender: AnyObject?) {
print("Checking for updates")
self.updater.checkForUpdates(sender)
}
func setupSystemMenuItem() {
// Add statusBarItem
statusBarItem = statusBar.statusItem(withLength: -1)
statusBarItem.menu = menu
let icon = NSImage(named: "leaf")
icon!.isTemplate = true
icon!.size = NSSize(width: 18, height: 16)
statusBarItem.button?.image = icon
// Add version to menu
versionMenuItem.title = "MongoDB"
if let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String? {
versionMenuItem.title = "MongoDB v\(version)"
}
menu.addItem(versionMenuItem)
// Add actionMenuItem to menu
statusMenuItem.title = "Running on Port 27017"
menu.addItem(statusMenuItem)
// Add separator
menu.addItem(NSMenuItem.separator())
// Add open logs to menu
openLogsMenuItem.title = "Open logs directory"
openLogsMenuItem.action = #selector(AppDelegate.openLogsDirectory(_:))
menu.addItem(openLogsMenuItem)
// Add separator
menu.addItem(NSMenuItem.separator())
// Add check for updates to menu
updatesMenuItem.title = "Check for Updates..."
updatesMenuItem.action = #selector(AppDelegate.checkForUpdates(_:))
menu.addItem(updatesMenuItem)
// Add about to menu
aboutMenuItem.title = "About"
aboutMenuItem.action = #selector(NSApplication.orderFrontStandardAboutPanel(_:))
menu.addItem(aboutMenuItem)
// Add docs to menu
docsMenuItem.title = "Documentation..."
docsMenuItem.action = #selector(AppDelegate.openDocumentationPage(_:))
menu.addItem(docsMenuItem)
// Add separator
menu.addItem(NSMenuItem.separator())
// Add quitMenuItem to menu
quitMenuItem.title = "Quit"
quitMenuItem.action = #selector(NSApplication.shared.terminate)
menu.addItem(quitMenuItem)
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
createDirectories()
setupSystemMenuItem()
startServer()
}
func applicationWillTerminate(_ notification: Notification) {
stopServer()
}
}