在平時開發過程當中咱們使用了不少的Xcode插件,雖然官方對於插件製做沒有提供任何支持,可是加載三方的插件,默認仍是被容許的。第三方的插件,存放在 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins文件夾中,後綴名必須是.xcplugin ,其其實是一種bundle。因此咱們建立一個插件工程,直接建立bundle工程便可。而後經過修改後綴名爲.xcplugin,將其放到~/Library/Application Support/Developer/Shared/Xcode/Plug-ins目錄中便可。git
Xcode插件開發如今主要經過兩種方式實現,其實也就是一種,只不過其中一種是使用別人提供的開發模板來省去不少中間步驟而已。文章會依次詳細介紹兩種的實現方法。github
1.建立Bundle工程web
2.工程設置正則表達式
插件工程和普通的bundle工程仍是有區別的,因此須要進行特殊的設置。json
1)工程的plist文件數組
添加三項:
XCPluginHasUI = NO
XC4Compatible = YES
DVTPlugInCompatibilityUUIDs 這是一個數組。數組內容字符串,指示了該插件兼容的Xcode版本,只有對應版本的Xcode的UIID加入這個數組,插件才能被加載。不然,即便將插件放入Xcode的插件文件夾,插件也不會被加載。
獲取當前版本的Xcode的UUID方式:app
在terminal中輸入命令:ui
defaults read /Applications/Xcode.app/Contents/Info DVTPlugInCompatibilityUUID url
terminal會返回一串字符串,這就是Xcode的DVTPlugInCompatibilityUUID。spa
2)Build Setting
Installation Build Products Location 設置爲 ${HOME} [顯示的時候,顯示的是你的用戶目錄],這個是products的根目錄。
Installation Directory 設置爲 /Library/Application Support/Developer/Shared/Xcode/Plug-ins,這個是指定你的插件安裝的目錄。 注意,這裏填入的實際上是相對目錄。插件的絕對目錄是這樣的,例如 /Users/yohunl/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins/Alcatraz.xcplugin ,最後的絕對目錄是 Installation Build Products Location和Installation Directory的結合,這也是爲何二者都要設置的緣由。
Deployment Location 設置爲 YES,這個是指示該工程不使用設置裏的build location,而是用Installation Directory來肯定build後放置的位置。
默認工程生成的相關文件位置都是 Build Locations指定的,經過Deployment Location 設置爲 YES告訴工程,咱們不使用這個默認的設置,而是咱們自定義的。
Wrapper extension 設置爲 xcplugin,後綴名必須爲xcplugin,不然不會被加載。
1)下載Xcode插件開發模板
地址:https://github.com/kattrali/Xcode-Plugin-Template
2)將下載下來的template複製到 ~/Library/Developer/Xcode/Templates/Project Templates/Application Plug-in/Xcode Plugin.xctemplate文件夾中,若是沒有對應的文件夾就本身手動建立一個。
3)重啓Xcode,當你新建一個工程的時候就能夠在OS X中看到一個Application Plug-in的選項,裏面有一個Xcode Plug-in模板。
經過以上的兩種準備方式,咱們已能夠建立Xcode插件工程,接下來就是如何實現插件功能。
1.功能需求
在當前選中文件中實現代碼風格重構,目前主要實現setter方法這一風格重構。例如,
[self setName:@"Davy"]; ==> self.name = @"Davy";
2.思路分析
1)找到當前文件中符合setter方法命名風格的方法調用。
2)替換找到的符合重構風格的代碼,提醒用戶保存。
3.技術難點
1)Xcode代碼編輯框文件內容操做。
2)正則表達式書寫。
3)Xcode代碼編輯框提醒用戶保存文件。
關於最後一點,由於Xcode對於沒有保存的已修改過的文件會顯灰以提示用戶該文件須要保存,咱們能夠借鑑這種方式。另外,在查找時,若是可以實現高亮而且跟隨滾動,效果會更佳。
4.關鍵代碼
以上這些問題,本人在「Refactor Code」插件中所有實現,如今放上關鍵方法。
1)添加菜單
-(void) setupMenuItem { // Menu Item: NSMenuItem *editMenuItem = [[NSApp mainMenu] itemWithTitle:@"Edit"]; if (editMenuItem) { [[editMenuItem submenu] addItem:[NSMenuItem separatorItem]]; NSMenu *refactorCodeMenu = [[NSMenu alloc] initWithTitle:@"Refactor Code"]; NSMenuItem *menuItem; menuItem = [[NSMenuItem alloc] initWithTitle:@"Refactor Method Style" action:@selector(refactorMethodStyleMenuAction) keyEquivalent:@""]; [menuItem setTarget:self]; [refactorCodeMenu addItem:menuItem]; NSMenuItem *refactorCodeMenuItem = [[NSMenuItem alloc] initWithTitle:@"Refactor Code" action:nil keyEquivalent:@""]; [refactorCodeMenuItem setSubmenu:refactorCodeMenu]; [[editMenuItem submenu] addItem:refactorCodeMenuItem]; } }
效果圖以下:
2)顯示操做面板
- (void)refactorMethodStyleMenuAction { [self.operateController showWindow:nil]; NSURL *url = [[NSBundle bundleForClass:[self class]] URLForResource:@"DZOperateController" withExtension:@"nib"]; if (!url) { NSAlert *alert = [[NSAlert alloc] init]; alert.messageText = @"Refactor Method Style could not be shown because the plugin is corrupted."; alert.informativeText = @"If you build the plugin from sources using Xcode, make sure to perform 「Clean Build Folder「 in
Xcode and then build the plugin again.\n\nIf you installed the plugin via Alctraz, there is a pending issue causing
some files to be missing in the plugin. Prefer to install it via the plugin webpage."; [alert addButtonWithTitle:@"Download Latest"]; [alert addButtonWithTitle:@"Cancel"]; NSModalResponse result = [alert runModal]; if (result == NSAlertFirstButtonReturn) { [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://github.com/CharsDavy/RefactorCodePlugin-Xcode"]]; } } }
效果圖以下:
3)查找替換代碼風格
這一部分是重點部分,包括如何書寫正則表達式,而且利用正則表達式生成替換字符。還包括高亮代碼,具體能夠參見本人源碼:https://github.com/CharsDavy/RefactorCodePlugin-Xcode
4)最終效果圖
1.打開Alcatraz的插件包倉庫,地址:https://github.com/supermarin/alcatraz-packages
2.在簡介裏能夠看到Alcatraz的包分爲三類,分別爲:插件(plugins),配色方案(color schemes)和模板(templates)。
每一個包都必須包含」name」、」url」和」description」字段,還有一個可選的」screenshot」字段。
3.Fork這個倉庫,再克隆到本地。
4.以添加」Refactor Code」插件爲例,打開packages.json文件,在」plugins」數組裏加入:
{ "name": "Refactor Code", "url": "https://github.com/CharsDavy/RefactorCodePlugin-Xcode.git", "description": "Refactor code style,such as setter method.", "screenshot": "https://github.com/CharsDavy/RefactorCodePlugin-Xcode/raw/master/Screenshots/window.png" }
5.提交代碼到Fork的地址,再提交一個pull request到Master便可。
6.merged成功以後,便可看見如下效果圖
但願對你們有所幫助~