跳至内容

创建生成式项目

在空目录或现有项目的目录中运行 tuist init:

bash
mise x tuist@latest -- tuist init
bash
tuist init

该命令将引导你完成创建生成式项目或将现有 Xcode 项目或工作区集成进来的步骤。它帮助你将本地环境与远程服务器连接,让你能够使用选择性测试、previews和registry等功能。

迁移现有项目

如果你希望将现有项目迁移到生成式项目以改善开发者体验并利用我们的缓存功能,请查看我们的迁移指南。

添加依赖#

项目通常依赖第三方库来提供额外功能。为此,请运行以下命令以获得最佳的项目编辑体验:

bash
tuist edit

将打开一个包含你项目文件的 Xcode 项目。编辑 Tuist/Package.swift 并添加 Kingfisher:

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"))
]
)

然后编辑项目中的应用 target,将 Kingfisher 声明为依赖:

swift
import ProjectDescription
let project = Project(
name: "MyApp",
targets: [
.target(
name: "MyApp",
destinations: .iOS,
product: .app,
bundleId: "dev.tuist.MyApp",
infoPlist: .extendingDefault(
with: [
"UILaunchStoryboardName": "LaunchScreen.storyboard",
]
),
buildableFolders: [
"MyApp/Sources",
"MyApp/Resources",
],
dependencies: [
.external(name: "Kingfisher")
]
),
.target(
name: "MyAppTests",
destinations: .iOS,
product: .unitTests,
bundleId: "dev.tuist.MyAppTests",
infoPlist: .default,
sources: ["MyApp/Tests/**"],
resources: [],
dependencies: [.target(name: "MyApp")]
),
]
)

然后运行 tuist install,使用 Swift Package Manager 解析并拉取依赖。

使用 SwiftPM 作为依赖解析器

Tuist 推荐的依赖管理方式仅使用 Swift Package Manager(SPM)来解析依赖。随后,Tuist 会将它们转换为 Xcode 项目和 target,以实现最大程度的可配置性和控制力。

可视化项目结构#

你可以运行以下命令来可视化项目结构:

bash
tuist graph

该命令将在项目目录中输出并打开一个 graph.png 文件:

项目图

使用依赖#

运行 tuist generate 在 Xcode 中打开项目,并对 ContentView.swift 文件进行以下更改:

swift
import SwiftUI
import Kingfisher
public struct ContentView: View {
public init() {}
public var body: some View {
KFImage(URL(string: "https://tuist.dev/images/[email protected]")!)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

从 Xcode 运行应用,你应该能看到从 URL 加载的图片。