Created
December 10, 2019 15:30
-
-
Save zenangst/5cc85bee0fef0fca886291a1a34ca308 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
class DeveloperToolsController { | |
let commandController: CommandController | |
init(commandController: CommandController) { | |
self.commandController = commandController | |
} | |
#if DEBUG | |
var enabled: Bool = true | |
#else | |
var enabled: Bool = false | |
#endif | |
func debug(_ object: AnyObject) { | |
guard enabled else { return } | |
switch object { | |
case let controller as NSViewController: | |
let controllerName = "\(type(of: controller))" | |
var toolTip = controllerName | |
if let superclassName = controller.superclass { | |
toolTip += ": \(superclassName)" | |
} | |
controller.view.toolTip = toolTip | |
let menu = NSMenu() | |
let menuItem = NSMenuItem(title: "Open \(controllerName).swift", | |
action: #selector(openController(_:)), | |
keyEquivalent: "") | |
menuItem.toolTip = controllerName | |
menuItem.target = self | |
menu.addItem(menuItem) | |
controller.view.menu = menu | |
default: | |
break | |
} | |
} | |
@objc func openController(_ menuItem: NSMenuItem) { | |
guard let path = ProcessInfo.processInfo.environment["SOURCE_ROOT"] else { return } | |
guard let fileName = menuItem.toolTip else { return } | |
let fileManager = FileManager.default | |
guard let enumerator = fileManager.enumerator(atPath: path) else { | |
return | |
} | |
let elements = enumerator.allObjects.compactMap({ $0 as? String }) | |
for element in elements { | |
if (element as NSString).lastPathComponent == "\(fileName).swift" { | |
let filePath = "\(path)/\(element)" | |
let fileCommand = Command.fileCommand(fileAtPath: filePath, withAppName: "Xcode") | |
do { | |
try commandController.execute(fileCommand) | |
} catch let error { | |
debugPrint(error) | |
} | |
break | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment