-
Notifications
You must be signed in to change notification settings - Fork 5
/
initKmm.kts
executable file
·126 lines (105 loc) · 4.23 KB
/
initKmm.kts
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
#!/bin/bash
//usr/bin/env echo '
/**** BOOTSTRAP kscript ****\'>/dev/null
command -v kscript >/dev/null 2>&1 || curl -L "https://git.io/fpF1K" | bash 1>&2
exec kscript $0 "$@"
\*** IMPORTANT: Any code including imports and annotations must come after this line ***/
import java.io.File
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
//// APP
val (appName, packageName) = getAppNameAndPackage()
renamePackagesInAndroidApp(packageName)
renamePackagesInShared(packageName)
// rename rootProject.name in settings.gradle.kts
renameTextInPath("template-kit/settings.gradle.kts", "template-kit", appName)
renameRootFolder(appName)
println("KMM App: $appName created!")
//// END APP
// region functions
fun renameRootFolder(appName: String) {
moveFilesToPath("template-kit", appName)
}
fun renamePackagesInAndroidApp(packageName: String) {
// move androidApp to new package
val packagePath = packageName.replace('.', '/')
moveFilesToPath("template-kit/androidApp/src/main/java/com/rudolfhladik/kmm/template/android", "template-kit/androidApp/src/main/kotlin/${packagePath}/android")
val androidAppJavaFolder = File("template-kit/androidApp/src/main/java")
androidAppJavaFolder.deleteRecursively()
// rename package and imports
renameTextInFilePath("template-kit/androidApp/","com.rudolfhladik.kmm.template", packageName)
}
fun renamePackagesInShared(packageName: String) {
val packagePath = packageName.replace('.', '/')
val targets = listOf(
"androidMain",
"androidTest",
"commonMain",
"commonTest",
"iosMain",
"iosTest"
)
val temp = "temp"
targets.forEach {
val sourcePackage = getSourceSharedPackageForTarget(it)
moveFilesToPath(sourcePackage, temp)
val sourceFile = File("template-kit/shared/src/${it}/kotlin/com")
sourceFile.deleteRecursively()
val targetPackage = getNewSharedPackageForTarget(it, packagePath)
moveFilesToPath(temp, targetPackage)
val tempFolder = File(temp)
tempFolder.deleteRecursively()
}
// move SQL entity to correct package
val sqlSource = "template-kit/shared/src/commonMain/sqldelight/com/rudolfhladik/kmm/template"
moveFilesToPath(sqlSource, temp)
val sourceFile = File("template-kit/shared/src/commonMain/sqldelight/com")
sourceFile.deleteRecursively()
val targetPackage = "template-kit/shared/src/commonMain/sqldelight/${packagePath}"
moveFilesToPath(temp, targetPackage)
val tempFolder = File(temp)
tempFolder.deleteRecursively()
// renamePackagesInSharedClasses(packageName)
renameTextInFilePath("template-kit/shared/", "com.rudolfhladik.kmm.template", packageName)
}
fun renameTextInFilePath(path: String, oldText: String, newText: String) {
val f = File(path)
f.walk().forEach {
if (it.isDirectory.not()) {
renameTextInPath(it.path,oldText, newText)
}
}
}
fun getSourceSharedPackageForTarget(target: String): String {
return "template-kit/shared/src/${target}/kotlin/com/rudolfhladik/kmm/template"
}
fun getNewSharedPackageForTarget(target: String, packagePath: String): String {
return "template-kit/shared/src/${target}/kotlin/${packagePath}"
}
fun renameTextInPath(path: String, oldText: String, newText: String) {
val path: Path = Paths.get(path)
val charset = StandardCharsets.UTF_8
var content = String(Files.readAllBytes(path), charset)
content = content.replace(oldText.toRegex(), newText)
Files.write(path, content.toByteArray(charset))
}
fun moveFilesToPath(from: String, to: String) {
val f = File(to)
f.mkdirs()
val sourcePath = Paths.get(from)
val targetPath = Paths.get(to)
Files.move(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING)
}
fun getAppNameAndPackage(): Pair<String, String> {
println("Let's create KMM app")
print("Enter app name: ")
val appName: String = readLine() ?: error("You need to enter name")
println("Enter app package name")
println("e.g. com.example.test")
print("Package name: ")
val packageName = readLine() ?: error("You need to enter package name")
return appName to packageName
}