-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.swift
94 lines (80 loc) · 2.42 KB
/
generate.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
#!/usr/bin/env swift
import Foundation
let sharedSourcePaths: [String] = [
"FulliOS/Sources/Common/**",
"FulliOS/Sources/Components/**",
"FulliOS/Sources/Constants/**",
"FulliOS/Sources/Generated/**",
"FulliOS/Sources/FulliOSApp.swift"
]
let swiftToolsVersion = "5.7"
let iOSDeploymentTarget = "16.0"
guard CommandLine.arguments.count > 1 else {
print("Usage: ./generate.swift <AppName>")
exit(1)
}
let appName = CommandLine.arguments[1]
let subAppPath = "FulliOS/Sources/Apps/\(appName)"
guard FileManager.default.fileExists(atPath: subAppPath) else {
print("❌ The folder `\(subAppPath)` does not exist.")
exit(1)
}
let projectSwiftContent = """
import ProjectDescription
let projectName = "\(appName)"
let dependencies: [TargetDependency] = [
.external(name: "Alamofire")
]
let project = Project(
name: projectName,
organizationName: "MyOrg",
packages: [
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.6.0")
],
targets: [
Target(
name: projectName,
platform: .iOS,
product: .app,
bundleId: "com.example.\(appName.lowercased())",
deploymentTarget: .iOS(targetVersion: "\(iOSDeploymentTarget)", devices: [.iphone, .ipad]),
infoPlist: .default,
sources: [
"\(subAppPath)/**"
] + [
\(sharedSourcePaths.map { "\"\($0)\"" }.joined(separator: ", "))
],
resources: [
"\(subAppPath)/**/*.xcassets",
"\(subAppPath)/**/*.xib",
"\(subAppPath)/**/*.storyboard"
],
dependencies: dependencies
)
]
)
"""
let projectFilePath = "Project.swift"
do {
print("🔧 Generating `Project.swift` for app: \(appName)")
try projectSwiftContent.write(toFile: projectFilePath, atomically: true, encoding: .utf8)
} catch {
print("❌ Failed to write \(projectFilePath): \(error)")
exit(1)
}
let genResult = runShellCommand("tuist generate")
guard genResult == 0 else {
print("❌ tuist generate failed.")
exit(1)
}
_ = runShellCommand("open \(appName).xcworkspace")
@discardableResult
func runShellCommand(_ command: String) -> Int32 {
print(">> \(command)")
let task = Process()
task.launchPath = "/bin/bash"
task.arguments = ["-c", command]
task.launch()
task.waitUntilExit()
return task.terminationStatus
}