Skip to content

Instantly share code, notes, and snippets.

func writeMockOutput(_ invocation: XCSourceEditorCommandInvocation,
firstSelection: XCSourceTextRange,
selectedClassName: String,
protocolName: String) throws {
let outputResult = FileManager.default.currentDirectoryPath + "/output.swift"
var data = try String(contentsOfFile: outputResult, encoding: .utf8)
data = data.replacingOccurrences(of: "class Mock\(protocolName)", with: "class \(selectedClassName)")
invocation.buffer.lines.insert(data, at: firstSelection.start.line)
}
func executeMockolo(selectedProtocolPath: String,
protocolName: String) {
var commandLineArgs = ["-srcs", "\(selectedProtocolPath)"]
commandLineArgs.append("--annotation")
commandLineArgs.append("\(protocolName)")
commandLineArgs.append("--exclude-imports")
commandLineArgs.append("true")
commandLineArgs.append("-d")
commandLineArgs.append("./output.swift")
func getSelectedProtocolFilePath(protocolsDict: [String: String],
protocolName: String,
selectedProtocolPath: String,
inheritedProtocol: String) throws {
if let relativePath = protocolsDict[protocolName] {
let contents = try String(contentsOfFile: relativePath, encoding: .utf8)
selectedProtocolPath = relativePath
let rows = contents.split(separator: "\n")
for row in rows {
func findSourceFiles() -> [URL] {
let enumerator = FileManager.default.enumerator(at: projectRoot,
includingPropertiesForKeys: [.nameKey],
errorHandler: nil)
var files = [URL]()
while let file = enumerator?.nextObject() as? URL {
if file.pathExtension == "swift" {
files.append(file)
}
}
func parseProtocol(_ rows: NSMutableArray) -> String {
var protocolName = ""
for item in rows {
if let row = item as? String,
row.contains("class") {
if let protocolMacth = row.range(of: "(?<=: )[^{]+", options: .regularExpression),
let protocolNameMatch = row[protocolMacth].split(separator: ",").first {
protocolName = protocolNameMatch.replacingOccurrences(of: ":", with: "")
.replacingOccurrences(of: " ", with: "")
}
final class XcodeProjectPathFinder {
func findOpenProjectPath() -> URL? {
if let workspace = findOpenWorkspacePath(), workspace.hasPrefix("/") {
return URL(fileURLWithPath: workspace).deletingLastPathComponent()
}
return nil
}
func findOpenWorkspacePath() -> String? {
precondition(Thread.isMainThread, "NSAppleScript must be run on the main thread.")
final class SourceEditorCommand: NSObject, XCSourceEditorCommand {
let stubGenerator = StubGenerator()
func perform(with invocation: XCSourceEditorCommandInvocation,
completionHandler: @escaping (Error?) -> Void ) -> Void {
stubGenerator.genarateStub(with: invocation, completionHandler: completionHandler)
completionHandler(nil)
}
}
func selectDirectory() {
let panel = NSOpenPanel()
panel.message = "Choose your project directory"
panel.prompt = "Grant permission"
panel.allowsOtherFileTypes = false
panel.canChooseFiles = false
panel.canChooseDirectories = true
panel.directoryURL = projectPath
if panel.runModal() == NSApplication.ModalResponse.OK {
bookmark(panel.url)
public extension UIImageView {
static func swizzleSetImageWithUrl() {
guard let setImageWithMethod = class_getInstanceMethod(UIImageView.self, #selector(setImageWith(path:placeholder:completion:))),
let swizzledMethod = class_getInstanceMethod(UIImageView.self, #selector(setStaticImage)) else { return }
method_exchangeImplementations(setImageWithMethod, swizzledMethod)
}
@objc private func setStaticImage() {
sd_setImage(with: URL(string: ""), placeholderImage: UIImage(named: "mock_snapshot_image"), completed: nil)
}