Skip to content

Commit 245e12e

Browse files
committed
chore(resource-mover-cli): add compose resource support
1 parent 3c2f706 commit 245e12e

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

cli/resource-mover-cli/src/main/kotlin/net/thunderbird/cli/resource/mover/ResourceMoverCli.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ class ResourceMoverCli(
1212
name = "resource-mover",
1313
) {
1414
private val from: String by option(
15-
help = "Source module path",
15+
help = "Source module path.",
1616
).required()
1717

1818
private val to: String by option(
19-
help = "Target module path",
19+
help = "Target module path. If the target module is a KMP module, ensure the " +
20+
"commonMain/composeResources, otherwise the CLI won't be able to detect it automatically.",
2021
).required()
2122

2223
private val keys: List<String> by option(
23-
help = "Keys to move",
24+
help = "Keys to move. When multiple separate by ','.",
2425
).split(",").required()
2526

2627
override fun help(context: Context): String = "Move string resources from one file to another"

cli/resource-mover-cli/src/main/kotlin/net/thunderbird/cli/resource/mover/StringResourceMover.kt

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ import kotlin.system.exitProcess
77
class StringResourceMover {
88

99
fun moveKeys(source: String, target: String, keys: List<String>) {
10-
val sourcePath = File(source + RESOURCE_PATH)
11-
val targetPath = File(target + RESOURCE_PATH)
10+
fun File.isComposeResource(): Boolean = File(this, COMPOSE_RESOURCE_PATH).exists()
11+
12+
val sourceDir = File(source)
13+
val sourceBaseResourcePath = if (sourceDir.isComposeResource()) COMPOSE_RESOURCE_PATH else RESOURCE_PATH
14+
val sourcePath = File(source + sourceBaseResourcePath)
15+
16+
val targetDir = File(target)
17+
val isTargetComposeResources = targetDir.isComposeResource()
18+
val targetBaseResourcePath = if (isTargetComposeResources) COMPOSE_RESOURCE_PATH else RESOURCE_PATH
19+
val targetPath = File(target + targetBaseResourcePath)
1220

1321
if (!sourcePath.exists()) {
1422
println("\nSource path does not exist: $sourcePath\n")
@@ -18,28 +26,28 @@ class StringResourceMover {
1826
println("\nMoving keys $keys")
1927
println(" from \"$sourcePath\" -> \"$targetPath\"\n")
2028
for (key in keys) {
21-
moveKey(sourcePath, targetPath, key)
29+
moveKey(sourcePath, targetPath, key, isTargetComposeResources)
2230
}
2331
}
2432

25-
private fun moveKey(sourcePath: File, targetPath: File, key: String) {
33+
private fun moveKey(sourcePath: File, targetPath: File, key: String, isTargetComposeResources: Boolean) {
2634
println("\nMoving key: $key\n")
2735

2836
sourcePath.walk()
2937
.filter { it.name.startsWith(VALUES_PATH) }
3038
.forEach { sourceDir ->
3139
val sourceFile = sourceDir.resolve(STRING_RESOURCE_FILE_NAME)
3240
if (sourceFile.exists()) {
33-
moveKeyDeclaration(sourceFile, targetPath, key)
41+
moveKeyDeclaration(sourceFile, targetPath, key, isTargetComposeResources)
3442
}
3543
}
3644
}
3745

38-
private fun moveKeyDeclaration(sourceFile: File, targetPath: File, key: String) {
46+
private fun moveKeyDeclaration(sourceFile: File, targetPath: File, key: String, isTargetComposeResources: Boolean) {
3947
if (containsKey(sourceFile, key)) {
4048
println("\nFound key in file: ${sourceFile.path}\n")
4149

42-
val targetFile = getOrCreateTargetFile(targetPath, sourceFile)
50+
val targetFile = getOrCreateTargetFile(targetPath, sourceFile, isTargetComposeResources)
4351
val keyDeclaration = extractKeyDeclaration(sourceFile, key)
4452

4553
println(" Key declaration: $keyDeclaration")
@@ -134,7 +142,7 @@ class StringResourceMover {
134142
return sourceContent.contains(STRING_CLOSING_TAG).not() && sourceContent.contains(PLURALS_CLOSING_TAG).not()
135143
}
136144

137-
private fun getOrCreateTargetFile(targetPath: File, sourceFile: File): File {
145+
private fun getOrCreateTargetFile(targetPath: File, sourceFile: File, isTargetComposeResources: Boolean): File {
138146
val targetFilePath = targetPath.resolve(sourceFile.parentFile.name)
139147
val targetFile = File(targetFilePath, sourceFile.name)
140148
val targetDirectory = targetFile.parentFile
@@ -145,20 +153,22 @@ class StringResourceMover {
145153
}
146154

147155
if (!targetFile.exists()) {
148-
createTargetFile(targetFile)
156+
createTargetFile(targetFile, isTargetComposeResources)
149157
}
150158

151159
return targetFile
152160
}
153161

154-
private fun createTargetFile(targetFile: File) {
162+
private fun createTargetFile(targetFile: File, isTargetComposeResources: Boolean) {
155163
val isNewFileCreated: Boolean = targetFile.createNewFile()
156164
if (!isNewFileCreated) {
157165
printError("Target file could not be created: ${targetFile.path}")
158166
exitProcess(-1)
159167
}
160168

161-
targetFile.writeText(TARGET_FILE_CONTENT)
169+
targetFile.writeText(
170+
if (isTargetComposeResources) TARGET_FILE_CONTENT_COMPOSE_RESOURCE else TARGET_FILE_CONTENT,
171+
)
162172
println("Target file ${targetFile.path} created")
163173
}
164174

@@ -168,6 +178,7 @@ class StringResourceMover {
168178

169179
private companion object {
170180
const val RESOURCE_PATH = "/src/main/res/"
181+
const val COMPOSE_RESOURCE_PATH = "/src/commonMain/composeResources/"
171182
const val KEY_PLACEHOLDER = "{KEY}"
172183
const val KEY_PATTERN = """name="$KEY_PLACEHOLDER""""
173184
const val VALUES_PATH = "values"
@@ -182,5 +193,12 @@ class StringResourceMover {
182193
</resources>
183194
184195
""".trimIndent()
196+
197+
val TARGET_FILE_CONTENT_COMPOSE_RESOURCE = """
198+
<?xml version="1.0" encoding="UTF-8"?>
199+
<resources>
200+
</resources>
201+
202+
""".trimIndent()
185203
}
186204
}

0 commit comments

Comments
 (0)