Published on

๐ŸŽ Swift - Package

Authors
  • avatar
    Name
    ์ด์ฐฝ์ค€
    Twitter

Package (ํŒจํ‚ค์ง€)

ํŒจํ‚ค์ง€๋ž€ ํ”„๋กœ์ ํŠธ ๋‚ด์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ/๋ฆฌ์†Œ์Šค๋“ค์„ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์‰ฝ๋„๋ก ๋ฌถ์–ด ์ •๋ˆํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๋‚˜ ์ปค๋ฎค๋‹ˆํ‹ฐ์™€ ๊ณต์œ ํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

Xcode๋Š” ์ด๋Ÿฐ ํŒจํ‚ค์ง€ ๊ธฐ๋Šฅ์„ Swift Package Manager๋ฅผ ํ†ตํ•ด ์‰ฝ๊ณ  ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋„ค ๋งž์•„์š”..

์ €ํฌ๊ฐ€ ๋งจ๋‚  ์“ฐ๋˜ ๊ทธ๊ฑฐ์š”..

๊ทธ๋Ÿฌ๋ฉด ๋ฉ‹์žˆ๊ณ  ํ™”๋ คํ•œ RxSwift์™€ ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์‹œ์ž‘ํ•ด๋ณด์ฃ ?

Swift Package ๋งŒ๋“ค๊ธฐ

์ „๋ฐ˜์ ์ธ ํŒจํ‚ค์ง€ ์ƒ์„ฑ ๊ณผ์ •

ํŒจํ‚ค์ง€ ๋งŒ๋“ค๊ธฐ ์ •๋ง ์‰ฝ์Šต๋‹ˆ๋‹ค.

Xcode์˜ ๋ฉ”๋‰ด์—์„œ File โžก๏ธ New โžก๏ธ Package ๋ฅผ ๋ˆŒ๋Ÿฌ์ค์‹œ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์ด๋ ‡๊ฒŒ ํ”„๋กœ์ ํŠธ๋‚˜ ํŒŒ์ผ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด ํŒจํ‚ค์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฐฝ์ด ๋ณด์ž…๋‹ˆ๋‹ค.

์ €๋Š” Favor ํ”„๋กœ์ ํŠธ์— ํŒจํ‚ค์ง€๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด Add to์™€ Group๋„ ์„ค์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ๋น„์–ด์žˆ๋Š” ํŒจํ‚ค์ง€ ๋งŒ๋“ค๊ธฐ ๋์ž…๋‹ˆ๋‹ค.

ํŒจํ‚ค์ง€๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ์ด๋Ÿฐ ๊ตฌ์กฐ์˜ ํŒŒ์ผ๋“ค์ด ํ”„๋กœ์ ํŠธ์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ๊ฐ๊ฐ์„ ํ•œ ๋ฒˆ ์•Œ์•„๋ณผ๊นŒ์š”?

  • README.md

๋ญ ์„ค๋ช…ํ•  ๊ฒƒ๋„ ์—†์ฃ . ํŒจํ‚ค์ง€์— ๋Œ€ํ•œ ์„ค๋ช…์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

  • Package.swift

ํ”ํžˆ๋“ค manifest๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ํŒจํ‚ค์ง€์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ํŒŒ์ผ์ž…๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ ์•ˆ์—์„œ ํŒจํ‚ค์ง€์˜ name, products, targets, dependencies๋“ค์„ ์„ค์ •ํ•ด์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

  • Sources

์†Œ์Šค ํŒŒ์ผ๋“ค์€ ์ด Sources ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด๋ถ€์— ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Target์ด๋ผ๋Š” ํ•˜๋‚˜์˜ ์ด๋ฆ„ ์•ˆ์— ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

  • Tests

ํŒจํ‚ค์ง€์— ๋Œ€ํ•œ Unit Test ์ฝ”๋“œ๋“ค์ด ์—ฌ๊ธฐ์— ํ•ด๋‹น๋ฉ๋‹ˆ๋‹ค.

Package.swift ์ž‘์„ฑ

Favor ํ”„๋กœ์ ํŠธ๋Š” ์„ธ๊ฐ€์ง€ ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

  • FavorCoreKit

