An opinionated SwiftUI project template for tuist
SwiftUI template for Tuist
Tuist is a great tool to manage Swift projects.
But I still have to write a lot of codes to config my project.
So I decide to share my configuration. SwiftUItemplate is my opionionated configuration for Tuist.
Install
- create your first project by
tuist init --platform ios - open
Tuist/Config.swift, change it to
import ProjectDescription
let config = Config(
plugins: [
.git(url: "https://github.com/haifengkao/SwiftUITemplate", branch: "main")
]
)
- run
tuist fetchin terminal to downloadSwiftUITemplate
If the above procedure doesn’t work, please check Tuist documentation for more info.
Usage
SwiftUITemplate follows microfeature architecture.
An app is composed by mutiple microfeatures. Each microfeature contains
- example target to deomstrate the feature
- unit test target to test the feature
- framework codes which implements the feature
A module might be microfeature or a swift package (Cocoapods and Carthage are not supported, PR welcome).
To add a microfeature, create Tuist/ProjectDescriptionHelpers/Modules.swift.
Then add the microfeature to modules, e.g.
import ProjectDescription
import SwiftUITemplate
private extension Module {
static var MyApp: Module {
.uFeature(name: "MyApp", targets: [
.exampleApp: .resourcesOnly,
.unitTests: .default,
.framework: .default,
])
}
}
public let modules: [Module] = [
Module.MyApp,
Module.Quick,
Module.Nimble,
]
By default, SwiftUITemplate uses Nimble and Quick as the unit test framework. It’s mandatory to include Nimble and Quick for the unit test placeholder codes to compile
You can specify project name and the targets in Project.swift.
import ProjectDescription
import ProjectDescriptionHelpers
import SwiftUITemplate
let project: Project = Project(name: "MyApp",
organizationName: "example.SwiftUITemplate",
targets: modules.allProjectTargets)
modules.allProjectTargets tells tuist to include all targets(exampleApp, unit test, frameworks) into the generated project.
To include swift packages, we can create a swift package with .package.
The following example adds Alamofire into MyApp‘s framework traget.
import ProjectDescription
import SwiftUITemplate
private extension Module {
static var MyApp: Module {
.uFeature(name: "MyApp", targets: [
.exampleApp: .resourcesOnly,
.unitTests: .default,
.framework: .hasDependencies([
.Alamofire
]),
])
}
static var Alamofire: Module {
.package(name: "Alamofire", url: "https://github.com/Alamofire/Alamofire", requirement: .upToNextMajor(from: "5.5.0"))
}
}
public let modules: [Module] = [
Module.MyApp,
Module.Quick,
Module.Nimble,
]
We don’t have to add Module.Alamofire into modules because SwiftUITemplate will automatically find Module.Alamofire in Module.MyApp‘s dependencies.
Tuist requires us to specify swift packages in Dependencies.swift.
So we have to create Tuist/Dependencies.swift with the following content
import ProjectDescription
import ProjectDescriptionHelpers
import SwiftUITemplate
let dependencies = Dependencies(
swiftPackageManager: .init(
modules.allSwiftPacakges,
targetSettings: modules.allTargetSettings
),
platforms: [.iOS]
)
SwiftUITemplate will find out all swift packages defined in modules and its dependencies.
To generate the .xcworkspace
- run
tuist fetchto download swift packages - run
tuist generateto generate xcworkspace
If the above procedure doesn’t work, please check Tuist tutorial for more info.
TargetConfig
TargetConfig has four modes
defaultindicates the target has no resources to include and no dependencies to other modulesresourcesOnlyindicates the target has resources but no dependencieshasDependencies([Modules])let you specify the target’s dependencieshasResourcesAndDependencies([Modules])indicates the target has resources and dependencies
File structure
├── Tuist
│ └── (Tuist setting files)
└── Features
├── MyApp
│ ├── Example (example target codes)
│ ├── Tests (unit test target codes)
│ └── Sources (framework codes)
└── MyFeature1
├── Example
├── Tests
└── Sources
All features are located in Features folder. SwiftUITemplate will find corresponding codes and resources in the predefined subfolders.
This plugin provides a command to create a microfeature’s placeholder files (unimplemented).
tuist swiftui create MyFeature1
Contribute
To start working on the project, you can follow the steps below:
- Clone the project.
- cd
fixtures/Example1 tuist fetchto install the plugintuist editto add new modulestuist generateto generate xcworkspace