Skip to content

Commit

Permalink
Merge pull request #166 from pontem-network/url-param-for-decompiler
Browse files Browse the repository at this point in the history
decompiler dialog: add network url, better validation
  • Loading branch information
mkurnikov authored Jun 29, 2024
2 parents 546f268 + 41d83e9 commit 8e7da61
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
40 changes: 37 additions & 3 deletions src/main/kotlin/org/move/bytecode/FetchAptosPackageDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.columns
import com.intellij.ui.dsl.builder.panel
import org.move.cli.settings.getAptosCli
import org.move.cli.settings.isAptosConfigured
import org.move.openapiext.RsProcessResult
import org.move.openapiext.pathField
import org.move.stdext.blankToNull
import org.move.stdext.unwrapOrElse
import javax.swing.JComponent
import kotlin.io.path.Path
Expand All @@ -33,6 +35,7 @@ class FetchAptosPackageDialog(val project: Project): DialogWrapper(project, true

val profileField = JBTextField("default")
val nodeApiKey = JBTextField()
val networkUrl = JBTextField()
val connectionTimeout = IntegerField("Connection timeout", 0, Int.MAX_VALUE)

init {
Expand All @@ -42,19 +45,37 @@ class FetchAptosPackageDialog(val project: Project): DialogWrapper(project, true
outputDirField.text = project.basePath.orEmpty()
decompileCheckbox.isSelected = true

connectionTimeout.defaultValue = -1
connectionTimeout.setDefaultValueText("30")

init()

if (!project.isAptosConfigured) {
setErrorText("Aptos CLI is not provided in the plugin settings")
okAction.isEnabled = false
}
}

override fun createCenterPanel(): JComponent {
return panel {
row("Address:") {
cell(addressTextField).align(AlignX.FILL)
.validationOnApply {
if (it.text.isBlank()) {
return@validationOnApply error("Cannot be empty")
}
null
}
.comment("Address of the account containing the package.")
}
row("Package name:") {
cell(packageTextField).align(AlignX.FILL)
.validationOnApply {
if (it.text.isBlank()) {
return@validationOnApply error("Cannot be empty")
}
null
}
}
row("Output directory:") { cell(outputDirField).align(AlignX.FILL) }
row { cell(decompileCheckbox) }
Expand All @@ -69,6 +90,12 @@ class FetchAptosPackageDialog(val project: Project): DialogWrapper(project, true
"such as the REST URL, the Faucet URL, and the private key arguments."
)
}
row("Network URL:") {
cell(networkUrl).align(AlignX.FILL)
.comment(
"URL to a fullnode on the network. Leaving it blank defaults to the URL in the `default` profile."
)
}
row("Node API Key:") {
cell(nodeApiKey).align(AlignX.FILL)
.comment(
Expand All @@ -93,12 +120,18 @@ class FetchAptosPackageDialog(val project: Project): DialogWrapper(project, true
val outputDir = this.outputDirField.text
val decompile = this.decompileCheckbox.isSelected

// cannot be null, it's checked at time of window creation
val aptos = project.getAptosCli(this.disposable) ?: return

val aptosProfile = this.profileField.text
val nodeApiKey = this.nodeApiKey.text
val aptosProfile = this.profileField.text.blankToNull()
val nodeApiKey = this.nodeApiKey.text.blankToNull()
val networkUrl = this.networkUrl.text.blankToNull()
val connectionTimeout = this.connectionTimeout.value

if (okAction.isEnabled) {
applyFields()
}

val downloadTask = object: Task.WithResult<RsProcessResult<ProcessOutput>, Exception>(
project,
"Downloading $accountAddress::$packageName...",
Expand All @@ -107,6 +140,7 @@ class FetchAptosPackageDialog(val project: Project): DialogWrapper(project, true
override fun compute(indicator: ProgressIndicator): RsProcessResult<ProcessOutput> {
return aptos.downloadPackage(project, accountAddress, packageName, outputDir,
profile = aptosProfile,
networkUrl = networkUrl,
connectionTimeoutSecs = connectionTimeout,
nodeApiKey = nodeApiKey,
runner = { runProcessWithProgressIndicator(indicator) })
Expand Down Expand Up @@ -136,7 +170,7 @@ class FetchAptosPackageDialog(val project: Project): DialogWrapper(project, true
}
}

super.doOKAction()
close(OK_EXIT_CODE)
}

override fun getPreferredFocusedComponent(): JComponent = addressTextField
Expand Down
18 changes: 13 additions & 5 deletions src/main/kotlin/org/move/cli/runConfigurations/aptos/Aptos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ data class Aptos(val cliLocation: Path, val parentDisposable: Disposable?): Disp
accountAddress: String,
packageName: String,
outputDir: String,
profile: String = "default",
connectionTimeoutSecs: Int = 30,
profile: String? = null,
networkUrl: String? = null,
connectionTimeoutSecs: Int = -1,
nodeApiKey: String? = null,
runner: CapturingProcessHandler.() -> ProcessOutput = {
runProcessWithGlobalProgress(
Expand All @@ -139,11 +140,18 @@ data class Aptos(val cliLocation: Path, val parentDisposable: Disposable?): Disp
add("--package"); add(packageName)
add("--bytecode")
add("--output-dir"); add(outputDir)
add("--profile"); add(profile)
add("--connection-timeout-secs"); add(connectionTimeoutSecs.toString())
if (nodeApiKey != null) {
if (!profile.isNullOrBlank()) {
add("--profile"); add(profile)
}
if (!networkUrl.isNullOrBlank()) {
add("--url"); add(networkUrl)
}
if (!nodeApiKey.isNullOrBlank()) {
add("--node-api-key"); add(nodeApiKey)
}
if (connectionTimeoutSecs != -1) {
add("--connection-timeout-secs"); add(connectionTimeoutSecs.toString())
}
},
workingDirectory = project.basePath?.let { Path.of(it) },
environmentVariables = EnvironmentVariablesData.DEFAULT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ fun Project.getAptosCli(parentDisposable: Disposable? = null): Aptos? {
return aptos
}

val Project.isAptosConfigured: Boolean get() = this.getAptosCli() != null

fun Project.getAptosCliDisposedOnFileChange(file: VirtualFile): Aptos? {
val anyChangeDisposable = this.createDisposableOnFileChange(file)
return this.getAptosCli(anyChangeDisposable)
Expand Down

0 comments on commit 8e7da61

Please sign in to comment.