Foundation์˜ extension๋“ค, OS์™€ ๋ฐ€์ ‘ํ•˜๊ฒŒ ๋‹ฟ์•„์žˆ๋Š” ๊ธฐ๋Šฅ๋“ค (PhotosUI, CoreGraphics ๋“ฑ)์„ ๋‹ด์•„๋‘ก๋‹ˆ๋‹ค. ๋ณดํ†ต BaseViewController์™€ ๊ฐ™์€ Base ํŒŒ์ผ๋“ค๋„ ์—ฌ๊ธฐ์— ๋‹ด๊ธด๋‹ค๊ณ  ํ•˜๋Š”๋ฐ ์•„์ง ์ €ํฌ ํ”„๋กœ์ ํŠธ๋Š” ์ด๋Ÿฐ ํŒŒ์ผ๋“ค์ด ๋งŽ์ง€ ์•Š์•„์„œ (BaseVC ํ•˜๋‚˜) ์ถ”๊ฐ€์ ์ธ ํšŒ์˜๋ฅผ ํ†ตํ•ด ๊ฒฐ์ •ํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

  • FavorUIKit

UIKit์˜ extension๋“ค๊ณผ ์ปค์Šคํ…€ UI ์ปดํฌ๋„ŒํŠธ๋“ค, ํฐํŠธ์™€ ์•„์ด์ฝ˜ ๊ฐ™์€ Resources๋“ค๊ณผ UI์— ์‚ฌ์šฉ๋˜๋Š” ์ƒ์ˆ˜ ํŒŒ์ผ๋“ค์ด ํฌํ•ฉ๋ฉ๋‹ˆ๋‹ค.

  • FavorNetworkKit

HTTP ํ†ต์‹ ๊ณผ ๊ฐ™์€ ๋„คํŠธ์›Œํฌ ์ž‘์—…์— ํ•„์š”ํ•œ ํŒŒ์ผ๋“ค์ด ๋ชจ๋‘ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์ €ํฌ ํ”„๋กœ์ ํŠธ๋Š” Moya๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— BaseTargetType๊ณผ API ํŒŒ์ผ๋“ค์„ ์—ฌ๊ธฐ์— ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

์ด ์ค‘์—์„œ FavorUIKit์„ ํ•œ ๋ฒˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
  name: "FavorUIKit",
  platforms: [
    .iOS(.v15)
  ],
  products: [
    // Products define the executables and libraries a package produces, and make them visible to other packages.
    .library(
      name: "FavorUIKit",
      targets: ["FavorUIKit"]),
  ],
  dependencies: [
    // Dependencies declare other packages that this package depends on.
    // .package(url: /* package url */, from: "1.0.0"),
    .package(url: "https://github.com/ReactiveX/RxSwift.git", .upToNextMajor(from: "6.5.0")),
    .package(url: "https://github.com/SnapKit/SnapKit.git", .upToNextMajor(from: "5.6.0")),
    .package(path: "../FavorCoreKit")
  ],
  targets: [
    // Targets are the basic building blocks of a package. A target can define a module or a test suite.
    // Targets can depend on other targets in this package, and on products in packages this package depends on.
    .target(
      name: "FavorUIKit",
      dependencies: [
        "RxSwift",
        .product(name: "RxCocoa", package: "RxSwift"),
        "SnapKit",
        "FavorCoreKit"
      ],
      resources: [
        .process("Resources")
      ]
    ),
    .testTarget(
      name: "FavorUIKitTests",
      dependencies: ["FavorUIKit"]),
  ]
)
  • name

name์€ ๋ง ๊ทธ๋Œ€๋กœ ํŒจํ‚ค์ง€์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค.

  • platforms

platforms๋Š” ํŒจํ‚ค์ง€๊ฐ€ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ํ”Œ๋žซํผ์„ ๋ช…์‹œํ•ด๋‘ก๋‹ˆ๋‹ค. .iOS, .macOS์™€ ๊ฐ™์€ ํ”Œ๋žซํผ case์™€ .v15์™€ ๊ฐ™์€ ๋ฒ„์ „ case๋ฅผ ๋„ฃ์–ด ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • products

