From 1172da23ffd2f7e65dc2d8d0adb5fac350829b87 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Fri, 31 Jan 2025 09:58:26 +0200 Subject: [PATCH] ci: Preprocess strings before pushing to Crowdin (#4383) --- .github/workflows/pull_strings.yml | 2 +- .github/workflows/push_strings.yml | 3 ++ crowdin.yml | 2 +- patches/build.gradle.kts | 18 ++++++++- .../app/revanced/util/CrowdinPreprocessor.kt | 40 +++++++++++++++++++ 5 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 patches/src/main/kotlin/app/revanced/util/CrowdinPreprocessor.kt diff --git a/.github/workflows/pull_strings.yml b/.github/workflows/pull_strings.yml index ab40435fd..ace03dbc0 100644 --- a/.github/workflows/pull_strings.yml +++ b/.github/workflows/pull_strings.yml @@ -2,7 +2,7 @@ name: Pull strings on: schedule: - - cron: "0 */8 * * *" + - cron: "0 */6 * * *" workflow_dispatch: jobs: diff --git a/.github/workflows/push_strings.yml b/.github/workflows/push_strings.yml index 0cd3b4eb6..2a52ac2cc 100644 --- a/.github/workflows/push_strings.yml +++ b/.github/workflows/push_strings.yml @@ -18,6 +18,9 @@ jobs: with: fetch-depth: 0 + - name: Preprocess strings + run: ./gradlew clean preprocessCrowdinStrings + - name: Push strings uses: crowdin/github-action@v2 with: diff --git a/crowdin.yml b/crowdin.yml index 148f321cd..56c6308fa 100644 --- a/crowdin.yml +++ b/crowdin.yml @@ -3,6 +3,6 @@ api_token_env: "CROWDIN_PERSONAL_TOKEN" preserve_hierarchy: false files: - - source: patches/src/main/resources/addresources/values/strings.xml + - source: patches/build/tmp/crowdin/strings.xml translation: patches/src/main/resources/addresources/values-%android_code%/strings.xml skip_untranslated_strings: true diff --git a/patches/build.gradle.kts b/patches/build.gradle.kts index 459d50a0c..27f98b740 100644 --- a/patches/build.gradle.kts +++ b/patches/build.gradle.kts @@ -21,6 +21,22 @@ dependencies { compileOnly(project(":patches:stub")) } +tasks { + register("preprocessCrowdinStrings") { + description = "Preprocess strings for Crowdin push" + + dependsOn(build) + + classpath = sourceSets["main"].runtimeClasspath + mainClass.set("app.revanced.util.CrowdinPreprocessorKt") + + args = listOf( + "src/main/resources/addresources/values/strings.xml", + "build/tmp/crowdin/strings.xml" + ) + } +} + kotlin { compilerOptions { freeCompilerArgs = listOf("-Xcontext-receivers") @@ -38,4 +54,4 @@ publishing { } } } -} +} \ No newline at end of file diff --git a/patches/src/main/kotlin/app/revanced/util/CrowdinPreprocessor.kt b/patches/src/main/kotlin/app/revanced/util/CrowdinPreprocessor.kt new file mode 100644 index 000000000..c49c4c642 --- /dev/null +++ b/patches/src/main/kotlin/app/revanced/util/CrowdinPreprocessor.kt @@ -0,0 +1,40 @@ +package app.revanced.util + +import java.io.File + +/** + * Comments out the non-standard and tags. + * + * Previously this was done on Crowdin after pushing. + * But Crowdin preprocessing has randomly failed but still used the unmodified + * strings.xml file, which effectively deletes all patch strings from Crowdin. + */ +internal fun main(args: Array) { + if (args.size != 2) { + throw RuntimeException("Exactly two arguments are required: ") + } + + val inputFilePath = args[0] + val inputFile = File(inputFilePath) + if (!inputFile.exists()) { + throw RuntimeException( + "Input file not found: $inputFilePath currentDirectory: " + File(".").canonicalPath + ) + } + + // Comment out the non-standard tags. Otherwise Crowdin interprets the file + // not as Android but instead a generic xml file where strings are + // identified by xml position and not key. + val content = inputFile.readText() + val tagRegex = """(()|()|()|())""".toRegex() + val modifiedContent = content.replace(tagRegex, """""") + + // Write modified content to the output file (creates file if it doesn't exist). + val outputFilePath = args[1] + val outputFile = File(outputFilePath) + outputFile.parentFile?.mkdirs() + outputFile.writeText(modifiedContent) + + println("Preprocessed strings.xml to: $outputFilePath") +} +