1
1
package app.passwordstore.gradle.crowdin
2
2
3
- import java.io.File
3
+ import java.nio.file.Path
4
4
import javax.xml.parsers.DocumentBuilderFactory
5
+ import kotlin.io.path.ExperimentalPathApi
6
+ import kotlin.io.path.deleteIfExists
7
+ import kotlin.io.path.deleteRecursively
8
+ import kotlin.io.path.inputStream
9
+ import kotlin.io.path.isDirectory
10
+ import kotlin.io.path.listDirectoryEntries
11
+ import kotlin.io.path.name
12
+ import kotlin.io.path.pathString
13
+ import kotlin.io.path.walk
5
14
import org.gradle.api.DefaultTask
6
15
import org.gradle.api.GradleException
7
16
import org.gradle.api.file.DirectoryProperty
@@ -10,6 +19,7 @@ import org.gradle.api.tasks.TaskAction
10
19
import org.gradle.work.DisableCachingByDefault
11
20
import org.w3c.dom.Document
12
21
22
+ @OptIn(ExperimentalPathApi ::class )
13
23
@DisableCachingByDefault(because = " The task runs quickly and has complicated semantics" )
14
24
abstract class StringCleanupTask : DefaultTask () {
15
25
@@ -19,12 +29,12 @@ abstract class StringCleanupTask : DefaultTask() {
19
29
fun clean () {
20
30
val sourceSets = arrayOf(" main" , " nonFree" )
21
31
for (sourceSet in sourceSets) {
22
- val fileTreeWalk = sourceDirectory.dir(" $sourceSet /res" ).get().asFile.walkTopDown ()
32
+ val fileTreeWalk = sourceDirectory.dir(" $sourceSet /res" ).get().asFile.toPath().walk ()
23
33
val valuesDirectories =
24
- fileTreeWalk.filter { it.isDirectory }.filter { it.name.startsWith(" values" ) }
34
+ fileTreeWalk.filter { it.isDirectory() }.filter { it.name.startsWith(" values" ) }
25
35
val stringFiles = fileTreeWalk.filter { it.name == " strings.xml" }
26
36
val sourceFile =
27
- stringFiles.firstOrNull { it.path .endsWith(" values/strings.xml" ) }
37
+ stringFiles.firstOrNull { it.pathString .endsWith(" values/strings.xml" ) }
28
38
? : throw GradleException (" No root strings.xml found in '$sourceSet ' sourceSet" )
29
39
val sourceDoc = parseDocument(sourceFile)
30
40
val baselineStringCount = countStrings(sourceDoc)
@@ -34,22 +44,22 @@ abstract class StringCleanupTask : DefaultTask() {
34
44
val doc = parseDocument(file)
35
45
val stringCount = countStrings(doc)
36
46
if (stringCount < threshold) {
37
- file.delete ()
47
+ file.deleteIfExists ()
38
48
}
39
49
}
40
50
}
41
51
valuesDirectories.forEach { dir ->
42
- if (dir.listFiles ().isNullOrEmpty ()) {
43
- dir.delete ()
52
+ if (dir.listDirectoryEntries ().isEmpty ()) {
53
+ dir.deleteRecursively ()
44
54
}
45
55
}
46
56
}
47
57
}
48
58
49
- private fun parseDocument (file : File ): Document {
59
+ private fun parseDocument (path : Path ): Document {
50
60
val dbFactory = DocumentBuilderFactory .newInstance()
51
61
val documentBuilder = dbFactory.newDocumentBuilder()
52
- return documentBuilder.parse(file )
62
+ return documentBuilder.parse(path.inputStream() )
53
63
}
54
64
55
65
private fun countStrings (document : Document ): Int {
0 commit comments