美文网首页
OCLint 静态代码分析

OCLint 静态代码分析

作者: manajay | 来源:发表于2017-06-16 00:47 被阅读302次

    环境

    • Homebrew 终端安装 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    • oclint 安装 brew install oclint
    • xcpretty 安装 gem install xcpretty

    脚本 关键点

    • 明确项目是否依赖CocoaPods
    • 使用xcodebuild -list, 明确 scheme target configuration

    • xcodebuild clean
    • xcodebuild -target xxx -scheme xxx 或者 xcodebuild -workspace xxx -scheme xxx
    • xcodebuild |xcpretty -r json-compilation-database
      build/reports中得到文件compilation_db.json
    compilation_db.json
    • compilation_db.json 移到与sh脚本同路径下
      cp ./build/reports/compilation_db.json ./compile_commands.json

    • oclint-json-compilation-database -- -report-type html -o oclint_result.html 生成 oclint_result.html文件

    • 使用-e忽略不需要分析的目录, 比如CocoaPos生成的第三方库
      Pods 文件, 命令 -e Pods

    • oclint分析错误报 ”too many errors emitted, stopping now” 是因为项目中错误太多, 超过了oclint的默认上限, 所以oclint停止了工作.

    设置-max-priority-1 -max-priority-2 -max-priority-3的值, 提高上限,
    
    例:oclint-json-compilation-database -- -max-priority-1 10000 -max-priority-2 10000 -max-priority-3 10000 -rc LONG_LINE=150 -report-type pmd -o oclint.xml
    
    • oclint升级到0.11版本以上

    • 注意脚本的执行环境路径配置 ~/.bash_profile 或者 /etc/profile

    脚本的全文

    #! /bin/sh
    #或者source ~/.bash_profile
    source /etc/profile
    
    if which oclint 2>/dev/null; then
    echo 'oclint exist'
    else
    brew tap oclint/formulae
    brew install oclint
    fi
    if which xcpretty 2>/dev/null; then
    echo 'xcpretty exist'
    else
    gem install xcpretty
    fi
    
    # workspace 和 target 只能使用一种. 
    #myworkspace=XXX.xcworkspace # 替换workspace的名字
    #myscheme=XXX # 替换scheme的名字
    
    targetname=HttpManager
    myscheme=HttpManager
    
    xcodebuild -target $targetname -scheme $myscheme clean&&
    xcodebuild -target $targetname -scheme $myscheme \
    -configuration Debug \
    | xcpretty -r json-compilation-database
    
    cp ./build/reports/compilation_db.json ./compile_commands.json
    
    if [ -f ./compile_commands.json ]; then echo "compile_commands.json 文件存在";
    else echo "-----compile_commands.json文件不存在-----"; fi
    
    oclint-json-compilation-database  -- \
    -report-type html -o oclint_result.html \
    -rc LONG_LINE=200 \
    -rc=NCSS_METHOD=100 \
    -max-priority-1=100000 \
    -max-priority-2=100000 \
    -max-priority-3=100000; \
    rm compile_commands.json;
    if [ -f ./oclint_result.html ]; then echo '-----分析完毕-----'
    else echo "-----分析失败-----"; fi
    
    

    结果

    oclint-report.png
    • 也可以 设置 结果为 -report-type xcode
    结果-01.png 结果-02.png
    #! /bin/sh
    #或者source ~/.bash_profile
    source /etc/profile
    cd ${SRCROOT}
    
    if which oclint 2>/dev/null; then
    echo 'oclint exist'
    else
    brew tap oclint/formulae
    brew install oclint
    fi
    if which xcpretty 2>/dev/null; then
    echo 'xcpretty exist'
    else
    gem install xcpretty
    fi
    
    xcodebuild  clean&&
    xcodebuild | xcpretty -r json-compilation-database
    
    cp ./build/reports/compilation_db.json ./compile_commands.json
    
    if [ -f ./compile_commands.json ]; then echo "compile_commands.json 文件存在";
    else echo "-----compile_commands.json文件不存在-----"; fi
    
    
    oclint-json-compilation-database  -- \
    -report-type xcode \
    -rc LONG_LINE=200 \
    -rc=NCSS_METHOD=100 \
    -max-priority-1=100000 \
    -max-priority-2=100000 \
    -max-priority-3=100000; \
    rm compile_commands.json;
    

    问题

    • 随意创建的项目测试没有问题, 不过我公司的项目测试的时候,发现使用-report-type xcode 法忽略某些第三方库 -e [filePath] 也没用作用. 但是-report-type html 却可以.

    相关链接


    OCLint 安装与使用
    iOS使用OCLint做静态代码分析

    关于OCLint自定义规则
    使用 oclint + Jenkins 进行自动化 Code Review


    iOS Infer 静态代码分析

    相关文章

      网友评论

          本文标题:OCLint 静态代码分析

          本文链接:https://www.haomeiwen.com/subject/uhkpqxtx.html