iOS Continuous Integration Series - Automated Code Review

Keywords: JSON xcode Database brew

introduce

In order to ensure code quality, code review is very important. Usually we only pay attention to the realization of interface and logic in the process of development, but do not consider the standardization of code. Code review is to strictly require our own. We use OCLint+xcpretty to review the code. Here, I will only introduce the local code review.

What is OCLint

OCLint is a code static analysis tool that can be used to improve quality and reduce defects. It currently supports C, C++, Objective-C languages.

What is xcpretty

xcpretty is a format for the output of xcodebuild. It also includes the output report function.

install

We use homebrew to install oclint:

$ brew tap oclint/formulae

$ brew install oclint

If it can't be installed, it can be installed in other ways. Course.

Then install xcpretty:

$ gem install xcpretty

Generate compile_commands.json file

Enter the project catalogue and run to get the file:

$ xcodebuild | xcpretty -r json-compilation-database –output compile_commands.json

If your project has CocoaPods, you need to specify - workspace test.xcworkspace -scheme test when building Xcode

Configure Xcode

Open your Xcode project and add a new target:

Take a simpler name, such as OCLint:

After the creation is complete, add a script for the target:

Copy the following into run Script:

source ~/.bash_profile
cd ${SRCROOT}
xcodebuild clea
xcodebuild | xcpretty -r json-compilation-database
oclint-json-compilation-database \
-e Pods \
-- \
-disable-rule ObjCAssignIvarOutsideAccessors \
-rc=MINIMUM_CASES_IN_SWITCH=3 \
-rc=LONG_VARIABLE_NAME=20 \
-disable-rule ShortVariableName \
-rc=CYCLOMATIC_COMPLEXITY=10 \
-rc=LONG_CLASS=700 \
-rc=LONG_LINE=200 
-rc=LONG_METHOD=80 \
-rc=NCSS_METHOD=40 \
-rc=NESTED_BLOCK_DEPTH=5 \
-rc=TOO_MANY_FIELDS=20 \
-rc=TOO_MANY_METHODS=30 \
-rc=TOO_MANY_PARAMETERS=6 \
-report-type xcode \

Some files we do not want to do static analysis, such as some third-party libraries, we can set up to ignore folders:

-e Pods

OCLint has a set of default rules. I modify these default scanning rules, such as ignoring some rules or changing the threshold of some rules. Here are some scanning rules that I have customized:

# Ignore if folding
-disable-rule CollapsibleIfStatements \
#Ignore direct use variables
-disable-rule ObjCAssignIvarOutsideAccessors \
#Minimum number of switch case s
-rc=MINIMUM_CASES_IN_SWITCH=3 \
#Maximum byte of variable name
-rc=LONG_VARIABLE_NAME=20 \
#Minimum byte of variable name
-disable-rule ShortVariableName \
#Loop complexity
-rc=CYCLOMATIC_COMPLEXITY=10 \
#Number of rows per class
-rc=LONG_CLASS=700 \
#Number of bytes per line
-rc=LONG_LINE=200 
#Number of rows per method
-rc=LONG_METHOD=80 \
#Number of valid lines of code that ignore parentheses after comments
-rc=NCSS_METHOD=40 \
#Nesting Depth
-rc=NESTED_BLOCK_DEPTH=5 \
#Number of fields
-rc=TOO_MANY_FIELDS=20 \
#Method quantity
-rc=TOO_MANY_METHODS=30 \
#Method parameter
-rc=TOO_MANY_PARAMETERS=6 \
-report-type xcode \

Select the right Scheme to build Command+B

If the compilation is successful, we can see the following results:

Epilogue

This is a method of local code review. If you need to cooperate with svn for server review, I will sort it out later.

Posted by stormcloud on Mon, 15 Apr 2019 01:21:33 -0700