-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinit_template.main.kts
More file actions
266 lines (228 loc) · 7.62 KB
/
Copy pathinit_template.main.kts
File metadata and controls
266 lines (228 loc) · 7.62 KB
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
#!/usr/bin/env kotlin
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import kotlin.io.path.exists
import kotlin.io.path.extension
import kotlin.io.path.isRegularFile
data class UserInput(
val appName: String,
val androidPackageName: String,
val iosBetaAppIdentifier: String,
)
val templatePackageName = "app.futured.kmptemplate"
val templatePackagePath: Path = Path.of("app/futured/kmptemplate")
val (appName, appPackageName, iosBetaAppId) = readInput()
val appPackagePath = Path.of(appPackageName.replace('.', '/'))
// region Android + KMP + Gradle
renameGradleSubproject("buildSrc", appPackageName)
renameGradleSubproject("baselineprofile", appPackageName)
renameGradleSubproject("androidApp", appPackageName)
renameGradleSubproject("shared/app", appPackageName)
renameGradleSubproject("shared/feature", appPackageName)
renameGradleSubproject("shared/network/graphql", appPackageName)
renameGradleSubproject("shared/network/rest", appPackageName)
renameGradleSubproject("shared/persistence", appPackageName)
renameGradleSubproject("shared/platform", appPackageName)
renameGradleSubproject("shared/resources", appPackageName)
findAndReplaceInFileTree(
parent = Path.of("shared/arkitekt-decompose"),
search = templatePackageName,
replaceWith = appPackageName,
)
findAndReplaceInFileTree(
parent = Path.of("shared/arkitekt-cr-usecases"),
search = templatePackageName,
replaceWith = appPackageName,
)
findAndReplaceInFileTree(
parent = Path.of(".github"),
search = templatePackageName,
replaceWith = appPackageName,
)
findAndReplaceInFile(
file = File("gradle/libs.versions.toml"),
search = templatePackageName,
replaceWith = appPackageName,
)
findAndReplaceInFile(
file = File("build.gradle.kts"),
search = templatePackageName,
replaceWith = appPackageName,
)
findAndReplaceInFile(
file = File("settings.gradle.kts"),
search = "KMP_Futured_template",
replaceWith = appName,
)
// endregion
// region XCode project
updateFastfileEnvVariables(
file = File("iosApp/fastlane/Fastfile"),
varName = "APP_IDENTIFIER",
newValue = iosBetaAppId,
)
updateFastfileEnvVariables(
file = File("iosApp/fastlane/Fastfile"),
varName = "APP_NAME",
newValue = appName,
)
updateFastfileEnvVariables(
file = File("iosApp/fastlane/Fastfile"),
varName = "APP_SCHEME",
newValue = appName,
)
findAndReplaceInFile(
file = File("iosApp/iosApp.xcodeproj/project.pbxproj"),
search = "orgIdentifier.iosApp",
replaceWith = appPackageName,
)
findAndReplaceInFile(
file = File("iosApp/iosApp.xcodeproj/project.pbxproj"),
search = "orgIdentifier.iosApp.iosAppTests",
replaceWith = "${appPackageName}.${appName}Test",
)
findAndReplaceInFile(
file = File("iosApp/iosApp.xcodeproj/project.pbxproj"),
search = "orgIdentifier.iosApp.iosAppUITests",
replaceWith = "${appPackageName}.${appName}UITest",
)
findAndReplaceInFileTree(parent = Path.of("iosApp/iosApp.xcodeproj"), search = "iosApp", replaceWith = appName)
moveFileTree(
parent = Path.of("iosApp"),
fromPath = Path.of("iosApp"),
toPath = Path.of(appName),
)
moveFileTree(
parent = Path.of("iosApp"),
fromPath = Path.of("iosApp.xcodeproj"),
toPath = Path.of("$appName.xcodeproj"),
)
Files.move(
File("iosApp/iosAppTests/AppTests.swift").toPath(),
File("iosApp/iosAppTests/${appName}Tests.swift").toPath(),
)
moveFileTree(
parent = Path.of("iosApp"),
fromPath = Path.of("iosAppTests"),
toPath = Path.of("${appName}Tests"),
)
// endregion
// region Repo
File("LICENSE").delete()
File("init_template.main.kts").delete()
File("init_template.sh").delete()
if (confirmBuild()) {
ProcessBuilder("./gradlew", "assembleKMPDebugXCFramework").inheritIO().start().waitFor()
}
// endregion
// region Functions
fun renameGradleSubproject(subproject: String, appPackage: String) {
val sourceSets = listOf(
Path.of("$subproject/src/main/kotlin"),
Path.of("$subproject/src/commonMain/kotlin"),
Path.of("$subproject/src/commonTest/kotlin"),
Path.of("$subproject/src/androidMain/kotlin"),
Path.of("$subproject/src/iosMain/kotlin"),
)
for (sourceSet in sourceSets) {
moveFileTree(
parent = sourceSet,
fromPath = templatePackagePath,
toPath = appPackagePath,
)
}
findAndReplaceInFileTree(
parent = Path.of(subproject),
search = templatePackageName,
replaceWith = appPackage,
extensionFilter = { extension -> extension == "kt" || extension == "kts" },
)
}
fun findAndReplaceInFileTree(
parent: Path,
search: String,
replaceWith: String,
extensionFilter: (extension: String) -> Boolean = { true },
) {
if (parent.exists().not()) {
return
}
Files.walk(parent)
.filter { path -> path.isRegularFile() }
.filter { path -> extensionFilter(path.extension) }
.map { it.toFile() }
.forEach { file ->
with(file) {
val text = readText(Charsets.UTF_8)
val shouldOverwrite = text.contains(search)
if (shouldOverwrite) {
writeText(text.replace(search, replaceWith))
}
}
}
}
fun findAndReplaceInFile(
file: File,
search: String,
replaceWith: String,
) {
if (file.exists().not()) {
return
}
with(file) { writeText(readText(Charsets.UTF_8).replace(search, replaceWith)) }
}
fun moveFileTree(parent: Path, fromPath: Path, toPath: Path) {
if (parent.exists().not()) {
return
}
val sourcePath = parent.resolve(fromPath)
val targetPath = parent.resolve(toPath)
Files.walk(parent.resolve(fromPath))
.filter { path -> path.isRegularFile() }
.forEach { source ->
val target = targetPath.resolve(sourcePath.relativize(source))
Files.createDirectories(target.parent)
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING)
}
}
fun updateFastfileEnvVariables(file: File, varName: String, newValue: String) {
if (!file.exists()) {
println("File does not exist: $file")
return
}
var content = file.readText()
// Look for the pattern ENV['VAR_NAME'] = 'var_value' and change 'var_value'
val envVarPattern = "ENV\\['$varName'] = '(.*)'".toRegex()
if (envVarPattern.containsMatchIn(content)) {
// The variable is found in the file - replace the old value with the new value
content = content.replace(envVarPattern, "ENV['$varName'] = '$newValue'")
file.writeText(content)
} else {
println("Variable $varName was not found in Fastfile.")
}
}
fun readInput(): UserInput {
print("Project name: ")
val appName: String = readlnOrNull()
?.takeIf { it.isNotBlank() }
?.replace(" ", "_")
?: error("Invalid name entered")
print("KMP + Android package name (e.g. com.example.test): ")
val packageName = readlnOrNull()
?.takeIf { it.isNotBlank() }
?: error("Invalid package name")
print("iOS Beta app identifier: app.futured.")
val iosBetaAppId = readlnOrNull()
?.takeIf { it.isNotBlank() }
?.let { "app.futured.$it" }
?: error("Invalid app identifier")
return UserInput(appName, packageName, iosBetaAppId)
}
fun confirmBuild(): Boolean {
println()
println("The script will now build Swift Package for the first time.\n(You can skip this, but will need to do later using './gradlew assembleKMPDebugXCFramework')\n\nConfirm [Y/n]: ")
return readlnOrNull()?.trim()?.lowercase() == "y"
}
// endregion