🔥 TargetDependencyCondition

231자
3분

TargetDependencyCondition 클래스는 타겟이 의존하는 대상을 어떤 상황에서 사용할지 정해주는 조건이에요. 쉽게 말해서 특정 플랫폼에서만 의존성을 가지도록 만들 수 있는 거죠.

메서드

TargetDependencyCondition 클래스에는 아래와 같은 메서드가 있어요.

when(platforms:)

이 메서드는 TargetDependencyCondition 인스턴스를 만들어 주는 역할을 해요. 메서드를 호출할 때는 이렇게 해요.

swift
static func when(platforms: [Platform]? = nil) -> TargetDependencyCondition
swift
static func when(platforms: [Platform]? = nil) -> TargetDependencyCondition

platforms 매개변수에는 이 조건이 적용되는 플랫폼 목록을 넣어줘요. 만약 아무것도 넣지 않으면 (nil) 모든 플랫폼에 적용돼요.

간단한 예시를 들어볼게요.

swift
let condition = TargetDependencyCondition.when(platforms: [.iOS, .macOS])
// iOS와 macOS 플랫폼에서만 사용되는 의존성 조건을 만들었어요.
swift
let condition = TargetDependencyCondition.when(platforms: [.iOS, .macOS])
// iOS와 macOS 플랫폼에서만 사용되는 의존성 조건을 만들었어요.

이렇게 만든 TargetDependencyCondition 인스턴스를 의존성을 설정할 때 사용하면, 해당 조건에 맞는 경우에만 의존성이 적용된답니다.

플랫폼마다 의존하는 대상을 다르게 하고 싶을 때 유용하게 쓸 수 있어요. 예를 들어 iOS 앱과 macOS 앱을 동시에 개발하는데, 일부 의존성은 특정 플랫폼에서만 필요한 경우가 있겠죠? 이럴 때 TargetDependencyCondition을 사용하면 쉽게 설정할 수 있어요.

swift
let package = Package(
    name: "MyPackage",
    products: [
        .library(name: "MyLibrary", targets: ["MyLibrary"])
    ],
    dependencies: [
        .package(url: "<https://github.com/apple/example-package-deckofplayingcards.git>", from: "3.0.0"),
        .package(url: "<https://github.com/apple/example-package-fisheryates.git>", from: "2.0.0")
    ],
    targets: [
        .target(
            name: "MyLibrary",
            dependencies: [
                .product(name: "DeckOfPlayingCards",
                         package: "example-package-deckofplayingcards",
                         condition: .when(platforms: [.iOS])),
                .product(name: "FisherYates",
                         package: "example-package-fisheryates",
                         condition: .when(platforms: [.macOS]))
            ]
        )
    ]
)
swift
let package = Package(
    name: "MyPackage",
    products: [
        .library(name: "MyLibrary", targets: ["MyLibrary"])
    ],
    dependencies: [
        .package(url: "<https://github.com/apple/example-package-deckofplayingcards.git>", from: "3.0.0"),
        .package(url: "<https://github.com/apple/example-package-fisheryates.git>", from: "2.0.0")
    ],
    targets: [
        .target(
            name: "MyLibrary",
            dependencies: [
                .product(name: "DeckOfPlayingCards",
                         package: "example-package-deckofplayingcards",
                         condition: .when(platforms: [.iOS])),
                .product(name: "FisherYates",
                         package: "example-package-fisheryates",
                         condition: .when(platforms: [.macOS]))
            ]
        )
    ]
)

위 예시처럼 Package.swift 파일에서 의존성을 설정할 때 condition 파라미터에 TargetDependencyCondition을 넣어주면 돼요. iOS에서는 DeckOfPlayingCards를, macOS에서는 FisherYates를 사용하도록 했네요.

이렇게 TargetDependencyCondition을 적절히 사용하면 플랫폼에 따라 유연하게 의존성을 설정할 수 있어요. 패키지를 만들 때 쓸데없는 의존성을 줄이고 더 잘 구조화하는 데 도움이 될 거예요!

YouTube 영상

채널 보기
NestJS 커스텀 데코레이터 인자 전달 및 파이프 검증 활용법 | NestJS 가이드
미들웨어 vs 가드, 왜 NestJS에서는 가드가 더 똑똑할까? | NestJS 가이드
변환 파이프로 컨트롤러 코드 깔끔하게 만들기 | NestJS 가이드
함수 객체의 보편적 구성 | 프로그래머를 위한 카테고리 이론
NestJS 커스텀 데코레이터, createParamDecorator 사용 | NestJS 가이드
존 매카시가 들려주는 인공지능의 탄생 이야기
Writer 펑터와 클라이슬리 카테고리 | 프로그래머를 위한 카테고리 이론
매번 ValidationPipe 복붙하세요? NestJS 전역 파이프로 한 번에 해결하기 | NestJS 가이드