skip to Main Content

I am looking for a way to add multiple SPM packages to an Xcode project (similar to how cocoapods works using it’s podfile and calling pod install)

I know I can manually go to File->Add Package Dependencies... to manually add one package at a time.

When building a SPM package I can add dependencies through the Package.swift file. I tried adding a Package.swift to a regular Xcode project but there seems to be no way for Xcode to used that file when working with iOS projects. There also doesn’t appear to be a command for Xcode’s command line build tools to update an Xcode project using a Package.swift file?

Is there any other way for me to add SPM packages to an iOS Xcode project?

  • Added a Package.swift (expected Xcode to read dependencies as it does when creating a Swift Package)
  • Searched documentation for Xcode command line tools (expected to find a command for adding a package to an Xcode project)

2

Answers


  1. I was also facing the same problem. I switched to Tuist because of both the ability to install more than one package at the same time and the compilation time. If you want, take a look at the temp files, I hope it will be useful for you.

    Github: https://github.com/tuist/tuist

    Login or Signup to reply.
  2. I was very frustrated by SPM not supporting command line editing of Package.swift files (yet). I created clspm, a tool for working with SPM packages through the command line.

    The tool creates either a CSV or JSON file that lists all the dependencies already in your project. In your project directory call

    clspm init --csv
    

    The file it creates is called spmfile and by default will get created in your project directory. (If you don’t want any trace of this package manager in your repo you can pass in an outside directory and file name with --spmfile <FILE_DIR>)

    Add the names of dependencies you’d like to use to the spmfile and call

    clspm install
    

    Since you have used cocoapods before this should all be pretty familiar.

    (If your spmfile lives outside of your project directory pass in the directory and file name from your init call with --spmfile <FILE_DIR>)

    If this is the first time you are using those dependencies the tool will ask you for either a GitHub user name or the url of the repo. If you create other packages it remembers the repo locations (~/.swiftclpm/dependencies) and install runs without interruption.

    Some extra info you didn’t ask about:

    • My main reason for creating this tool was to be able to switch back and forth between local and remote implementation of my Swift Packages. If you are interested in that run install with the -l option, followed by all the package names you’d like to work on locally. Calling install again switches it back to remote implementations.
    • You can call
    clspm create -g <NAME_OF_A_GROUP>
    

    to quickly create a local package from an Xcode project group. It creates the package and adds it to your project.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search