Skip to content

Commit

Permalink
Use Gradle lazy configuration (Property) for the UpdateTranslationsTa…
Browse files Browse the repository at this point in the history
…sk parameters.
  • Loading branch information
m-sasha committed Mar 12, 2024
1 parent 3156c0d commit dc0aada
Show file tree
Hide file tree
Showing 149 changed files with 221 additions and 210 deletions.
95 changes: 53 additions & 42 deletions buildSrc/src/main/kotlin/UpdateTranslationsTask.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.util.*
import javax.xml.parsers.DocumentBuilderFactory
import kotlin.concurrent.thread
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.TaskAction
import org.w3c.dom.Element

/*
* Copyright 2024 The Android Open Source Project
*
Expand All @@ -26,71 +14,91 @@ import org.w3c.dom.Element
* limitations under the License.
*/

import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.util.*
import javax.xml.parsers.DocumentBuilderFactory
import kotlin.concurrent.thread
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.TaskAction
import org.w3c.dom.Element

/**
* A task that checks out an Android repository with string translations, extracts the translations
* we're interested in and writes Kotlin source files that provide them.
*/
open class UpdateTranslationsTask : DefaultTask() {
abstract class UpdateTranslationsTask : DefaultTask() {

/**
* The git binary to use.
*/
@Input
var git: String = "git"
@get:Input
abstract val git: Property<String>
init {
@Suppress("LeakingThis")
git.convention("git")
}

/**
* The URL of the repository to check out.
*/
@Input
lateinit var gitRepo: String
@get:Input
abstract val gitRepo: Property<String>

/**
* The root resources directories in the repo.
*
* Note that there may be more than one because Android shares resources across modules, and
* some modules use resources from more than one.
*/
@Input
lateinit var repoResDirectories: List<String>
@get:Input
abstract val repoResDirectories: ListProperty<String>

/**
* The strings to translate.
*
* The keys are the names of the Android resources in the XML file, and the values are the names
* of the Kotlin `Strings` constants.
*/
@Input
lateinit var stringByResourceName: Map<String, String>
@get:Input
abstract val stringByResourceName: MapProperty<String, String>

/**
* The locales to get the translations for, in `language(_region)` format; e.g. "fr-CA" or just
* "fr".
*
* Note that language may not be an empty string; use "en" for the default locale.
*/
@Input
lateinit var locales: List<String>
@get:Input
abstract val locales: ListProperty<String>

/**
* The directory where the Kotlin source files are to be written.
*
* Note that this directory is deleted first in order to clear translations that are no longer
* needed.
*/
@InputDirectory
lateinit var targetDirectory: File
@get:InputDirectory
abstract val targetDirectory: DirectoryProperty

/**
* The name of the package of the Kotlin source files to be written.
*/
@Input
lateinit var targetPackageName: String
@get:Input
abstract val targetPackageName: Property<String>

/**
* The package name of the Kotlin `Strings.kt` file.
*/
@Input
lateinit var kotlinStringsPackageName: String
@get:Input
abstract val kotlinStringsPackageName: Property<String>

/**
* Updates the translations.
Expand All @@ -102,45 +110,48 @@ open class UpdateTranslationsTask : DefaultTask() {
dir.mkdirs()
dir.deleteOnExit()

val targetDirectory = targetDirectory.get().asFile
if (targetDirectory.isDirectory && !targetDirectory.deleteRecursively())
throw IOException("Unable to delete directory $targetDirectory")

if (!targetDirectory.isDirectory && !targetDirectory.mkdirs())
throw IOException("Unable to create directory $targetDirectory")

// The directory into which the repo will be cloned
val repoDir = File(dir, gitRepo.substringAfterLast('/'))
val repoDir = File(dir, gitRepo.get().substringAfterLast('/'))
repoDir.deleteRecursively()

// The directories in the repo to check out.
// For each locale, there could be several values directories.
val valuesDirsByLocale: Map<Locale, List<String>> = locales.associate { localeTag ->
val valuesDirsByLocale: Map<Locale, List<String>> = locales.get().associate { localeTag ->
val locale = Locale.fromTag(localeTag)
val valuesDirs = repoResDirectories.map {
val valuesDirs = repoResDirectories.get().map {
val resDir = if (it.endsWith("/")) it else "$it/"
resDir + locale.valuesDirName()
}
locale to valuesDirs
}

val gitCommand = git.getOrElse("git")

// Clone the repo, but don't check out any files
execCommand(dir, git, "clone", "-n", "--depth=1", "--filter=tree:0", gitRepo)
execCommand(dir, gitCommand, "clone", "-n", "--depth=1", "--filter=tree:0", gitRepo.get())

// Set a sparse checkout to download only the directories we need
val allValuesDirs = valuesDirsByLocale.values.flatten()
execCommand(repoDir, git, "sparse-checkout", "set", "--no-cone",
execCommand(repoDir, gitCommand, "sparse-checkout", "set", "--no-cone",
*allValuesDirs.toTypedArray())

// Actually download them
execCommand(repoDir, git, "checkout")
execCommand(repoDir, gitCommand, "checkout")

// Write the per-language translation files
val localesGroupedByLanguage = valuesDirsByLocale.keys.groupBy { it.language }
for ((language, locales) in localesGroupedByLanguage) {
writeLanguageFile(
language = language,
locales = locales,
stringByResourceName = stringByResourceName,
stringByResourceName = stringByResourceName.get(),
repoDir = repoDir,
valuesDirsByLocale = valuesDirsByLocale
)
Expand Down Expand Up @@ -177,10 +188,10 @@ open class UpdateTranslationsTask : DefaultTask() {
val kotlinFileName = language.replaceFirstChar { it.uppercase() } + ".kt"
println("Writing $kotlinFileName for locales ${locales.joinToString()}")

File(targetDirectory, kotlinFileName).bufferedWriter().use {
File(targetDirectory.get().asFile, kotlinFileName).bufferedWriter().use {
it.write(kotlinFilePreamble())
it.appendLine("import $kotlinStringsPackageName.Strings")
it.appendLine("import $kotlinStringsPackageName.Translations")
it.appendLine("import ${kotlinStringsPackageName.get()}.Strings")
it.appendLine("import ${kotlinStringsPackageName.get()}.Translations")

for (locale in locales) {
// Keep track of the strings for which translations were found, to be able to detect
Expand Down Expand Up @@ -231,9 +242,9 @@ open class UpdateTranslationsTask : DefaultTask() {
* Writes the `Translations.kt` file which maps all locales to the actual translations.
*/
private fun writeTranslationsFile(locales: List<Locale>) {
File(targetDirectory, "Translations.kt").bufferedWriter().use {
File(targetDirectory.asFile.get(), "Translations.kt").bufferedWriter().use {
it.write(kotlinFilePreamble())
it.appendLine("import $kotlinStringsPackageName.Translations")
it.appendLine("import ${kotlinStringsPackageName.get()}.Translations")
it.appendLine()
it.appendLine("""
/**
Expand Down Expand Up @@ -277,7 +288,7 @@ open class UpdateTranslationsTask : DefaultTask() {
* limitations under the License.
*/
package $targetPackageName
package ${targetPackageName.get()}
""".trimIndent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.af() = mapOf(
Strings.NavigationMenu to "Navigasiekieslys",
Strings.ExposedDropdownMenu to "Aftrekkieslys",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.am() = mapOf(
Strings.NavigationMenu to "የዳሰሳ ምናሌ",
Strings.ExposedDropdownMenu to "ተቆልቋይ ምናሌ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.ar() = mapOf(
Strings.NavigationMenu to "قائمة تنقل",
Strings.ExposedDropdownMenu to "قائمة منسدلة",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.`as`() = mapOf(
Strings.NavigationMenu to "নেভিগেশ্বন মেনু",
Strings.ExposedDropdownMenu to "ড্ৰপডাউনৰ মেনু",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.az() = mapOf(
Strings.NavigationMenu to "Naviqasiya menyusu",
Strings.ExposedDropdownMenu to "Aşağı açılan menyu",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.be() = mapOf(
Strings.NavigationMenu to "Меню навігацыі",
Strings.ExposedDropdownMenu to "Выпадное меню",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.bg() = mapOf(
Strings.NavigationMenu to "Меню за навигация",
Strings.ExposedDropdownMenu to "Падащо меню",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.bn() = mapOf(
Strings.NavigationMenu to "নেভিগেশন মেনু",
Strings.ExposedDropdownMenu to "ড্রপডাউন মেনু",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.bs() = mapOf(
Strings.NavigationMenu to "Meni za navigaciju",
Strings.ExposedDropdownMenu to "Padajući meni",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.ca() = mapOf(
Strings.NavigationMenu to "Menú de navegació",
Strings.ExposedDropdownMenu to "Menú desplegable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.cs() = mapOf(
Strings.NavigationMenu to "Navigační nabídka",
Strings.ExposedDropdownMenu to "Rozbalovací nabídka",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.da() = mapOf(
Strings.NavigationMenu to "Navigationsmenu",
Strings.ExposedDropdownMenu to "Rullemenu",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.de() = mapOf(
Strings.NavigationMenu to "Navigationsmenü",
Strings.ExposedDropdownMenu to "Drop-down-Menü",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.el() = mapOf(
Strings.NavigationMenu to "Μενού πλοήγησης",
Strings.ExposedDropdownMenu to "Αναπτυσσόμενο μενού",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package androidx.compose.material.l10n
import androidx.compose.material.Strings
import androidx.compose.material.Translations

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.en() = mapOf(
Strings.NavigationMenu to "Navigation menu",
Strings.ExposedDropdownMenu to "Dropdown menu",
Expand All @@ -30,7 +30,7 @@ internal fun Translations.en() = mapOf(
Strings.SliderRangeEnd to "Range end",
)

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.enAU() = mapOf(
Strings.NavigationMenu to "Navigation menu",
Strings.ExposedDropdownMenu to "Drop-down menu",
Expand All @@ -41,7 +41,7 @@ internal fun Translations.enAU() = mapOf(
Strings.SliderRangeEnd to "Range end",
)

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.enCA() = mapOf(
Strings.NavigationMenu to "Navigation menu",
Strings.ExposedDropdownMenu to "Dropdown menu",
Expand All @@ -52,7 +52,7 @@ internal fun Translations.enCA() = mapOf(
Strings.SliderRangeEnd to "Range end",
)

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.enGB() = mapOf(
Strings.NavigationMenu to "Navigation menu",
Strings.ExposedDropdownMenu to "Drop-down menu",
Expand All @@ -63,7 +63,7 @@ internal fun Translations.enGB() = mapOf(
Strings.SliderRangeEnd to "Range end",
)

@Suppress("UnusedReceiverParameter")
@Suppress("UnusedReceiverParameter", "DuplicatedCode")
internal fun Translations.enIN() = mapOf(
Strings.NavigationMenu to "Navigation menu",
Strings.ExposedDropdownMenu to "Drop-down menu",
Expand Down
Loading

0 comments on commit dc0aada

Please sign in to comment.