Skip to content

Commit

Permalink
兼容MacOS Catalina执行命令不生效的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-dhl committed Nov 25, 2022
1 parent 08c8884 commit d15c439
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'com.hi.dhl'
version '2.3'
version '2.4'

repositories {
mavenLocal()
Expand Down Expand Up @@ -103,6 +103,10 @@ patchPluginXml {
<ul>
<li>执行命时检查是否安装sshpass</li>
</ul>
<b>Version 2.4</b>
<ul>
<li>兼容MacOS Catalina执行命令不生效的问题</li>
</ul>
</html>
"""
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/hi/dhl/console/CommandManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ object CommandManager {
|| password.equals(R.String.ui.tfRemotePassword)) {
return ""
} else {
return "set +e;chmod 777 ${shellInstallSshpass} && bash ${shellInstallSshpass} && sshpass -p '${remoteMachineInfo.remoteUserPassword}' "
return "set +e;chmod 777 ${shellInstallSshpass} && bash ${shellInstallSshpass}; ${getSshpassPath()} -p '${remoteMachineInfo.remoteUserPassword}' "
}
}

fun getSshpassPath() = "/usr/local/bin/sshpass"

fun makeSshpassCommand(remoteMachineInfo: RemoteMachineInfo): String{
if (SystemInfoRt.isWindows) {
Expand All @@ -172,7 +173,7 @@ object CommandManager {
) {
return ""
} else {
return "sshpass -p '${remoteMachineInfo.remoteUserPassword}' "
return "${getSshpassPath()} -p '${remoteMachineInfo.remoteUserPassword}' "
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/hi/dhl/ext/PluginVersion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ object PluginVersion {
val VERSION_2_1 = "2.1"
val VERSION_2_2 = "2.2"
val VERSION_2_3 = "2.3"
val CURRENT_VERSION = VERSION_2_3
val VERSION_2_4 = "2.4"
val CURRENT_VERSION = VERSION_2_4
}

fun upgrad(localProjectBasePath: String) {
Expand Down
41 changes: 38 additions & 3 deletions src/main/java/com/hi/dhl/utils/ExecutableUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ object ExecutableUtils {

fun findExecutable(): String {
LogUtils.logI(executablePath)
if (!executablePath.isNullOrEmpty()) {
if (!executablePath.isEmpty()) {
return executablePath
}
executablePath = findExecutableOnPath("bash")
return if (!executablePath.isNullOrEmpty()) executablePath else "/bin/bash"
if (compareVersion(SystemInfoRt.OS_VERSION, "10.15.0") >= 0) {
executablePath = findExecutableOnPath("zsh")
if (executablePath.isEmpty()) {
executablePath = findExecutableOnPath("bash")
}
} else {
executablePath = findExecutableOnPath("bash")
}
return if (!executablePath.isEmpty()) executablePath
else if (compareVersion(SystemInfoRt.OS_VERSION, "10.15.0") >= 0) "/bin/zsh"
else "/bin/bash"
}

fun findExecutableOnPath(name: String): String {
Expand All @@ -36,4 +45,30 @@ object ExecutableUtils {
return ""
}

fun compareVersion(version1: String?, version2: String?): Int {
if (version1 == null || version2 == null) {
return -1
}
val versions1 = version1.split(".")
val versions2 = version2.split(".")
var index = 0
val minLength = Math.min(versions1.size, versions2.size)
var diff = 0

while (index < minLength
&& (versions1[index].length - versions2[index].length) == 0
&& versions1[index].compareTo(versions2[index]) == 0
) {
++index
}
if(index < minLength){
diff = versions1[index].compareTo(versions2[index])
}
return diff
}
}

fun main() {
val result = ExecutableUtils.compareVersion("10.15.7" ,"10.14.7");
println(result)
}

0 comments on commit d15c439

Please sign in to comment.