18

My library FlexColorPicker recently adopted SPM support. It works but I don't like that when FlexColorPicker package is added via Xcode, some unneeded files are downloaded. For example, FlexColorPicker.podspec and the entire GifsAndScreenshots folder.

Is there a way to prevent downloading these unnecessary files?

To add a package using Xcode: File → Swift Packages → Add Package Dependency... → choose target → enter https://github.com/RastislavMirek/FlexColorPicker → confirm

aheze
  • 24,434
  • 8
  • 68
  • 125
Rasto
  • 17,204
  • 47
  • 154
  • 245
  • Hey there! I have been and still in the exact situation. Like you referenced, I am going with the dot prefix for now. Did you find a proper solution to this problem? Thanks. – badhanganesh Nov 08 '21 at 17:23

3 Answers3

12

You can do this with the exclude parameter on a target in your Package.swift file. It accepts an array of paths (relative to the target root).

From the docs:

/// 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.
...

Using it in a Package.swift file looks something like this:

...
  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"]
    ),
...

Also, developer.apple.com has some docs for the target -> exclude parameter

Austin
  • 1,369
  • 1
  • 13
  • 19
  • 3
    this doesn't wokr – Inder Kumar Rathore Nov 01 '20 at 08:08
  • @InderKumarRathore You're probably using a package manager which is too old. I think you need version 5.x. You should adapt the version in `Package.swift` to `// swift-tools-version:5.0` or newer. – clemens Nov 15 '20 at 10:02
  • 1
    You might try shortening your `exclude` folder path to start not at the package root (aka where Package.swift is), but instead the target root. Just worked for me. – Porter Child May 01 '21 at 01:09
  • 2
    This doesn't really work for the question being asked. The author wanted to prevent certain folders or files from being downloaded with the package. In this reply, even if we exclude a folder or file, they are still being downloaded into the client's code. – Edudjr Jan 13 '22 at 16:50
  • exclude is used to exclude files from what is included in Package targets when built by the compiler. It does not affect what is downloaded or displayed by Xcode. – Duncan Babbage Jan 16 '23 at 01:07
4

This is not currently possible. SwiftPM does a clone of the git repo so it will get any of those files as well.

I know that git can clone specific paths from a repo but I'm not sure of the limitations. To support for such a feature with SwiftPM there would need to be a Swift Evolution proposal.

bscothern
  • 1,894
  • 11
  • 15
  • 8
    For reference, it is at least possible to hide some files or folders from in XCode browser by prefixing their names with period. – Rasto Oct 11 '19 at 10:46
2

It is possible to exclude folders from your Package, by adding a Package.swift file to the folder that you want to exclude, with these contents:

// swift-tools-version:5.5

import PackageDescription

let package = Package()

This will no longer make it visible in the XCode navigator.

es97
  • 43
  • 1
  • 5