Skip to content

Commit

Permalink
fix bundling of aptos cli for custom runIde task
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Aug 15, 2024
1 parent 458959e commit 95020aa
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 100 deletions.
25 changes: 14 additions & 11 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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") {
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/move/cli/sdks/AptosSdk.kt
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions src/main/kotlin/org/move/cli/settings/aptos/ChooseAptosCliPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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() }
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
101 changes: 101 additions & 0 deletions src/main/kotlin/org/move/openapiext/BundledAptosManager.kt
Original file line number Diff line number Diff line change
@@ -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<PlatformOS> =
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<OpenSSLInfoService>().openssl3
private val log = logger<BundledAptosManager>()
}
77 changes: 0 additions & 77 deletions src/main/kotlin/org/move/openapiext/PluginPathManager.kt

This file was deleted.

0 comments on commit 95020aa

Please sign in to comment.