首页 > 解决方案 > 如何在编译和轻松管理方面最好地管理 Swift Package Manager 依赖项?

问题描述

我有这个巨大的外部依赖项,将在我们的应用程序中使用,一些将仅用于主应用程序,一些将由我们的内部包使用。

import PackageDescription

let package = Package(
    name: "AppDependencies",
    platforms: [.iOS(.v12)],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "AppDependencies",
            targets: ["AppDependencies"]),
        .library(
            name: "AmityUIIOS",
            targets: ["AmityUIIOS"]),
        .library(
            name: "IGListKit",
            targets: ["IGListKit"]),
        .library(
            name: "Realm",
            targets: ["Realm"]),
        .library(
            name: "RealmSwift",
            targets: ["RealmSwift"]),
        .library(
            name: "JitsiMeet",
            targets: ["JitsiMeet"]),
        .library(
            name: "WebRTC",
            targets: ["WebRTC"]),
        .library(
            name: "AmitySDK",
            targets: ["AmitySDK"]),
        .library(
            name: "AmityUIKit",
            targets: ["AmityUIKit"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/AFNetworking/AFNetworking", .exact("4.0.1")),
        .package(url: "https://github.com/janlionly/Aspects", .revision("48a77dbe0e8e9037114deaaec5d0a3f4f3638053")),
        .package(url: "https://github.com/danielgindi/Charts", .exact("4.0.1")),
        .package(url: "https://github.com/jverdi/JVFloatLabeledTextField", .exact("1.2.5")),
        .package(url: "https://github.com/SDWebImage/SDWebImage", .exact("5.11.1")),
        .package(url: "https://github.com/ProcedureKit/ProcedureKit", .exact("5.2.0")),
        .package(url: "https://github.com/totocaster/Typist", .exact("1.4.3")),
        .package(url: "https://github.com/MortimerGoro/MGSwipeTableCell", .revision("02ef4455392a6c3369866f31bb1293ebbd01da25")),
        .package(url: "https://github.com/michaeltyson/TPKeyboardAvoiding", .exact("1.3.5")),
        .package(name: "PhotoEditorSDK", url: "https://github.com/imgly/pesdk-ios-build", .exact("10.26.1")),
        .package(name: "AgoraRtcKit", url: "https://github.com/AgoraIO/AgoraRtcEngine_iOS", .revision("9eb13651bd9b2a45a384cedcfb3dfa26350ee6f4")),
        .package(url: "https://github.com/CocoaLumberjack/CocoaLumberjack", .exact("3.7.2")),
        .package(url: "https://github.com/malcommac/SwiftDate", .exact("6.3.1")),
        .package(url: "https://github.com/daltoniam/Starscream", .exact("4.0.4")),
        .package(url: "https://github.com/datatheorem/TrustKit", .exact("2.0.0")),
        .package(url: "https://github.com/huri000/SwiftEntryKit", .exact("1.2.7")),
        .package(url: "https://github.com/marmelroy/PhoneNumberKit", .exact("3.3.3")),
        .package(name: "Firebase", url: "https://github.com/firebase/firebase-ios-sdk", .exact("8.7.0")),
        .package(name: "GoogleSignIn", url: "https://github.com/google/GoogleSignIn-iOS", .exact("6.0.2")),
        .package(name: "AppAuth", url: "https://github.com/openid/AppAuth-iOS", .exact("1.4.0")),
        
        .package(url: "https://github.com/EkoCommunications/DateTools", .revision("0139f60de63334d06878d00a9c8a16dcf9585a34")),
        .package(url: "https://github.com/scenee/FloatingPanel", .exact("2.4.1")),
        .package(url: "https://github.com/WenchaoD/FSPagerView", .revision("1c2aaef6bfdfca7938e47a44c1a63fbdc5782ee7")),
        .package(url: "https://github.com/alexisakers/HTMLString", .exact("6.0.1")),
        .package(url: "https://github.com/patchthecode/JTAppleCalendar", .exact("8.0.3")),
        .package(url: "https://github.com/ninjaprox/NVActivityIndicatorView", .exact("5.1.1")),
        .package(url: "https://github.com/moliya/PPBadgeView", .revision("455550094ddd50dc7e048cea53131f820e1692c0")),
        .package(url: "https://github.com/jrendel/SwiftKeychainWrapper", .exact("4.0.1")),
        .package(url: "https://github.com/eddiekaiger/SwiftyAttributes", .revision("295b17ef68811f423ed4b281d902df12d4d33963")),
        .package(url: "https://github.com/SwipeCellKit/SwipeCellKit", .exact("2.7.1")),
        .package(url: "https://github.com/pinterest/PINCache", .exact("3.0.3")),
        .package(url: "https://github.com/mstfy/TTTAttributedLabel", .revision("a38fcd41afad4ad3a4b00e28a4505c6936b02b99")),
    ],
    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.
        .binaryTarget(name: "AmityUIIOS", path: "Artifacts/AmityUIIOS.xcframework"),
        .binaryTarget(name: "IGListKit", path: "Artifacts/IGListKit.xcframework"),
        .binaryTarget(name: "Realm", path: "Artifacts/Realm.xcframework"),
        .binaryTarget(name: "RealmSwift", path: "Artifacts/RealmSwift.xcframework"),
        .binaryTarget(name: "JitsiMeet", path: "Artifacts/JitsiMeet.xcframework"),
        .binaryTarget(name: "WebRTC", path: "Artifacts/WebRTC.xcframework"),
        .binaryTarget(name: "AmitySDK", path: "Artifacts/AmitySDK.xcframework"),
        .binaryTarget(name: "AmityUIKit", path: "Artifacts/AmityUIKit.xcframework"),
        .target(
            name: "AppDependencies",
            dependencies: [
                "AFNetworking",
                "JVFloatLabeledTextField",
                "SDWebImage",
                "ProcedureKit",
                "Typist",
                "MGSwipeTableCell",
                "TPKeyboardAvoiding",
                "PhotoEditorSDK",
                "AgoraRtcKit",
                "CocoaLumberjack",
                .product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"),
                "SwiftDate",
                "Starscream",
                "TrustKit",
                "SwiftEntryKit",
                "PhoneNumberKit",
                "AppAuth",
                "GoogleSignIn",
                .product(name: "FirebaseCrashlytics", package: "Firebase"),
                .product(name: "FirebaseAnalytics", package: "Firebase"),
                "Aspects",
                "Charts",
                "DateTools",
                "FloatingPanel",
                "FSPagerView",
                "HTMLString",
                "JTAppleCalendar",
                "NVActivityIndicatorView",
                "PPBadgeView",
                "SwiftKeychainWrapper",
                "SwiftyAttributes",
                "SwipeCellKit",
                "PINCache",
                "TTTAttributedLabel",
            ]),
        .testTarget(
            name: "AppDependenciesTests",
            dependencies: ["AppDependencies"]),
    ]
)

我把它放在一个包中是因为它更容易维护,因为你可以在一个地方看到所有的外部依赖。

如果您只想使用其中一个依赖项,则这种方法的问题来自内部包,您必须重新编译 allAppDependency的依赖项。

另一种方法是在每个内部包中声明外部依赖项,但这会导致声明重复(尽管我知道 Swift PM 只会签出一个解析 repo)。然而,这种方法会使我们更难维护,因为当更新一个外部库时,我们必须找到所有依赖这些外部库来更新版本的内部包(我们总是使用 .exact,尽管使用其他 .updateToMajor 会有所帮助,但不会解决重复问题)。

对这方面的最佳实践有什么建议吗?

标签: iosswiftswift-package-manager

解决方案


推荐阅读