Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add periodic update check for aptos/revela/movefmt #262

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions src/main/kotlin/org/move/bytecode/AptosDecompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ import com.intellij.openapi.vfs.newvfs.BulkFileListener
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
import org.move.cli.runConfigurations.aptos.Aptos
import org.move.openapiext.pathAsPath
import org.move.openapiext.rootPath
import org.move.openapiext.rootPluginDisposable
import org.move.stdext.RsResult
import org.move.stdext.unwrapOrElse
import java.io.File
import java.nio.file.Path
import kotlin.io.path.exists
import kotlin.io.path.relativeTo

// todo: this is disabled for now, it's a process which requires ReadAction, and that's why
// it needs to be run in the indexing phase
Expand All @@ -33,13 +31,6 @@ class AptosBytecodeDecompiler: BinaryFileDecompiler {
val bytes = file.readBytes()
return LoadTextUtil.getTextByBinaryPresentation(bytes, file)
}
// val project =
// ProjectLocator.getInstance().getProjectsForFile(file).firstOrNull { it?.isAptosConfigured == true }
// ?: ProjectManager.getInstance().defaultProject.takeIf { it.isAptosConfigured }
// ?: return file.readText()
// val targetFileDir = getDecompilerTargetFileDirOnTemp(project, file) ?: return file.readText()
// val targetFile = decompileFile(project, file, targetFileDir) ?: return file.readText()
// return LoadTextUtil.loadText(targetFile)
}

