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

main.phraseapp.repositories.operations.Downloader.kt Maven / Gradle / Ivy

The newest version!
package phraseapp.repositories.operations

import kotlinx.coroutines.coroutineScope
import phraseapp.internal.platforms.Platform
import phraseapp.internal.printers.FileOperation
import phraseapp.internal.xml.Resource
import phraseapp.network.*
import phraseapp.parsers.xml.DEFAULT_IGNORE_COMMENTS
import phraseapp.repositories.operations.helpers.LocalHelper
import phraseapp.repositories.operations.helpers.PrinterHelper
import phraseapp.repositories.operations.helpers.ReducerHelper

class Downloader(
    platform: Platform,
    buildDir: String,
    fileOperation: FileOperation,
    private val network: PhraseAppNetworkDataSource
) {
    private val localHelper = LocalHelper(platform)
    private val reducerHelper = ReducerHelper(platform)
    private val printerHelper = PrinterHelper(platform, buildDir, fileOperation)

    suspend fun download(
        resFolders: Map>,
        overrideDefaultFile: Boolean = DEFAULT_OVERRIDE_DEFAULT_FILE,
        exceptions: Map = DEFAULT_EXCEPTIONS,
        placeholder: Boolean = DEFAULT_PLACEHOLDER,
        localeNameRegex: String = DEFAULT_REGEX,
        ignoreComments: Boolean = DEFAULT_IGNORE_COMMENTS,
        allowedLocaleCodes: List = DEFAULT_ALLOWED_LOCALE_CODES,
        dontDeleteKeys: Boolean = DEFAULT_DONT_DELETE_KEYS,
    ) = coroutineScope {
        val strings = localHelper.getStringsFileByResFolder(resFolders)
        val locales = network.downloadAllLocales(
            overrideDefaultFile,
            exceptions,
            placeholder,
            localeNameRegex,
            allowedLocaleCodes
        )
        val resources = reducerHelper.reduceKeysForAllStringsFilesAndForAllLocales(
            strings,
            locales,
            ignoreComments,
            dontDeleteKeys
        )
        printerHelper.printResources(resources)
        printerHelper.printLocales(getTypes(resources))
        return@coroutineScope resources
    }

    private fun getTypes(configurations: Map>): List =
        configurations.entries.first().value.keys.toList()
}

sealed class ResFolderType
object DefaultType : ResFolderType()
class LanguageType(val language: String) : ResFolderType()
class LocaleType(val language: String, val country: String) : ResFolderType()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy