From 95020aa6b2383f86f9fdcd9bec7c7b69b92f75bc Mon Sep 17 00:00:00 2001 From: Maksim Kurnikov Date: Thu, 15 Aug 2024 14:52:00 +0300 Subject: [PATCH] fix bundling of aptos cli for custom runIde task --- build.gradle.kts | 25 +++-- src/main/kotlin/org/move/cli/sdks/AptosSdk.kt | 4 +- .../cli/settings/aptos/ChooseAptosCliPanel.kt | 20 ++-- .../move/openapiext/BundledAptosManager.kt | 101 ++++++++++++++++++ .../org/move/openapiext/PluginPathManager.kt | 77 ------------- 5 files changed, 127 insertions(+), 100 deletions(-) create mode 100644 src/main/kotlin/org/move/openapiext/BundledAptosManager.kt delete mode 100644 src/main/kotlin/org/move/openapiext/PluginPathManager.kt diff --git a/build.gradle.kts b/build.gradle.kts index 945588817..3fe38515b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -72,7 +72,7 @@ version = pluginVersion plugins { id("java") kotlin("jvm") version "1.9.25" - id("org.jetbrains.intellij.platform") version "2.0.0" + id("org.jetbrains.intellij.platform") version "2.0.1" id("org.jetbrains.grammarkit") version "2022.3.2.2" id("net.saliman.properties") version "1.5.2" id("org.gradle.idea") @@ -198,10 +198,6 @@ allprojects { duplicatesStrategy = DuplicatesStrategy.EXCLUDE } - testIdeUi { - enabled = false - } - task("downloadAptosBinaries") { val baseUrl = "https://github.com/aptos-labs/aptos-core/releases/download/aptos-cli-v$aptosVersion" doLast { @@ -257,13 +253,8 @@ allprojects { } prepareSandbox { -// enabled = true dependsOn("downloadAptosBinaries") - // copy bin/ directory inside the plugin zip file - from("$rootDir/bin") { - into("$pluginName/bin") - include("**") - } + copyDownloadedAptosBinaries(this) } } @@ -278,6 +269,11 @@ allprojects { // systemProperty("org.move.aptos.bundled.force.unsupported", true) // systemProperty("idea.log.debug.categories", "org.move.cli") } + + prepareSandboxTask { + dependsOn("downloadAptosBinaries") + copyDownloadedAptosBinaries(this) + } } task("resolveDependencies") { @@ -339,6 +335,13 @@ allprojects { //} +fun copyDownloadedAptosBinaries(copyTask: AbstractCopyTask) { + copyTask.from("$rootDir/bin") { + into("$pluginName/bin") + include("**") + } +} + val Project.dependencyCachePath get(): String { val cachePath = file("${rootProject.projectDir}/deps") diff --git a/src/main/kotlin/org/move/cli/sdks/AptosSdk.kt b/src/main/kotlin/org/move/cli/sdks/AptosSdk.kt index be8f60967..076e753a2 100644 --- a/src/main/kotlin/org/move/cli/sdks/AptosSdk.kt +++ b/src/main/kotlin/org/move/cli/sdks/AptosSdk.kt @@ -1,7 +1,7 @@ package org.move.cli.sdks import com.intellij.openapi.util.SystemInfo -import org.move.openapiext.PluginPathManager +import org.move.openapiext.BundledAptosManager import java.io.File data class AptosSdk(val sdksDir: String, val version: String) { @@ -13,7 +13,7 @@ data class AptosSdk(val sdksDir: String, val version: String) { val githubArchiveFileName: String get() { - return "aptos-cli-$version-${PluginPathManager.getCurrentOS()}-x86_64.zip" + return "aptos-cli-$version-${BundledAptosManager.getCurrentOS().title}-x86_64.zip" } val targetFile: File diff --git a/src/main/kotlin/org/move/cli/settings/aptos/ChooseAptosCliPanel.kt b/src/main/kotlin/org/move/cli/settings/aptos/ChooseAptosCliPanel.kt index 1f412e1d1..45903d02a 100644 --- a/src/main/kotlin/org/move/cli/settings/aptos/ChooseAptosCliPanel.kt +++ b/src/main/kotlin/org/move/cli/settings/aptos/ChooseAptosCliPanel.kt @@ -8,7 +8,7 @@ import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory import com.intellij.openapi.ui.popup.JBPopupFactory import com.intellij.openapi.ui.popup.JBPopupFactory.ActionSelectionAid.SPEEDSEARCH import com.intellij.openapi.util.Disposer -import com.intellij.openapi.util.SystemInfo +import com.intellij.openapi.util.io.toNioPathOrNull import com.intellij.openapi.util.registry.Registry import com.intellij.ui.components.DropDownLink import com.intellij.ui.components.JBRadioButton @@ -24,7 +24,8 @@ import org.move.cli.settings.aptos.AptosExecType.LOCAL import org.move.cli.settings.isValidExecutable import org.move.ide.actions.DownloadAptosSDKAction import org.move.ide.notifications.logOrShowBalloon -import org.move.openapiext.PluginPathManager +import org.move.openapiext.BundledAptosManager +import org.move.openapiext.SUPPORTED_PLATFORMS import org.move.openapiext.pathField import org.move.stdext.blankToNull import org.move.stdext.toPathOrNull @@ -43,15 +44,15 @@ enum class AptosExecType { if (Registry.`is`("org.move.aptos.bundled.force.unsupported", false)) { return false } - return !SystemInfo.isMac + return BundledAptosManager.getCurrentOS() in SUPPORTED_PLATFORMS } - fun bundledPath(): String? = PluginPathManager.bundledAptosCli + val bundledAptosCLIPath: Path? get() = BundledAptosManager.getBundledAptosPath() fun aptosExecPath(execType: AptosExecType, localAptosPath: String?): Path? { val pathCandidate = when (execType) { - BUNDLED -> bundledPath()?.toPathOrNull() + BUNDLED -> bundledAptosCLIPath LOCAL -> localAptosPath?.blankToNull()?.toPathOrNull() } return pathCandidate?.takeIf { it.isValidExecutable() } @@ -68,7 +69,7 @@ class ChooseAptosCliPanel(versionUpdateListener: (() -> Unit)?): Disposable { var data: Data get() { - val execType = if (bundledRadioButton.isSelected) BUNDLED else LOCAL + val execType = if (isBundledSelected) BUNDLED else LOCAL val path = localPathField.text.blankToNull() return Data( aptosExecType = execType, @@ -103,6 +104,8 @@ class ChooseAptosCliPanel(versionUpdateListener: (() -> Unit)?): Disposable { private val bundledRadioButton = JBRadioButton("Bundled") private val localRadioButton = JBRadioButton("Local") + private val isBundledSelected get() = bundledRadioButton.isSelected + private val downloadPrecompiledBinaryAction = DownloadAptosSDKAction().also { it.onFinish = { sdk -> bundledRadioButton.isSelected = false @@ -183,10 +186,7 @@ class ChooseAptosCliPanel(versionUpdateListener: (() -> Unit)?): Disposable { private fun updateVersion() { val aptosPath = - when { - bundledRadioButton.isSelected -> AptosExecType.bundledPath() - else -> localPathField.text - }?.toPathOrNull() + if (isBundledSelected) AptosExecType.bundledAptosCLIPath else localPathField.text.toNioPathOrNull() versionLabel.updateAndNotifyListeners(aptosPath) } diff --git a/src/main/kotlin/org/move/openapiext/BundledAptosManager.kt b/src/main/kotlin/org/move/openapiext/BundledAptosManager.kt new file mode 100644 index 000000000..28ef88535 --- /dev/null +++ b/src/main/kotlin/org/move/openapiext/BundledAptosManager.kt @@ -0,0 +1,101 @@ +package org.move.openapiext + +import com.intellij.execution.configurations.GeneralCommandLine +import com.intellij.ide.plugins.IdeaPluginDescriptor +import com.intellij.ide.plugins.PluginManagerCore +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.components.Service +import com.intellij.openapi.components.service +import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.extensions.PluginId +import com.intellij.openapi.util.SystemInfo +import org.move.ide.notifications.logOrShowBalloon +import java.nio.file.Files +import java.nio.file.Path +import java.util.EnumSet + +const val PLUGIN_ID: String = "org.move.lang" + +fun plugin(): IdeaPluginDescriptor = PluginManagerCore.getPlugin(PluginId.getId(PLUGIN_ID))!! + +@Service(Service.Level.APP) +class OpenSSLInfoService { + var openssl3: Boolean = true + + init { + if (!SystemInfo.isWindows && !SystemInfo.isMac) { + val fut = ApplicationManager.getApplication().executeOnPooledThread { + val openSSLVersion = determineOpenSSLVersion() + when { + openSSLVersion.startsWith("OpenSSL 1") -> openssl3 = false + else -> openssl3 = true + } + } + // blocks + fut.get() + } + } + + private fun determineOpenSSLVersion(): String { +// if (!isUnitTestMode) { +// checkIsBackgroundThread() +// } + return GeneralCommandLine("openssl", "version").execute()?.stdoutLines?.firstOrNull() + ?: "OpenSSL 3.0.2" + } +} + +enum class PlatformOS(val title: String, val shortTitle: String, val binaryName: String) { + Windows("Windows", "windows", "aptos.exe"), + MacOS("MacOSX", "macos", "aptos"), + Ubuntu("Ubuntu", "ubuntu", "aptos"), + Ubuntu22("Ubuntu-22.04", "ubuntu22", "aptos"); +} + +val SUPPORTED_PLATFORMS: Set = + EnumSet.of(PlatformOS.Windows, PlatformOS.Ubuntu, PlatformOS.Ubuntu22) + +object BundledAptosManager { + fun getCurrentOS(): PlatformOS { + return when { + SystemInfo.isMac -> PlatformOS.MacOS + SystemInfo.isWindows -> PlatformOS.Windows + else -> { + if (isOpenSSL3) PlatformOS.Ubuntu22 else PlatformOS.Ubuntu + } + } + } + + fun getBundledAptosPath(): Path? { + val platformOS = getCurrentOS() + + val os = platformOS.shortTitle + val binaryName = platformOS.binaryName + val bundledPath = pluginDir().resolve("bin/$os/$binaryName") + if (!Files.exists(bundledPath)) { + log.logOrShowBalloon( + "Bundled Aptos CLI Error: file `$bundledPath` does not exist" + ) + return null + } + val isExecutable = Files.isExecutable(bundledPath) + if (!isExecutable) { + log.logOrShowBalloon( + "Bundled Aptos CLI Error: file `$bundledPath` is not an executable" + ) + val set = bundledPath.toFile().setExecutable(true) + if (!set) { + log.logOrShowBalloon( + "Bundled Aptos CLI Error: " + + "file `$bundledPath` cannot be made executable, not enough permissions" + ) + return null + } + } + return bundledPath + } + private fun pluginDir(): Path = plugin().pluginPath + + private val isOpenSSL3 get() = service().openssl3 + private val log = logger() +} diff --git a/src/main/kotlin/org/move/openapiext/PluginPathManager.kt b/src/main/kotlin/org/move/openapiext/PluginPathManager.kt deleted file mode 100644 index 728d171b4..000000000 --- a/src/main/kotlin/org/move/openapiext/PluginPathManager.kt +++ /dev/null @@ -1,77 +0,0 @@ -package org.move.openapiext - -import com.intellij.execution.configurations.GeneralCommandLine -import com.intellij.ide.plugins.IdeaPluginDescriptor -import com.intellij.ide.plugins.PluginManagerCore -import com.intellij.openapi.application.ApplicationManager -import com.intellij.openapi.components.Service -import com.intellij.openapi.components.service -import com.intellij.openapi.extensions.PluginId -import com.intellij.openapi.util.SystemInfo -import java.nio.file.Files -import java.nio.file.Path - -const val PLUGIN_ID: String = "org.move.lang" - -fun plugin(): IdeaPluginDescriptor = PluginManagerCore.getPlugin(PluginId.getId(PLUGIN_ID))!! - -@Service(Service.Level.APP) -class OpenSSLInfoService { - var openssl3: Boolean = true - - init { - if (!SystemInfo.isWindows) { - val fut = ApplicationManager.getApplication().executeOnPooledThread { - val openSSLVersion = determineOpenSSLVersion() - when { - openSSLVersion.startsWith("OpenSSL 1") -> openssl3 = false - else -> openssl3 = true - } - } - // blocks - fut.get() - } - } - - private fun determineOpenSSLVersion(): String { -// if (!isUnitTestMode) { -// checkIsBackgroundThread() -// } - return GeneralCommandLine("openssl", "version").execute()?.stdoutLines?.firstOrNull() - ?: "OpenSSL 3.0.2" - } -} - -object PluginPathManager { - private fun pluginDir(): Path = plugin().pluginPath - - fun getCurrentOS(): String { - val openssl3 = service().openssl3 - return when { - SystemInfo.isMac -> "MacOSX" - SystemInfo.isWindows -> "Windows" - else -> { - if (openssl3) "Ubuntu-22.04" else "Ubuntu" - } - } - } - - val bundledAptosCli: String? - get() { - val platform = getCurrentOS() - val (os, binaryName) = - when (platform) { - "Ubuntu-22.04" -> "ubuntu22" to "aptos" - "Ubuntu" -> "ubuntu" to "aptos" - "MacOSX" -> "macos" to "aptos" - "Windows" -> "windows" to "aptos.exe" - else -> error("unreachable") - } - val aptosCli = pluginDir().resolve("bin/$os/$binaryName").takeIf { Files.exists(it) } ?: return null - return if (Files.isExecutable(aptosCli) || aptosCli.toFile().setExecutable(true)) { - aptosCli.toString() - } else { - null - } - } -}