com.likethesalad.placeholder.tasks.actions.ResolvePlaceholdersAction.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.resolver.TemplateResolver
import java.io.File
class ResolvePlaceholdersAction(
private val filesProvider: FilesProvider,
private val resourcesHandler: ResourcesHandler,
private val templateResolver: TemplateResolver
) {
fun getTemplatesFiles(): List {
return filesProvider.getAllTemplatesFiles()
}
fun getResolvedFiles(): List {
return filesProvider.getAllExpectedResolvedFiles()
}
fun resolve() {
for (templateFile in filesProvider.getAllTemplatesFiles()) {
val templatesModel = resourcesHandler.getTemplatesFromFile(templateFile)
val resolvedTemplates = templateResolver.resolveTemplates(templatesModel)
val curatedTemplates =
filterNonTranslatableStringsForLanguage(templatesModel.suffix, resolvedTemplates)
if (curatedTemplates.isNotEmpty()) {
resourcesHandler.saveResolvedStringList(
curatedTemplates,
templatesModel.suffix
)
} else {
// Clean up
resourcesHandler.removeResolvedStringFileIfExists(templatesModel.suffix)
}
}
}
private fun filterNonTranslatableStringsForLanguage(
suffix: String,
resolvedStrings: List
): List {
if (suffix.isNotEmpty()) {
// It is a language specific file
return resolvedStrings.filter { it.translatable }
}
return resolvedStrings
}
}