3

I use SwiftLint to control the code style of my project, it is working fine almost all the time. But I met a little trouble: when the .swiftlint.yml file not in the same folder with the project file '.xcodeproj', if I use command swiftlint autocorrect or use Xcode build to use SwiftLint I need change .swiftlint.yml every time. The below is the details:

  1. My project structure like below:
> 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

the .swiftlint.yml in the root path, but SampleProject.xcodeproj in the sub folder;

  1. I've installed SwiftLint via brew install swiftlint;
  2. I've add SwiftLint Script with below code in Xcode for project 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
    

enter image description here

  1. .swiftlint.yml code as below:

          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. When I build project with Xcode with SwiftLint with file SampleProject/SampleProject/Manager/Configs.swift, I need update the included item in .swiftlint.yml as below:

      included: # paths to include during linting. `--path` is ignored if present.
          - ./SampleProject/Manager/Configs.swift
    
  3. When I run command 'swiftlint autocorrect' with SwiftLint with file SampleProject/SampleProject/Manager/Configs.swift, I need update the included item in .swiftlint.yml as below:

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

I want find a way to help on this is do not change .swiftlint.yml file but command 'swiftlint autocorrect' and Xcode build are working fine with SwiftLint.

Raymond Liao
  • 1,799
  • 3
  • 20
  • 32

0 Answers0