# 410-Creating Swift Packages
# Overview
- Creating local packages
- Publishing packages
- Package manifest API
- Editing packages
- Open source project
# Creating local packages
File -> New -> Swift Package,创建 local package, 然后添加 code, 在 Target -> General -> Framework, Libraries, and Embedded Content 中添加创建的 library
Local package 具有以下特点
- Can be thought of as sub-projects in a Xcode workspace
- Platform independent
- Great for refactoring out reusable code
- Not versioned
- Can publish and share with others later
# Publishing packages
版本号遵循 Semantic Versioning (opens new window)
Major version zero is a special case which you can use during initial development. Changes to minor and patch versions can also break API.
You can use prerelease versions to ask your clients to test APIs before you make the final release for a version. For example, 2.0.0-beta.1
编写 README 文档,修改 Package.swift 文件,具体语法 Package manifest API,然后把创建的 local package push to GitHub 或者其它 Xcode 支持的代码管理仓库。
# Package manifest API
Package.swift
// swift-tools-version:5.1
import PackageDescription
let package = Package(
name: "MyLibrary",
products: [
.library(name: "MyLibrary", targets: ["MyLibrary"]),
],
dependencies: [
.package(url: "https://github.com/ReactiveX/RxSwift.git", .upToNextMajor(from: "5.0.0")),
],
targets: [
.target(name: "MyLibrary", dependencies: []),
.testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"]),
]
)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# swift-tools-version: 5.1
The swift-tools-version declares the minimum version of Swift required to build this package.
Xcode ensures that the Swift Tools version of all of your package dependencies are always compatible with Tools version of your package.
# name
The name of the Swift package.
# products
Products are used to export target from a package so other packages can use them.
# dependencies
Declare other packages that this package depends on.
# targets
注意:这里的 target 跟 Project 里的 target 没有关系
Targets are the basic building blocks of a package. A target can define a module or a test suite.
Library Targets are under "source" directory and each target should have its own subdirectory.
Test targets are under a "Tests" directory, and they also have their own subdirectory.
Targets can depend on other targets in this package, and on products in packages which this package depends on.
target 中有个 path
参数,指定 target 的路径,其实就是代码的路径,默认是[PackageRoot]/Sources/[TargetName]
,你也可以自己指定一个路径。
# platforms
Predefined minimum deployment version
- macOS 10.10
- iOS 8.0
- tvOS 9.0
- watchOS 2.0
A platform that the Swift package supports
platforms: [.macOS(.v10_15), .iOS(.v13)] or
platforms: [.macOS("10.15"), .iOS("13")]
2
# swiftLanguageVersions
The list of Swift versions that this package is compatible with
swiftLanguageVersions: [.v4, .v5]
# Adding support in existing projects
添加并按要求编写 Package.swift,重要的一点,指定 target path, 就可以支持 Swift Packages 包管理了
# Editing packages
Local package and standalone package can be edit.
But, package dependencies are locked for editing, because they're automatically managed by Xcode.
# Open source project
Swift PM consists of four command line tools
swift build
, build a package.swift run,
run the executable products.swift test
, run tests.swift package
, perform various nonbuild operations on the package.
You can also use xcodebuild
to build packages on the command line or on CI.
Swift package support in Xcode is built on top of the libSwiftPM library, and SourceKit-LSP was built on top of the open source libSwiftPM.
# Links
Swift Package Manager Guide (opens new window)
Swift Package Manager Document (opens new window)
# 期望
Swift packages currently only supports source code and unit tests. We're looking forward to add support for resources such as images, text files, or other data files.