@@ -7,8 +7,16 @@ import kotlin.system.exitProcess
77class 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 (" \n Source path does not exist: $sourcePath \n " )
@@ -18,34 +26,42 @@ class StringResourceMover {
1826 println (" \n Moving 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 (" \n Moving 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 (" \n Found 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