Section 4/131 menit

4. XcodeGen — Konfigurasi dan Penggunaan

4. XcodeGen — Konfigurasi dan Penggunaan

Instalasi

swift
# Via Homebrew (paling mudah)
brew install xcodegen

# Via Mint (version-locked per project)
mint install yonaskolb/XcodeGen
# Kemudian jalankan via:
mint run xcodegen generate

project.yml Minimal

swift
# project.yml — file konfigurasi utama XcodeGen

name: MyApp
options:
  bundleIdPrefix: com.company
  deploymentTarget:
    iOS: "17.0"
  xcodeVersion: "16.0"
  defaultConfig: Debug

configs:
  Debug: debug
  Staging: release
  Release: release

settings:
  base:
    SWIFT_VERSION: 6.0
    ENABLE_STRICT_OBJC_MSGSEND: YES
  configs:
    Debug:
      OTHER_SWIFT_FLAGS: "$(inherited) -DDEBUG"
    Staging:
      OTHER_SWIFT_FLAGS: "$(inherited) -DSTAGING"

targets:
  MyApp:
    type: application
    platform: iOS
    sources:
      - Sources/App
    resources:
      - Sources/Resources
    info:
      path: Sources/App/Info.plist
      properties:
        CFBundleShortVersionString: "1.0.0"
        CFBundleVersion: "$(BUILD_NUMBER)"
    settings:
      base:
        PRODUCT_BUNDLE_IDENTIFIER: com.company.myapp
        INFOPLIST_FILE: Sources/App/Info.plist
    dependencies:
      - target: NetworkKit
      - target: FeatureHome
      - package: Alamofire
    scheme:
      testTargets:
        - MyAppTests
        - MyAppUITests

  NetworkKit:
    type: framework
    platform: iOS
    sources: Modules/NetworkKit/Sources
    settings:
      base:
        PRODUCT_BUNDLE_IDENTIFIER: com.company.networkkit

  FeatureHome:
    type: framework
    platform: iOS
    sources: Modules/FeatureHome/Sources
    dependencies:
      - target: NetworkKit

  MyAppTests:
    type: bundle.unit-test
    platform: iOS
    sources: Tests/MyAppTests
    dependencies:
      - target: MyApp

  MyAppUITests:
    type: bundle.ui-testing
    platform: iOS
    sources: Tests/MyAppUITests
    dependencies:
      - target: MyApp

packages:
  Alamofire:
    url: https://github.com/Alamofire/Alamofire
    from: "5.9.0"
  SnapKit:
    url: https://github.com/SnapKit/SnapKit
    from: "5.7.0"

Penggunaan Sehari-hari

swift
# Generate project (jalankan setiap kali project.yml berubah)
xcodegen generate

# Generate dengan path eksplisit
xcodegen generate --spec ./project.yml --project ./

# Gunakan sebagai git hook (otomatis regenerate setelah checkout)
# .git/hooks/post-checkout:
#!/bin/bash
xcodegen generate --quiet

Template untuk Target yang Berulang

swift
# project.yml dengan template target

templates:
  FeatureModule:
    type: framework
    platform: iOS
    settings:
      base:
        SWIFT_VERSION: 6.0
        IPHONEOS_DEPLOYMENT_TARGET: "17.0"
    scheme:
      testTargets:
        - "${target_name}Tests"

targets:
  FeatureHome:
    template: FeatureModule
    sources: Modules/FeatureHome/Sources
    dependencies:
      - target: NetworkKit

  FeatureProfile:
    template: FeatureModule
    sources: Modules/FeatureProfile/Sources
    dependencies:
      - target: NetworkKit
      - target: SharedUI

  FeatureHomeTests:
    type: bundle.unit-test
    platform: iOS
    sources: Modules/FeatureHome/Tests
    dependencies:
      - target: FeatureHome

  FeatureProfileTests:
    type: bundle.unit-test
    platform: iOS
    sources: Modules/FeatureProfile/Tests
    dependencies:
      - target: FeatureProfile

.gitignore untuk XcodeGen

swift
# .gitignore — tidak commit .xcodeproj yang di-generate
*.xcodeproj
!*.xcodeproj/xcshareddata/  # tapi commit shared data (schemes, dsb)
*.xcworkspace               # jika di-generate, bukan CocoaPods workspace