com.likethesalad.placeholder.tasks.actions.GatherRawStringsAction.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of string-reference Show documentation
Show all versions of string-reference Show documentation
This is a Gradle plugin for Android applications which resolves XML string references in other XML strings.
package com.likethesalad.placeholder.tasks.actions
import com.likethesalad.placeholder.data.resources.ResourcesHandler
import com.likethesalad.placeholder.data.storage.FilesProvider
import com.likethesalad.placeholder.models.StringResourceModel
import com.likethesalad.placeholder.models.StringsGatheredModel
import com.likethesalad.placeholder.models.raw.FlavorValuesRawFiles
import com.likethesalad.placeholder.models.raw.RawFiles
import com.likethesalad.placeholder.utils.AndroidXmlResDocument
import java.io.File
class GatherRawStringsAction(
private val filesProvider: FilesProvider,
private val resourcesHandler: ResourcesHandler
) {
fun getInputFiles(): List {
return filesProvider.getAllFoldersRawResourcesFiles().map {
it.getRawFilesMetaList().flatten()
}.flatten()
}
fun getOutputFile(): File {
return filesProvider.getGatheredStringsFile()
}
fun gatherStrings() {
for (folderRawFiles in filesProvider.getAllFoldersRawResourcesFiles()) {
gatherStringsForFolder(folderRawFiles)
}
}
private fun gatherStringsForFolder(rawFilesModel: RawFiles) {
val mainStrings = getStringListFromFiles(rawFilesModel.mainValuesRawFiles)
val complimentaryStrings: List> = when (rawFilesModel) {
is FlavorValuesRawFiles -> listOf(getStringListFromFiles(rawFilesModel.complimentaryRawFiles))
else -> listOf()
}
if (mainStrings.isNotEmpty() || complimentaryStrings.isNotEmpty()) {
resourcesHandler.saveGatheredStrings(
StringsGatheredModel(
rawFilesModel.suffix,
mainStrings,
complimentaryStrings
)
)
}
}
private fun getStringListFromFiles(files: List): List {
return files.map {
AndroidXmlResDocument.readFromFile(it)
}.flatMap { it.getStringResourceList() }
}
}