10 June 2021 3min read

Make a Swift Package Collection with your favorites

WWDC21 has brought many cool things and there is one that will facilitate a lot the creation of new projects with dependencies. Until now it was a bit of a pain to remember which packages you wanted to add on almost every new project. It was such an inconvenience that I even have my small CLI to create new Swift packages. But I think now there is a better way!

Note that SPM may have soon a way of creating packages from templates. That’s not what I’m talking about here but it will be an even better addition to our toolbelt. See the pitch for a package creation command.

A package collection is nothing more than a json file describing a collection of packages. SPM interprets those and they are stored locally on your computer so it’s easier to add packages from those collections into your projects.

This new feature came trough Swift Evolution in SE-0291. The nice thing is that not only SPM supports it but Apple also added integration directly into Xcode!

xcode spm collections

Xcode comes with a default collection containing all Apple’s Swift Packages. Many of them feel like part of the standard library so it’s very nice to have them available with just one click.

One click?

Yes, because Xcode now can offer a fix-it when you are trying to import a module, and if the module is in a collection you are just one click away!

xcode spm collection suggest import

Author collections

The folks at the Swift Package Index have just released their first feature using the new collections: Author Collections. That is great for adding collections from your favorite authors.

Of course the first thing I did was to add the collection from pointfree!

Your own collection

But since a collection is just a JSON you shouldn’t be afraid of having your own. And that’s the nice thing about this. I think that making and maintaining your own collection is great for helping your future self bootstrap new projects and packages easily!

I recommend you to watch WWDC21 Discover and curate Swift Packages using Collections for a great introduction and step-by-step guide on how to do it. Is very easy!

Just use the generator to convert a simple JSON to the expected format. Look at my input.json as an example:

{
    "name": "Alexito's favorite packages",
    "overview": "This collection contains my favorite Swift packages.",
    "keywords": [
        "favorite"
    ],
    "packages": [
        {
            "url": "https://github.com/nicklockwood/SwiftFormat.git"
        },
        ...
    ],
    "author": {
        "name": "Alejandro Martinez"
    }
}

You can specify more things on the JSON but if you pass a GitHub --auth-token to the generator it will automatically get all the information that it needs from every repository. I find that very convenient.

Once you have your output json you just need to host it on some server. A GitHub repo is good enough for your own use.

Mine is already on GitHub: Alexito’s favorite packages. This will make it easier so much easier to import the packages that I need when prototyping ideas.

Now it’s your time! Go, create and share collections of your favorite packages!

If you enjoyed this post

Continue reading