From 4291cc8eb1f4385528afcc9cab2592c8b3afa44a Mon Sep 17 00:00:00 2001 From: Andreas Date: Sun, 14 Aug 2022 16:43:28 +0200 Subject: [PATCH] Add task to generate locales_config.xml (#7754) --- .gitignore | 3 ++ app/build.gradle.kts | 4 +- app/src/main/AndroidManifest.xml | 1 + buildSrc/build.gradle.kts | 8 ++++ buildSrc/settings.gradle.kts | 7 ++++ .../src/main/kotlin/LocalesConfigPlugin.kt | 40 +++++++++++++++++++ 6 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 buildSrc/settings.gradle.kts create mode 100644 buildSrc/src/main/kotlin/LocalesConfigPlugin.kt diff --git a/.gitignore b/.gitignore index 93b8ea71f..8e157b61c 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ app/**/output.json # Hebrew assets are copied on build app/src/main/res/values-iw/ + +# Generated +locales_config.xml \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 47fd447dd..953d0f418 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -281,6 +281,8 @@ dependencies { } tasks { + val localesConfigTask = registerLocalesConfigTask(project) + withType { useJUnitPlatform() testLogging { @@ -313,7 +315,7 @@ tasks { } preBuild { - dependsOn(formatKotlin, copyHebrewStrings) + dependsOn(formatKotlin, copyHebrewStrings, localesConfigTask) } } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c639dc583..b3368ade7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -27,6 +27,7 @@ android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" + android:localeConfig="@xml/locales_config" android:largeHeap="true" android:requestLegacyExternalStorage="true" android:roundIcon="@mipmap/ic_launcher_round" diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index aebe26153..c2d953689 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -1,6 +1,14 @@ plugins { `kotlin-dsl` } + +dependencies { + compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinLibs.versions.kotlin.version.get()}") + + implementation(gradleApi()) +} + repositories { mavenCentral() + google() } diff --git a/buildSrc/settings.gradle.kts b/buildSrc/settings.gradle.kts new file mode 100644 index 000000000..807fe41ca --- /dev/null +++ b/buildSrc/settings.gradle.kts @@ -0,0 +1,7 @@ +dependencyResolutionManagement { + versionCatalogs { + create("kotlinLibs") { + from(files("../gradle/kotlinx.versions.toml")) + } + } +} diff --git a/buildSrc/src/main/kotlin/LocalesConfigPlugin.kt b/buildSrc/src/main/kotlin/LocalesConfigPlugin.kt new file mode 100644 index 000000000..8a51e32a6 --- /dev/null +++ b/buildSrc/src/main/kotlin/LocalesConfigPlugin.kt @@ -0,0 +1,40 @@ +import org.gradle.api.Project +import org.gradle.api.Task +import org.gradle.api.tasks.TaskProvider +import org.gradle.kotlin.dsl.TaskContainerScope + +fun TaskContainerScope.registerLocalesConfigTask(project: Project): TaskProvider { + return with(project) { + register("generateLocalesConfig") { + val emptyResourcesElement = "\\s*<\\/resources>|".toRegex() + val valuesPrefix = "values-?".toRegex() + + val languages = fileTree("$projectDir/src/main/res/") + .matching { + include("**/strings.xml") + } + .filterNot { + it.readText().contains(emptyResourcesElement) + } + .joinToString(separator = "\n") { + val language = it.parentFile.name + .replace(valuesPrefix, "") + .takeIf(String::isNotBlank) ?: "en" + " " + } + + + val content = """ + + +$languages + + """.trimIndent() + + val localeFile = file("$projectDir/src/main/res/xml/locales_config.xml") + localeFile.parentFile.mkdirs() + localeFile.writeText(content) + } + } +} +