-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
160 lines (138 loc) · 5.09 KB
/
build.gradle.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
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
import java.nio.file.Files
import java.nio.file.Path
import java.io.IOException
plugins {
alias(libs.plugins.ktfmt)
alias(libs.plugins.kover)
alias(libs.plugins.taskinfo)
alias(libs.plugins.compose.multiplatform) apply false
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.kotlin.cocoapods) apply false
}
allprojects {
repositories {
google()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
}
subprojects {
group = properties["group"]!!
version = properties["version"]!!
plugins.withType<com.ncorti.ktfmt.gradle.KtfmtPlugin>().configureEach {
ktfmt {
kotlinLangStyle()
}
}
}
koverReport {
defaults {}
}
/*
* artifactName=kmp-template
camelCaseName=KmpTemplate
titleName=KMP Template
group=org.chiachat.kmp.template
version=1.0.0
* */
val OLD_PACKAGE_PATH = "org.chiachat.kmp.template".replace('.', File.separatorChar)
val OLD_PACKAGE_DIRECTIVE = "org.chiachat.kmp.template"
val OLD_ARTIFACT = "kmp-template"
val OLD_CAMEL_CASE = "KmpTemplate"
val OLD_TITLE = "KMP Template"
val OLD_FILE_NAME = "KmpTemplate"
val EXCLUDED_PATHS = setOf(".git", ".gradle")
tasks.register("updateProperties") {
doLast {
val newGroupPath = getProperty("group").replace('.', File.separatorChar)
val newPackageDirective = getProperty("group")
val artifactName = getProperty("artifactName")
val camelCaseName = getProperty("camelCaseName")
val titleName = getProperty("titleName")
// Filter out unnecessary files
val filesToProcess = project.rootDir.walk()
.filterNot { it.path.containsAny(EXCLUDED_PATHS) }
.filterNot { it.extension == "kts" }
.toList()
// Process each file
filesToProcess.forEach { file ->
if (file.isFile) {
updateFile(file, OLD_PACKAGE_DIRECTIVE, newPackageDirective, artifactName, camelCaseName, titleName)
}
if (file.path.contains(OLD_PACKAGE_PATH)) {
moveFile(file, OLD_PACKAGE_PATH, newGroupPath)
}
}
// Remove old empty directories
removeEmptyDirs(project.rootDir.toPath())
}
}
fun updateFile(
file: File,
oldPackageDirective: String,
newPackageDirective: String,
artifactName: String,
camelCaseName: String,
titleName: String
) {
try {
val text = file.readText()
val updatedForPackages = updatePackageDirectives(text, oldPackageDirective, newPackageDirective)
val updatedForNames = updateNames(updatedForPackages, artifactName, camelCaseName, titleName)
val updatedForFilePaths = updateFilePathReferences(updatedForNames, newPackageDirective)
if (text != updatedForFilePaths) {
println("Updating content in file ${file.path}")
file.writeText(updatedForFilePaths)
}
} catch (e: IOException) {
println("WARNING: Couldn't update ${file.path}. Possible reason: File might be in use or other IO issues. Error: ${e.message}")
}
}
fun updatePackageDirectives(text: String, oldPackageDirective: String, newPackageDirective: String): String {
return text.replace(oldPackageDirective, newPackageDirective)
}
fun updateNames(text: String, artifactName: String, camelCaseName: String, titleName: String): String {
return text
.replace(OLD_ARTIFACT, artifactName)
.replace(OLD_CAMEL_CASE, camelCaseName)
.replace(OLD_TITLE, titleName)
.replace(OLD_FILE_NAME, camelCaseName)
}
fun updateFilePathReferences(text: String, newPackageDirective: String): String {
return text.replace(OLD_PACKAGE_PATH, newPackageDirective.replace('.', File.separatorChar))
}
fun getProperty(propertyName: String): String {
return properties[propertyName]?.toString()
?: error("$propertyName not specified in gradle.properties")
}
fun moveFile(file: File, old: String, new: String) {
val newFilePath = file.path.replace(old, new)
val newFile = File(newFilePath)
try {
newFile.parentFile.mkdirs()
println("Moving file ${file.path} to $newFilePath")
Files.move(file.toPath(), newFile.toPath())
} catch (e: IOException) {
println("WARNING: Couldn't move ${file.path}. Possible reason: File might be in use or other IO issues. Error: ${e.message}")
}
}
fun removeEmptyDirs(path: Path) {
try {
if (Files.isDirectory(path)) {
Files.list(path).forEach { removeEmptyDirs(it) }
Files.newDirectoryStream(path).use {
if (!it.iterator().hasNext()) {
Files.delete(path)
println("Deleted empty directory $path")
}
}
}
} catch (e: IOException) {
println("WARNING: Couldn't delete directory $path. Possible reason: Directory might be in use or other IO issues. Error: ${e.message}")
}
}
fun String.containsAny(paths: Set<String>): Boolean {
return paths.any { this.contains(it) }
}