遷移 Xcode 專案
除非你使用 Tuist 建立新專案(這種情況下所有設定都會自動完成),否則你必須使用 Tuist 的原語來定義你的 Xcode 專案。這個過程有多繁瑣,取決於你的專案有多複雜。
如你所知,Xcode 專案隨著時間推移可能會變得雜亂且複雜:群組與目錄結構不符、檔案在多個 target 之間共用,或檔案參照指向不存在的檔案(僅舉幾例)。這些累積的複雜性讓我們難以提供一個能可靠遷移專案的命令。
此外,手動遷移是清理並簡化專案的絕佳練習。不僅專案中的開發人員會因此受益,Xcode 也會因為處理和索引速度更快而受惠。一旦你全面採用 Tuist,它將確保專案定義一致且保持簡潔。
為了減輕這項工作,我們根據從使用者收到的回饋提供一些指引。
建立專案骨架#
首先,使用下列 Tuist 檔案為你的專案建立骨架:
import ProjectDescription
let tuist = Tuist()
import ProjectDescription
let project = Project(
name: "MyApp-Tuist",
targets: [
/** Targets will go here **/
]
)
// swift-tools-version: 5.9
import PackageDescription
#if TUIST
import ProjectDescription
let packageSettings = PackageSettings(
// Customize the product types for specific package product
// Default is .staticFramework
// productTypes: ["Alamofire": .framework,]
productTypes: [:]
)
#endif
let package = Package(
name: "MyApp",
dependencies: [
// Add your own dependencies here:
// .package(url: "https://github.com/Alamofire/Alamofire", from: "5.0.0"),
// You can read more about dependencies here: https://tuist.dev/en/docs/guides/features/projects/dependencies
]
)
Project.swift 是你定義專案的 manifest 檔案,而 Package.swift 是你定義相依性的 manifest 檔案。Tuist.swift 檔案則用於定義專案範圍內的 Tuist 設定。
為了避免與現有的 Xcode 專案衝突,我們建議在專案名稱中加入 -Tuist 後綴。當你完全遷移專案至 Tuist 後,即可移除此後綴。
在 CI 中建置並測試 Tuist 專案#
為了確保每次變更的遷移都有效,我們建議擴充你的持續整合流程,以建置並測試由 manifest 檔案透過 Tuist 產生的專案:
tuist install
tuist generate
xcodebuild build {xcodebuild flags} # or tuist test
將專案建置設定提取到 .xcconfig 檔案中#
將專案的建置設定提取到 .xcconfig 檔案中,讓專案更精簡且更容易遷移。你可以使用以下命令將專案的建置設定提取到 .xcconfig 檔案中:
mkdir -p xcconfigs/
tuist migration settings-to-xcconfig -p MyApp.xcodeproj -x xcconfigs/MyApp-Project.xcconfig
接著更新你的 Project.swift 檔案,使其指向你剛建立的 .xcconfig 檔案:
import ProjectDescription
let project = Project(
name: "MyApp",
settings: .settings(configurations: [
.debug(name: "Debug", xcconfig: "./xcconfigs/MyApp-Project.xcconfig"),
.release(name: "Release", xcconfig: "./xcconfigs/MyApp-Project.xcconfig"),
]),
targets: [
/** Targets will go here **/
]
)
然後擴充你的持續整合管線,執行以下命令以確保對建置設定的變更直接寫入 .xcconfig 檔案:
tuist migration check-empty-settings -p Project.xcodeproj
提取套件相依性#
將你專案的所有相依性提取到 Tuist/Package.swift 檔案中:
// swift-tools-version: 5.9
import PackageDescription
#if TUIST
import ProjectDescription
let packageSettings = PackageSettings(
// Customize the product types for specific package product
// Default is .staticFramework
// productTypes: ["Alamofire": .framework,]
productTypes: [:]
)
#endif
let package = Package(
name: "MyApp",
dependencies: [
// Add your own dependencies here:
// .package(url: "https://github.com/Alamofire/Alamofire", from: "5.0.0"),
// You can read more about dependencies here: https://tuist.dev/en/docs/guides/features/projects/dependencies
.package(url: "https://github.com/onevcat/Kingfisher", .upToNextMajor(from: "7.12.0"))
]
)
你可以將特定套件加入 PackageSettings 結構中的 productTypes 字典,以覆寫該套件的產品類型。預設情況下,Tuist 假設所有套件都是靜態框架。
決定遷移順序#
我們建議從被依賴程度最高的 target 開始遷移,逐步到被依賴程度最低的 target。你可以使用以下命令列出專案的 target,並依相依性數量排序:
tuist migration list-targets -p Project.xcodeproj
從清單頂端開始遷移 target,因為它們是被依賴程度最高的。
遷移 target#
逐一遷移 target。我們建議為每個 target 建立一個 pull request,以確保在合併前經過審查與測試。
將 target 建置設定提取到 .xcconfig 檔案中#
如同處理專案建置設定一樣,將 target 的建置設定提取到 .xcconfig 檔案中,讓 target 更精簡且更容易遷移。你可以使用以下命令將 target 的建置設定提取到 .xcconfig 檔案中:
tuist migration settings-to-xcconfig -p MyApp.xcodeproj -t TargetX -x xcconfigs/TargetX.xcconfig
在 Project.swift 檔案中定義 target#
在 Project.targets 中定義 target:
import ProjectDescription
let project = Project(
name: "MyApp",
settings: .settings(configurations: [
.debug(name: "Debug", xcconfig: "./xcconfigs/Project.xcconfig"),
.release(name: "Release", xcconfig: "./xcconfigs/Project.xcconfig"),
]),
targets: [
.target(
name: "TargetX",
destinations: .iOS,
product: .framework, // or .staticFramework, .staticLibrary...
bundleId: "dev.tuist.targetX",
sources: ["Sources/TargetX/**"],
dependencies: [
/** Dependencies go here **/
/** .external(name: "Kingfisher") **/
/** .target(name: "OtherProjectTarget") **/
],
settings: .settings(configurations: [
.debug(name: "Debug", xcconfig: "./xcconfigs/TargetX.xcconfig"),
.release(name: "Release", xcconfig: "./xcconfigs/TargetX.xcconfig"),
])
),
]
)
如果該 target 有關聯的測試 target,你也應該在 Project.swift 檔案中定義它,並重複相同步驟。
驗證 target 遷移#
執行 tuist generate,接著執行 xcodebuild build 以確保專案可以建置,並執行 tuist test 以確保測試通過。此外,你可以使用 xcdiff 比較產生的 Xcode 專案與現有專案,以確保變更正確無誤。
重複#
重複上述步驟直到所有 target 都完全遷移。完成後,我們建議更新你的 CI 與 CD 管線,使用 tuist generate 接著執行 xcodebuild build 與 tuist test 來建置並測試專案。
疑難排解#
因缺少檔案導致編譯錯誤。#
如果與你的 Xcode 專案 target 關聯的檔案並未全部包含在代表該 target 的檔案系統目錄中,可能會導致專案無法編譯。請確保使用 Tuist 產生專案後的檔案清單與 Xcode 專案中的檔案清單相符,並藉此機會將檔案結構與 target 結構對齊。