Introduction
In peacetime development process we use a lot of Xcode plug-ins, although the official plug-in production does not provide any support, but loaded three-party plug-ins, the default is allowed. Third-party plugins are stored in the ~/library/application Support/developer/shared/xcode/plug-ins folder, and the suffix must be. Xcplugin, which is actually a bundle. So we create a plug-in project to create the bundle project directly. Then, by modifying the suffix named. Xcplugin, place it in the ~/library/application support/developer/shared/xcode/plug-ins directory.
Xcode plug-in development is now mainly implemented in two ways, in fact, one of them is to use other people to provide a development template to save a lot of intermediate steps. This article will detail two implementation methods in turn.
How to prepare for work: through bundles
1. Create a bundle project
2. Project Setup
Plug-in engineering and common bundle works are still different, so special settings need to be made.
1) plist Documents of the project
Add three items:
Xcpluginhasui = NO
Xc4compatible = YES
Dvtplugincompatibilityuuids this is an array. Array content string, indicating that the plugin is compatible with the Xcode version, only the corresponding version of Xcode uiID added to this array, the plugin can be loaded. Otherwise, the plugin will not be loaded even if the plug-in is placed in the Xcode plug-in folder.
Get the current version of Xcode's UUID mode:
Enter the command in terminal:
Defaults Read/applications/xcode.app/contents/info Dvtplugincompatibilityuuid
Terminal will return a string of strings, which is the dvtplugincompatibilityuuid of Xcode.
2) Build Setting
Installation Build products location is set to ${home} [display is your user directory], this is the root directory of products.
Installation directory is set to/Library/Application Support/developer/shared/xcode/plug-ins, which is the directory that specifies the installation of your plugin. Note that this is actually a relative directory. The absolute directory of plugins is like this, for example/users/yohunl/library/application\ Support/developer/shared/xcode/plug-ins/alcatraz.xcplugin, The final absolute directory is the combination of installation Build products location and installation directory, which is why both are set.
Deployment location is set to Yes, which indicates that the project does not use the build locations in the settings, but instead uses installation directory to determine where to place the build.
The relevant file locations generated by the default project are all specified by build locations, and by deployment location set to Yes tells the project that we do not use this default setting, but we customize it.
Wrapper extension is set to Xcplugin, the suffix name must be xcplugin, otherwise it will not be loaded.
Mode two: through the template implementation
1) Download the Xcode plugin development template
Address: Https://github.com/kattrali/Xcode-Plugin-Template
2) Copy the downloaded template to ~/library/developer/xcode/templates/project templates/application Plug-in/xcode Plugin.xctemplate folder, if you do not have a corresponding folder, you manually create one.
3) Restart Xcode and when you create a new project you will see a application plug-in option in OS X with a Xcode plug-in template.
Realize
With these two preparation methods, we have been able to create the Xcode plug-in project, and the next step is to implement the plugin functionality.
1. Functional Requirements
In the current selected file to implement code style refactoring, the main implementation of setter method this style refactoring. For example
[Self setname:@ "Davy"]; ==> self.name = @ "Davy";
2. Analysis of Ideas
1) Find the method call in the current file that conforms to the setter method naming style.
2) Replace the found code that conforms to the refactoring style, reminding the user to save.
3. Technical difficulties
1) Xcode code edit box file content operation.
2) write the regular expression.
3) The Xcode Code edit box reminds the user to save the file.
As for the last point, because Xcode is grayed out for files that have not been saved, we can use this method to prompt the user to save the file. In addition, when looking, if you can highlight and follow the scrolling, the effect will be better.
4. Key code
These problems, I in the "Refactor Code" plug-in all implemented, now put the key method.
1) Add menu
-(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]; }}
As follows:
2) Display operator panel
- (void) refactormethodstylemenuaction{[Self.operatecontroller Showwindow:nil]; Nsurl*url = [[NSBundle bundleforclass:[selfclass]] Urlforresource:@"Dzoperatecontroller"Withextension:@"nib"]; if(!URL) {Nsalert*alert =[[Nsalert alloc] init]; Alert.messagetext=@"Refactor Method Style could not being 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 your installed the plugin via Alctraz, there is a pending issue causing
Some files to is 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"]]; } }}
As follows:
3) Find a replacement code style
This section is the focus, including how to write regular expressions, and use regular expressions to generate replacement characters. Also includes the highlight code, specific can see my source: Https://github.com/CharsDavy/RefactorCodePlugin-Xcode
4) Final
Submit Plugin to Alcatraz
1. Open Alcatraz Add-on package warehouse, address: https://github.com/supermarin/alcatraz-packages
2. In the introduction you can see that Alcatraz's packages are divided into three categories: plug-ins (plugins), color schemes (color schemes), and templates (templates).
Each package must contain the "name", "url", and "description" fields, as well as an optional "screenshot" field.
3.Fork this warehouse, and then clone to local.
4. To add the "refactor Code" plugin as an example, open the Packages.json file and add it to the "plugins" array:
{ "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. Submit the code to the fork's address, and then submit a pull request to master.
After 6.merged success, you will see the following
Hope to be helpful to everyone ~
Xcode Plugin Development Case Tutorial