💻
ExistOrLive' Gitbook
  • README
  • ReadMe
  • 开发语言学习
    • Python
      • 0.概述
      • 1.基本数据类型
      • 2.变量
      • 3.函数
      • 4.运算符
      • 5.字符串
      • 6.列表
      • 7.元组
      • 8.集合
      • 9.字典
      • 10.流程控制
      • 12.函数
      • 13.模块
      • 14.输入输出格式化
      • 15.面向对象
    • Swift
      • 1.基础部分
        • 常量和变量以及基本类型_1
        • 常量和变量以及基本类型_2
      • 2.基本运算符
        • 基本运算符
      • 3.字符串和字符
        • 字符串
        • 字符串操作
      • 4.集合类型
        • 概述
        • Array
        • Set
        • Dictionary
      • 6.函数
        • 函数
        • 函数返回值
        • 函数类型
      • 7.闭包
        • 闭包
        • 闭包表达式
        • 尾随闭包
        • 捕获变量或常量
        • 闭包是引用类型
        • 逃逸闭包
        • 自动闭包
      • 8.枚举
        • 枚举
        • 枚举与switch语句
      • 9.类和结构体
        • 类和结构体
        • 属性
          • 属性
          • 属性观察器
          • 属性包装器
          • 全局变量和局部变量
          • 类属性
        • 方法
    • Shell
      • Shell变量和基本类型
      • Shell函数
      • Shell基本运算符
      • Shell脚本的参数
      • Shell流程控制
      • Shell输入输出
      • Shell文件包含
  • iOS
    • UI
      • UIKit
        • UIView
          • UIView
          • drawRect
        • UITableView
          • UITableView
          • UITableView的加载过程
  • 学习开发工具
    • 静态分析
      • OCLint
      • infer
      • SwiftLint
    • iOS构建工具
      • fastlane
        • fastlane
        • fastlane的安装
        • fastlane一键打包
        • fastlane证书管理
    • Cocoapods
      • 安装pod库-podfile
      • 创建pod库-podspec
  • 开源库
    • Tool
      • Swiftlint源码学习
      • 利用Swiftlint自定义规则实现自动化code review
由 GitBook 提供支持
在本页
  • 1. 安装及使用
  • 2. 配置文件 .swiftlint.yml
  • 3. 集成方式
  • 4. 参考文档

这有帮助吗?

在GitHub上编辑
  1. 学习开发工具
  2. 静态分析

SwiftLint

1. 安装及使用

#swiftlint #tool #Swift

1.1 安装

  • homebrew

    • 不侵入原工程

    • 安装方便

    • 不便于版本控制 ​

# 安装当前最新版本的swiftlint
brew install swiftlint

​

  • cocoapods

    • 通过 podfile.lock 可以控制版本

    • 修改podfile, 侵入原工程

# podfile

pod 'swiftlint'

# 执行命令
pod install 
# path 检查的路径
# reporter 输出报告的格式 xcode, json, csv, checkstyle, codeclimate, junit, html, emoji, sonarqube, markdown, github-actions-logging
swiftlint lint --path <path> --reporter <reporter> 

​

2. 配置文件 .swiftlint.yml

  • disabled_rules: 关闭某些默认开启的规则。

  • opt_in_rules: 一些规则是可选的。

  • only_rules: 不可以和 disabled_rules 或者 opt_in_rules 并列。类似一个白名单,只有在这个列表中的规则才是开启的。

disabled_rules: # 执行时排除掉的规则
  - colon
  - comma
  - control_statement
opt_in_rules: # 一些规则仅仅是可选的
  - empty_count
  - missing_docs
  # 可以通过执行如下指令来查找所有可用的规则:
  # swiftlint rules
included: # 执行 linting 时包含的路径。如果出现这个 `--path` 会被忽略。
  - Source
excluded: # 执行 linting 时忽略的路径。 优先级比 `included` 更高。
  - Carthage
  - Pods
  - Source/ExcludedFolder
  - Source/ExcludedFile.swift

# 可配置的规则可以通过这个配置文件来自定义
# 二进制规则可以设置他们的严格程度
force_cast: warning # 隐式
force_try:
  severity: warning # 显式
# 同时有警告和错误等级的规则,可以只设置它的警告等级
# 隐式
line_length: 110
# 可以通过一个数组同时进行隐式设置
type_body_length:
  - 300 # warning
  - 400 # error
# 或者也可以同时进行显式设置
file_length:
  warning: 500
  error: 1200
# 命名规则可以设置最小长度和最大程度的警告/错误
# 此外它们也可以设置排除在外的名字
type_name:
  min_length: 4 # 只是警告
  max_length: # 警告和错误
    warning: 40
    error: 50
  excluded: iPhone # 排除某个名字
identifier_name:
  min_length: # 只有最小长度
    error: 4 # 只有错误
  excluded: # 排除某些名字
    - id
    - URL
    - GlobalAPIKey
reporter: "xcode" # 报告类型 (xcode, json, csv, checkstyle, codeclimate, junit, html, emoji, sonarqube, markdown, github-actions-logging)

3. 集成方式

3.1 集成至Xcode中

在build phrases中创建 run script中,执行 swiftlint ​

  • 每次编译时执行

  • 直接在代码出显示warning或error

  • 一般开发直接忽略warning !

# Type a script or drag a script file from your workspace to insert its path.
echo "开始检查swiftlint安装"
if ! [ -e `which swiftlint` ] 
then 
    echo "swiftlint未安装,开始安装swiftlint"
    brew intall swiftlint
fi 

changeFilesArray=(`git diff --name-only head | grep '.swift$'`)

echo "一共修改了${#changeFilesArray[*]}文件"
echo ${changeFilesArray[*]}

swiftlint lint --path ${changeFilesArray[*]}

3.2 githook在pre-commit

4. 参考文档

上一页infer下一页iOS构建工具

最后更新于3年前

这有帮助吗?

​

截屏2022-01-14 17.16.45.png
截屏2022-01-14 16.37.32.png

​

​

​

GitHub-SwiftLint
Git官方文档-githooks
pre-commi(A framework for managing and maintaining multi-language pre-commit hooks)
GitHub-SwiftLint
Continuous code inspection with SwiftLint for iOS Apps
Git官方文档-githooks
pre-commi(A framework for managing and maintaining multi-language pre-commit hooks)
聊聊SwiftLint在团队的实践