Skip to content

Commit

Permalink
Reuse ProcessBuilder from save-cli (#10)
Browse files Browse the repository at this point in the history
### What's done:
 * replaced myDeleteRecursively with deleteRecursively from okio

(#3)
  • Loading branch information
sanyavertolet authored Apr 12, 2023
1 parent a455fe6 commit d557a61
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")}" }
Expand Down Expand Up @@ -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
Expand All @@ -177,6 +177,7 @@ class ProcessBuilder(private val useInternalRedirections: Boolean, private val f
val listOfCommands = if (separator != "") command.split(separator) as MutableList<String> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit d557a61

Please sign in to comment.