fun decompileFileToTheSameDir(aptos: Aptos, file: VirtualFile): RsResult<VirtualFile, String> {
Expand Down Expand Up @@ -79,15 +70,6 @@ class AptosBytecodeDecompiler: BinaryFileDecompiler {
return RsResult.Ok(decompiledFile)
}

fun getDecompilerTargetFileDirOnTemp(project: Project, file: VirtualFile): Path? {
val rootDecompilerDir = decompiledArtifactsFolder()
val projectDecompilerDir = rootDecompilerDir.resolve(project.name)
val root = project.rootPath ?: return null
val relativeFilePath = file.parent.pathAsPath.relativeTo(root)
val targetFileDir = projectDecompilerDir.toPath().resolve(relativeFilePath)
return targetFileDir
}

fun sourceFileName(file: VirtualFile): String {
val fileName = file.name
return "$fileName.move"
Expand All @@ -97,12 +79,10 @@ class AptosBytecodeDecompiler: BinaryFileDecompiler {
val fileName = file.name
return "$fileName#decompiled.move"
}

companion object {
fun decompiledArtifactsFolder() = File(FileUtil.getTempDirectory(), "intellij-move-decompiled-artifacts")
}
}

val DECOMPILED_ARTIFACTS_FOLDER = File(FileUtil.getTempDirectory(), "intellij-move-decompiled-artifacts")

fun Project.createDisposableOnFileChange(file: VirtualFile): Disposable {
val filePath = file.path
val disposable = Disposer.newDisposable("Dispose on any change to the ${file.name} file")
Expand Down
67 changes: 65 additions & 2 deletions src/main/kotlin/org/move/cli/runConfigurations/aptos/Aptos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ package org.move.cli.runConfigurations.aptos
import com.fasterxml.jackson.core.JacksonException
import com.intellij.execution.configuration.EnvironmentVariablesData
import com.intellij.execution.process.CapturingProcessHandler
import com.intellij.execution.process.ProcessEvent
import com.intellij.execution.process.ProcessListener
import com.intellij.execution.process.ProcessOutput
import com.intellij.openapi.Disposable
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.Key
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.execution.ParametersListUtil
import org.move.cli.MoveProject
import org.move.cli.MvConstants
import org.move.cli.externalLinter.ExternalLinter
import org.move.cli.runConfigurations.AptosCommandLine
import org.move.cli.settings.moveSettings
import org.move.cli.update.AptosTool
import org.move.cli.update.UpdateCheckResult
import org.move.cli.update.UpdateCheckResult.*
import org.move.openapiext.*
import org.move.openapiext.common.isUnitTestMode
import org.move.stdext.RsResult
Expand Down Expand Up @@ -79,7 +84,7 @@ data class Aptos(val cliLocation: Path, val parentDisposable: Disposable?): Disp
fun checkProject(
project: Project,
linterArgs: AptosExternalLinterArgs
): RsResult<ProcessOutput, RsProcessExecutionException.Start> {
): RsResult<ProcessOutput, RsProcessExecutionOrDeserializationException> {
val lintCommand = linterArgs.linter.command
val extraArguments = ParametersListUtil.parse(linterArgs.extraArguments)
val arguments = when (linterArgs.linter) {
Expand Down Expand Up @@ -175,6 +180,62 @@ data class Aptos(val cliLocation: Path, val parentDisposable: Disposable?): Disp
return executeCommandLine(commandLine)
}

fun checkForToolUpdate(tool: AptosTool): RsResult<UpdateCheckResult, RsProcessExecutionOrDeserializationException> {
val commandLine =
AptosCommandLine(
subCommand = "update",
arguments = buildList { add(tool.id); add("--check") },
workingDirectory = null
)
val processOutput = executeAptosCommandLine(commandLine)
.unwrapOrElse { return Err(it) }

val checkResult = when (val exitStatus = processOutput.exitStatus) {
is AptosExitStatus.Result -> {
val message = exitStatus.message
run {
when {
message.startsWith("Update is available") -> {
val match = APTOS_VERSION_REGEX.find(message) ?: return@run MalformedResult(message)
UpdateIsAvailable(match.value)
}
message.startsWith("Already up to date") -> {
val match = APTOS_VERSION_REGEX.find(message) ?: return@run MalformedResult(message)
UpToDate(match.value)
}
else -> MalformedResult(message)
}
}
}
is AptosExitStatus.Error -> UpdateError(exitStatus.message)
is AptosExitStatus.Malformed -> MalformedResult(exitStatus.message)
}
return Ok(checkResult)
}

fun doToolUpdate(tool: AptosTool, processListener: ProcessListener): AptosProcessResult<Unit> {
val commandLine =
AptosCommandLine(
subCommand = "update",
arguments = buildList { add(tool.id) },
workingDirectory = null
)
val yesNoListener = object: ProcessListener {
override fun onTextAvailable(event: ProcessEvent, outputType: Key<*>) {
if (event.text.contains("Do you want to continue? [Y/n]")) {
val handler = event.processHandler
handler.processInput?.use { it.write("Y".toByteArray()) }
}
}
}
val compositeListener = CompositeProcessListener(yesNoListener, processListener)
val output = executeAptosCommandLine(commandLine, listener = compositeListener)
.unwrapOrElse {
return Err(it)
}
return Ok(output)
}

private fun executeCommandLine(
commandLine: AptosCommandLine,
colored: Boolean = false,
Expand Down Expand Up @@ -230,4 +291,6 @@ data class Aptos(val cliLocation: Path, val parentDisposable: Disposable?): Disp
}


val MoveProject.workingDirectory: Path get() = this.currentPackage.contentRoot.pathAsPath
val MoveProject.workingDirectory: Path get() = this.currentPackage.contentRoot.pathAsPath

val APTOS_VERSION_REGEX = Regex("""\d+.\d+.\d""")
7 changes: 2 additions & 5 deletions src/main/kotlin/org/move/cli/sdks/DownloadAptosSdkDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.intellij.ui.components.JBTextField
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.runConfigurations.aptos.APTOS_VERSION_REGEX
import org.move.openapiext.pathField
import javax.swing.JComponent

Expand Down Expand Up @@ -36,7 +37,7 @@ class DownloadAptosSdkDialog(val project: Project?): DialogWrapper(project, true
.align(AlignX.FILL)
.columns(10)
.validationOnApply { field ->
if (!field.text.matches(VERSION_REGEX)) {
if (!field.text.matches(APTOS_VERSION_REGEX)) {
ValidationInfo("Version is invalid. Should be in form of MAJOR.MINOR.PATCH")
} else {
null
Expand All @@ -54,8 +55,4 @@ class DownloadAptosSdkDialog(val project: Project?): DialogWrapper(project, true
}
}
}

companion object {
private val VERSION_REGEX = Regex("""\d+.\d+.\d""")
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package org.move.cli.settings

import com.intellij.openapi.Disposable
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.StoragePathMacros
import com.intellij.openapi.components.service
import com.intellij.openapi.options.advanced.AdvancedSettings
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.vfs.VirtualFile
import org.move.bytecode.createDisposableOnFileChange
import org.move.cli.runConfigurations.aptos.Aptos
import org.move.cli.settings.MvProjectSettingsService.MoveProjectSettings
import org.move.cli.settings.aptos.AptosExecType
import org.move.cli.update.PeriodicCheckForUpdatesService
import org.move.ide.notifications.updateAllNotifications
import org.move.openapiext.common.isUnitTestMode
import org.move.stdext.exists
import org.move.stdext.isExecutableFile
import java.nio.file.Path
Expand Down Expand Up @@ -70,6 +73,17 @@ class MvProjectSettingsService(
}
}

init {
// load update tool check scheduler
if (!isUnitTestMode) {
if (PeriodicCheckForUpdatesService.isEnabled) {
// do not load in tests
project.service<PeriodicCheckForUpdatesService>()
updateAllNotifications(project)
}
}
}

override fun createSettingsChangedEvent(
oldEvent: MoveProjectSettings,
newEvent: MoveProjectSettings
Expand Down
Loading
Loading