3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Swift] Package.swiftを見やすくしたい

Posted at

はじめに

こんにちは!
アプリ開発が好きで、Swiftの勉強をしている大学生です。
夏休みに入ったので、アウトプット頑張っていきたいと思っています。
温かい目で見ていただけると幸いです。

解決したい問題

個人的に以下のような書き方は、モジュールやdependenciesが増えていくと見づらいと感じる場面もある(※あんまりない)ので、ちょっとした工夫をしてみたいと思います。

let package = Package(
    name: "HogeHogeApp",
    platforms: [.iOS(.v15)],
    products: [
        .library(
            name: "HogeHogeFeature",
            targets: ["HogeHogeFeature"]
        ),
        .library(
            name: "FirebaseClient",
            targets: ["FirebaseClient"]
        )
        .library(
            name: "Domain",
            targets: ["Domain"]
        )
    ] dependencies: [
        .package(url: "https://github.com/Alamofire/Alamofire.git", exact: "5.7.0"),
        .package(url: "https://github.com/firebase/firebase-ios-sdk.git", exact: "10.10.0"),
       "7.0.0"),
    ],
    targets: [
         .target(
            name: "HogeHogeFeature",
            dependencies: [
                "FirebaseClient",
                "Domain"
            ]
        )
        .target(
            name: "FirebaseClient",
            dependencies: [
                .product(name: "FirebaseAuth", package: "Firebase"),
                .product(name: "FirebaseStorage", package: "Firebase"),
                .product(name: "FirebaseAuth", package: "FirebaseAuth")
            ]
        ),
        .target(
            name: "Domain",
            dependencies: [
                .product(name: "Alamofire", package: "Alamofire")
            ]
        )
    ]
)



実装

下記のようなextensionを定義します

extension Target.Dependency {
    static var alamofire: Self { .product(name: "Alamofire", package: "Alamofire") }
    static var firebaseAuth: Self { .product(name: "FirebaseAuth", package: "firebase-ios-sdk") }
    static var firebaseStorage: Self { .product(name: "FirebaseStorage", package: "firebase-ios-sdk") }
    static var firebaseFirestore: Self { .product(name: "FirebaseFirestore", package: "firebase-ios-sdk") }
}

使い方は以下の通りです。

let package = Package(
    name: "HogeHogeApp",
    platforms: [.iOS(.v15)],
    products: [
        .library(
            name: "HogeHogeFeature",
            targets: ["HogeHogeFeature"]
        ),
        .library(
            name: "FirebaseClient",
            targets: ["FirebaseClient"]
        )
        .library(
            name: "Domain",
            targets: ["Domain"]
        )
    ] dependencies: [
        .package(url: "https://github.com/Alamofire/Alamofire.git", exact: "5.7.0"),
        .package(url: "https://github.com/firebase/firebase-ios-sdk.git", exact: "10.10.0"),
       "7.0.0"),
    ],
    targets: [
         .target(
            name: "HogeHogeFeature",
            dependencies: [
                "FirebaseClient",
                "Domain"
            ]
        )
        .target(
            name: "FirebaseClient",
            dependencies: [
                .firebaseAuth,
                .firebaseStorage,
                .firebaseFirestore,
            ]
        ),
        .target(
            name: "Domain",
            dependencies: [
                .alamofire
            ]
        )
    ]
)

完全に好みの話にもなると思いますが、こっちの書き方の方が気に入ってます!

おまけ

Pulginを導入する場合は下記のようなextensionを定義します。ほとんど同じです。

extension Target.PluginUsage {
    static var swiftLintPlugin: Self { .plugin(name: "SwiftLintPlugin", package: "SwiftLint") }
    static var swiftFormatPlugin: Self { .plugin(name: "SwiftFormatPlugin", package: "SwiftFormat") }
}

終わりに

誰かの役に立つことができていれば幸いです。
アウトプットを頑張ろうと思っているので温かい目で見ていただけると嬉しいです。

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?