Skip to content

Commit

Permalink
Update to Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
gcollazo committed Oct 27, 2016
1 parent 62eca48 commit e2f83f6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 46 deletions.
17 changes: 16 additions & 1 deletion MongoDB.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,13 @@
attributes = {
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0700;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = "Giovanni Collazo";
TargetAttributes = {
7D25A7E11A697586007EC13C = {
CreatedOnToolsVersion = 6.1.1;
DevelopmentTeam = FJZ2YT5Y98;
LastSwiftMigration = 0800;
SystemCapabilities = {
com.apple.Sandbox = {
enabled = 0;
Expand All @@ -290,6 +291,7 @@
};
7D25A7F11A697586007EC13C = {
CreatedOnToolsVersion = 6.1.1;
LastSwiftMigration = 0800;
TestTargetID = 7D25A7E11A697586007EC13C;
};
};
Expand Down Expand Up @@ -384,8 +386,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand All @@ -394,6 +398,7 @@
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand Down Expand Up @@ -427,8 +432,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand All @@ -437,6 +444,7 @@
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
Expand All @@ -446,6 +454,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
};
name = Release;
};
Expand All @@ -464,12 +473,14 @@
);
INFOPLIST_FILE = MongoDB/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.9;
OTHER_LDFLAGS = "-Wl,-rpath,@loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "io.blimp.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SWIFT_OBJC_BRIDGING_HEADER = "MongoDB-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -488,11 +499,13 @@
);
INFOPLIST_FILE = MongoDB/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.9;
OTHER_LDFLAGS = "-Wl,-rpath,@loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "io.blimp.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
SWIFT_OBJC_BRIDGING_HEADER = "MongoDB-Bridging-Header.h";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -513,6 +526,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "io.blimp.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/MongoDB.app/Contents/MacOS/MongoDB";
};
name = Debug;
Expand All @@ -530,6 +544,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "io.blimp.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/MongoDB.app/Contents/MacOS/MongoDB";
};
name = Release;
Expand Down
88 changes: 44 additions & 44 deletions MongoDB/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var updater: SUUpdater!

var paths = NSSearchPathForDirectoriesInDomains(
NSSearchPathDirectory.DocumentDirectory,
NSSearchPathDomainMask.UserDomainMask, true)
FileManager.SearchPathDirectory.documentDirectory,
FileManager.SearchPathDomainMask.userDomainMask, true)

var documentsDirectory: AnyObject
var dataPath: String
var logPath: String

var task: NSTask = NSTask()
var pipe: NSPipe = NSPipe()
var file: NSFileHandle
var task: Process = Process()
var pipe: Pipe = Pipe()
var file: FileHandle

var statusBar = NSStatusBar.systemStatusBar()
var statusBar = NSStatusBar.system()
var statusBarItem: NSStatusItem = NSStatusItem()
var menu: NSMenu = NSMenu()

