@@ -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,28 +26,28 @@ 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)
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