Bundle executable or shareable code into a standalone Swift package.
Overview
A Swift package is a directory that contains sources, dependencies, and has a Package.swift manifest file at its root. Swift packages can provide libraries, executables, tests, plugins, and macros. The package manifest defines what is in the package, which is itself written in Swift. The API reference for PackageDescription defines the types, properties, and functions that go into assembling a package manifest.
Creating a Library Package
Swift package manager supports creating packages using swift package init. By default, the package manager creates a package structure focused on providing a library. For example, you can create a directory and run the command swift package init to create a package:
$ mkdir MyPackage$ cd MyPackage$ swift package init
The structure provided follows package manager conventions, and provides a fully operational example. In addition to the package manifest, Swift sources are collected by target name under the Sources directory, and tests collected, also by target name, under the Tests directory:
Swift Package Manager can also create a new package with a simplified structure focused on creating executables. For example, create a directory and run the init command with the option --type executable to get a package that provides a “Hello World” executable:
$ mkdir MyExecutable$ cd MyExecutable$ swift package init --type executable$ swift runHello, World!
There is an additional option for creating a command-line executable based on the swift-argument-parser, convenient for parsing command line arguments and structuring commands. Use tool for the type option in swift package init. Like the executable template, it is fully operational and also prints “Hello World”.
Creating a Macro Package
Swift Package Manager can generate boilerplate for custom macros:
$ mkdir MyMacro$ cd MyMacro$ swift package init --type macro$ swift build$ swift runThe value 42 was produced by the code "a + b"
This creates a package with:
A .macro type target with its required dependencies on swift-syntax,