Scipio打包SwiftPM为XCFramework教程

1 安装

1.1 使用命令行

1
2
3
4
5
$ git clone https://github.com/giginet/Scipio.git
$ cd Scipio
$ swift run -c release scipio --help
# Add reference .build/release/scipio to the PATH variable.
$ export PATH=/path/to/scipio:$PATH

1.2 作为Package使用

推荐使用这种方式,较少命令行中参数,在Swift代码中EntryPoint配置方便。

2 准备您应用程序的所有依赖项

2.1 创建一个新的Swift包来描述依赖关系

1
2
3
$ mkdir MyAppDependencies
$ cd MyAppDependencies
$ swift package init

2.2 编辑 Package.swift 以描述应用程序的依赖关系下一步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// swift-tools-version: 5.6
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "MyAppDependencies",
platforms: [
// Specify platforms to build
.iOS(.v14),
],
products: [],
dependencies: [
// Add dependencies
.package(url: "https://github.com/onevcat/APNGKit.git", exact: "2.2.1"),
],
targets: [
.target(
name: "MyAppDependency",
dependencies: [
// List all dependencies to build
.product(name: "APNGKit", package: "APNGKit"),
]),
]
)

3 手动Rswift generate到项目中

如何找到R文件:R.generated.swift

任意找到一个_R,点击定义,FileShow in Finder

4a 命令行打包

1
2
3
4
5
6
7
8
$ scipio prepare path/to/MyAppDependencies
> 🔁 Resolving Dependencies...
> 🗑️ Cleaning MyAppDependencies...
> 📦 Building APNGKit for iOS
> 🚀 Combining into XCFramework...
> 📦 Building Delegate for iOS
> 🚀 Combining into XCFramework...
> ❇️ Succeeded.

4b 自定义打包配置

4b.1 创建可执行包

1
2
3
4
5
6
7
8
$ mkdir my-build-tool
$ cd my-build-tool
$ swift package init --type executable
Creating executable package: my-build-tool
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift

4b.2 编辑Package

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// swift-tools-version: 5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "my-build-tool",
platforms: [
.macOS(.v12)
],
dependencies: [
.package(
url: "https://github.com/giginet/Scipio.git",
revision: "0.15.0" // Use the latest version
),
],
targets: [
.executableTarget(
name: "my-build-tool",
dependencies: [
.product(name: "ScipioKit", package: "Scipio"),
],
path: "Sources"
),
]
)

4b.3 实现构建脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import Foundation
import ScipioKit

@main
struct EntryPoint {
private static let myPackageDirectory = URL(fileURLWithPath: "/path/to/MyPackage")

static func main() async throws {
let runner = Runner(
mode: .prepareDependencies,
options: .init(
baseBuildOptions: .init(
buildConfiguration: .release,
isSimulatorSupported: true
)
)
)

try await runner.run(
packageDirectory: myPackageDirectory,
frameworkOutputDir: .default
)
}
}

4b.4 命令行打包

1
$ swift run -c release my-build-tool

引用

  1. https://github.com/giginet/Scipio.git
  2. https://github.com/mac-cain13/R.swift/blob/main/Plugins/RswiftGeneratePublicResources/RswiftGeneratePublicResources.swift