SwiftLint란?
SwiftLint는 Swift 정적 분석기의 일종이다.
자세한 설명은 다음의 링크를 참고한다.
https://github.com/realm/SwiftLint
https://academy.realm.io/kr/posts/slug-jp-simard-swiftlint/
Rules
SwiftLint의 정적 분석기는 Github의 스위프트 코드 스타일에 기반을 두고 있으며, 구현된 룰들은 Customized가 가능하다.
룰에 대한 설명은 다음의 링크를 참고한다.
https://github.com/realm/SwiftLint/blob/master/Rules.md
프로젝트의 Root Path에 .swiftlint.yml 파일을 생성하고 아래 예시와 같이 내용을 기입하면, 린트의 Customized된 규칙을 적용할 수 있다.
disabled_rules: # rule identifiers to exclude from running - colon - comma - control_statement - switch_case_alignment - vertical_whitespace - trailing_whitespace - trailing_newline opt_in_rules: # some rules are only opt-in - empty_count # Find all the available rules by running: # swiftlint rules excluded: # paths to ignore during linting. Takes precedence over `included`. - Carthage - Pods # configurable rules can be customized from this configuration file # binary rules can set their severity level force_cast: warning # implicitly force_try: severity: warning # explicitly # rules that have both warning and error levels, can set just the warning level # implicitly line_length: 180 # they can set both implicitly with an array type_body_length: - 1000 # warning - 1500 # error # or they can set both explicitly file_length: warning: 1000 error: 2000 # naming rules can set warnings/errors for min_length and max_length # additionally they can set excluded names type_name: min_length: 2 # only warning max_length: # warning and error warning: 80 error: 100 excluded: iPhone # excluded via string identifier_name: min_length: # only min_length error: 2 # only error excluded: # excluded via string array - id - URL - GlobalAPIKey - ad reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, junit, html, emoji)
* 위 yml 파일은 예시로 작성된 것임.
Build Phase에서의 사용
iOS 프로젝트에서는 SwiftLint가 로컬에 설치되지 않은 머신에서도 린트 분석 결과를 볼 수 있게 하기 위하여 Cocoapods를 활용하여 프로젝트에 포함시킬 수 있다.
이를 사용하기 위해 먼저 Podfile에 린트를 추가한다.
pod 'SwiftLint'
Build Phases 로 이동하면 린트의 스크립트를 추가할 수 있다.Custom Run Script로 추가된 린트는 빌드를 하면 자동으로 실행되며, 컴파일러의 Warning, Error사인과 같이 빌드 결과에 표출된다.
린트의 규칙에 따라 일부는 Build Fail을 마크할 수 있음에 유의한다.
아래 그림은 린트가 적용된 빌드 Report의 일부이다.
AppCode Plugin
AppCode에서는 SwiftLint PlugIn을 제공한다.
아래와 같은 절차를 통해 플러그인을 사용할 수 있다.
-
local에 SwiftLint 설치
brew install swiftlint
cocoapods를 통해서 린트를 설치하였지만, AppCode플러그인을 통해서 사용하고자 할 때는 로컬에 설치하여 사용하는 것이 여러모로 유리하다.
물론 프로젝트 Root 디렉터리로부터 린트의 실행 path를 지정할 수도 있지만, 다양한 프로젝트를 작업하는 환경에서 불리하다.
-
AppCode 플러그인 설치 및 적용
Preferences→Plugins 로 들어가서 swiftlint를 검색한다. 그러면 “Search in repositories” 항목이 나온다. 이를 클릭한다.
그러면 아래 그림처럼 SwiftLint 플러그인을 설치할 수 있다. 인스톨을 누르고 AppCode를 재시작한다.
AppCode를 재시작하고 나서 Preferences→Inspections 로 진입한다.
서브 항목에 Swift 항목을 펼치면 아래 그림과 같이 SwiftLint Inspections 항목이 있다. 체크해서 활성화 해준다.
아래 그림과 같이 SwiftLint 플러그인이 활성화되어 있는지 확인한다.
모든 작업이 완료되면 아래와 같이 AppCode의 Autocorrect Quick-fix를 활용할 수 있다.
* AutoCorrect는 린트의 규칙 중에 지원되는 항목에서만 사용할 수 있다.
njkim0529 네이버 메일확인 부탁드립니다.
관심은 감사합니다만, 생각이 없습니다 ^^