aboutsummaryrefslogtreecommitdiff
path: root/buildSrc/src/main/kotlin/org/jetbrains/CrossPlatformExec.kt
diff options
context:
space:
mode:
authorPaweł Marks <pmarks@virtuslab.com>2020-07-17 16:36:09 +0200
committerPaweł Marks <pmarks@virtuslab.com>2020-07-17 16:36:09 +0200
commit6996b1135f61c7d2cb60b0652c6a2691dda31990 (patch)
treed568096c25e31c28d14d518a63458b5a7526b896 /buildSrc/src/main/kotlin/org/jetbrains/CrossPlatformExec.kt
parentde56cab76f556e5b4af0b8c8cb08d8b482b86d0a (diff)
parent1c3530dcbb50c347f80bef694829dbefe89eca77 (diff)
downloaddokka-6996b1135f61c7d2cb60b0652c6a2691dda31990.tar.gz
dokka-6996b1135f61c7d2cb60b0652c6a2691dda31990.tar.bz2
dokka-6996b1135f61c7d2cb60b0652c6a2691dda31990.zip
Merge branch 'dev-0.11.0'
Diffstat (limited to 'buildSrc/src/main/kotlin/org/jetbrains/CrossPlatformExec.kt')
-rw-r--r--buildSrc/src/main/kotlin/org/jetbrains/CrossPlatformExec.kt63
1 files changed, 63 insertions, 0 deletions
diff --git a/buildSrc/src/main/kotlin/org/jetbrains/CrossPlatformExec.kt b/buildSrc/src/main/kotlin/org/jetbrains/CrossPlatformExec.kt
new file mode 100644
index 00000000..e02bdd61
--- /dev/null
+++ b/buildSrc/src/main/kotlin/org/jetbrains/CrossPlatformExec.kt
@@ -0,0 +1,63 @@
+package org.jetbrains
+
+import org.gradle.api.tasks.AbstractExecTask
+import org.gradle.internal.os.OperatingSystem
+import java.io.File
+import java.nio.file.Files
+import java.nio.file.Path
+import java.nio.file.Paths
+
+open class CrossPlatformExec : AbstractExecTask<CrossPlatformExec>(CrossPlatformExec::class.java) {
+ private val windowsExtensions = listOf(".bat", ".cmd", ".exe")
+ private val unixExtensions = listOf("", ".sh")
+
+ private val isWindows = OperatingSystem.current().isWindows
+
+ override fun exec() {
+ val commandLine: MutableList<String> = this.commandLine
+
+ if (commandLine.isNotEmpty()) {
+ commandLine[0] = findCommand(commandLine[0])
+ }
+
+ if (isWindows && commandLine.isNotEmpty() && commandLine[0].isNotBlank()) {
+ this.commandLine = listOf("cmd", "/c") + commandLine
+ } else {
+ this.commandLine = commandLine
+ }
+
+ super.exec()
+ }
+
+ private fun findCommand(command: String): String {
+ val command = normalizeCommandPaths(command)
+ val extensions = if (isWindows) windowsExtensions else unixExtensions
+
+ return extensions.map { extension ->
+ resolveCommandFromFile(Paths.get("$command$extension"))
+ }.firstOrNull { it.isNotBlank() } ?: command
+ }
+
+ private fun resolveCommandFromFile(commandFile: Path) =
+ if (!Files.isExecutable(commandFile)) {
+ ""
+ } else {
+ commandFile.toAbsolutePath().normalize().toString()
+ }
+
+
+ private fun normalizeCommandPaths(command: String): String {
+ // need to escape backslash so it works with regex
+ val backslashSeparator = "\\"
+ val forwardSlashSeparator = "/"
+
+ // get the actual separator
+ val separator = if (File.separatorChar == '\\') backslashSeparator else File.separator
+
+ return command
+ // first replace all of the backslashes with forward slashes
+ .replace(backslashSeparator, forwardSlashSeparator)
+ // then replace all forward slashes with whatever the separator actually is
+ .replace(forwardSlashSeparator, separator)
+ }
+} \ No newline at end of file