products๋Š” ํŒจํ‚ค์ง€๊ฐ€ ์ œ๊ณตํ•˜๋Š” ํ”„๋กœ๋•์ธ ๋“ค์„ ๋ช…์‹œํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. .library ์™ธ์—๋„ .executable, .plugin๊ณผ ๊ฐ™์€ case๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜๋‚˜์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ targets๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ RxSwift์˜ ๊ฒฝ์šฐ RxSwift ํ•˜๋‚˜์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— RxRelay, RxCocoa ๋“ฑ์˜ ์—ฌ๋Ÿฌ ํƒ€๊ฒŸ๋“ค์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์ฃ .

  • dependencies

ํŒจํ‚ค์ง€๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋‹ค๋ฅธ ํŒจํ‚ค์ง€๋“ค์„ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์— ์ ํž ๋‚ด์šฉ์€ ๋ณดํ†ต ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฆฌ๋“œ๋ฏธ ํŒŒ์ผ์— ์ ์–ด๋‘๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์—ฌ ์ ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๊นƒํ—ˆ๋ธŒ์— ์˜ฌ๋ผ์™€์žˆ๋Š” ํŒจํ‚ค์ง€์˜ ๊ฒฝ์šฐ .package(url:)์„ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋˜๊ณ , ๋กœ์ปฌ ํŒจํ‚ค์ง€์˜ ๊ฒฝ์šฐ .package(path:)๋ฅผ ์‚ฌ์šฉํ•ด์ค๋‹ˆ๋‹ค.

๋’ค์— ๋ถ™๋Š” ๋ฒ„์ „์˜ ๊ฒฝ์šฐ

  • from

    ํ•ด๋‹น ๋ฒ„์ „ ์ด์ƒ๋ถ€ํ„ฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

  • uptoNextMajor

    ๋ฉ”์ด์ € ๋ฒ„์ „์ด ๋ฐ”๋€Œ๊ธฐ ์ „๊นŒ์ง€ ์—…๋ฐ์ดํŠธํ•˜๋ฉฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    ex) 5.0.0 ~ 6.0.0

  • uptoNextMinor ๋งˆ์ด๋„ˆ ๋ฒ„์ „์ด ๋ฐ”๋€Œ๊ธฐ ์ „๊นŒ์ง€ ์—…๋ฐ์ดํŠธํ•˜๋ฉฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ex) 5.0.0 ~ 5.1.0

  • exact ํ•ด๋‹น ๋ฒ„์ „์œผ๋กœ ๊ณ ์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

  • targets

ํŒจํ‚ค์ง€์— ์‚ฌ์šฉ๋œ ํƒ€๊ฒŸ๋“ค์„ ๊ฐ๊ฐ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค.

dependencies์—๋Š” ๋ณดํ†ต ์œ„์˜ dependencies์— ์ ํžŒ ํŒจํ‚ค์ง€๋“ค์ด ์ ํžˆ๊ณ , ํ•˜๋‚˜์˜ ํŒจํ‚ค์ง€์— ํƒ€๊ฒŸ์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ .product(name: "RxCocoa", package: "RxSwift")์™€ ๊ฐ™์ด ๊ฐ๊ฐ ์ ์–ด์ค๋‹ˆ๋‹ค.

resources์—๋Š” ํƒ€๊ฒŸ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฆฌ์†Œ์Šค ํŒŒ์ผ๋“ค์„ ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค.

๋‹จ .xcassets, .storyboard, .xib, .nib ํŒŒ์ผ๋“ฑ๊ณผ ๊ฐ™์€ ๋ฆฌ์†Œ์Šค ํŒŒ์ผ๋“ค์€ Xcode๊ฐ€ ๊ทธ ์‚ฌ์šฉ์ฒ˜๊ฐ€ ๋ถ„๋ช…ํ•˜๋‹ค๊ณ  ํŒ๋‹จํ•˜์—ฌ ์ž๋™์œผ๋กœ ํŒจํ‚ค์ง•์ด ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ €ํฌ ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ ํฐํŠธ์™€ ๊ฐ™์€ ์ž๋™์œผ๋กœ ํŒจํ‚ค์ง•๋˜์ง€ ์•Š๋Š” ํƒ€์ž…์˜ ๋ฆฌ์†Œ์Šค๊ฐ€ ์žˆ๊ธฐ ๋–„๋ฌธ์— .process("Resources")์™€ ๊ฐ™์ด ๋ช…์‹œํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๋ฆฌ์†Œ์Šค ๋ช…์‹œ๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

  • process

ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” ๋ชจ๋“  ๋ฆฌ์†Œ์Šค๋ฅผ ํ•˜๋‚˜์˜ depth๋กœ ํ‰์ค€ํ™”ํ•ฉ๋‹ˆ๋‹ค.

