Skip to content

Commit

Permalink
fix minor things (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov authored Nov 13, 2024
1 parent f027c5f commit 54b2413
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 47 deletions.
11 changes: 5 additions & 6 deletions src/main/kotlin/org/move/cli/MoveProjectsSyncTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class MoveProjectsSyncTask(
projects: MutableList<MoveProject>,
context: SyncContext
) {
val projectRoot = moveTomlFile.parent?.toNioPathOrNull() ?: error("cannot be invalid path")
val packageRoot = moveTomlFile.parent?.toNioPathOrNull() ?: error("cannot be invalid path")
var (moveProject, rootMoveToml) =
runReadAction {
val tomlFile = moveTomlFile.toTomlFile(project)!!
Expand All @@ -156,7 +156,7 @@ class MoveProjectsSyncTask(
)
} else {
context.runWithChildProgress("Fetch dependencies") { childContext ->
val taskResult = fetchDependencyPackages(childContext, projectRoot)
val taskResult = fetchDependencyPackages(childContext, packageRoot)
if (taskResult is TaskResult.Err) {
moveProject = moveProject.copy(
fetchDepsStatus = UpdateStatus.UpdateFailed("Failed to fetch dependency packages")
Expand Down Expand Up @@ -204,17 +204,16 @@ class MoveProjectsSyncTask(
}

private fun fetchDependencyPackages(
childContext: SyncContext, projectRoot: Path
childContext: SyncContext, packageRoot: Path
): TaskResult<ProcessOutput> {
val skipLatest = project.moveSettings.skipFetchLatestGitDeps
val aptos = project.getAptosCli(parentDisposable = this)
return when {
aptos == null -> TaskResult.Err("Invalid Aptos CLI configuration")
else -> {
val aptosProcessOutput =
aptos.fetchPackageDependencies(
projectRoot,
skipLatest,
project,
packageRoot,
runner = {
// populate progress bar
addProcessListener(object: AnsiEscapedProcessAdapter() {
Expand Down
10 changes: 8 additions & 2 deletions src/main/kotlin/org/move/cli/runConfigurations/AptosRunState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import com.intellij.execution.process.KillableColoredProcessHandler
import com.intellij.execution.process.ProcessHandler
import com.intellij.execution.process.ProcessTerminatedListener
import com.intellij.execution.runners.ExecutionEnvironment
import com.intellij.openapi.util.Disposer
import org.move.cli.MoveFileHyperlinkFilter
import org.move.cli.runConfigurations.CommandConfigurationBase.CleanConfiguration

abstract class AptosRunStateBase(
environment: ExecutionEnvironment, val cleanConfiguration: CleanConfiguration.Ok
environment: ExecutionEnvironment,
val cleanConfiguration: CleanConfiguration.Ok
): CommandLineState(environment) {

val project = environment.project
Expand All @@ -21,7 +23,11 @@ abstract class AptosRunStateBase(
val generalCommandLine =
commandLine.toGeneralCommandLine(cleanConfiguration.aptosPath, emulateTerminal = true)
val handler = KillableColoredProcessHandler(generalCommandLine)
consoleBuilder.console.attachToProcess(handler)

val console = consoleBuilder.console
Disposer.register(this.environment, console)
console.attachToProcess(handler)

ProcessTerminatedListener.attach(handler) // shows exit code upon termination
return handler
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.execution.configurations.*
import com.intellij.execution.runners.ExecutionEnvironment
import com.intellij.openapi.options.advanced.AdvancedSettings
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.NlsContexts
import com.intellij.util.execution.ParametersListUtil
import org.jdom.Element
Expand All @@ -17,6 +18,7 @@ import org.move.cli.runConfigurations.test.AptosTestRunState
import org.move.cli.settings.aptosCliPath
import org.move.cli.writePath
import org.move.cli.writeString
import org.move.openapiext.rootPluginDisposable
import org.move.stdext.exists
import java.nio.file.Path

Expand Down Expand Up @@ -55,6 +57,11 @@ abstract class CommandConfigurationBase(

override fun getState(executor: Executor, environment: ExecutionEnvironment): AptosRunStateBase? {
val config = clean().ok ?: return null

// environment is disposable to which all internal RunState disposables are connected
// todo: find shorter living disposable?
Disposer.register(project.rootPluginDisposable, environment)

return if (showTestToolWindow(config.cmd)) {
AptosTestRunState(environment, config)
} else {
Expand Down
47 changes: 28 additions & 19 deletions src/main/kotlin/org/move/cli/runConfigurations/aptos/Aptos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import com.intellij.util.execution.ParametersListUtil
import org.move.cli.MoveProject
import org.move.cli.MvConstants
import org.move.cli.runConfigurations.AptosCommandLine
import org.move.cli.settings.moveSettings
import org.move.openapiext.*
import org.move.openapiext.common.isUnitTestMode
import org.move.stdext.CollectionBuilder
import org.move.stdext.RsResult
import org.move.stdext.RsResult.Err
import org.move.stdext.RsResult.Ok
Expand Down Expand Up @@ -61,41 +63,31 @@ data class Aptos(val cliLocation: Path, val parentDisposable: Disposable?): Disp
}

fun fetchPackageDependencies(
projectDir: Path,
skipLatest: Boolean,
project: Project,
packageRoot: Path,
runner: CapturingProcessHandler.() -> ProcessOutput = { runProcessWithGlobalProgress() }
): AptosProcessResult<Unit> {
val commandLine =
AptosCommandLine(
subCommand = "move compile",
arguments = listOfNotNull(
"--skip-fetch-latest-git-deps".takeIf { skipLatest }
),
workingDirectory = projectDir
arguments = compileArguments(project),
workingDirectory = packageRoot
)
return executeAptosCommandLine(commandLine, colored = true, runner = runner)
}

fun checkProject(args: AptosCompileArgs): RsResult<ProcessOutput, RsProcessExecutionException.Start> {
fun checkProject(
project: Project,
args: AptosCompileArgs
): RsResult<ProcessOutput, RsProcessExecutionException.Start> {
// val useClippy = args.linter == ExternalLinter.CLIPPY
// && !checkNeedInstallClippy(project, args.cargoProjectDirectory)
// val checkCommand = if (useClippy) "clippy" else "check"
val extraArguments = ParametersListUtil.parse(args.extraArguments)
val commandLine =
AptosCommandLine(
"move compile",
buildList {
// add("--message-format=json")
if ("--skip-fetch-latest-git-deps" !in extraArguments) {
add("--skip-fetch-latest-git-deps")
}
if (args.enableMove2) {
if ("--move-2" !in extraArguments) {
add("--move-2")
}
}
addAll(ParametersListUtil.parse(args.extraArguments))
},
arguments = compileArguments(project) { addAll(extraArguments) },
args.moveProjectDirectory,
)
return executeCommandLine(commandLine).ignoreExitCode()
Expand Down Expand Up @@ -207,6 +199,23 @@ data class Aptos(val cliLocation: Path, val parentDisposable: Disposable?): Disp
return Ok(aptosProcessOutput)
}

private fun compileArguments(
project: Project,
builder: (CollectionBuilder<String>.() -> Unit)? = null
): List<String> {
val settings = project.moveSettings
val initialArguments = buildList { builder?.let { this.it() } }
return buildList {
addAll(initialArguments)
if (settings.enableMove2 && "--move-2" !in initialArguments) {
add("--move-2")
}
if (settings.skipFetchLatestGitDeps && "--skip-fetch-latest-git-deps" !in initialArguments) {
add("--skip-fetch-latest-git-deps")
}
}
}

override fun dispose() {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package org.move.cli.runConfigurations.aptos

import com.fasterxml.jackson.core.JacksonException
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import org.intellij.lang.annotations.Language
import org.move.stdext.blankToNull

sealed class AptosExitStatus(val message: String) {
class Result(message: String): AptosExitStatus(message)
Expand All @@ -14,22 +16,23 @@ sealed class AptosExitStatus(val message: String) {
companion object {
@Throws(JacksonException::class)
fun fromJson(@Language("JSON") json: String): AptosExitStatus {
val parsedResult = JSON_MAPPER.readValue(json, AptosJsonResult::class.java)
return when {
parsedResult.Error != null -> Error(parsedResult.Error)
parsedResult.Result != null -> Result(parsedResult.Result)
else -> Malformed(json)
val rootNode = JSON_MAPPER.readTree(json)
if (rootNode.has("Result")) {
return Result(nodeToText(rootNode.get("Result")))
}
if (rootNode.has("Error")) {
return Error(nodeToText(rootNode.get("Error")))
}
return Malformed(json)
}

private fun nodeToText(jsonNode: JsonNode): String {
// blank for containers
return jsonNode.asText().blankToNull() ?: jsonNode.toString()
}
}
}

@Suppress("PropertyName")
private data class AptosJsonResult(
val Result: String?,
val Error: String?
)

private val JSON_MAPPER: ObjectMapper = ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerKotlinModule()
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ object RsExternalLinterUtils {

override fun run(indicator: ProgressIndicator) {
widget?.inProgress = true
future.complete(check(aptosCli, args))
future.complete(check(project, aptosCli, args))
}

override fun onFinished() {
Expand All @@ -118,13 +118,14 @@ object RsExternalLinterUtils {
}

private fun check(
project: Project,
aptosCli: Aptos,
aptosCompileArgs: AptosCompileArgs
): RsExternalLinterResult? {
ProgressManager.checkCanceled()
val started = Instant.now()
val output = aptosCli
.checkProject(aptosCompileArgs)
.checkProject(project, aptosCompileArgs)
.unwrapOrElse { e ->
LOG.error(e)
return null
Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/org/move/ide/refactoring/MvImportOptimizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ class MvImportOptimizer : ImportOptimizer {
private fun removeCurlyBracesIfPossible(rootUseSpeck: MvUseSpeck, psiFactory: MvPsiFactory) {
val itemUseSpeck = rootUseSpeck.useGroup?.asTrivial ?: return

val newUseSpeck = psiFactory.useSpeck("0x1::dummy::call")
val newUseSpeck = psiFactory.useSpeck("0x1::dummy::call as mycall")
val newUseSpeckPath = newUseSpeck.path
newUseSpeckPath.path?.replace(rootUseSpeck.path)
itemUseSpeck.path.identifier?.let { newUseSpeckPath.identifier?.replace(it) }

val useAlias = itemUseSpeck.useAlias
if (useAlias != null) {
newUseSpeck.add(useAlias)
val oldUseAlias = itemUseSpeck.useAlias
if (oldUseAlias != null) {
newUseSpeck.useAlias?.replace(oldUseAlias)
} else {
newUseSpeck.useAlias?.delete()
}

rootUseSpeck.replace(newUseSpeck)
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/org/move/openapiext/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ inline fun <R> Project.nonBlocking(crossinline block: () -> R, crossinline uiCon
}

@Service(PROJECT)
class RootPluginDisposable: Disposable {
override fun dispose() {}
}
class RootPluginDisposable(val project: Project): Disposable.Default

val Project.rootPluginDisposable get() = this.service<RootPluginDisposable>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,23 @@ class AptosExitStatusTest: MvTestBase() {
)
check(status is AptosExitStatus.Malformed)
}

fun `test parse array of items`() {
val status = AptosExitStatus.fromJson(
"""
{
"Result": [
"0000000000000000000000000000000000000000000000000000000000000001::system_addresses",
"0000000000000000000000000000000000000000000000000000000000000001::guid"
]
}
}
"""
)
check(status is AptosExitStatus.Result)
check(status.message ==
"[\"0000000000000000000000000000000000000000000000000000000000000001::system_addresses\"," +
"\"0000000000000000000000000000000000000000000000000000000000000001::guid\"]",
{ status.message })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,32 @@ module 0x1::main {
}
""")

fun `test unused import with Self as`() = doTest("""
module 0x1::pool {
struct X1 {}
public fun create_pool() {}
}
module 0x1::main {
use 0x1::pool::{Self as mypool, X1};
fun main() {
mypool::create_pool();
}
}
""", """
module 0x1::pool {
struct X1 {}
public fun create_pool() {}
}
module 0x1::main {
use 0x1::pool::Self as mypool;
fun main() {
mypool::create_pool();
}
}
""")

fun `test duplicate self import`() = doTest("""
module 0x1::pool {
struct X1 {}
Expand Down Expand Up @@ -377,5 +403,33 @@ module 0x1::main {
// let _a = string::utf8(b"hello");
// }
//}
// """)

// todo
// fun `test vector import should be test_only if used only in tests`() = doTest("""
//module 0x1::vector {
// public fun call() {}
//}
//module 0x1::main {
// use 0x1::vector;
//
// #[test]
// fun test_main() {
// vector::call();
// }
//}
// """, """
//module 0x1::vector {
// public fun call() {}
//}
//module 0x1::main {
// #[test_only]
// use 0x1::vector;
//
// #[test]
// fun test_main() {
// vector::call();
// }
//}
// """)
}

0 comments on commit 54b2413

Please sign in to comment.