From 79ec42607f3bc090ef40547a01aabcd1cd55886e Mon Sep 17 00:00:00 2001
From: Kamil Doległo <kamilok1965@interia.pl>
Date: Fri, 31 Jan 2020 14:04:42 +0100
Subject: Minor fixes, rewritten CrossPlatformExec.kt

---
 .../main/kotlin/org/jetbrains/BintrayPublishing.kt |  6 +-
 .../main/kotlin/org/jetbrains/CrossPlatformExec.kt | 67 ++++++++++++++++++++++
 .../kotlin/org/jetbrains/DistMavenPublishing.kt    | 10 ++--
 3 files changed, 73 insertions(+), 10 deletions(-)
 create mode 100644 buildSrc/src/main/kotlin/org/jetbrains/CrossPlatformExec.kt

(limited to 'buildSrc/src/main/kotlin/org')

diff --git a/buildSrc/src/main/kotlin/org/jetbrains/BintrayPublishing.kt b/buildSrc/src/main/kotlin/org/jetbrains/BintrayPublishing.kt
index 19e032a8..78e4257f 100644
--- a/buildSrc/src/main/kotlin/org/jetbrains/BintrayPublishing.kt
+++ b/buildSrc/src/main/kotlin/org/jetbrains/BintrayPublishing.kt
@@ -4,9 +4,7 @@ import com.jfrog.bintray.gradle.BintrayExtension
 import org.gradle.api.Project
 import org.gradle.kotlin.dsl.provideDelegate
 
-fun Project.configureBintrayPublication(publication: String) = configureBintrayPublication(listOf(publication))
-
-fun Project.configureBintrayPublication(publications: List<String>) {
+fun Project.configureBintrayPublication(vararg publications: String) {
     val dokka_version: String by this
     val dokka_publication_channel: String by this
     extensions.configure<BintrayExtension>("bintray") {
@@ -24,6 +22,6 @@ fun Project.configureBintrayPublication(publications: List<String>) {
                 name = dokka_version
             }
         }
-        setPublications(*publications.toTypedArray())
+        setPublications(*publications)
     }
 }
\ No newline at end of file
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..feb32cac
--- /dev/null
+++ b/buildSrc/src/main/kotlin/org/jetbrains/CrossPlatformExec.kt
@@ -0,0 +1,67 @@
+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) {
+            if (commandLine.isNotEmpty() && commandLine[0].isNotBlank()) {
+                commandLine
+            }
+            commandLine.add(0, "/c")
+            commandLine.add(0, "cmd")
+        }
+
+        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() ?: 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
diff --git a/buildSrc/src/main/kotlin/org/jetbrains/DistMavenPublishing.kt b/buildSrc/src/main/kotlin/org/jetbrains/DistMavenPublishing.kt
index ec536bd7..175bbd0b 100644
--- a/buildSrc/src/main/kotlin/org/jetbrains/DistMavenPublishing.kt
+++ b/buildSrc/src/main/kotlin/org/jetbrains/DistMavenPublishing.kt
@@ -9,12 +9,10 @@ fun Project.configureDistMaven() { // TODO: This can probably be written cleaner
     val repoLocation = uri(file("${rootProject.buildDir}/dist-maven"))
     var distMaven: MavenArtifactRepository? = null
     pluginManager.withPlugin("maven-publish") {
-        this@configureDistMaven.extensions.findByType(PublishingExtension::class.java)?.let {
-            it.repositories {
-                distMaven = maven {
-                    name = "distMaven"
-                    url = repoLocation
-                }
+        this@configureDistMaven.extensions.findByType(PublishingExtension::class.java)?.repositories {
+            distMaven = maven {
+                name = "distMaven"
+                url = repoLocation
             }
         }
     }
-- 
cgit