21

我正在 Xcode 11(beta 5)中创建一个新的 iOS 应用程序,我想尝试使用 Swift Package Manager 而不是 CocoaPods 来管理依赖项。

使用 SwiftLint 和 CocoaPods 时的一个常见模式是添加 SwiftLint 作为依赖项,然后添加一个构建阶段来执行${PODS_ROOT}/SwiftLint/swiftlint;这样,所有开发人员最终都使用相同版本的 SwiftLint。

如果我尝试在 Xcode 中添加 SwiftLint 作为 SwiftPM 依赖项,我需要的可执行目标将被禁用:

添加包截图

我可以通过创建一个Package.swift没有产品或目标的虚拟对象并swift run swiftlint在我的构建阶段运行来伪造它,但它感觉很笨拙和奇怪:

// swift-tools-version:5.1
import PackageDescription

let package = Package(
    name: "dummy-package",
    products: [],
    dependencies: [
        .package(url: "https://github.com/realm/SwiftLint.git", from: "0.34.0")
    ],
    targets: []
)

有没有办法在不创建虚拟包的情况下做到这一点?或者 Swift 包管理器不是这个特定用例的正确工具?

4

2 回答 2

9

所有滥用 iOS 代码依赖管理器来运行构建工具的方法都是老生常谈和怪异的。

版本 SPM 兼容工具依赖项的正确方法是使用Mint:安装和运行 Swift CLI 包的包管理器。另请参阅更好的 iOS 项目:如何使用 mint 管理您的工具

于 2019-09-21T00:35:02.847 回答
1

I use xcodegen to genreate a Xcode project that has the ability to run scripts. This lets me see swiftlint warnings in Xcode while developing packages.

This tool creates a Xcode project from a project.yml definition. In that definition, you can add a script that runs swiftlint as a post compile task. Example.

Advantages of this method:

  • swiftlint warnings in Xcode.
  • Xcode settings beyond what SPM offers.

Disadvantages:

  • You rely on a third-party tool that could break or go away. However, you can drop this dependency at any time and go back to edit the Package.swift in Xcode.
  • You need to learn to write project.yml files.
  • If you use SPM bundles you need to generate the bundle accessor yourself.

A word about generating a bundle accessor. This is needed when working from a Xcode project because only SPM generates the file resource_bundle_accessor.swift to the project. If you already compiled after opening the Package.swift with Xcode, the file should be here:

find ~/Library/Developer/Xcode/DerivedData* -iname resource_bundle_accessor.swift

You can add it to the project, but if you are creating a framework, the bundle accessor can be as simple as:

import class Foundation.Bundle

// This file is only used when using a xcodegen-generated project.
// Otherwise this file should not be in the path.

private class BundleFinder {}

extension Foundation.Bundle {
    static var module = Bundle(for: BundleFinder.self)
}
于 2021-05-16T22:27:07.150 回答