├── README.md └── oclint_apply.sh /README.md: -------------------------------------------------------------------------------- 1 | # oclint_apply 2 | 3 | 使用oclint检测代码,在学习的过程中,为了方便写的一些脚本 4 | 5 | ``` 6 | # 替换workspace的名字 7 | myworkspace="test.xcworkspace" 8 | # 替换scheme的名字 9 | myscheme="test" 10 | # 输出方式 xcode/pmd/html 11 | reportType="xcode" 12 | 13 | # [自定义report](http://docs.oclint.org/en/stable/howto/selectreporters.html) 如: 14 | nowReportType="-report-type html -o oclint_result.html" 15 | 16 | # 自定义排除警告的目录,将目录字符串加到数组里面,结果中将不会含有Pods文件夹下文件编译警告 17 | exclude_files=("test_js" "Pods") 18 | ``` 19 | 20 | 1. Using OCLint in Xcode => 输出方式 xcode 21 | 2. Using OCLint with Jenkins CI => 输出方式 pmd 22 | -------------------------------------------------------------------------------- /oclint_apply.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 指定编码 4 | export LANG="zh_CN.UTF-8" 5 | export LC_COLLATE="zh_CN.UTF-8" 6 | export LC_CTYPE="zh_CN.UTF-8" 7 | export LC_MESSAGES="zh_CN.UTF-8" 8 | export LC_MONETARY="zh_CN.UTF-8" 9 | export LC_NUMERIC="zh_CN.UTF-8" 10 | export LC_TIME="zh_CN.UTF-8" 11 | export LC_ALL= 12 | 13 | function checkDepend () { 14 | command -v xcpretty >/dev/null 2>&1 || { 15 | echo >&2 "I require xcpretty but it's not installed. Install:gem install xcpretty"; 16 | exit 17 | } 18 | command -v oclint-json-compilation-database >/dev/null 2>&1 || { 19 | echo >&2 "I require oclint-json-compilation-database but it's not installed. Install:brew install oclint"; 20 | exit 21 | } 22 | } 23 | 24 | function oclintForProject () { 25 | 26 | # 检测依赖 27 | checkDepend 28 | 29 | projectName=$1 30 | scheme=$2 31 | reportType=$3 32 | 33 | REPORT_PMD="pmd" 34 | REPORT_XCODE="xcode" 35 | REPORT_HTML="html" 36 | 37 | myworkspace=${projectName} 38 | myscheme=${scheme} 39 | echo "myworkspace是:${myworkspace}" 40 | echo "myscheme是:${myscheme}" 41 | echo "reportType为:${reportType}" 42 | 43 | # 清除上次编译数据 44 | if [ -d ./build/derivedData ]; then 45 | echo '-----清除上次编译数据derivedData-----' 46 | rm -rf ./build/derivedData 47 | fi 48 | 49 | # xcodebuild -workspace $myworkspace -scheme $myscheme clean 50 | xcodebuild clean 51 | 52 | echo '-----开始编译-----' 53 | 54 | # 生成编译数据 55 | xcodebuild -workspace ${myworkspace} -scheme ${myscheme} -sdk iphonesimulator -derivedDataPath ./build/derivedData -configuration Debug COMPILER_INDEX_STORE_ENABLE=NO | xcpretty -r json-compilation-database -o compile_commands.json 56 | 57 | 58 | if [ -f ./compile_commands.json ] 59 | then 60 | echo '-----编译数据生成完毕-----' 61 | else 62 | echo "-----生成编译数据失败-----" 63 | return -1 64 | fi 65 | 66 | echo '-----分析中-----' 67 | 68 | # 自定义排除警告的目录,将目录字符串加到数组里面 69 | # 转化为:-e Debug.m -e Port.m -e Test 70 | exclude_files=("cardloan_js" "Pods") 71 | 72 | exclude="" 73 | for i in ${exclude_files[@]}; do 74 | exclude=${exclude}"-e "${i}" " 75 | done 76 | echo "排除目录:${exclude}" 77 | 78 | # 分析reportType 79 | if [[ ${reportType} =~ ${REPORT_PMD} ]] 80 | then 81 | nowReportType="-report-type pmd -o pmd.xml" 82 | elif [[ ${reportType} =~ ${REPORT_HTML} ]] 83 | then 84 | nowReportType="-report-type html -o oclint_result.html" 85 | else 86 | nowReportType="-report-type xcode" 87 | fi 88 | 89 | # 自定义report 如: 90 | # nowReportType="-report-type html -o oclint_result.html" 91 | 92 | # 生成报表 93 | oclint-json-compilation-database ${exclude} -- \ 94 | ${nowReportType} \ 95 | -rc LONG_LINE=200 \ 96 | -disable-rule ShortVariableName \ 97 | -disable-rule ObjCAssignIvarOutsideAccessors \ 98 | -disable-rule AssignIvarOutsideAccessors \ 99 | -max-priority-1=100000 \ 100 | -max-priority-2=100000 \ 101 | -max-priority-3=100000 102 | 103 | rm compile_commands.json 104 | if [[ ${reportType} =~ ${REPORT_PMD} ]] && [ ! -f ./pmd.xml ] 105 | then 106 | echo "-----分析失败-----" 107 | return -1 108 | else 109 | echo '-----分析完毕-----' 110 | return 0 111 | fi 112 | } 113 | 114 | # 替换workspace的名字 115 | myworkspace="test.xcworkspace" 116 | # 替换scheme的名字 117 | myscheme="test" 118 | # 输出方式 xcode/pmd 119 | reportType="xcode" 120 | 121 | oclintForProject ${myworkspace} ${myscheme} ${reportType} 122 | --------------------------------------------------------------------------------