创建Package.swift
mkdir webDemo
cd webDemo
swift package init
设置版本的语法:
// Depending on a branch (master in this case):
.package(
url: "https://github.com/johnsundell/files.git",
.branch("master")
)
// Depending on an exact commit:
.package(
url: "https://github.com/johnsundell/files.git",
.revision("0e0c6aca147add5d5750ecb7810837ef4fd10fc2")
)
// Tag
.package(
url: "https://github.com/johnsundell/files.git",
.exact("4.0.0")
)
.package(url: "https://github.com/apple/example-package-playingcard.git", from: "3.0.0"),
// Using 'path', we can depend on a local package that's
// located at a given path relative to our package's folder:
.package(path: "../CalendarKit")
运行项目
swift build
这时候会下载依赖的package,下载完后会编译项目。
完整Demo
// swift-tools-version:5.2
import PackageDescription
let package = Package(
name: "hello",
platforms: [
.macOS(.v10_15)
],
dependencies: [
// 💧 A server-side Swift web framework.
.package(url: "https://hub.fastgit.org/liuyujie/vapor.git", .branch("main")),
],
targets: [
.target(
name: "App",
dependencies: [
.product(name: "Vapor", package: "vapor")
],
swiftSettings: [
// Enable better optimizations when building in Release configuration. Despite the use of
// the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
// builds. See <https://github.com/swift-server/guides/blob/main/docs/building.md#building-for-production> for details.
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
]
),
.target(name: "Run", dependencies: [.target(name: "App")]),
.testTarget(name: "AppTests", dependencies: [
.target(name: "App"),
.product(name: "XCTVapor", package: "vapor"),
])
]
)
重新生成 xcodeproj文件(仅Mac适用)
swift package generate-xcodeproj
官方文档
https://hub.fastgit.org/apple/swift-package-manager/blob/main/Documentation/PackageDescription.md
网友评论