Swift Package Manager๋กœ ํŒจํ‚ค์ง€ ๋งŒ๋“ค๊ณ  ์ ์šฉํ•˜๊ธฐ

2021. 11. 11. 22:23ใ†iOS

1. Swift Package Manager๋Š” ๋ญ˜๊นŒ? ์™œ ํ•„์š”ํ•œ๊ฑธ๊นŒ?

Swift Package Manager๋Š” ์ข…์†์„ฑ์„ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ด๋•Œ '๊ด€๋ฆฌ'๋ผ ํ•จ์€ Swift ๋นŒ๋“œ ์‹œ์Šคํ…œ๊ณผ ํ†ตํ•ฉ๋˜์–ด ์ข…์†์„ฑ์„ ๋‹ค์šด๋กœ๋“œ, ์ปดํŒŒ์ผ ๋ฐ ์—ฐ๊ฒฐํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ์ž๋™ํ™”ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

2. ํŒจํ‚ค์ง€ ๋งŒ๋“ค๊ธฐ

+์—ฌ๋‹ด) ์ฒ˜์Œ์— ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ๋“ค์„ ๋ณด๊ณ  ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค๊ฐ€ ํ™ฉ๋‹นํ•œ ์—๋Ÿฌ๋ฅผ ์ ‘ํ–ˆ์—ˆ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์€ ๊ฐ™์€ ์‹œ๊ฐ„ ๋‚ญ๋น„๋ฅผ ํ•˜์ง€ ์•Š๊ธฐ๋ฅผ ๋ฐ”๋ผ๋ฉฐ..

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

 

1) ํ„ฐ๋ฏธ๋„๋กœ ๋งŒ๋“ค๊ธฐ

ํ„ฐ๋ฏธ๋„์—์„œ ๋ช…๋ น์–ด๋กœ ๋ฐ”๋กœ ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ณ  ๊นƒ์— ์—…๋กœ๋“œํ•˜๋Š” ๊ฑด ์• ํ”Œ ๊ณต์‹ ๋ ˆํฌ๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.

https://github.com/apple/swift-package-manager/blob/main/Documentation/Usage.md#testing

 

GitHub - apple/swift-package-manager: The Package Manager for the Swift Programming Language

The Package Manager for the Swift Programming Language - GitHub - apple/swift-package-manager: The Package Manager for the Swift Programming Language

github.com

ํ•ด๋‹น ๊ณต์‹๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด library๋ฒ„์ „ package๊ฐ€ ์žˆ๊ณ  command line์—์„œ๋„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” executable๋ฒ„์ „ package ์„ค์ • ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ๋‚˜๋Š” executable๋ฒ„์ „์œผ๋กœ ํ…Œ์ŠคํŠธํ•ด๋ณด์•˜๋‹ค. (github repo์—์„œ ํ™•์ธ ๊ฐ€๋Šฅ: https://github.com/n-y-kim/MyExecutable)

 

GitHub - n-y-kim/MyExecutable

Contribute to n-y-kim/MyExecutable development by creating an account on GitHub.

github.com

 

<library๋ฒ„์ „์œผ๋กœ ๋งŒ๋“ค๊ธฐ>

mkdir MyPackage
cd MyPackage
swift package init # or swift package init --type library
swift build
swift test

MyPackage๋ผ๋Š” ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ ,

ํ•ด๋‹น ํด๋”๋กœ ์ด๋™ํ•ด์„œ 'swift package init'์„ ํ•ด์ฃผ๊ณ , build์™€ test๊นŒ์ง€ ํ•ด์ค€๋‹ค๋ฉด(๋‚˜๋Š” executable๋ฒ„์ „์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์— swift run์ด ๋จนํžˆ๊ณ  ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค!)

ํ„ฐ๋ฏธ๋„

์ด์ œ ํ•„์š”ํ•œ ํŒŒ์ผ๋“ค์„ ๋‹ค ๋งŒ๋“ค์—ˆ์œผ๋‹ˆ ๋ ˆํฌ์— ์˜ฌ๋ ค์ค˜์•ผ ํ•œ๋‹ค.

 

๋นˆ ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ , ๋‹ค์‹œ ํ„ฐ๋ฏธ๋„๋กœ ๋Œ์•„์˜จ๋‹ค.

git init
git add .
git remote add origin [github-URL] #์ƒˆ๋กœ ์ƒ์„ฑํ•œ ๋ ˆํฌ์˜ .git url์„ ๋„ฃ์–ด์ค€๋‹ค
git commit -m "Initial Commit"
git tag 1.0.0
git push origin main --tags

์ด๋•Œ ์ฃผ์˜ํ•˜๋ฉด ์ข‹์€๊ฑด ์• ํ”Œ์˜ ๊ณต์‹ ๋ ˆํฌ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ธŒ๋žœ์น˜๊ฐ€ ์šฐ๋ฆฌ๊ฐ€ ํ†ต์ƒ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” 'main'์ด ์•„๋‹ˆ๋ผ 'master'๋ผ์„œ git init์„ ํ•ด์ฃผ๋ฉด ์ด๋Ÿฐ ๋ฌธ๊ตฌ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ๋งˆ์ง€๋ง‰ ์ค„์„ ๋ณด๋ฉด git branch -m <name>์œผ๋กœ ์ดˆ๊ธฐ์— ์‚ฌ์šฉํ•˜๋Š” ๋ธŒ๋žœ์น˜๋ฅผ ์กฐ์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค๊ณ  ํ•˜๋‹ˆ ์นจ์ฐฉํ•˜๊ฒŒ git branch -m main์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.(๊ฒฐ๊ณผ ํ™•์ธ์„ ์œ„ํ•ด git status๋กœ ๋‚˜์˜ ๋ธŒ๋žœ์น˜๊ฐ€ ํ˜„์žฌ ๋ฌด์—‡์ธ์ง€ ๋‹ค์‹œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค)

1.0.0 ๋ฒ„์ „์ด๋ผ๋Š” ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ๊นŒ์ง€ ์™„๋ฃŒํ•˜๋ฉด ๋ ˆํฌ๋Š” ์ด๋ ‡๊ฒŒ ์ƒ๊ฒจ์•ผ ํ•œ๋‹ค.(๊ผญ ์ด ํ˜•์‹์ด์–ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ ๋‹ค๋ฅด๋‹ค? ์ž˜๋ชป ๋งŒ๋“ ๊ฒƒ.. ํŒŒ์ผ ํ˜•์‹์„ ๋งž์ถฐ ์ฃผ๊ฑฐ๋‚˜ ์ƒˆ๋กœ ๋งŒ๋“ค๊ฑฐ๋‚˜..ํ•ฉ์‹œ๋‹ค)

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํŒจํ‚ค์ง€๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

2) Xcode๋กœ ๋งŒ๋“ค๊ธฐ

