2

我使用SwiftLint来控制我的项目的代码风格,它几乎一直运行良好。但是我遇到了一个小麻烦:当.swiftlint.yml文件与项目文件'.xcodeproj'不在同一个文件夹中时,如果我使用命令swiftlint autocorrect或使用Xcode构建来使用SwiftLint.swiftlint.yml ,我每次都需要更改。以下是详细信息:

  1. 我的项目结构如下:</li>
> 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在子文件夹中;

  1. 我已经通过安装了 SwiftLint brew install swiftlint
  2. 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
    

在此处输入图像描述

  1. .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)
    
  2. 当我使用带有文件的 SwiftLint 使用 Xcode 构建项目时SampleProject/SampleProject/Manager/Configs.swift,我需要更新以下included项目.swiftlint.yml

      included: # paths to include during linting. `--path` is ignored if present.
          - ./SampleProject/Manager/Configs.swift
    
  3. 当我使用带有文件的 SwiftLint 运行命令“swiftlint autocorrect”时SampleProject/SampleProject/Manager/Configs.swift,我需要更新以下included项目.swiftlint.yml

      included: # paths to include during linting. `--path` is ignored if present.
          - SampleProject/SampleProject/Manager/Configs.swift
    

我想找到一种方法来帮助解决这个问题,即不要更改.swiftlint.yml文件,但命令 'swiftlint autocorrect' 和 Xcode build 与 SwiftLint 一起工作正常。

4

0 回答 0