Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit e5ca6b1

Browse files
committed
refactor(build): migrate to NIO
1 parent 2a0b7e0 commit e5ca6b1

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

build-logic/src/main/kotlin/app/passwordstore/gradle/crowdin/StringCleanupTask.kt

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package app.passwordstore.gradle.crowdin
22

3-
import java.io.File
3+
import java.nio.file.Path
44
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
514
import org.gradle.api.DefaultTask
615
import org.gradle.api.GradleException
716
import org.gradle.api.file.DirectoryProperty
@@ -10,6 +19,7 @@ import org.gradle.api.tasks.TaskAction
1019
import org.gradle.work.DisableCachingByDefault
1120
import org.w3c.dom.Document
1221

22+
@OptIn(ExperimentalPathApi::class)
1323
@DisableCachingByDefault(because = "The task runs quickly and has complicated semantics")
1424
abstract class StringCleanupTask : DefaultTask() {
1525

@@ -19,12 +29,12 @@ abstract class StringCleanupTask : DefaultTask() {
1929
fun clean() {
2030
val sourceSets = arrayOf("main", "nonFree")
2131
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()
2333
val valuesDirectories =
24-
fileTreeWalk.filter { it.isDirectory }.filter { it.name.startsWith("values") }
34+
fileTreeWalk.filter { it.isDirectory() }.filter { it.name.startsWith("values") }
2535
val stringFiles = fileTreeWalk.filter { it.name == "strings.xml" }
2636
val sourceFile =
27-
stringFiles.firstOrNull { it.path.endsWith("values/strings.xml") }
37+
stringFiles.firstOrNull { it.pathString.endsWith("values/strings.xml") }
2838
?: throw GradleException("No root strings.xml found in '$sourceSet' sourceSet")
2939
val sourceDoc = parseDocument(sourceFile)
3040
val baselineStringCount = countStrings(sourceDoc)
@@ -34,22 +44,22 @@ abstract class StringCleanupTask : DefaultTask() {
3444
val doc = parseDocument(file)
3545
val stringCount = countStrings(doc)
3646
if (stringCount < threshold) {
37-
file.delete()
47+
file.deleteIfExists()
3848
}
3949
}
4050
}
4151
valuesDirectories.forEach { dir ->
42-
if (dir.listFiles().isNullOrEmpty()) {
43-
dir.delete()
52+
if (dir.listDirectoryEntries().isEmpty()) {
53+
dir.deleteRecursively()
4454
}
4555
}
4656
}
4757
}
4858

49-
private fun parseDocument(file: File): Document {
59+
private fun parseDocument(path: Path): Document {
5060
val dbFactory = DocumentBuilderFactory.newInstance()
5161
val documentBuilder = dbFactory.newDocumentBuilder()
52-
return documentBuilder.parse(file)
62+
return documentBuilder.parse(path.inputStream())
5363
}
5464

5565
private fun countStrings(document: Document): Int {

build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtCheckTask.kt

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package app.passwordstore.gradle.ktfmt
22

33
import app.passwordstore.gradle.KtfmtPlugin
44
import com.facebook.ktfmt.format.Formatter
5-
import java.io.File
5+
import java.nio.file.Path
6+
import kotlin.io.path.pathString
7+
import kotlin.io.path.readText
8+
import kotlin.io.path.relativeTo
69
import kotlinx.coroutines.Dispatchers
710
import kotlinx.coroutines.ExperimentalCoroutinesApi
811
import kotlinx.coroutines.async
@@ -35,7 +38,7 @@ abstract class KtfmtCheckTask : SourceTask() {
3538
fun execute() {
3639
runBlocking(Dispatchers.IO.limitedParallelism(PARALLEL_TASK_LIMIT)) {
3740
coroutineScope {
38-
val results = inputFiles.map { async { checkFile(it) } }.awaitAll()
41+
val results = inputFiles.map { async { checkFile(it.toPath()) } }.awaitAll()
3942
if (results.any { (notFormatted, _) -> notFormatted }) {
4043
val prettyDiff =
4144
results
@@ -48,10 +51,12 @@ abstract class KtfmtCheckTask : SourceTask() {
4851
}
4952
}
5053

51-
private fun checkFile(input: File): Pair<Boolean, List<KtfmtDiffEntry>> {
54+
private fun checkFile(input: Path): Pair<Boolean, List<KtfmtDiffEntry>> {
5255
val originCode = input.readText()
5356
val formattedCode = Formatter.format(KtfmtPlugin.DEFAULT_FORMATTING_OPTIONS, originCode)
54-
val pathNormalizer = { file: File -> file.toRelativeString(projectDirectory.asFile.get()) }
57+
val pathNormalizer = { file: Path ->
58+
file.relativeTo(projectDirectory.asFile.get().toPath()).pathString
59+
}
5560
return (originCode != formattedCode) to
5661
KtfmtDiffer.computeDiff(input, formattedCode, pathNormalizer)
5762
}

build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffer.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import com.github.difflib.DiffUtils
44
import com.github.difflib.patch.ChangeDelta
55
import com.github.difflib.patch.DeleteDelta
66
import com.github.difflib.patch.InsertDelta
7-
import java.io.File
7+
import java.nio.file.Path
8+
import kotlin.io.path.readText
89

910
object KtfmtDiffer {
1011
fun computeDiff(
11-
inputFile: File,
12+
inputFile: Path,
1213
formattedCode: String,
13-
pathNormalizer: (File) -> String,
14+
pathNormalizer: (Path) -> String,
1415
): List<KtfmtDiffEntry> {
1516
val originCode = inputFile.readText()
1617
return DiffUtils.diff(originCode, formattedCode, null).deltas.map {

build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtWorkerAction.kt

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ package app.passwordstore.gradle.ktfmt
22

33
import app.passwordstore.gradle.KtfmtPlugin
44
import com.facebook.ktfmt.format.Formatter
5-
import java.io.File
5+
import java.nio.file.Path
6+
import kotlin.io.path.pathString
7+
import kotlin.io.path.readText
8+
import kotlin.io.path.relativeTo
9+
import kotlin.io.path.writeText
610
import org.gradle.api.logging.LogLevel
711
import org.gradle.api.logging.Logger
812
import org.gradle.api.logging.Logging
@@ -12,22 +16,22 @@ import org.gradle.workers.WorkAction
1216
abstract class KtfmtWorkerAction : WorkAction<KtfmtWorkerParameters> {
1317
private val logger: Logger =
1418
DefaultContextAwareTaskLogger(Logging.getLogger(KtfmtFormatTask::class.java))
15-
private val files: List<File> = parameters.files.toList()
16-
private val projectDirectory: File = parameters.projectDirectory.asFile.get()
19+
private val files: List<Path> = parameters.files.toList().map { it.toPath() }
20+
private val projectDirectory: Path = parameters.projectDirectory.asFile.get().toPath()
1721
private val name: String = parameters.name.get()
1822

1923
override fun execute() {
2024
try {
2125
files.forEach { file ->
2226
val sourceText = file.readText()
23-
val relativePath = file.toRelativeString(projectDirectory)
27+
val relativePath = file.relativeTo(projectDirectory).pathString
2428

2529
logger.log(LogLevel.DEBUG, "$name checking format: $relativePath")
2630

2731
val formattedText = Formatter.format(KtfmtPlugin.DEFAULT_FORMATTING_OPTIONS, sourceText)
2832

2933
if (!formattedText.contentEquals(sourceText)) {
30-
logger.log(LogLevel.QUIET, "${file.toRelativeString(projectDirectory)}: Format fixed")
34+
logger.log(LogLevel.QUIET, "$relativePath: Format fixed")
3135
file.writeText(formattedText)
3236
}
3337
}

0 commit comments

Comments
 (0)