所有 Swift Package 都可以打包为 SDK!
从 Package.swift 中的 products 配置可以看到:
products: [
.library(
name: "BaseCore", // ✅ 可以打包
targets: ["BaseCore"]
)
]
| 模块 | 类型 | 可打包 | 说明 |
|---|---|---|---|
| BaseCore | 基础核心层 | ✅ | 基础接口和实现 |
| BaseCommon | 业务封装层 | ✅ | 业务通用功能 |
| CapabilityPush | 能力层 | ✅ | 推送能力 SDK |
| CapabilityShare | 能力层 | ✅ | 分享能力 SDK(待创建) |
| CapabilitySocketIO | 能力层 | ✅ | SocketIO 能力 SDK(待创建) |
cd /Users/Zhuanz/Documents/code/xindazhou/xdz_ios/BaseCore
swift build # 编译
swift test # 运行测试
cd /Users/Zhuanz/Documents/code/xindazhou/xdz_ios/BaseCore
# 1. 提交代码
git add .
git commit -m "Release BaseCore 1.0.0"
# 2. 打标签
git tag 1.0.0
git push origin main
git push origin 1.0.0
# 3. 在其他项目中使用
# 在 Package.swift 或 Xcode 中添加:
# .package(url: "https://github.com/your-org/BaseCore.git", from: "1.0.0")
// 在其他项目的 Package.swift 中
dependencies: [
.package(path: "../BaseCore") // 本地路径
]
cd /Users/Zhuanz/Documents/code/xindazhou/xdz_ios/BaseCore
# 创建临时 Xcode 项目(如果需要)
# 或者直接使用 Swift Package
# 构建 XCFramework
xcodebuild archive \
-scheme BaseCore \
-destination "generic/platform=iOS" \
-archivePath "./build/BaseCore-iOS.xcarchive" \
SKIP_INSTALL=NO \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES
# 创建 XCFramework
xcodebuild -create-xcframework \
-archive "./build/BaseCore-iOS.xcarchive" \
-framework "BaseCore.framework" \
-output "./build/BaseCore.xcframework"
将生成的 BaseCore.xcframework 打包分发:
cd build
zip -r BaseCore.xcframework.zip BaseCore.xcframework
如果需要支持 CocoaPods,创建 .podspec 文件:
# BaseCore.podspec
Pod::Spec.new do |s|
s.name = 'BaseCore'
s.version = '1.0.0'
s.summary = '基础核心层 SDK'
s.homepage = 'https://github.com/your-org/BaseCore'
s.license = { :type => 'MIT' }
s.author = { 'Your Name' => 'your@email.com' }
s.source = { :git => 'https://github.com/your-org/BaseCore.git', :tag => s.version }
s.ios.deployment_target = '15.0'
s.source_files = 'Sources/BaseCore/**/*.swift'
end
// CapabilityPush/Package.swift
let package = Package(
name: "CapabilityPush",
products: [
.library(
name: "CapabilityPush", // ✅ 定义为 library,可以打包
targets: ["CapabilityPush"]
)
],
dependencies: [
.package(path: "../BaseCore") // 依赖 BaseCore
],
// ...
)
cd /Users/Zhuanz/Documents/code/xindazhou/xdz_ios/CapabilityPush
# 编译测试
swift build
# 运行测试
swift test
cd /Users/Zhuanz/Documents/code/xindazhou/xdz_ios/CapabilityPush
# 1. 初始化 Git(如果还没有)
git init
git add .
git commit -m "Initial commit: CapabilityPush 1.0.0"
# 2. 添加远程仓库
git remote add origin https://github.com/your-org/CapabilityPush.git
# 3. 打标签并推送
git tag 1.0.0
git push origin main
git push origin 1.0.0
// 其他项目的 Package.swift
dependencies: [
.package(url: "https://github.com/your-org/CapabilityPush.git", from: "1.0.0"),
.package(url: "https://github.com/your-org/BaseCore.git", from: "1.0.0")
]
Package.swift 中定义了 .library productswift buildswift testimport CapabilityPush创建 build_sdk.sh:
#!/bin/bash
# 快速打包 SDK 脚本
PACKAGE_NAME=$1
VERSION=$2
if [ -z "$PACKAGE_NAME" ] || [ -z "$VERSION" ]; then
echo "用法: ./build_sdk.sh <PackageName> <Version>"
echo "示例: ./build_sdk.sh CapabilityPush 1.0.0"
exit 1
fi
cd "$PACKAGE_NAME"
echo "=== 打包 $PACKAGE_NAME v$VERSION ==="
# 1. 编译
echo "1. 编译..."
swift build
if [ $? -ne 0 ]; then
echo "❌ 编译失败"
exit 1
fi
# 2. 测试
echo "2. 运行测试..."
swift test
if [ $? -ne 0 ]; then
echo "❌ 测试失败"
exit 1
fi
# 3. 打标签(如果使用 Git)
if [ -d ".git" ]; then
echo "3. 创建 Git 标签..."
git tag "$VERSION"
git push origin main
git push origin "$VERSION"
echo "✅ 标签已创建: $VERSION"
fi
echo "✅ $PACKAGE_NAME v$VERSION 打包完成!"
使用方法:
chmod +x build_sdk.sh
./build_sdk.sh CapabilityPush 1.0.0
版本管理:使用语义化版本(Semantic Versioning)
依赖管理:
文档:每个 SDK 都应该有:
测试:确保每个 SDK 都有完整的测试
所有模块都可以打包为 SDK:
推荐打包方式: