Skip to content

Commit b16ac44

Browse files
Merge pull request #10050 from rafaeltonholo/chore/10047/add-compose-resource-support
chore(resource-mover-cli): add compose resource support
2 parents 3c2f706 + b686ad5 commit b16ac44

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
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: 39 additions & 13 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,34 +26,42 @@ 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)
43-
val keyDeclaration = extractKeyDeclaration(sourceFile, key)
50+
val targetFile = getOrCreateTargetFile(targetPath, sourceFile, isTargetComposeResources)
51+
val originalKeyDeclaration = extractKeyDeclaration(sourceFile, key)
52+
val keyDeclaration = originalKeyDeclaration.let { keyDeclaration ->
53+
if (isTargetComposeResources && keyDeclaration.contains("<xliff:g id")) {
54+
val regex = """(<xliff:g\s+id="[^"]+">)(.*?)(</xliff:g>)""".toRegex()
55+
keyDeclaration.replace(regex, "$2")
56+
} else {
57+
keyDeclaration
58+
}
59+
}
4460

4561
println(" Key declaration: $keyDeclaration")
4662

4763
copyKeyToTarget(targetFile, keyDeclaration, key)
48-
deleteKeyFromSource(sourceFile, keyDeclaration)
64+
deleteKeyFromSource(sourceFile, originalKeyDeclaration)
4965

5066
if (isSourceFileEmpty(sourceFile)) {
5167
println(" Source file is empty: ${sourceFile.path} -> deleting it.")
@@ -134,7 +150,7 @@ class StringResourceMover {
134150
return sourceContent.contains(STRING_CLOSING_TAG).not() && sourceContent.contains(PLURALS_CLOSING_TAG).not()
135151
}
136152

137-
private fun getOrCreateTargetFile(targetPath: File, sourceFile: File): File {
153+
private fun getOrCreateTargetFile(targetPath: File, sourceFile: File, isTargetComposeResources: Boolean): File {
138154
val targetFilePath = targetPath.resolve(sourceFile.parentFile.name)
139155
val targetFile = File(targetFilePath, sourceFile.name)
140156
val targetDirectory = targetFile.parentFile
@@ -145,20 +161,22 @@ class StringResourceMover {
145161
}
146162

147163
if (!targetFile.exists()) {
148-
createTargetFile(targetFile)
164+
createTargetFile(targetFile, isTargetComposeResources)
149165
}
150166

151167
return targetFile
152168
}
153169

154-
private fun createTargetFile(targetFile: File) {
170+
private fun createTargetFile(targetFile: File, isTargetComposeResources: Boolean) {
155171
val isNewFileCreated: Boolean = targetFile.createNewFile()
156172
if (!isNewFileCreated) {
157173
printError("Target file could not be created: ${targetFile.path}")
158174
exitProcess(-1)
159175
}
160176

161-
targetFile.writeText(TARGET_FILE_CONTENT)
177+
targetFile.writeText(
178+
if (isTargetComposeResources) TARGET_FILE_CONTENT_COMPOSE_RESOURCE else TARGET_FILE_CONTENT,
179+
)
162180
println("Target file ${targetFile.path} created")
163181
}
164182

@@ -168,6 +186,7 @@ class StringResourceMover {
168186

169187
private companion object {
170188
const val RESOURCE_PATH = "/src/main/res/"
189+
const val COMPOSE_RESOURCE_PATH = "/src/commonMain/composeResources/"
171190
const val KEY_PLACEHOLDER = "{KEY}"
172191
const val KEY_PATTERN = """name="$KEY_PLACEHOLDER""""
173192
const val VALUES_PATH = "values"
@@ -182,5 +201,12 @@ class StringResourceMover {
182201
</resources>
183202
184203
""".trimIndent()
204+
205+
val TARGET_FILE_CONTENT_COMPOSE_RESOURCE = """
206+
<?xml version="1.0" encoding="UTF-8"?>
207+
<resources>
208+
</resources>
209+
210+
""".trimIndent()
185211
}
186212
}

0 commit comments

Comments
 (0)