blob: 4be9388507841e49533006868d99ab9f4d313623 (
about) (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
import de.undercouch.gradle.tasks.download.Download
import java.io.File
import javax.xml.parsers.DocumentBuilderFactory
import okhttp3.OkHttpClient
import okhttp3.Request
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Copy
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.register
import org.w3c.dom.Document
private const val EXCEPTION_MESSAGE =
"""Applying `crowdin-plugin` requires a projectName to be configured via the "crowdin" extension."""
private const val CROWDIN_BUILD_API_URL =
"https://api.crowdin.com/api/project/%s/export?login=%s&account-key=%s"
class CrowdinDownloadPlugin : Plugin<Project> {
override fun apply(project: Project) {
with(project) {
val extension = extensions.create<CrowdinExtension>("crowdin")
afterEvaluate {
val projectName = extension.projectName
if (projectName.isEmpty()) {
throw GradleException(EXCEPTION_MESSAGE)
}
tasks.register("buildOnApi") {
doLast {
val login = providers.environmentVariable("CROWDIN_LOGIN").forUseAtConfigurationTime()
val key =
providers.environmentVariable("CROWDIN_PROJECT_KEY").forUseAtConfigurationTime()
if (!login.isPresent) {
throw GradleException("CROWDIN_LOGIN environment variable must be set")
}
if (!key.isPresent) {
throw GradleException("CROWDIN_PROJECT_KEY environment variable must be set")
}
val client = OkHttpClient()
val url = CROWDIN_BUILD_API_URL.format(projectName, login.get(), key.get())
val request = Request.Builder().url(url).get().build()
client.newCall(request).execute()
}
}
tasks.register<Download>("downloadCrowdin") {
dependsOn("buildOnApi")
src("https://crowdin.com/backend/download/project/$projectName.zip")
dest("$buildDir/translations.zip")
overwrite(true)
}
tasks.register<Copy>("extractCrowdin") {
dependsOn("downloadCrowdin")
doFirst { File(buildDir, "translations").deleteRecursively() }
from(zipTree("$buildDir/translations.zip"))
into("$buildDir/translations")
}
tasks.register<Copy>("extractStrings") {
dependsOn("extractCrowdin")
from("$buildDir/translations/")
into("${projectDir}/src/")
setFinalizedBy(setOf("removeIncompleteStrings"))
}
tasks.register("removeIncompleteStrings") {
doLast {
val sourceSets = arrayOf("main", "nonFree")
for (sourceSet in sourceSets) {
val stringFiles =
File("${projectDir}/src/$sourceSet").walkTopDown().filter {
it.name == "strings.xml"
}
val sourceFile =
stringFiles.firstOrNull { it.path.endsWith("values/strings.xml") }
?: throw GradleException("No root strings.xml found in '$sourceSet' sourceSet")
val sourceDoc = parseDocument(sourceFile)
val baselineStringCount = countStrings(sourceDoc)
val threshold = 0.80 * baselineStringCount
stringFiles.forEach { file ->
if (file != sourceFile) {
val doc = parseDocument(file)
val stringCount = countStrings(doc)
if (stringCount < threshold) {
file.delete()
}
}
}
}
}
}
tasks.register("crowdin") {
dependsOn("extractStrings")
if (!extension.skipCleanup) {
doLast {
File("$buildDir/translations").deleteRecursively()
File("$buildDir/nonFree-translations").deleteRecursively()
File("$buildDir/translations.zip").delete()
}
}
}
}
}
}
private fun parseDocument(file: File): Document {
val dbFactory = DocumentBuilderFactory.newInstance()
val documentBuilder = dbFactory.newDocumentBuilder()
return documentBuilder.parse(file)
}
private fun countStrings(document: Document): Int {
// Normalization is beneficial for us
// https://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
document.documentElement.normalize()
return document.getElementsByTagName("string").length
}
}
|