我使用SwiftLint来控制项目的代码样式,它几乎一直都能正常工作。但是我遇到了一个麻烦:当.swiftlint.yml
文件与项目文件'.xcodeproj'不在同一文件夹中时,如果我使用命令swiftlint autocorrect
或使用Xcode build来使用SwiftLint每次都需要更改.swiftlint.yml
。下面是详细信息:
> SampleProject > ├── .swiftlint.yml > ├── SampleProject > │ ├── Frameworks > │ ├── Info.plist > │ ├── SampleProject > │ ├── SampleProject.xcodeproj > │ └── SampleProjectTests > ├── SampleProject.xcworkspace > ├── Configuration > ├── CommonFoundation > │ ├── CommonFoundation > │ ├── CommonFoundation.xcodeproj > │ └── CommonFoundationTests > ├── Gemfile > ├── Gemfile.lock > ├── Podfile > ├── Podfile.lock > ├── Pods > ├── README.md > ├── fastlane > └── release.sh
根路径中的.swiftlint.yml
,但子文件夹中的SampleProject.xcodeproj
;
brew install swiftlint
安装了SwiftLint; 我在项目SwiftLint Script
的Xcode中用以下代码添加了SampleProject.xcodeproj
:
if which swiftlint >/dev/null; then
swiftlint --config ../.swiftlint.yml
else
echo "warning: SwiftLint not installed, 'brew install swiftlint' or download from https://github.com/realm/SwiftLint"
fi
.swiftlint.yml
代码如下:
disabled_rules: # rule identifiers to exclude from running
- colon
- comma
- control_statement
- line_length
- type_body_length
- function_body_length
- cyclomatic_complexity
opt_in_rules: # some rules are only opt-in
- empty_count
included: # paths to include during linting. `--path` is ignored if present.
- ./SampleProject/Manager/Configs.swift
excluded: # paths to ignore during linting. Takes precedence over `included`.
- Carthage
- Pods
analyzer_rules: # Rules run by `swiftlint analyze` (experimental)
- explicit_self
force_cast: warning # implicitly
force_try:
severity: warning # explicitly
type_body_length:
- 300 # warning
- 400 # error
# or they can set both explicitly
file_length:
warning: 1000
error: 1500
type_name:
min_length: 4 # only warning
max_length: # warning and error
warning: 40
error: 50
excluded: iPhone # excluded via string
identifier_name:
min_length: # only min_length
error: 3 # only error
excluded: # excluded via string array
- i
- j
- id
- GlobalAPIKey
reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, junit, html, emoji, sonarqube, markdown)
当我使用带有SwiftLint和文件SampleProject/SampleProject/Manager/Configs.swift
的Xcode构建项目时,需要更新included
中的.swiftlint.yml
项目,如下所示:
included: # paths to include during linting. `--path` is ignored if present.
- ./SampleProject/Manager/Configs.swift
当我使用文件SampleProject/SampleProject/Manager/Configs.swift
对SwiftLint运行命令'swiftlint autocorrect'时,我需要更新included
中的.swiftlint.yml
项目,如下所示:
included: # paths to include during linting. `--path` is ignored if present.
- SampleProject/SampleProject/Manager/Configs.swift
我想找到一种解决方法,那就是不要更改.swiftlint.yml
文件,但命令'swiftlint autocorrect'和Xcode构建可以在SwiftLint上正常工作。