Swiftpack.co - nearfri/XCResource as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by nearfri.
nearfri/XCResource 0.9.17
A command line tool for generating Swift code for xcassets and generating localizable strings from Swift code
⭐️ 1
πŸ•“ 3 weeks ago
.package(url: "https://github.com/nearfri/XCResource.git", from: "0.9.17")


Swift codecov

XCResourceλŠ” xcassets λ¦¬μ†ŒμŠ€ λ‘œλ”©κ³Ό λ‹€κ΅­μ–΄ 지원을 λ„μ™€μ£ΌλŠ” μ»€λ§¨λ“œλΌμΈ νˆ΄μž…λ‹ˆλ‹€.

이λ₯Ό μ΄μš©ν•΄ 이미지, 컬러, λ‹€κ΅­μ–΄ λ¬Έμžμ—΄μ„ μ‰½κ²Œ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€:

let image = UIImage.named(.settings)
let color = UIColor.named(.coralPink)
let string = String.localized(.done)
let text = String.formatted(.alert_attachTooLargeVideo(maxMinutes: maxMinutes))


xcresourceλŠ” λ‹€μŒμ˜ ν•˜μœ„ μ»€λ§¨λ“œλ₯Ό 가지고 μžˆμŠ΅λ‹ˆλ‹€:

  • xcassets2swift: xcassets을 μœ„ν•œ Swift μ½”λ“œλ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
  • swift2strings: Swift enum으둜 strings νŒŒμΌμ„ μƒμ„±ν•©λ‹ˆλ‹€.
  • key2form: Swift enum으둜 format string μ½”λ“œλ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
  • strings2csv: strings 파일둜 CSV νŒŒμΌμ„ μƒμ„±ν•©λ‹ˆλ‹€.
  • csv2strings: CSV 파일둜 strings νŒŒμΌμ„ μƒμ„±ν•©λ‹ˆλ‹€.
  • init: Manifest νŒŒμΌμ„ μƒμ„±ν•©λ‹ˆλ‹€.
  • run (default): Manifest νŒŒμΌμ— λ‚˜μ—΄λœ ν•˜μœ„ μ»€λ§¨λ“œλ“€μ„ μ‹€ν–‰ν•©λ‹ˆλ‹€.



mint install nearfri/XCResource


git clone https://github.com/nearfri/XCResource.git
cd XCResource
make build install

μ‚¬μš© 예제

xcassets 이미지 λ‘œλ”©ν•˜κΈ°

xcresource xcassets2swiftλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€:

xcrun --sdk macosx mint run xcresource xcassets2swift \
    --xcassets-path ../SampleApp/Assets.xcassets \
    --asset-type imageset \
    --swift-path ../SampleApp/ResourceKeys/ImageKey.swift \
    --swift-type-name ImageKey

μ•„λž˜μ™€ 같은 μ½”λ“œκ°€ μƒμ„±λ©λ‹ˆλ‹€:

