Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exclude files/folders from repo with Swift Package Manager?

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

like image 401
Rasto Avatar asked Oct 10 '19 11:10

Rasto


People also ask

When does the Swift Package Manager copy a file?

If no optimization is available for a file /// type, the Swift Package Manager copies the file. /// /// If the given path represents a directory, the Swift Package Manager /// applies the process rule recursively to each file in the directory. /// /// If possible use this rule instead of `copy (_:)`.

What is a package resolved file in Swift?

The Package.resolved file records the results of the dependency resolution and lives in the top-level directory of a Swift package. If you add the Swift package as a package dependency to an app for an Apple platform, you can find the Package.resolved file inside your .xcodeproj or .xcworkspace.

Where should I put resources in the Swift Package Manager?

Similar to source code, the Swift Package Manager scopes resources to a target, so you must put them into the folder that corresponds to the target they belong to. For example, any resources for the MyLibrary target must reside in Sources/MyLibrary.

Does Swifty stuff work in package manager?

Swifty stuff also works in Package.swift since the package declaration file is itself a .swift file. Here are some examples which use Swift 5.3 Package Manager Conditional Target Dependencies SE-0273 condition and when.


2 Answers

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

like image 147
Austin Avatar answered Oct 01 '22 02:10

Austin


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.

like image 33
bscothern Avatar answered Oct 01 '22 02:10

bscothern