一个自以为是的 SwiftUI 项目模板

适用于Tuist的SwiftUI模板

Tuist 是管理 Swift 项目的好工具。 但我仍然需要编写很多代码来配置我的项目。 所以我决定分享我的配置。 是我对 的 opionated 配置。SwiftUItemplateTuist

安装

  1. 通过以下方式创建您的第一个项目tuist init --platform ios
  2. 打开 ,将其更改为Tuist/Config.swift

import ProjectDescription

let config = Config(
    plugins: [
        .git(url: "https://github.com/haifengkao/SwiftUITemplate", branch: "main")
    ]
)
  1. 在终端中运行下载tuist fetchSwiftUITemplate

如果上述过程不起作用,请查看Tuist文档以获取更多信息。

用法

SwiftUITemplate遵循微特征架构

应用程序由多个微功能组成。每个包含microfeature

  • 用于演示功能的示例目标
  • 用于测试功能的单元测试目标
  • 实现该功能的框架代码

可能是或快速包(不支持Cocoapods和Carthage,欢迎PR)。modulemicrofeature

要添加 ,请创建 。 然后将 添加到 ,例如microfeatureTuist/ProjectDescriptionHelpers/Modules.swiftmicrofeaturemodules

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, uses Nimble and Quick as the unit test framework. It’s mandatory to include and for the unit test placeholder codes to compileSwiftUITemplateNimbleQuick

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 to include all targets(exampleApp, unit test, frameworks) into the generated project.tuist

To include swift packages, we can create a swift package with . The following example adds into ‘s framework traget..packageAlamofireMyApp

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 into because will automatically find in ‘s dependencies.Module.AlamofiremodulesSwiftUITemplateModule.AlamofireModule.MyApp

Tuist requires us to specify swift packages in . So we have to create with the following contentDependencies.swiftTuist/Dependencies.swift

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 and its dependencies.modules

To generate the .xcworkspace

  1. run to download swift packagestuist fetch
  2. run to generate xcworkspacetuist generate

If the above procedure doesn’t work, please check Tuist tutorial for more info.

TargetConfig

TargetConfig has four modes

  • default indicates the target has no resources to include and no dependencies to other modules
  • resourcesOnly indicates the target has resources but no dependencies
  • hasDependencies([Modules]) let you specify the target’s dependencies
  • hasResourcesAndDependencies([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 folder. will find corresponding codes and resources in the predefined subfolders.FeaturesSwiftUITemplate

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:

  1. Clone the project.
  2. cd fixtures/Example1
  3. tuist fetch to install the plugin
  4. tuist edit to add new modules
  5. tuist generate to generate xcworkspace

GitHub

点击跳转