• [iOS/ Swift] SwiftLint & SwiftFormat 적용하기

    2021. 6. 13.

    by. dundin

    반응형

    비슷하면서 다른 SwiftLint, SwiftFormat, 적용하는 방법을 기록해 보도록 하겠습니다. 

     

    SwiftLint 

    https://github.com/realm/SwiftLint

    Rule list: https://realm.github.io/SwiftLint/rule-directory.html

    - 빌드타임에 정해진 룰에 의해서 error, warning을 내는 것이 주 기능으로 전체 줄 수, 파라미터 갯수, 함수의 길이 등을 관리한다. 

    - swiftlint autocorrect 라는 기능이 있지만 나는 최초 한번만 실행했다. 

     

    SwiftFormat 

    https://github.com/nicklockwood/SwiftFormat

    Rule list: https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md

    - indent 수, maxWidth 등을 설정해서 commit 타임에 자동으로 적용되도록 한다. 

     

     

    두 개가 상충하는 조건을 걸지 않는다면 함께 써도 무방하다. 보통 포맷 관련을 swiftFormat으로 몰고 SwiftLint는 워닝 용도로만 사용하면 좋을 것 같다. 

     

    SwiftLint 적용방법 

    1. Podfile에 swiftLint 추가 

    pod 'SwiftLint'

    2. swiftLint 설치 

    $ pod install 

    3. Project > Target > Build Phase > + New Run Script Phase 하고 아래와 같이 적어준다. 

     

    아래와 같이 적으면 빌드할 때 마다 자동 수정까지 해 준다. 

    $ {PODS_ROOT}/SwiftLint/swiftlint autocorrect 

    4. 프로젝트의 최상단 루트에 .swiftlint.yml 파일을 추가한다. 여기에서 적용하지 않을 rule의 리스트와 특정 룰에 대해서 워닝을 낼지, 에러를 낼지 등을 조정할 수 있다. 

    disabled_rules:
        - identifier_name
        - function_body_length
        - line_length
        - multiple_closures_with_trailing_closure
        - trailing_whitespace
        - shorthand_operator
        - force_try
        - trailing_comma
        - type_name
        - cyclomatic_complexity
        - large_tuple
        - force_cast
        
    type_body_length:
        warning: 300
        error: 700
        
    function_parameter_count:
        warning: 6
        error: 20
        
    excluded: 
      - Pods
    
    vertical_whitespace:
        max_empty_lines: 2
    

     

    5.  프로젝트 루트에서 다음과 같은 명령어로 실행하면 최초 1번 린트를 돌면서 오토코렉션을 해준다.

    $ swiftlint autocorrect

    변경된 파일을 꼼꼼히 확인하고 원하지 않는 변경이 있다면 설정 파일을 수정해 주도록 하자. 

     

     

    SwiftFotmat 적용방법 

    1. Podfile에 SwiftFormat 추가

    pod 'SwiftFormat/CLI'

    2. SwiftFormat 설치 

    $ pod install 

    3. 이제 이 SwiftFormat이 커밋이 될 때마다 적용 되도록 훅을 걸어줘야 하는데 훅을 거는 방법은 다음과 같다. 

    아래 경로에 pre-commit 파일을 추가하고 

    $ vi .git/hooks/pre-commit

    아래와 같은 내용을 넣어준다. 

    git diff --diff-filter=d --staged --name-only | grep -e '\(.*\).swift$' | while read line; do
      swiftformat "${line}";
      git add "$line";
    done

    마지막으로 아래 명령어로  hook을 enable 시킨다. 

    $ chmod +x .git/hooks/pre-commit

     

    4. SwiftLint와 마찬가지로 config파일을 만들어서 어떤 룰을 포함시킬지 선택할 수 있다. 프로젝트의 최상단 패스에 .swiftFormat 을 추가해준다. 아래처럼 적어줬는데, 프로젝트 상황에 맞게 수정하면 된다. 

    --swiftversion 5.1
    
    # file options
    
    --exclude Pods
    
    # format options
    --indent 4
    --maxwidth 100
    --disable blankLinesAtStartOfScope
    --disable emptyBraces
    --stripunusedargs closure-only
    --ifdef no-indent
    

     

    5. 아래 명령어로 최초 한번, 전체 코드에 대해서 swiftFormat을 진행한다. 

    $ swiftformat .

     

    6. 이 이후로는 커밋을 할때 자동으로 내용이 바뀌어서 들어가는 것을 알 수 있다. 테스트 해보려면 엔터를 여러번 쳐서 커밋해 보면, 막상 커밋된 내용을 확인했을때 아무것도 없는 것으로 나온다. 

     

    참고: https://dev.to/theagilemonkeys/setting-up-a-code-formatter-in-a-ios-project-550g

     

    Setting up a code formatter in an iOS project

    People come and go, but projects stay. Having a uniform and idiomatic codebase is nicer, eases the on...

    dev.to

     

    반응형

    댓글