ํ”Œ๋žซํผ์— ๋”ฐ๋ผ Xcode๊ฐ€ ์ตœ์ ํ™”๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ตœ์ ํ™”๋ฅผ ์ง„ํ–‰ํ•ด์„œ ๋ฆฌ์†Œ์Šค ๋ฒˆ๋“ค์˜ ์ตœ์ƒ์œ„ ๋””๋ ‰ํ† ๋ฆฌ์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.

  • copy

๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌํ•˜์—ฌ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ๊ตณ์ด ์œ ์ง€ํ•ด์•ผํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด ์ตœ์ ํ™”๋„ ์ง„ํ–‰๋˜๋Š” process๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ ‘๊ทผ ์ œ์–ด์ž

ํŒจํ‚ค์ง€ํ™”๋ฅผ ํ•œ ์ด์ƒ ์ ‘๊ทผ์ œ์–ด์ž์— ๋”์šฑ ์‹ ๊ฒฝ์„ ์จ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Swift์—๋Š” ๋‹ค์„ฏ๊ฐ€์ง€ ์ ‘๊ทผ ์ œ์–ด์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • open, public

ํ”„๋กœ์ ํŠธ ๋‚ด์˜ ๋ชจ๋“  ๊ณณ์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

open๊ณผ public์˜ ์ฐจ์ด๊ฐ€ ํŒจํ‚ค์ง•์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ค‘์š”ํ•ด์ง‘๋‹ˆ๋‹ค.

open์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ overclass๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ public์€ ์ด๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ BaseViewController์™€ ๊ฐ™์ด ์ƒ์†์ด ํ•„์š”ํ•œ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ open์œผ๋กœ ์ ‘๊ทผ์„ ์—ด์–ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • internal

์•„๋ฌด ์ ‘๊ทผ์ œ์–ด์ž๋„ ์ž‘์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ ์šฉ๋˜๋Š” ์ ‘๊ทผ์ œ์–ด์ž์ž…๋‹ˆ๋‹ค. ์ž‘์„ฑ๋œ ๋ชจ๋“ˆ ์•ˆ์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

  • fileprivate

์ž‘์„ฑ๋œ ์†Œ์Šค ํŒŒ์ผ ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

  • private

๊ฐ€์žฅ ์ œํ•œ์ ์ธ ์ ‘๊ทผ์ œ์–ด์ž์ž…๋‹ˆ๋‹ค. ์ž‘์„ฑ๋œ ๊ฐ์ฒด ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

ํŒจํ‚ค์ง€ ์ ์šฉํ•˜๊ธฐ

ํ”„๋กœ์ ํŠธ์˜ ์•ฑ ํƒ€๊ฒŸ์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€๋ฅผ ๋ˆŒ๋Ÿฌ์ค๋‹ˆ๋‹ค.

Workspace ๋‚ด๋ถ€์— ๋งŒ๋“ค์–ด๋‘์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ํŒจํ‚ค์ง€๋ฅผ ๋ฐ”๋กœ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

import๊นŒ์ง€ ํ•ด๋ณด๋ฉด์„œ ์ •์ƒ์ ์œผ๋กœ ์ถ”๊ฐ€๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค!

References

Apple - Creating a standalone Swift package with Xcode

Apple - Bundling resources with a Swift package

KakaoTech - Swift Package Manager ์ ์šฉ๊ธฐ

ZeddiOS - Copy์™€ Process์˜ ์ฐจ์ด์ 