250x250
반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자 수니

[iOS/Swift] SwiftGen 적용하기 (pod 'SwiftGen') 본문

iOS - Swift

[iOS/Swift] SwiftGen 적용하기 (pod 'SwiftGen')

개발자 수니 2022. 2. 4. 18:20
728x90
반응형

📌  이번 글은

Xcode 프로젝트에 SwiftGen 적용 방법입니다.

 

이번에 토이 프로젝트에 SwiftGen을 적용하면서,

굉장히 유용한 라이브러리 같아 정리해봅니다.

 

 

SwiftGen
- 프로젝트 리소스(예: images, localised strings 등)에 대한 Swift Code를 자동으로 생성하여 사용하기에 안전한 유형으로 만드는 도구입니다.

 

 1. 프로젝트에 SwiftGen 설치

 

GitHub - SwiftGen/SwiftGen: The Swift code generator for your assets, storyboards, Localizable.strings, … — Get rid of all S

The Swift code generator for your assets, storyboards, Localizable.strings, … — Get rid of all String-based APIs! - GitHub - SwiftGen/SwiftGen: The Swift code generator for your assets, storyboards...

github.com

CocoaPods을 통한 설치방법

podfile에 추가 > 터미널에서 pod update

pod 'SwiftGen', '~> 6.0'

Cocoa Pods 참고 포스팅

 

[iOS/Xcode] CocoaPods(코코아팟) 사용하기

CocoaPods는 Swift 및 Objective-C 코코아 프로젝트의 종속성 관리자입니다. 28,000개가 넘는 라이브러리를 가지고 있으며 170만 개가 넘는 응용 프로그램(앱)에서 사용되고 있습니다. CocoaPod은 프로젝트를

sunidev.tistory.com

 

2. 터미널에서 Configuration File 생성

터미널에서 프로젝트 폴더로 이동

$ cd {Xcode 프로젝트 위치}

명령어 입력 

$ Pods/SwiftGen/bin/swiftgen config init

 

프로젝트 폴더에 swiftgen.yml 파일이 자동 생성되었습니다.

 

3. swiftgen.yml 설정

기본 생성된 swiftgen.yml 파일 (한글 번역하였습니다.)

## 참고: 아래의 모든 구성 항목은 예제일 뿐입니다. 불편할 때는 반드시 수정하고 필요에 맞게 조정하세요.

## 구성 항목이 모두 공통 입/출력 상위 디렉토리를 사용하는 경우 여기서 해당 디렉토리를 지정할 수 있습니다.
## 그러면 나머지 구성의 모든 입/출력 경로가 이를 기준으로 표현됩니다.
## 이 두 개의 최상위 키는 선택 사항이며 기본값은 "."(구성 파일의 디렉토리)입니다.
# input_dir: MyLib/Sources/
# output_dir: MyLib/Generated/


## 지역화된 문자열에 대한 상수를 생성합니다.
## SwiftGen은 하나의 locale(일반적으로 Base.lproj, en.lproj 또는 개발 지역)만 구문 분석해야 합니다. 그렇지 않으면 동일한 키가 여러 번 생성됩니다.
## SwiftGen은 해당 폴더에서 발견된 모든 '.strings' 파일을 구문 분석합니다.
# strings:
#   inputs:
#     - Resources/Base.lproj
#   outputs:
#     - templateName: structured-swift5
#       output: Strings+Generated.swift


## images, colors, ARKit resources 등에 대한 constants를 포함하여 Assets Catalogs에 대한 constants를 생성합니다.
## 또한 이 예에서는 출력을 사용자 정의하기 위해 템플릿에 추가 매개 변수를 제공하는 방법을 보여 줍니다.
## - 특히 'forceProvidesNamespaces: true' 매개변수는 "Provides Namespaces"가 없는 폴더/그룹을 포함하여 Assets Catalogs에서 사용되는 각 폴더/그룹에 대해 하위 네임스페이스를 생성하도록 강제합니다.
## 이 매개 변수가 없으면 SwiftGen은 Inspector 창에서 "Provides Namespace" 상자가 선택된 폴더/그룹에 대한 하위 네임스페이스만 생성합니다.
## - Template에 지원되는 param을 알기 위해서는 'swiftgen template doc xcassets swift5'를 사용하여 GitHub에서 template 문서를 엽니다.
# xcassets:
#   inputs:
#     - Main.xcassets
#     - ProFeatures.xcassets
#   outputs:
#     - templateName: swift5
#       params:
#         forceProvidesNamespaces: true
#       output: XCAssets+Generated.swift


## storyboards 및 XIB에 대한 상수를 생성합니다.
## 하나는 storyboard scenes을 담고 있는 출력 파일이고 다른 하나는 segues를 위한 출력 파일이다.
## (IB 파일에서 segues를 사용하지 않는 경우 segues 항목을 제거할 수 있습니다.)
## 'inputs'의 경우 여기서 "."(aka "current directory")를 사용할 수 있습니다.
## 그리고 SwiftGen은 모든 *.storyboard와 *.xib 파일을 반복적으로 찾을 것이다.
# ib:
#   inputs:
#     - .
#   outputs:
#     - templateName: scenes-swift5
#       output: IB-Scenes+Generated.swift
#     - templateName: segues-swift5
#       output: IB-Segues+Generated.swift


## 필요에 따라 사용할 수 있는 다른 parsers는 다음과 같습니다.
##  - `fonts` (사용자 지정 ttf/ttc 글꼴 파일이 있는 경우)
##  - `coredata` (CoreData 모델용)
##  - `json`, `yaml` and `plist` (사용자 지정 JSON/YAML/Plist 파일을 parse하고 해당 콘텐츠에서 코드를 생성하려면)
## …
##
## 자세한 내용은 'swiftgen config doc'을 사용하여 GitHub에 대한 전체 문서를 여십시오.
## https://github.com/SwiftGen/SwiftGen/tree/6.5.1/Documentation/

프로젝트에 맞게 swiftgen.yml 파일 설정

input_dir: ${PROJECT_DIR}/${TARGET_NAME}/Resources
output_dir: ${PROJECT_DIR}/${TARGET_NAME}/Resources

xcassets:
  inputs:
    - Icon.xcassets
    - Assets.xcassets
  outputs:
    - templateName: swift5
      output: Assets.swift

 

4. Xcode에 SwiftGen 적용

Targets > Build Phases > + > New Run Script Phase

이름을 SwiftGen Script로 변경

Run Script 입력

if [[ -f "${PODS_ROOT}/SwiftGen/bin/swiftgen" ]]; then
  "${PODS_ROOT}/SwiftGen/bin/swiftgen"
else
  echo "warning: SwiftGen is not installed. Run 'pod install --repo-update' to install it."
fi

 

5. 빌드

지정한 위치에 Asset.swift 파일이 생성되었습니다.

이제 SwiftGen 적용이 완료되었습니다.

 

👩🏻‍💻 사용 예제

let image: UIImage = Asset.Assets.logo.image

 

 

 

🙋🏻‍♀️  참고

[iOS]SwiftGen 사용법

728x90
반응형
Comments