Skip to content

Commit

Permalink
Generate locales_config.xml in build dir
Browse files Browse the repository at this point in the history
  • Loading branch information
AntsyLich committed Aug 19, 2024
1 parent 777ae24 commit ac41bff
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.gradle.kotlin.dsl.the
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.compose.compiler.gradle.ComposeCompilerGradlePluginExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.File

val Project.androidx get() = the<LibrariesForAndroidx>()
val Project.compose get() = the<LibrariesForCompose>()
Expand Down Expand Up @@ -106,3 +107,5 @@ internal fun Project.configureTest() {
}
}
}

val Project.generatedBuildDir: File get() = project.layout.buildDirectory.asFile.get().resolve("generated/mihon")
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package mihon.buildlogic.tasks
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.TaskProvider
import java.io.File

private val emptyResourcesElement = "<resources>\\s*</resources>|<resources/>".toRegex()
private val emptyResourcesElement = "<resources>\\s*</resources>|<resources\\s*/>".toRegex()

fun Project.getLocalesConfigTask(): TaskProvider<Task> {
fun Project.getLocalesConfigTask(outputResourceDir: File): TaskProvider<Task> {
return tasks.register("generateLocalesConfig") {
val locales = fileTree("$projectDir/src/commonMain/moko-resources/")
.matching { include("**/strings.xml") }
Expand All @@ -16,7 +17,6 @@ fun Project.getLocalesConfigTask(): TaskProvider<Task> {
.replace("base", "en")
.replace("-r", "-")
.replace("+", "-")
.takeIf(String::isNotBlank) ?: "en"
}
.sorted()
.joinToString("\n") { "| <locale android:name=\"$it\"/>" }
Expand All @@ -28,10 +28,9 @@ fun Project.getLocalesConfigTask(): TaskProvider<Task> {
|</locale-config>
""".trimMargin()

file("$projectDir/src/androidMain/res/xml/locales_config.xml").apply {
outputResourceDir.resolve("xml/locales_config.xml").apply {
parentFile.mkdirs()
writeText(content)
}
}
}

2 changes: 0 additions & 2 deletions i18n/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
# Generated
locales_config.xml
13 changes: 9 additions & 4 deletions i18n/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mihon.buildlogic.generatedBuildDir
import mihon.buildlogic.tasks.getLocalesConfigTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

Expand All @@ -21,13 +22,17 @@ kotlin {
}
}

val generatedAndroidResourceDir = generatedBuildDir.resolve("android/res")

android {
namespace = "tachiyomi.i18n"

sourceSets {
named("main") {
res.srcDir("src/commonMain/resources")
}
val main by getting
main.res.srcDirs(
"src/commonMain/resources",
generatedAndroidResourceDir,
)
}

lint {
Expand All @@ -40,7 +45,7 @@ multiplatformResources {
}

tasks {
val localesConfigTask = project.getLocalesConfigTask()
val localesConfigTask = project.getLocalesConfigTask(generatedAndroidResourceDir)
preBuild {
dependsOn(localesConfigTask)
}
Expand Down

0 comments on commit ac41bff

Please sign in to comment.