All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.likethesalad.placeholder.tasks.actions.GatherTemplatesAction.kt Maven / Gradle / Ivy

Go to download

This is a Gradle plugin for Android applications which resolves XML string references in other XML strings.

There is a newer version: 1.3.0
Show newest version
package com.likethesalad.placeholder.tasks.actions

import com.likethesalad.placeholder.data.Constants
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.StringsTemplatesModel
import java.io.File

class GatherTemplatesAction(
    private val filesProvider: FilesProvider,
    private val resourcesHandler: ResourcesHandler
) {
    fun getStringFiles(): List {
        return filesProvider.getAllGatheredStringsFiles()
    }

    fun getTemplatesFiles(): List {
        return filesProvider.getAllExpectedTemplatesFiles()
    }

    fun gatherTemplateStrings() {
        val baseStringsFile = filesProvider.getGatheredStringsFile()
        val baseStrings = resourcesHandler.getGatheredStringsFromFile(baseStringsFile).getMergedStrings()
        for (stringFile in filesProvider.getAllGatheredStringsFiles()) {
            val templates = generateTemplatesForStringFile(stringFile, baseStrings)
            val templatesFile = filesProvider.getTemplateFile(templates.suffix)
            val oldTemplates = resourcesHandler.getTemplatesFromFile(templatesFile)
            if (templates != oldTemplates) {
                // Update the templates file only if needed.
                resourcesHandler.saveTemplatesToFile(templates, templatesFile)
            }
        }
    }

    private fun generateTemplatesForStringFile(
        stringFile: File,
        baseStrings: Map
    ): StringsTemplatesModel {
        val stringsMap = baseStrings.toMutableMap()
        val specificStrings = resourcesHandler.getGatheredStringsFromFile(stringFile)
        for (specificString in specificStrings.getMergedStrings().values) {
            stringsMap[specificString.name] = specificString
        }
        val mergedStrings = stringsMap.values
        val stringTemplates = mergedStrings.filter { Constants.TEMPLATE_STRING_REGEX.containsMatchIn(it.name) }
        val placeholdersResolved = getPlaceholdersResolved(mergedStrings, stringTemplates)

        return StringsTemplatesModel(specificStrings.suffix, stringTemplates, placeholdersResolved)
    }

    private fun getPlaceholdersResolved(
        strings: Collection,
        templates: Collection
    ): Map {
        val stringsMap = stringResourcesToMap(strings)
        val placeholders = templates.map { Constants.PLACEHOLDER_REGEX.findAll(it.content) }
            .flatMap { it.toList().map { m -> m.groupValues[1] } }.toSet()

        val placeholdersResolved = mutableMapOf()

        for (it in placeholders) {
            placeholdersResolved[it] = stringsMap.getValue(it)
        }

        return placeholdersResolved
    }

    private fun stringResourcesToMap(list: Collection): Map {
        val map = mutableMapOf()
        for (it in list) {
            map[it.name] = it.content
        }
        return map
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy