如何使用 Swift Package Manager 从 repo 中排除文件/文件夹?

Ras*_*sto 9 git xcode github swift-package-manager xcode11

我的库FlexColorPicker最近采用了 SPM 支持。它有效,但我不喜欢当通过 XCode 添加 FlexColorPicker 包时(文件?Swift 包?添加包依赖...?选择目标?输入https://github.com/RastislavMirek/FlexColorPicker?确认)一些不需要的文件被下载,例如FlexColorPicker.podspec和整个GifsAndScreenshots文件夹。

有没有办法防止下载这些不必要的文件?

es9*_*s97 7

Package.swift通过将文件添加到要排除的文件夹中,可以从包中排除文件夹,其中包含以下内容:

// swift-tools-version:5.5

import PackageDescription

let package = Package()
Run Code Online (Sandbox Code Playgroud)

这将不再使其在 XCode 导航器中可见。


Aus*_*tin 6

您可以使用文件中exclude目标上的参数执行此Package.swift操作。它接受一组路径(相对于目标根)。

文档

/// Create a library or executable target.
///
/// A target can either contain Swift or C-family source files. You cannot
/// mix Swift and C-family source files within a target. A target is
/// considered to be an executable target if there is a `main.swift`,
/// `main.m`, `main.c` or `main.cpp` file in the target's directory. All
/// other targets are considered to be library targets.
///
/// - Parameters:
///   - name: The name of the target.
///   - dependencies: The dependencies of the target. These can either be other targets in the package or products from package dependencies.
///   - path: The custom path for the target. By default, targets will be looked up in the <package-root>/Sources/<target-name> directory.
///       Do not escape the package root, i.e. values like "../Foo" or "/Foo" are invalid.
///   - exclude: A list of paths to exclude from being considered source files. This path is relative to the target's directory.
///   - sources: An explicit list of source files.
///   - publicHeadersPath: The directory containing public headers of a C-family family library target.
///   - cSettings: The C settings for this target.
///   - cxxSettings: The C++ settings for this target.
///   - swiftSettings: The Swift settings for this target.
///   - linkerSettings: The linker settings for this target.
...
Run Code Online (Sandbox Code Playgroud)

在 Package.swift 文件中使用它看起来像这样:

...
  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 which this package depends on.
    .target(
      name: "MyApp",
      dependencies: [],
      exclude: ["example.swift"]
    ),
...
Run Code Online (Sandbox Code Playgroud)

此外,developer.apple.com 有一些目标文档 -> exclude 参数

  • 这不行 (3认同)
  • 这对于所提出的问题实际上并不起作用。作者想要阻止某些文件夹或文件与包一起下载。在此回复中,即使我们排除某个文件夹或文件,它们仍然会被下载到客户端的代码中。 (2认同)

bsc*_*ern 5

目前这是不可能的。SwiftPM 会克隆 git 存储库,因此它也会获取其中的任何文件。

我知道 git 可以从存储库克隆特定路径,但我不确定其限制。为了使用 SwiftPM 支持此类功能,需要有一个 Swift Evolution 提案。

  • 作为参考,至少可以通过在名称前添加句点来隐藏 XCode 浏览器中的某些文件或文件夹。 (10认同)