/
Project+Templates.swift
223 lines (188 loc) · 9.28 KB
/
Project+Templates.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import ProjectDescription
let reverseOrganizationName = "com.sonomos"
let featuresPath = "Features"
let exampleAppSuffix = "ExampleApp"
let examplePath = "Example"
/// Project helpers are functions that simplify the way you define your project.
/// Share code to create targets, settings, dependencies,
/// Create your own conventions, e.g: a func that makes sure all shared targets are "static frameworks"
/// See https://tuist.io/docs/usage/helpers/
public enum uFeatureTarget {
case framework
case unitTests
case exampleApp
}
public struct Module {
let name: String
let path: String
let frameworkDependancies: [TargetDependency]
let exampleDependencies: [TargetDependency]
let frameworkResources: [String]
let exampleResources: [String]
let testResources: [String]
let targets: Set<uFeatureTarget>
public init(name: String,
path: String,
frameworkDependancies: [TargetDependency],
exampleDependencies: [TargetDependency],
frameworkResources: [String],
exampleResources: [String],
testResources: [String],
targets: Set<uFeatureTarget> = Set([.framework, .unitTests, .exampleApp])) {
self.name = name
self.path = path
self.frameworkDependancies = frameworkDependancies
self.exampleDependencies = exampleDependencies
self.frameworkResources = frameworkResources
self.exampleResources = exampleResources
self.testResources = testResources
self.targets = targets
}
}
extension Project {
/// Helper function to create the Project for this ExampleApp
public static func app(name: String,
platform: Platform,
packages: [Package],
targetDependancies: [TargetDependency],
moduleTargets: [Module]) -> Project {
let organizationName = "Sonomos.com"
var dependencies = moduleTargets.map { TargetDependency.target(name: $0.name) }
dependencies.append(contentsOf: targetDependancies)
var targets = makeAppTargets(name: name,
platform: platform,
dependencies: dependencies)
targets += moduleTargets.flatMap({ makeFrameworkTargets(module: $0, platform: platform) })
let schemes = makeSchemes(targetName: name)
return Project(name: name,
organizationName: organizationName,
packages: packages,
targets: targets,
schemes: schemes)
}
public static func makeAppInfoPlist() -> InfoPlist {
let infoPlist: [String: InfoPlist.Value] = [
"CFBundleShortVersionString": "1.0",
"CFBundleVersion": "1",
"UIMainStoryboardFile": "",
"UILaunchStoryboardName": "LaunchScreen"
]
return InfoPlist.extendingDefault(with: infoPlist)
}
/// Helper function to create a framework target and an associated unit test target
public static func makeFrameworkTargets(module: Module, platform: Platform) -> [Target] {
let frameworkPath = "\(featuresPath)/\(module.path)"
let frameworkResourceFilePaths = module.frameworkResources.map { ResourceFileElement.glob(pattern: Path("\(featuresPath)/\(module.path)/" + $0), tags: [])}
let exampleResourceFilePaths = module.exampleResources.map { ResourceFileElement.glob(pattern: Path("\(featuresPath)/\(module.path)/\(examplePath)/" + $0), tags: [])}
let testResourceFilePaths = module.testResources.map { ResourceFileElement.glob(pattern: Path("\(featuresPath)/\(module.path)/Tests/" + $0), tags: [])}
var exampleAppDependancies = module.exampleDependencies
exampleAppDependancies.append(.target(name: module.name))
let exampleSourcesPath = "\(featuresPath)/\(module.path)/\(examplePath)/Sources"
var targets = [Target]()
if module.targets.contains(.exampleApp) {
targets.append(Target(name: "\(module.name)\(exampleAppSuffix)",
platform: platform,
product: .app,
bundleId: "\(reverseOrganizationName).\(module.name)\(exampleAppSuffix)",
infoPlist: makeAppInfoPlist(),
sources: ["\(exampleSourcesPath)/**"],
resources: ResourceFileElements(resources: exampleResourceFilePaths),
dependencies: exampleAppDependancies))
}
if module.targets.contains(.framework) {
targets.append(Target(name: module.name,
platform: platform,
product: .framework,
bundleId: "\(reverseOrganizationName).\(module.name)",
infoPlist: .default,
sources: ["\(frameworkPath)/Sources/**"],
resources: ResourceFileElements(resources: frameworkResourceFilePaths),
headers: Headers(public: ["\(frameworkPath)/Sources/**/*.h"]),
dependencies: module.frameworkDependancies))
}
if module.targets.contains(.unitTests) {
targets.append(Target(name: "\(module.name)Tests",
platform: platform,
product: .unitTests,
bundleId: "\(reverseOrganizationName).\(module.name)Tests",
infoPlist: .default,
sources: ["\(frameworkPath)/Tests/**"],
resources: ResourceFileElements(resources: testResourceFilePaths),
dependencies: [.target(name: module.name)]))
}
return targets
}
/// Helper function to create the application target and the unit test target.
public static func makeAppTargets(name: String, platform: Platform, dependencies: [TargetDependency]) -> [Target] {
let mainTarget = Target(
name: name,
platform: platform,
product: .app,
bundleId: "\(reverseOrganizationName).\(name)",
infoPlist: makeAppInfoPlist(),
sources: ["\(featuresPath)/\(name)/Sources/**"],
resources: ["\(featuresPath)/\(name)/Resources/**"
],
actions: [
TargetAction.post(path: "scripts/swiftlint.sh", arguments: ["$SRCROOT", "$TARGETNAME"], name: "SwiftLint")
],
dependencies: dependencies
)
let testTarget = Target(
name: "\(name)Tests",
platform: platform,
product: .unitTests,
bundleId: "\(reverseOrganizationName).\(name)Tests",
infoPlist: .default,
sources: ["\(featuresPath)/\(name)/Tests/**"],
resources: ["\(featuresPath)/\(name)/Tests/**/*.json",
"\(featuresPath)/\(name)/Tests/**/*.png"],
dependencies: [
.target(name: "\(name)")
])
let uiTestTarget = Target(
name: "\(name)UITests",
platform: platform,
product: .uiTests,
bundleId: "\(reverseOrganizationName).\(name)UITests",
infoPlist: .default,
sources: ["\(featuresPath)/\(name)/UITests/**"],
resources: [],
dependencies: [
.target(name: "\(name)")
])
return [mainTarget, testTarget, uiTestTarget]
}
public static func makeSchemes(targetName: String) -> [Scheme] {
let mainTargetReference = TargetReference(stringLiteral: targetName)
let debugConfiguration = "Debug"
let coverage = true
let codeCoverageTargets: [TargetReference] = [mainTargetReference]
let buildAction = BuildAction(targets: [mainTargetReference])
let executable = mainTargetReference
let asyncTestingLaunchArguments = Arguments(launchArguments: [LaunchArgument(name: "AsyncTesting", isEnabled: true)])
let uiTestingLaunchArguments = Arguments(launchArguments: [LaunchArgument(name: "UITesting", isEnabled: true)])
let testAction = TestAction(targets: [TestableTarget(stringLiteral: "\(targetName)UITests")],
configurationName: debugConfiguration,
coverage: coverage,
codeCoverageTargets: codeCoverageTargets)
let asyncTestingScheme = Scheme(
name: "\(targetName)AsyncNetworkTesting",
shared: false,
buildAction: buildAction,
runAction: RunAction(configurationName: debugConfiguration,
executable: executable,
arguments: asyncTestingLaunchArguments)
)
let uiTestingScheme = Scheme(
name: "\(targetName)UITesting",
shared: false,
buildAction: buildAction,
testAction: testAction,
runAction: RunAction(configurationName: debugConfiguration,
executable: executable,
arguments: uiTestingLaunchArguments)
)
return [asyncTestingScheme, uiTestingScheme]
}
}