From d557a619116609a60afb8aa5295423b4f2d55b44 Mon Sep 17 00:00:00 2001 From: Alexander Frolov Date: Wed, 12 Apr 2023 13:24:51 +0300 Subject: [PATCH] Reuse ProcessBuilder from save-cli (#10) ### What's done: * replaced myDeleteRecursively with deleteRecursively from okio (#3) --- .../saveourtool/processbuilder/ProcessBuilder.kt | 5 +++-- .../processbuilder/utils/FileUtils.kt | 10 ---------- .../utils/FileUtils.kt | 9 --------- .../utils/FileUtils.kt | 16 ---------------- 4 files changed, 3 insertions(+), 37 deletions(-) diff --git a/core/src/commonMain/kotlin/com/saveourtool/processbuilder/ProcessBuilder.kt b/core/src/commonMain/kotlin/com/saveourtool/processbuilder/ProcessBuilder.kt index 5483bdf..c92668f 100644 --- a/core/src/commonMain/kotlin/com/saveourtool/processbuilder/ProcessBuilder.kt +++ b/core/src/commonMain/kotlin/com/saveourtool/processbuilder/ProcessBuilder.kt @@ -8,7 +8,6 @@ import com.saveourtool.processbuilder.exceptions.ProcessExecutionException import com.saveourtool.processbuilder.exceptions.ProcessTimeoutException import com.saveourtool.processbuilder.utils.createFile import com.saveourtool.processbuilder.utils.isCurrentOsWindows -import com.saveourtool.processbuilder.utils.myDeleteRecursively import com.saveourtool.processbuilder.utils.readLines import io.github.oshai.KotlinLogging import okio.FileSystem @@ -92,7 +91,7 @@ class ProcessBuilder(private val useInternalRedirections: Boolean, private val f val stdout = fs.readLines(stdoutFile) val stderr = fs.readLines(stderrFile) - fs.myDeleteRecursively(tmpDir) + fs.deleteRecursively(tmpDir) logger.trace { "Removed temp directory $tmpDir" } if (stderr.isNotEmpty()) { logger.debug { "stderr of `$command`:\t${stderr.joinToString("\t")}" } @@ -158,6 +157,7 @@ class ProcessBuilder(private val useInternalRedirections: Boolean, private val f } // Command already contains correct signature. // We also believe not to met complex cases: `echo a; echo | set /p="a && echo b"` + // TODO: https://github.com/saveourtool/process-builder-multiplatform/issues/5 val cmdWithoutWhitespaces = command.replace(" ", "") if (cmdWithoutWhitespaces.contains("echo|set")) { return command @@ -177,6 +177,7 @@ class ProcessBuilder(private val useInternalRedirections: Boolean, private val f val listOfCommands = if (separator != "") command.split(separator) as MutableList else mutableListOf(command) listOfCommands.forEachIndexed { index, cmd -> if (cmd.contains("echo")) { + // TODO: https://github.com/saveourtool/process-builder-multiplatform/issues/5 var newEchoCommand = cmd.trim(' ').replace("echo ", " echo | set /p dummyName=\"") // Now we need to add closing `"` in proper place // Despite the fact, that we don't expect user redirections, for out internal tests we use them, diff --git a/core/src/commonMain/kotlin/com/saveourtool/processbuilder/utils/FileUtils.kt b/core/src/commonMain/kotlin/com/saveourtool/processbuilder/utils/FileUtils.kt index 7515d05..1b2e503 100644 --- a/core/src/commonMain/kotlin/com/saveourtool/processbuilder/utils/FileUtils.kt +++ b/core/src/commonMain/kotlin/com/saveourtool/processbuilder/utils/FileUtils.kt @@ -6,22 +6,12 @@ package com.saveourtool.processbuilder.utils -import io.github.oshai.KotlinLogging import okio.FileSystem import okio.Path import kotlin.jvm.JvmName expect val fs: FileSystem -internal val fileUtilsLogger = KotlinLogging.logger { } - -/** - * Delete this directory and all other files and directories in it - * - * @param path a path to a directory - */ -expect fun FileSystem.myDeleteRecursively(path: Path) - /** * Create file in [this] [FileSystem], denoted by [Path] [path] * diff --git a/core/src/jvmMain/kotlin/com.saveourtool.processbuilder/utils/FileUtils.kt b/core/src/jvmMain/kotlin/com.saveourtool.processbuilder/utils/FileUtils.kt index cb80401..a319c2b 100644 --- a/core/src/jvmMain/kotlin/com.saveourtool.processbuilder/utils/FileUtils.kt +++ b/core/src/jvmMain/kotlin/com.saveourtool.processbuilder/utils/FileUtils.kt @@ -5,14 +5,5 @@ package com.saveourtool.processbuilder.utils import okio.FileSystem -import okio.Path -import java.nio.file.Files actual val fs: FileSystem = FileSystem.SYSTEM - -actual fun FileSystem.myDeleteRecursively(path: Path) { - path.toFile().walkBottomUp().forEach { file -> - fileUtilsLogger.trace { "Attempt to delete file $file" } - Files.delete(file.toPath()) - } -} diff --git a/core/src/nativeMain/kotlin/com.saveourtool.processbuilder/utils/FileUtils.kt b/core/src/nativeMain/kotlin/com.saveourtool.processbuilder/utils/FileUtils.kt index e365f40..a319c2b 100644 --- a/core/src/nativeMain/kotlin/com.saveourtool.processbuilder/utils/FileUtils.kt +++ b/core/src/nativeMain/kotlin/com.saveourtool.processbuilder/utils/FileUtils.kt @@ -5,21 +5,5 @@ package com.saveourtool.processbuilder.utils import okio.FileSystem -import okio.Path -import platform.posix.FTW_DEPTH -import platform.posix.nftw -import platform.posix.remove - -import kotlinx.cinterop.staticCFunction -import kotlinx.cinterop.toKString actual val fs: FileSystem = FileSystem.SYSTEM - -@Suppress("MAGIC_NUMBER", "MagicNumber") -actual fun FileSystem.myDeleteRecursively(path: Path) { - nftw(path.toString(), staticCFunction { pathName, _, _, _ -> - val fileName = pathName!!.toKString() - fileUtilsLogger.trace { "Attempt to delete file $fileName" } - remove(fileName) - }, 64, FTW_DEPTH) -}