ํŒจํ‚ค์ง€ ์ƒ์„ฑ

ํŒจํ‚ค์ง€๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๊ณ , Github์— ํ•ด๋‹น ํ”„๋กœ์ ํŠธ๋ฅผ ๋ ˆํฌ๋กœ ์—…๋กœ๋“œ ํ•ด์ค€๋‹ค.(์‚ฌ์ „์— Github ๊ณ„์ • ์—ฐ๋™ ํ•„์ˆ˜)

๋‘ ๋ฒˆ์งธ ์•„์ด์ฝ˜์—์„œ Remotes -> New "`" Remote -> ๋ ˆํฌ ์ƒ์„ฑ ์™„๋ฃŒ

1.0.0 ํƒœ๊ทธ ๋‹ฌ๊ณ  ๋ฆด๋ฆฌ์ฆˆ

 

 

์ด๋ ‡๊ฒŒํ•˜๋ฉด ์šฐ๋ฆฌ๋Š” ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค! ์ด์ œ ํŒจํ‚ค์ง€๋ฅผ ์ ์šฉํ•ด๋ณด์ž

 

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

์šฐ์„  ์•ž์„œ ๋งŒ๋“  XcodeLibrary๋ผ๋Š” ํŒจํ‚ค์ง€์— ๋‚ด ํŒจํ‚ค์ง€์˜ ์œ ํšจ์„ฑ ํ™•์ธ์„ ์œ„ํ•ด ์ถ”๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์—ˆ๋‹ค.

์ด์ œ ๋‚ด ํŒจํ‚ค์ง€๋ฅผ ์ ์šฉํ•  ์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ๋กœ ๋“ค์–ด๊ฐ€์„œ ํŒจํ‚ค์ง€๋ฅผ ์ ์šฉํ•ด๋ณด์ž.

 

1. Package Dependencies์—์„œ + ๋กœ ์ถ”๊ฐ€ํ•˜๊ธฐ

2. ์•„๊นŒ ๋งŒ๋“ค์–ด๋‘” ๋ ˆํฌ url๋กœ ๊ฒ€์ƒ‰ํ•ด์„œ ํŒจํ‚ค์ง€ ์ฐพ์•„์ฃผ๊ธฐ

3. ์ด๋ ‡๊ฒŒ ๋–ด๋‹ค๋ฉด ์™„๋ฃŒ!

 

๊ทธ๋Ÿผ ์ด์ œ ํ•ด๋‹น ํŒจํ‚ค์ง€์™€ ๋ชจ๋“ˆ์ด ์ž˜ ๋Œ์•„๊ฐ€๋Š”์ง€ ์šฐ๋ฆฌ์˜ test() ํ•จ์ˆ˜๋กœ ์‹คํ—˜ํ•ด๋ณด์ž.

 

import๋„ ์ž˜ ๋˜๊ณ , 

์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๋„ ๋ฌธ์ œ์—†๋‹ค

 

๋งˆ์ง€๋ง‰์œผ๋กœ test()ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  RUNํ•˜๋‹ˆ Test Completed!๐Ÿš€ ์ถœ๋ ฅ ์™„๋ฃŒ!!

 

 

๋ชจ๋‘๋“ค Swift Package Manager ์—๋Ÿฌ ์—†์ด ์ž˜ ๋งŒ๋“ค๊ณ  ์ ์šฉํ•˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค๐Ÿค“