struct ImageKey: ExpressibleByStringLiteral, Hashable {
    var rawValue: String
    init(_ rawValue: String) {
        self.rawValue = rawValue
    init(stringLiteral value: String) {
        self.rawValue = value

// MARK: - Assets.xcassets

extension ImageKey {
    // MARK: Places
    static let places_authArrow: ImageKey = "Places/authArrow"
    static let places_authClose: ImageKey = "Places/authClose"
    // MARK: Settings
    static let settings: ImageKey = "settings"
    static let settingsAppearance: ImageKey = "settingsAppearance"

UIImage에 μƒμ„±μžλ₯Ό μΆ”κ°€ν•΄μ€λ‹ˆλ‹€:

extension UIImage {
    static func named(_ key: ImageKey) -> UIImage {
        return UIImage(named: key.rawValue, in: .module, compatibleWith: nil)!

이제 μžλ™μ™„μ„±κ³Ό ν•¨κ»˜ 이미지λ₯Ό 생성할 수 μžˆμŠ΅λ‹ˆλ‹€:

imageView.image = .named(.settings)

Swift enum으둜 strings 파일 λ§Œλ“€κΈ°

enum νƒ€μž…μ˜ StringKeyλ₯Ό λ§Œλ“€μ–΄μ€λ‹ˆλ‹€:

enum StringKey: String, CaseIterable {
    /// μ·¨μ†Œ
    case cancel
    /// 확인
    case confirm

xcresource swift2stringsλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€:

xcrun --sdk macosx mint run xcresource swift2strings \
    --swift-path ../SampleApp/ResourceKeys/StringKey.swift \
    --resources-path ../SampleApp \
    --merge-strategy ko:comment

μ•„λž˜μ™€ 같이 strings 파일이 μ—…λ°μ΄νŠΈ λ©λ‹ˆλ‹€:

/* μ·¨μ†Œ */
"cancel" = "μ·¨μ†Œ";

/* 확인 */
"confirm" = "확인";

String에 μƒμ„±μžλ₯Ό μΆ”κ°€ν•΄μ€λ‹ˆλ‹€:

extension String {
    static func localized(_ key: StringKey) -> String {
        return NSLocalizedString(key.rawValue, bundle: .module, comment: "")

이제 μžλ™μ™„μ„±κ³Ό ν•¨κ»˜ μ§€μ—­ν™”λœ λ¬Έμžμ—΄μ„ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€:

label.text = .localized(.cancel)

Swift enum으둜 format string μ½”λ“œ λ§Œλ“€κΈ°

StringKey의 case에 format string ν˜•μ‹μ˜ 주석을 μΆ”κ°€ν•©λ‹ˆλ‹€:

enum StringKey: String, CaseIterable {
    /// λ™μ˜μƒ μ²¨λΆ€λŠ” μ΅œλŒ€ %{maxMinutes}ldλΆ„κΉŒμ§€ κ°€λŠ₯ν•©λ‹ˆλ‹€.\nλ‹€λ₯Έ νŒŒμΌμ„ μ„ νƒν•΄μ£Όμ„Έμš”.
    case alert_attachTooLargeVideo

xcresource key2form을 μ‹€ν–‰ν•©λ‹ˆλ‹€:

xcrun --sdk macosx mint run xcresource key2form \
    --key-file-path ../SampleApp/ResourceKeys/StringKey.swift \
    --form-file-path ../SampleApp/ResourceKeys/StringForm.swift \
    --form-type-name StringForm \
    --issue-reporter xcode

μ•„λž˜μ™€ 같은 μ½”λ“œκ°€ μƒμ„±λ©λ‹ˆλ‹€:

struct StringForm {
    var key: String
    var arguments: [CVarArg]

extension StringForm {
    /// λ™μ˜μƒ μ²¨λΆ€λŠ” μ΅œλŒ€ %{maxMinutes}ldλΆ„κΉŒμ§€ κ°€λŠ₯ν•©λ‹ˆλ‹€.\nλ‹€λ₯Έ νŒŒμΌμ„ μ„ νƒν•΄μ£Όμ„Έμš”.
    static func alert_attachTooLargeVideo(maxMinutes: Int) -> StringForm {
        return StringForm(
            key: StringKey.alert_attachTooLargeVideo.rawValue,
            arguments: [maxMinutes])

String에 μƒμ„±μžλ₯Ό μΆ”κ°€ν•΄μ€λ‹ˆλ‹€:

extension String {
    static func formatted(_ form: StringForm) -> String {
        let format = NSLocalizedString(form.key, bundle: .module, comment: "")
        return String(format: format, locale: .current, arguments: form.arguments)

이제 μžλ™μ™„μ„±κ³Ό ν•¨κ»˜ μ§€μ—­ν™”λœ λ¬Έμžμ—΄μ„ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€:

label.text = .formatted(.alert_attachTooLargeVideo(maxMinutes: maxMinutes))

strings 파일둜 CSV 파일 λ§Œλ“€κΈ°

xcresource strings2csvλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€:

mint run xcresource strings2csv \
    --resources-path ../SampleApp \
    --development-language ko \
    --csv-path ./localizations.csv \
    --header-style long-ko \

μ•„λž˜μ™€ 같은 csv 파일이 λ§Œλ“€μ–΄μ§‘λ‹ˆλ‹€:

Key Comment ν•œκ΅­μ–΄ (ko) μ˜μ–΄ (en)
cancel μ·¨μ†Œ μ·¨μ†Œ
confirm 확인 확인

CSV 파일둜 strings 파일 λ§Œλ“€κΈ°

xcresource csv2stringsλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€:

mint run xcresource csv2strings \
    --csv-path ./localizations.csv \
    --header-style long-ko \
    --resources-path ../SampleApp

μ•„λž˜μ™€ 같이 strings 파일이 λ§Œλ“€μ–΄μ§‘λ‹ˆλ‹€:

/* μ·¨μ†Œ */
"cancel" = "μ·¨μ†Œ";

/* 확인 */
"confirm" = "확인";

XCResourceSample.xcworkspaceμ—μ„œ 적용 예제λ₯Ό λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

λΌμ΄μ„ μŠ€

XCResourceλŠ” MIT λΌμ΄μ„ μŠ€μ— 따라 λ°°ν¬λ©λ‹ˆλ‹€. μžμ„Έν•œ λ‚΄μš©μ€ LICENSEλ₯Ό μ°Έμ‘°ν•˜μ‹­μ‹œμ˜€.


Stars: 1
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Related Packages

Release Notes

3 weeks ago

Full Changelog: https://github.com/nearfri/XCResource/compare/0.9.16...0.9.17


  • xcresource.zip faec8969f8ef3cf676dd2a4536092145ec215e585d13211790426c972c3631e3
  • xcresource.artifactbundle.zip 736547d49b8f81d2a15e238071aa808c99f8fe867100cdfdb69a3759231bb622

Swift Package Manager snippet

    name: "xcresource",
    url: "https://github.com/nearfri/XCResource/releases/download/0.9.17/xcresource.artifactbundle.zip",
    checksum: "736547d49b8f81d2a15e238071aa808c99f8fe867100cdfdb69a3759231bb622"

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics