preface

OCLint is a static Code analysis tool that supports custom rules, which is a great way to standardize Code. This article mainly introduces how to use OCLint to let XCode Review and standardize Code for you. More information about OCLint can also be found on the OCLint website.

Install OCLint and XCpretty

The OCLint and XCpretty tools need to be installed here

1, install,xcpretty

Install xcpretty with gem

sudo gem install xcprettyCopy the code

Sudo is used to prevent problems with some permissions

2, installation,OCLint

There are two common methods of installing OCLint:

1) Through the installation package:

Download the installation package oclint-0.10.3-x86_64-Darwin-15.5.0.tar.gz from oclint’s Github project link. After decompressing, configure environment variables in the. Bashrc of the current user

OCLINT_HOME=/pathexport PATH=$PATH:OCLINT_HOME/binCopy the code

Then source.bashrc will do the job

2) Via Homebrew
brew tap oclint/formulae
brew install oclintCopy the code
3) Verify that the installation is successful

Run Oclint to verify that the environment is configured correctly

$ oclintCopy the code

The installation is successful if the following information is displayed:

oclint: Not enough positional commandline arguments specified! Must specify at least 1 positional arguments: See: oclint -helpCopy the code

The use of OCLint

Here are some simple and common usages that are sufficient for many scenarios, and more details can be found in the OCLint Manual.



oclint
oclint-json-compilation-database
oclint-xcodebuild

1. Oclint: Routine core instructions. 2. Oclint-json-compilation-database: Read configuration information from compile_commands. Json file and execute oclint. Oclint-xcodebuild is used to generate compile_commands.json files. It is no longer maintained, we can use xcpretty to generate JSON files instead.

Oclint instruction

Use the syntax

oclint [options] <source> -- [compiler flags]Copy the code

[options] indicates some parameter options, such as rule loading option, report form option, etc.

$(/path/to/bin/oclint)/.. /lib/oclint/rules 2, -disable-rule < rule name > : invalidates the corresponding rule (Oclint rule list). -rc < Parameter >=< Value > : changes the threshold. 4. -report-type < report type >, which can be text, HTML, JSON, PMD, and xcode.

eg:

oclint  -R  /path/to/rules   -disable-rule ObjCAssignIvarOutsideAccessors -report-type xcodeCopy the code
Oclint – json – compilation – database commands

This is the main instruction we use. It is an upgraded version of the Oclint instruction, which is relatively simple and convenient to use. You can add the Oclint option to the end of the directive by using –.

The oclint-json-compilation-database command has the option of filtering files. -I: to include certain files. -e: to filter certain files.

# Remove files from your Pods
oclint-json-compilation-database -e Pods 
# Files included in your Pods
oclint-json-compilation-database -i PodsCopy the code
Oclint – xcodebuild instructions.

Since the Oclint-XcodeBuild directive is no longer maintained and is replaced by xcpretty in practice, it will not be covered here.

Other command tools: Xcodebuild and xcpretty

xcodebuild

Xcodebuild is apple’s automated build release tool that compiles and packages Xcode projects using command-line scripts. You can query the usage by typing man xcodeBuild at the terminal

xcpretty

Xcpretty is used to generate json files for OClint parsing because Oclint-XcodeBuild is no longer maintained. Xcpretty is used to generate json files after XCode 8.

Check the code with XCode

First add the targets button:





Select the OCLint Target, go to the Build Phases option, click the plus sign in the upper left corner, and select “New Run Script Phase”.

Then enter the script:

Sample script:

source ~/.bash_profile
cd ${SRCROOT}
xcodebuild clean
xcodebuild  -workspace demo.xcworkspace -scheme demo | xcpretty -r json-compilation-database --output compile_commands.json
oclint-json-compilation-database -v \
-e Pods \
-e MGLivenessDetection \
-e MGBaseKit \
-e MGIDCard \
oclint_args -- -report-type xcode \
-disable-rule ObjCAssignIvarOutsideAccessors \
-disable-rule ShortVariableName \
-rc=MINIMUM_CASES_IN_SWITCH=3 \
-rc=CYCLOMATIC_COMPLEXITY=10 \
-rc=LONG_CLASS=700 \
-rc=LONG_LINE=200 \
-rc=NCSS_METHOD=40 \
-rc=NESTED_BLOCK_DEPTH=5 \
-rc=TOO_MANY_FIELDS=20 \
-rc=TOO_MANY_METHODS=30 \
-rc=TOO_MANY_PARAMETERS=6
exitCopy the code

Then select the OCLint Scheme and Command+B will execute. It is best to remove the data cache in DriveData before executing to ensure that the old compiled content is not parsed. Xcode will display a Warning on code that does not conform to the rules after successful compilation:

Reference links:

Static analysis of iOS code with OCLint