Expand All @@ -39,19 +39,19 @@ class AppDelegate: NSObject, NSApplicationDelegate {

override init() {
self.file = self.pipe.fileHandleForReading
self.documentsDirectory = self.paths[0]
self.dataPath = documentsDirectory.stringByAppendingPathComponent("MongoData")
self.logPath = documentsDirectory.stringByAppendingPathComponent("MongoData/Logs")
self.documentsDirectory = self.paths[0] as AnyObject
self.dataPath = documentsDirectory.appendingPathComponent("MongoData")
self.logPath = documentsDirectory.appendingPathComponent("MongoData/Logs")

super.init()
}

func startServer() {
self.task = NSTask()
self.pipe = NSPipe()
self.task = Process()
self.pipe = Pipe()
self.file = self.pipe.fileHandleForReading

if let path = NSBundle.mainBundle().pathForResource("mongod", ofType: "", inDirectory: "Vendor/mongodb") {
if let path = Bundle.main.path(forResource: "mongod", ofType: "", inDirectory: "Vendor/mongodb") {
self.task.launchPath = path
}

Expand All @@ -73,15 +73,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {
print("Terminate mongod")
task.terminate()

let data: NSData = self.file.readDataToEndOfFile()
let data: Data = self.file.readDataToEndOfFile()
self.file.closeFile()

let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)! as String
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
print(output)
}

func openMongo(sender: AnyObject) {
if let path = NSBundle.mainBundle().pathForResource("mongo", ofType: "", inDirectory: "Vendor/mongodb") {
func openMongo(_ sender: AnyObject) {
if let path = Bundle.main.path(forResource: "mongo", ofType: "", inDirectory: "Vendor/mongodb") {
var source: String

if appExists("iTerm") {
Expand All @@ -108,30 +108,30 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

func openDocumentationPage(send: AnyObject) {
if let url: NSURL = NSURL(string: "https://github.com/gcollazo/mongodbapp") {
NSWorkspace.sharedWorkspace().openURL(url)
func openDocumentationPage(_ send: AnyObject) {
if let url: URL = URL(string: "https://github.com/gcollazo/mongodbapp") {
NSWorkspace.shared().open(url)
}
}

func openLogsDirectory(send: AnyObject) {
NSWorkspace.sharedWorkspace().openFile(self.logPath)
func openLogsDirectory(_ send: AnyObject) {
NSWorkspace.shared().openFile(self.logPath)
}

func createDirectories() {
if (!NSFileManager.defaultManager().fileExistsAtPath(self.dataPath)) {
if (!FileManager.default.fileExists(atPath: self.dataPath)) {
do {
try NSFileManager.defaultManager()
.createDirectoryAtPath(self.dataPath, withIntermediateDirectories: false, attributes: nil)
try FileManager.default
.createDirectory(atPath: self.dataPath, withIntermediateDirectories: false, attributes: nil)
} catch {
print("Something went wrong creating dataPath")
}
}

if (!NSFileManager.defaultManager().fileExistsAtPath(self.logPath)) {
if (!FileManager.default.fileExists(atPath: self.logPath)) {
do {
try NSFileManager.defaultManager()
.createDirectoryAtPath(self.logPath, withIntermediateDirectories: false, attributes: nil)
try FileManager.default
.createDirectory(atPath: self.logPath, withIntermediateDirectories: false, attributes: nil)
} catch {
print("Something went wrong creating logPath")
}
Expand All @@ -141,24 +141,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
print("Mongo logs directory: \(self.logPath)")
}

func checkForUpdates(sender: AnyObject?) {
func checkForUpdates(_ sender: AnyObject?) {
print("Checking for updates")
self.updater.checkForUpdates(sender)
}

func setupSystemMenuItem() {
// Add statusBarItem
statusBarItem = statusBar.statusItemWithLength(-1)
statusBarItem = statusBar.statusItem(withLength: -1)
statusBarItem.menu = menu

let icon = NSImage(named: "leaf")
icon!.template = true
icon!.isTemplate = true
icon!.size = NSSize(width: 18, height: 16)
statusBarItem.image = icon

// Add version to menu
versionMenuItem.title = "MongoDB"
if let version = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String? {
if let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String? {
versionMenuItem.title = "MongoDB v\(version)"
}
menu.addItem(versionMenuItem)
Expand All @@ -168,52 +168,52 @@ class AppDelegate: NSObject, NSApplicationDelegate {
menu.addItem(statusMenuItem)

// Add separator
menu.addItem(NSMenuItem.separatorItem())
menu.addItem(NSMenuItem.separator())

// Add open mongo to menu
openMongoMenuItem.title = "Open mongo"
openMongoMenuItem.action = Selector("openMongo:")
openMongoMenuItem.action = #selector(AppDelegate.openMongo(_:))
menu.addItem(openMongoMenuItem)

// Add open logs to menu
openLogsMenuItem.title = "Open logs directory"
openLogsMenuItem.action = Selector("openLogsDirectory:")
openLogsMenuItem.action = #selector(AppDelegate.openLogsDirectory(_:))
menu.addItem(openLogsMenuItem)

// Add separator
menu.addItem(NSMenuItem.separatorItem())
menu.addItem(NSMenuItem.separator())

// Add check for updates to menu
updatesMenuItem.title = "Check for Updates..."
updatesMenuItem.action = Selector("checkForUpdates:")
updatesMenuItem.action = #selector(AppDelegate.checkForUpdates(_:))
menu.addItem(updatesMenuItem)

// Add about to menu
aboutMenuItem.title = "About"
aboutMenuItem.action = Selector("orderFrontStandardAboutPanel:")
aboutMenuItem.action = #selector(NSApplication.orderFrontStandardAboutPanel(_:))
menu.addItem(aboutMenuItem)

// Add docs to menu
docsMenuItem.title = "Documentation..."
docsMenuItem.action = Selector("openDocumentationPage:")
docsMenuItem.action = #selector(AppDelegate.openDocumentationPage(_:))
menu.addItem(docsMenuItem)

// Add separator
menu.addItem(NSMenuItem.separatorItem())
menu.addItem(NSMenuItem.separator())

// Add quitMenuItem to menu
quitMenuItem.title = "Quit"
quitMenuItem.action = Selector("terminate:")
quitMenuItem.action = #selector(NSApplication.shared().terminate)
menu.addItem(quitMenuItem)
}

func appExists(appName: String) -> Bool {
func appExists(_ appName: String) -> Bool {
let found = [
"/Applications/\(appName).app",
"/Applications/Utilities/\(appName).app",
"\(NSHomeDirectory())/Applications/\(appName).app"
].map {
return NSFileManager.defaultManager().fileExistsAtPath($0)
return FileManager.default.fileExists(atPath: $0)
}.reduce(false) {
if $0 == false && $1 == false {
return false;
Expand All @@ -225,13 +225,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return found
}

func applicationDidFinishLaunching(aNotification: NSNotification) {
func applicationDidFinishLaunching(_ aNotification: Notification) {
createDirectories()
setupSystemMenuItem()
startServer()
}

func applicationWillTerminate(notification: NSNotification) {
func applicationWillTerminate(_ notification: Notification) {
stopServer()
}

Expand Down
2 changes: 1 addition & 1 deletion MongoDBTests/MongoDBTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MongoDBTests: XCTestCase {

func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock() {
self.measure() {
// Put the code you want to measure the time of here.
}
}
Expand Down

0 comments on commit e2f83f6

Please sign in to comment.