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

com.likethesalad.placeholder.data.storage.AndroidFilesProvider.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.data.storage

import com.likethesalad.placeholder.data.helpers.AndroidVariantHelper
import com.likethesalad.placeholder.models.raw.FlavorValuesRawFiles
import com.likethesalad.placeholder.models.raw.MainValuesRawFiles
import com.likethesalad.placeholder.models.raw.RawFiles
import java.io.File

class AndroidFilesProvider(
    private val androidVariantHelper: AndroidVariantHelper
) : FilesProvider {

    companion object {
        const val TEMPLATES_FOLDER_NAME = "templates"
        const val STRINGS_FOLDER_NAME = "strings"
        const val OUTPUT_RESOLVED_FILE_NAME = "resolved.xml"

        const val GATHERED_STRINGS_FILE_NAME = "strings"
        const val TEMPLATES_FILE_NAME = "templates"
        const val VALUES_FOLDER_NAME = "values"
        val RAW_VALUES_FILE_REGEX = Regex("^(?!resolved\\.xml)[A-Za-z0-9_]+\\.xml\$")
        val VALUES_FOLDER_REGEX = Regex("values(-[a-z]{2}(-r[A-Z]{2})*)*")
        val STRINGS_SUFFIX_REGEX = Regex("strings(-[a-zA-Z-]+)*")
        val TEMPLATES_SUFFIX_REGEX = Regex("templates(-[a-zA-Z-]+)*")
    }

    override fun getResolvedFile(suffix: String): File {
        val resourcesDirs = androidVariantHelper.resourceDirs
        val folder = if (resourcesDirs.hasFlavorDirs) {
            File(resourcesDirs.flavorDirs.first().absolutePath + "/" + "$VALUES_FOLDER_NAME$suffix")
        } else {
            File(resourcesDirs.mainDirs.first().absolutePath + "/" + "$VALUES_FOLDER_NAME$suffix")
        }
        if (!folder.exists()) {
            folder.mkdirs()
        }
        return File(folder, OUTPUT_RESOLVED_FILE_NAME)
    }

    override fun getAllExpectedResolvedFiles(): List {
        val resolvedFiles = mutableListOf()
        for (template in getAllTemplatesFiles()) {
            val suffix = TEMPLATES_SUFFIX_REGEX.find(template.name)!!.groupValues[1]
            resolvedFiles.add(getResolvedFile(suffix))
        }
        return resolvedFiles
    }

    override fun getGatheredStringsFile(suffix: String): File {
        return File(
            getGatheredStringsFolder(),
            "$GATHERED_STRINGS_FILE_NAME$suffix.json"
        )
    }

    override fun getAllGatheredStringsFiles(): List {
        return getGatheredStringsFolder().listFiles()?.toList() ?: emptyList()
    }

    override fun getTemplateFile(suffix: String): File {
        return File(
            getTemplatesFolder(),
            "$TEMPLATES_FILE_NAME$suffix.json"
        )
    }

    private fun getTemplatesFolder(): File {
        val folder = File(
            "${androidVariantHelper.incrementalDir}/$TEMPLATES_FOLDER_NAME"
        )
        if (!folder.exists()) {
            folder.mkdirs()
        }
        return folder
    }

    override fun getAllTemplatesFiles(): List {
        return getTemplatesFolder().listFiles()?.toList() ?: emptyList()
    }

    override fun getAllExpectedTemplatesFiles(): List {
        return getAllGatheredStringsFiles().map {
            getTemplateFile(STRINGS_SUFFIX_REGEX.find(it.name)!!.groupValues[1])
        }
    }

    override fun getRawResourcesFilesForFolder(valuesFolderName: String): RawFiles {
        val resourcesDirs = androidVariantHelper.resourceDirs
        val suffix = VALUES_FOLDER_REGEX.find(valuesFolderName)!!.groupValues[1]
        return if (androidVariantHelper.isFlavored) {
            getFlavorRawFiles(suffix, resourcesDirs.mainDirs, resourcesDirs.flavorDirs)
        } else {
            getMainRawFiles(suffix, resourcesDirs.mainDirs)
        }
    }

    override fun getAllFoldersRawResourcesFiles(): List {
        val rawFilesModels = mutableListOf()
        for (folderName in getValuesFolderNames()) {
            rawFilesModels.add(getRawResourcesFilesForFolder(folderName))
        }
        return rawFilesModels
    }

    private fun getFlavorRawFiles(
        suffix: String,
        mainResDirs: Set,
        flavorResDirs: Set
    ): FlavorValuesRawFiles {
        return FlavorValuesRawFiles(
            androidVariantHelper.flavor,
            suffix,
            getResourcesFiles(suffix, mainResDirs),
            getResourcesFiles(suffix, flavorResDirs)
        )
    }

    private fun getMainRawFiles(suffix: String, mainResDirs: Set): MainValuesRawFiles {
        return MainValuesRawFiles(
            suffix,
            getResourcesFiles(suffix, mainResDirs)
        )
    }

    private fun getValuesFolderNames(): Set {
        val valuesFoldersNames = mutableSetOf()
        val resDirs = androidVariantHelper.resourceDirs
        val mainDirsValueFiles = getAllValuesFolders(resDirs.mainDirs)
        valuesFoldersNames.addAll(mainDirsValueFiles.map { it.name })

        if (resDirs.hasFlavorDirs) {
            val flavorDirsValueFiles = getAllValuesFolders(resDirs.flavorDirs)
            valuesFoldersNames.addAll(flavorDirsValueFiles.map { it.name })
        }

        return valuesFoldersNames
    }

    private fun getResourcesFiles(suffix: String, resourcesDirs: Set): List {
        val valuesFolders = getValuesFolders(suffix, resourcesDirs)
        return valuesFolders.map {
            it.listFiles { _, name ->
                RAW_VALUES_FILE_REGEX.matches(name)
            }?.toList() ?: emptyList()
        }.flatten()
    }

    private fun getValuesFolders(suffix: String, resourcesDirs: Set): List {
        val valuesFolderName = VALUES_FOLDER_NAME + suffix
        return resourcesDirs.map {
            it.listFiles { _, name ->
                name == valuesFolderName
            }?.toList() ?: emptyList()
        }.flatten()
    }

    private fun getAllValuesFolders(resourcesDirs: Set): List {
        return resourcesDirs.map {
            it.listFiles { _, name ->
                VALUES_FOLDER_REGEX.matches(name)
            }?.toList() ?: emptyList()
        }.flatten()
    }

    private fun getGatheredStringsFolder(): File {
        val folder = File(
            "${androidVariantHelper.incrementalDir}/$STRINGS_FOLDER_NAME"
        )
        if (!folder.exists()) {
            folder.mkdirs()
        }
        return folder
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy