In recent weeks, using Swift as a command-line tool, Smck is used to analyze and process the code of existing OC projects. Future project switching to Swift can add support for Swift Engineering code. Yesterday I saw meow on Weibo that he was going to live coding next week. A swift command-line tool, Portal: Field Programming-create command-line tools with Swift fengniao-cli Part1, actually quite looking forward. Think with meow God is very predestined, recently under he developed IOS app Mail Me, at any time can record work and prepare lectures some inspiration, smck some of the implementation of the module design inspiration is also through this application at any time recorded down, so also recommend everyone use, really convenient. And the previous segmentfault invited me to do a lecture on their live platform 31st this month, Portal: Drill down into IOS compilation CLANG/LLVM-Daiming-Segmentfault Auditorium, previously written an article, live may be more conducive to the presentation and details of some details it. It seems like I'm going to do a lot of things with meow this time. Smck's code has been put on Github today, address: Https://github.com/ming1016/smck
What can smck do?
- Simple, such as the name of the rules of the inspection, according to the team standards, such as all inherited uiviewcontroller the end of the unified into VC, inherit the Jsonmodel named end of the unified Model, there is a specific function base class sub-class according to a certain naming rules.
- For example, find all Chinese strings to list which control each string is used on.
- Analyze which classes have never been called to determine whether a class is useless, based on rules such as whether the class is initialized or called directly.
- Inspection of the quality of the project, such as the Nsstring,block,nsarray property check for copy, and whether the protocol is a weak,array operation using a Category with safe operation and so on. Certainly need to check analysis and processing of certainly more than these, so in Smck this program has become a very useful to add a variety of inspection function module structure, through simple code written by everyone or team can easily write to add a variety of checker.
How do you do simple writing to add functionality?
Because the process of code analysis is done through a series of parser that have already been written, parser will complete the analysis and context of the token and the data information of all the nodes in the project's global analysis output. A wide range of functions can be customized based on these complete information and a defined set of structures with a complete containment relationship. Here are some parser and feature descriptions:
- Parsingmethod.swift: The method structure is returned, including the methods name, parameters, methods used within the method, temporary variables defined within the method, and so on.
- Parsingmethodcontent.swift: Tokens within the parsing method are processed based on the syntax to form an array structure. There are a lot of things to deal with here, and more data output is being added to the plan.
- Parsingmacro.swift: Handle macro definition, mainly output token to other parser to deal with.
- Parsingimport.swift: Returns the import struct, containing the introduced class name, the package name
- Parsingproperty.swift: Parses the defined attribute property information
- Parsinginterface.swift: According to this analysis of the number of categories defined in a file, the structure of the class Object class name, the parent class name, the class name will be resolved here.
- Parsingprotocol.swift: The parsed protocol is set to the Object structure body.
- Parsingobject.swift: At present, the main analysis of the use of the class information.
The resulting File structure contains a set of individual sub-structures, breakpoints
How do I debug Smck?
First fill in the corresponding command line parameters and values, set the parameter reference. Then you can run it.
Export as command line tool
Select Product-archive-export in Xcode to perform
./smck -o /User/your_project_path
The output is as follows
How do I write my own check function?
Because the engineering inspection rules are very diverse, so you need to write some Plugin, I will gradually pull out some of the common in the Plugin directory I put two examples, in the example can be seen how to subscribe Parser Different data from different nodes of the output for different checks. The code associated with Parser and Plugin in the console management related Checker class has also become very concise because of the use of RxSwift, as follows:
DoO (path:guard path. Characters. unuseobjectplugin (). Plug (ob: Parsingentire. Parsing (path: Path)}
Engineering Code Static Analysis command-line tools written in Swift Smck