cd $WORKSPACE
xcodebuild -workspace xxx.xcworkspace -scheme xxx clean build | tee xcodebuild.log | xcpretty --report json-compilation-database
mv build/reports/compilation_db.json compile_commands.json
oclint-json-compilation-database -exclude Pods -- -report-type pmd -o oclint.xml -max-priority-1 99999 -max-priority-2 99999 -max-priority-3 99999 -rc LONG_LINE=140 -rc LONG_METHOD=80 -rc NCSS_METHOD=50 -rc SHORT_VARIABLE_NAME=1 -rc CYCLOMATIC_COMPLEXITY=13 -rc MINIMUM_CASES_IN_SWITCH=2 -rc NPATH_COMPLEXITY=1500
rm -rf sonar-reports
mkdir sonar-reports
cat oclint.xml | sed "s#Switch Statements Should Have Default Rule#switch statements should have default#g" \
| sed "s#missing hash method#must override hash with isEqual#g" \
| sed "s#prefer early exits and continue#use early exits and continue#g" \
| sed "s#use boxed expression#replace with boxed expression#g" \
| sed "s#use container literal#replace with container literal#g" \
| sed "s#use number literal#replace with number literal#g" \
| sed "s#use object subscripting#replace with object subscripting#g" \
| sed "s#missing default in switch statements#switch statements should have default#g" \
| sed "s#unnecessary default statement in covered switch statement#switch statements don't need default when fully covered#g" \
| sed "s#covered switch statements dont need default#switch statements don't need default when fully covered#g" > sonar-reports/oclint.xml
rm -f sonar-project.properties
cat > sonar-project.properties <<- EOF
sonar.projectKey=xxx-iOS
sonar.projectName=xxx-iOS
sonar.projectVersion=x.x.x
sonar.language=objectivec
sonar.sources=sources
sonar.sourceEncoding=UTF-8
sonar.objectivec.oclint.reportPath=sonar-reports/oclint.xml
EOF
/bin/sh sonar-scanner -X
3 errors generated.
20 errors generated.
20 errors generated.
20 errors generated.
8 errors generated.
19 errors generated.
3 errors generated.
63 errors generated.
The rule 'OCLint:use number literal' does not exist.
The rule 'OCLint:use object subscripting' does not exist.
The rule 'OCLint:ill-placed default label in switch statement' does not exist.
The rule 'OCLint:Switch Statements Misplaced Default Label' does not exist.
sed "s#missing hash method#must override hash with isEqual#g"
brew install Swiftlint
gem install slather
sudo pip install lizard
cd $WORKSPACE
rm -rf kuai-swiftlint.txt
swiftlint lint --path Duobao > xxx-swiftlint.txt
rm -rf sonar-project.properties
cat > sonar-project.properties <<- EOF
sonar.projectKey=xxx-iOS-swift
sonar.projectName=xxx-iOS-swift
sonar.projectVersion=x.x.x
sonar.language=swift
sonar.projectDescription=xxx with Swift
sonar.sources=sources
sonar.swift.workspace=xxx.xcworkspace
sonar.swift.appScheme=xxx
sonar.sourceEncoding=UTF-8
sonar.swift.swiftlint.report=xxx-swiftlint.txt
EOF
/bin/sh sonar-scanner -X
objectivec_swift目录结构
--objectivec:完整项目文件
--swift:完整项目文件
sonar.projectKey=objectivec_swift
sonar.projectName=objectivec_swift
sonar.projectVersion=1.9.0
sonar.sourceEncoding=UTF-8
#分模块
sonar.modules=objective,swift
#构建objectivec
objective.sonar.projectName=objectivec
objective.sonar.language=objectivec
objective.sonar.projectBaseDir=objectivec
objective.sonar.sources=sources
objective.sonar.oclint.reportPath=sonar-reports/oclint.xml
#构建swift
swift.sonar.projectName=swift
swift.sonar.language=swift
swift.sonar.sources=sources
swift.sonar.projectBaseDir=swift
swift.sonar.swift.workspace=swift/xxx.xcworkspace
swift.sonar.swift.appScheme=Duobao
swift.sonar.sourceEncoding=UTF-8
swift.sonar.swift.swiftlint.report=swift/xxx-swiftlint.txt
最后附上github地址,随意取:
SonarQube Plugin for Objective C
>>特别鸣谢:冯海翼(独门秘籍)、吕凯(实践修改Objective-C插件源码)、张振勇(处理打包脚本)
本文来自网易实践者社区,经作者李乐授权发布。