generated from baekteun/Tuist_Modular_Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateModule.swift
313 lines (268 loc) · 9.64 KB
/
GenerateModule.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/swift
import Foundation
func handleSIGINT(_ signal: Int32) {
exit(0)
}
signal(SIGINT, handleSIGINT)
enum LayerType: String {
case feature = "Feature"
case domain = "Domain"
case core = "Core"
case shared = "Shared"
case userInterface = "UserInterface"
}
enum MicroTargetType: String {
case interface = "Interface"
case sources = ""
case testing = "Testing"
case unitTest = "Tests"
case uiTest = "UITests"
case demo = "Demo"
}
struct ModuleInfo {
let moduleName: String
let hasInterface: Bool
let hasTesting: Bool
let hasUnitTests: Bool
let hasUITests: Bool
let hasDemo: Bool
}
let fileManager = FileManager.default
let currentPath = "./"
let bash = Bash()
func registerModuleDependency() {
registerModulePaths()
makeProjectDirectory()
registerXCConfig()
let layerPrefix = layer.rawValue.lowercased()
let moduleEnum = ".\(layerPrefix)(.\(moduleName))"
var targetString = "[\n"
if hasInterface {
makeScaffold(target: .interface)
targetString += "\(tab(2)).interface(module: \(moduleEnum)),\n"
}
targetString += "\(tab(2)).implements(module: \(moduleEnum)"
if hasInterface {
targetString += ", dependencies: [\n\(tab(3)).\(layerPrefix)(target: .\(moduleName), type: .interface)\n\(tab(2))])"
} else {
targetString += ")"
}
if hasTesting {
makeScaffold(target: .testing)
let interfaceDependency = ".\(layerPrefix)(target: .\(moduleName), type: .interface)"
targetString += ",\n\(tab(2)).testing(module: \(moduleEnum), dependencies: [\n\(tab(3))\(interfaceDependency)\n\(tab(2))])"
}
if hasUnitTests {
makeScaffold(target: .unitTest)
targetString += ",\n\(tab(2)).tests(module: \(moduleEnum), dependencies: [\n\(tab(3)).\(layerPrefix)(target: .\(moduleName))\n\(tab(2))])"
}
if hasUITests {
makeScaffold(target: .uiTest)
// TODO: - ui test 타겟 설정 로직 추가
}
if hasDemo {
makeScaffold(target: .demo)
targetString += ",\n\(tab(2)).demo(module: \(moduleEnum), dependencies: [\n\(tab(3)).\(layerPrefix)(target: .\(moduleName))\n\(tab(2))])"
}
targetString += "\n\(tab(1))]"
makeProjectSwift(targetString: targetString)
makeSourceScaffold()
}
func tab(_ count: Int) -> String {
var tabString = ""
for _ in 0..<count {
tabString += " "
}
return tabString
}
func registerModulePaths() {
updateFileContent(
filePath: currentPath + "Plugin/DependencyPlugin/ProjectDescriptionHelpers/ModulePaths.swift",
finding: "enum \(layer.rawValue): String, MicroTargetPathConvertable {\n",
inserting: " case \(moduleName)\n"
)
print("Register \(moduleName) to ModulePaths.swift")
}
func registerXCConfig() {
makeDirectory(path: currentPath + "XCConfig/\(moduleName)")
let xcconfigContent = "#include \"../Shared.xcconfig\""
for configuration in ["DEV", "STAGE", "PROD"] {
writeContentInFile(
path: currentPath + "XCConfig/\(moduleName)/\(configuration).xcconfig",
content: xcconfigContent
)
}
}
func makeDirectory(path: String) {
do {
try fileManager.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil)
} catch {
fatalError("❌ failed to create directory: \(path)")
}
}
func makeDirectories(_ paths: [String]) {
paths.forEach(makeDirectory(path:))
}
func makeProjectSwift(targetString: String) {
let projectSwift = """
import DependencyPlugin
import ProjectDescription
import ProjectDescriptionHelpers
let project = Project.module(
name: ModulePaths.\(layer.rawValue).\(moduleName).rawValue,
targets: \(targetString)
)
"""
writeContentInFile(
path: currentPath + "Projects/\(layer.rawValue)/\(moduleName)/Project.swift",
content: projectSwift
)
}
func makeProjectDirectory() {
makeDirectory(path: currentPath + "Projects/\(layer.rawValue)/\(moduleName)")
}
func makeSourceScaffold() {
_ = try? bash.run(
commandName: "tuist",
arguments: ["scaffold", "Sources", "--name", "\(moduleName)", "--layer", "\(layer.rawValue)"]
)
}
func makeScaffold(target: MicroTargetType) {
_ = try? bash.run(
commandName: "tuist",
arguments: ["scaffold", "\(target.rawValue)", "--name", "\(moduleName)", "--layer", "\(layer.rawValue)"]
)
}
func writeContentInFile(path: String, content: String) {
let fileURL = URL(fileURLWithPath: path)
let data = Data(content.utf8)
try? data.write(to: fileURL)
}
func updateFileContent(
filePath: String,
finding findingString: String,
inserting insertString: String
) {
let fileURL = URL(fileURLWithPath: filePath)
guard let readHandle = try? FileHandle(forReadingFrom: fileURL) else {
fatalError("❌ Failed to find \(filePath)")
}
guard let readData = try? readHandle.readToEnd() else {
fatalError("❌ Failed to find \(filePath)")
}
try? readHandle.close()
guard var fileString = String(data: readData, encoding: .utf8) else { fatalError() }
fileString.insert(contentsOf: insertString, at: fileString.range(of: findingString)?.upperBound ?? fileString.endIndex)
guard let writeHandle = try? FileHandle(forWritingTo: fileURL) else {
fatalError("❌ Failed to find \(filePath)")
}
writeHandle.seek(toFileOffset: 0)
try? writeHandle.write(contentsOf: Data(fileString.utf8))
try? writeHandle.close()
}
func makeModuleInfo() -> ModuleInfo {
print("Enter module name", terminator: " : ")
let moduleInput = readLine()
guard let moduleNameUnwrapping = moduleInput, !moduleNameUnwrapping.isEmpty else {
print("Module name is empty")
exit(1)
}
let moduleName = moduleNameUnwrapping
print("Module name: \(moduleName)\n")
print("This module has a 'Interface' Target? (y\\n, default = n)", terminator: " : ")
let hasInterface = readLine()?.lowercased() == "y"
print("This module has a 'Testing' Target? (y\\n, default = n)", terminator: " : ")
let hasTesting = readLine()?.lowercased() == "y"
print("This module has a 'UnitTests' Target? (y\\n, default = n)", terminator: " : ")
let hasUnitTests = readLine()?.lowercased() == "y"
print("This module has a 'UITests' Target? (y\\n, default = n)", terminator: " : ")
let hasUITests = readLine()?.lowercased() == "y"
print("This module has a 'Demo' Target? (y\\n, default = n)", terminator: " : ")
let hasDemo = readLine()?.lowercased() == "y"
return ModuleInfo(
moduleName: moduleName,
hasInterface: hasInterface,
hasTesting: hasTesting,
hasUnitTests: hasUnitTests,
hasUITests: hasUITests,
hasDemo: hasDemo
)
}
func checkModuleInfo() -> Bool {
print("")
print("------------------------------------------------------------------------------------------------------------------------")
print("Is this the correct module information you are generating? (y\\n, default = y)")
print("Layer: \(layer.rawValue)")
print("Module name: \(moduleName)")
print("interface: \(hasInterface), testing: \(hasTesting), unitTests: \(hasUnitTests), uiTests: \(hasUITests), demo: \(hasDemo)")
print("------------------------------------------------------------------------------------------------------------------------")
guard var checkInput = readLine() else {
exit(1)
}
if checkInput.isEmpty {
checkInput = "y"
}
let isCorrect = checkInput.lowercased() == "y"
return !isCorrect
}
// MARK: - Starting point
print("Enter layer name\n(Feature | Domain | Core | Shared | UserInterface)", terminator: " : ")
let layerInput = readLine()
guard
let layerInput,
!layerInput.isEmpty,
let layerUnwrapping = LayerType(rawValue: layerInput)
else {
print("Layer is empty or invalid")
exit(1)
}
let layer = layerUnwrapping
print("Layer: \(layer.rawValue)\n")
var moduleName: String = ""
var hasInterface: Bool = false
var hasTesting: Bool = false
var hasUnitTests: Bool = false
var hasUITests: Bool = false
var hasDemo: Bool = false
repeat {
let moduleInfo = makeModuleInfo()
moduleName = moduleInfo.moduleName
hasInterface = moduleInfo.hasInterface
hasTesting = moduleInfo.hasTesting
hasUnitTests = moduleInfo.hasUnitTests
hasUITests = moduleInfo.hasUITests
hasDemo = moduleInfo.hasDemo
}
while checkModuleInfo()
registerModuleDependency()
print("✅ Module is created successfully!")
// MARK: - Bash
protocol CommandExecuting {
func run(commandName: String, arguments: [String]) throws -> String
}
enum BashError: Error {
case commandNotFound(name: String)
}
struct Bash: CommandExecuting {
func run(commandName: String, arguments: [String] = []) throws -> String {
return try run(resolve(commandName), with: arguments)
}
private func resolve(_ command: String) throws -> String {
guard var bashCommand = try? run("/bin/bash", with: ["-l", "-c", "which \(command)"]) else {
throw BashError.commandNotFound(name: command)
}
bashCommand = bashCommand.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
return bashCommand
}
private func run(_ command: String, with arguments: [String] = []) throws -> String {
let process = Process()
process.launchPath = command
process.arguments = arguments
let outputPipe = Pipe()
process.standardOutput = outputPipe
process.launch()
let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
let output = String(decoding: outputData, as: UTF8.self)
return output
}
}