Skip to content

Commit 356aa77

Browse files
authored
Refactor commandLine code a bit (#242)
* get rid of toColoredCommandLine * remove unused run configuration parameter * refactor AptosCommandLine * add proper ignoreExitCode
1 parent eaa55e3 commit 356aa77

File tree

7 files changed

+78
-76
lines changed

7 files changed

+78
-76
lines changed

src/main/kotlin/org/move/cli/runConfigurations/AptosCommandLine.kt

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,16 @@ import com.intellij.execution.configuration.EnvironmentVariablesData
44
import com.intellij.execution.configurations.GeneralCommandLine
55
import com.intellij.execution.configurations.PtyCommandLine
66
import com.intellij.util.execution.ParametersListUtil
7+
import org.move.cli.tools.MvCommandLine
78
import java.nio.file.Path
89

9-
data class AptosCommandLine(
10+
class AptosCommandLine(
1011
val subCommand: String?,
11-
val arguments: List<String> = emptyList(),
12-
val workingDirectory: Path? = null,
13-
val environmentVariables: EnvironmentVariablesData = EnvironmentVariablesData.DEFAULT
14-
) {
15-
val commandLineString: String
16-
get() = ParametersListUtil.join(subCommand?.split(" ").orEmpty() + arguments)
17-
18-
fun toGeneralCommandLine(cliExePath: Path): GeneralCommandLine {
19-
val generalCommandLine = GeneralCommandLine()
20-
.withExePath(cliExePath.toString())
21-
// subcommand can be null
22-
.withParameters(subCommand?.split(" ").orEmpty())
23-
.withParameters(this.arguments)
24-
.withWorkingDirectory(this.workingDirectory)
25-
.withCharset(Charsets.UTF_8)
26-
// disables default coloring for stderr
27-
.withRedirectErrorStream(true)
28-
this.environmentVariables.configureCommandLine(generalCommandLine, true)
29-
return generalCommandLine
30-
}
31-
32-
fun toColoredCommandLine(cliExePath: Path): GeneralCommandLine {
33-
// preudo-tty emulation makes aptos-cli recognize console as tty and show ANSI colors
34-
val generalCommandLine = PtyCommandLine()
35-
.withExePath(cliExePath.toString())
36-
// subcommand can be null
37-
.withParameters(subCommand?.split(" ").orEmpty())
38-
.withParameters(this.arguments)
39-
.withWorkingDirectory(this.workingDirectory)
40-
.withCharset(Charsets.UTF_8)
41-
// disables default coloring for stderr
42-
.withRedirectErrorStream(true)
43-
this.environmentVariables.configureCommandLine(generalCommandLine, true)
44-
return generalCommandLine
45-
}
46-
}
12+
arguments: List<String> = emptyList(),
13+
workingDirectory: Path? = null,
14+
environmentVariables: EnvironmentVariablesData = EnvironmentVariablesData.DEFAULT
15+
): MvCommandLine(
16+
subCommand?.split(" ").orEmpty() + arguments,
17+
workingDirectory,
18+
environmentVariables
19+
)

src/main/kotlin/org/move/cli/runConfigurations/AptosRunState.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@ import com.intellij.execution.process.ProcessHandler
77
import com.intellij.execution.process.ProcessTerminatedListener
88
import com.intellij.execution.runners.ExecutionEnvironment
99
import org.move.cli.MoveFileHyperlinkFilter
10+
import org.move.cli.runConfigurations.CommandConfigurationBase.CleanConfiguration
1011

1112
abstract class AptosRunStateBase(
12-
environment: ExecutionEnvironment,
13-
val runConfiguration: CommandConfigurationBase,
14-
val config: CommandConfigurationBase.CleanConfiguration.Ok
13+
environment: ExecutionEnvironment, val cleanConfiguration: CleanConfiguration.Ok
1514
): CommandLineState(environment) {
1615

1716
val project = environment.project
18-
val commandLine: AptosCommandLine = config.cmd
17+
val commandLine: AptosCommandLine = cleanConfiguration.cmd
1918

2019
override fun startProcess(): ProcessHandler {
21-
val generalCommandLine = commandLine.toColoredCommandLine(config.aptosPath)
20+
// emulateTerminal=true allows for the colored output
21+
val generalCommandLine =
22+
commandLine.toGeneralCommandLine(cleanConfiguration.aptosPath, emulateTerminal = true)
2223
val handler = KillableColoredProcessHandler(generalCommandLine)
2324
consoleBuilder.console.attachToProcess(handler)
2425
ProcessTerminatedListener.attach(handler) // shows exit code upon termination
@@ -36,11 +37,8 @@ abstract class AptosRunStateBase(
3637
}
3738

3839
class AptosRunState(
39-
environment: ExecutionEnvironment,
40-
runConfiguration: CommandConfigurationBase,
41-
config: CommandConfigurationBase.CleanConfiguration.Ok
42-
):
43-
AptosRunStateBase(environment, runConfiguration, config) {
40+
environment: ExecutionEnvironment, config: CleanConfiguration.Ok
41+
): AptosRunStateBase(environment, config) {
4442

4543
init {
4644
createFilters().forEach { consoleBuilder.addFilter(it) }

src/main/kotlin/org/move/cli/runConfigurations/CommandConfigurationBase.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ abstract class CommandConfigurationBase(
5656
override fun getState(executor: Executor, environment: ExecutionEnvironment): AptosRunStateBase? {
5757
val config = clean().ok ?: return null
5858
return if (showTestToolWindow(config.cmd)) {
59-
AptosTestRunState(environment, this, config)
59+
AptosTestRunState(environment, config)
6060
} else {
61-
AptosRunState(environment, this, config)
61+
AptosRunState(environment, config)
6262
}
6363
}
6464

@@ -79,16 +79,14 @@ abstract class CommandConfigurationBase(
7979
workingDirectory,
8080
environmentVariables
8181
)
82-
return CleanConfiguration.Ok(aptosPath, commandLine)
82+
return CleanConfiguration.
83+
Ok(aptosPath, commandLine)
8384
}
8485

8586
protected fun showTestToolWindow(commandLine: AptosCommandLine): Boolean =
8687
when {
8788
!AdvancedSettings.getBoolean(TEST_TOOL_WINDOW_SETTING_KEY) -> false
8889
commandLine.subCommand != "move test" -> false
89-
// "--nocapture" in commandLine.additionalArguments -> false
90-
// Cargo.TEST_NOCAPTURE_ENABLED_KEY.asBoolean() -> false
91-
// else -> !hasRemoteTarget
9290
else -> true
9391
}
9492

src/main/kotlin/org/move/cli/runConfigurations/aptos/Aptos.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,8 @@ data class Aptos(val cliLocation: Path, val parentDisposable: Disposable?): Disp
178178
listener: ProcessListener? = null,
179179
runner: CapturingProcessHandler.() -> ProcessOutput = { runProcessWithGlobalProgress() }
180180
): RsProcessResult<ProcessOutput> {
181-
val generalCommandLine = if (colored) {
182-
commandLine.toColoredCommandLine(this.cliLocation)
183-
} else {
184-
commandLine.toGeneralCommandLine(this.cliLocation)
185-
}
181+
val generalCommandLine =
182+
commandLine.toGeneralCommandLine(this.cliLocation, emulateTerminal = colored)
186183
return generalCommandLine.execute(innerDisposable, runner, listener)
187184
}
188185

@@ -193,11 +190,9 @@ data class Aptos(val cliLocation: Path, val parentDisposable: Disposable?): Disp
193190
runner: CapturingProcessHandler.() -> ProcessOutput = { runProcessWithGlobalProgress() }
194191
): AptosProcessResult<Unit> {
195192
val processOutput = executeCommandLine(commandLine, colored, listener, runner)
193+
.ignoreNonZeroExitCode()
196194
.unwrapOrElse {
197-
if (it !is RsProcessExecutionException.FailedWithNonZeroExitCode) {
198-
return Err(it)
199-
}
200-
it.output
195+
return Err(it)
201196
}
202197

203198
val json = processOutput.stdout

src/main/kotlin/org/move/cli/runConfigurations/test/AptosTestRunState.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import org.move.cli.runConfigurations.aptos.cmd.AptosCommandConfiguration
88

99
class AptosTestRunState(
1010
environment: ExecutionEnvironment,
11-
runConfiguration: CommandConfigurationBase,
12-
config: CommandConfigurationBase.CleanConfiguration.Ok
13-
): AptosRunStateBase(environment, runConfiguration, config) {
11+
cleanConfiguration: CommandConfigurationBase.CleanConfiguration.Ok
12+
): AptosRunStateBase(environment, cleanConfiguration) {
1413

1514
init {
1615
consoleBuilder =
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.move.cli.tools
2+
3+
import com.intellij.execution.configuration.EnvironmentVariablesData
4+
import com.intellij.execution.configurations.GeneralCommandLine
5+
import com.intellij.execution.configurations.PtyCommandLine
6+
import com.intellij.util.execution.ParametersListUtil
7+
import java.nio.file.Path
8+
9+
open class MvCommandLine(
10+
val arguments: List<String> = emptyList(),
11+
val workingDirectory: Path? = null,
12+
val environmentVariables: EnvironmentVariablesData = EnvironmentVariablesData.DEFAULT
13+
) {
14+
val commandLineString: String get() = ParametersListUtil.join(arguments)
15+
16+
fun toGeneralCommandLine(cliExePath: Path, emulateTerminal: Boolean = false): GeneralCommandLine {
17+
var commandLine = GeneralCommandLine()
18+
.withExePath(cliExePath.toString())
19+
.withParameters(this.arguments)
20+
.withWorkingDirectory(this.workingDirectory)
21+
.withCharset(Charsets.UTF_8)
22+
// disables default coloring for stderr
23+
.withRedirectErrorStream(true)
24+
this.environmentVariables.configureCommandLine(commandLine, true)
25+
if (emulateTerminal) {
26+
commandLine = PtyCommandLine(commandLine)
27+
}
28+
return commandLine
29+
}
30+
}

src/main/kotlin/org/move/openapiext/MvProcessResult.kt

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,41 @@ import org.move.stdext.RsResult
1212

1313
typealias RsProcessResult<T> = RsResult<T, RsProcessExecutionException>
1414

15-
sealed class RsProcessExecutionOrDeserializationException : RuntimeException {
16-
constructor(cause: Throwable) : super(cause)
17-
constructor(message: String) : super(message)
15+
sealed class RsProcessExecutionOrDeserializationException: RuntimeException {
16+
constructor(cause: Throwable): super(cause)
17+
constructor(message: String): super(message)
1818
}
1919

20-
class RsDeserializationException(cause: JacksonException) :
20+
class RsDeserializationException(cause: JacksonException):
2121
RsProcessExecutionOrDeserializationException(cause)
2222

23-
sealed class RsProcessExecutionException : RsProcessExecutionOrDeserializationException {
24-
constructor(message: String) : super(message)
25-
constructor(cause: Throwable) : super(cause)
23+
sealed class RsProcessExecutionException: RsProcessExecutionOrDeserializationException {
24+
constructor(message: String): super(message)
25+
constructor(cause: Throwable): super(cause)
2626

2727
abstract val commandLineString: String
2828

2929
class Start(
3030
override val commandLineString: String,
3131
cause: ExecutionException,
32-
) : RsProcessExecutionException(cause)
32+
): RsProcessExecutionException(cause)
3333

3434
class Canceled(
3535
override val commandLineString: String,
3636
val output: ProcessOutput,
3737
message: String = errorMessage(commandLineString, output),
38-
) : RsProcessExecutionException(message)
38+
): RsProcessExecutionException(message)
3939

4040
class Timeout(
4141
override val commandLineString: String,
4242
val output: ProcessOutput,
43-
) : RsProcessExecutionException(errorMessage(commandLineString, output))
43+
): RsProcessExecutionException(errorMessage(commandLineString, output))
4444

4545
/** The process exited with non-zero exit code */
4646
class FailedWithNonZeroExitCode(
4747
override val commandLineString: String,
4848
val output: ProcessOutput,
49-
) : RsProcessExecutionException(errorMessage(commandLineString, output))
49+
): RsProcessExecutionException(errorMessage(commandLineString, output))
5050

5151
companion object {
5252
fun errorMessage(commandLineString: String, output: ProcessOutput): String = """
@@ -68,3 +68,12 @@ fun RsProcessResult<ProcessOutput>.ignoreExitCode(): RsResult<ProcessOutput, RsP
6868
is RsProcessExecutionException.FailedWithNonZeroExitCode -> RsResult.Ok(err.output)
6969
}
7070
}
71+
72+
fun RsProcessResult<ProcessOutput>.ignoreNonZeroExitCode(): RsResult<ProcessOutput, RsProcessExecutionException> =
73+
when (this) {
74+
is RsResult.Ok -> this
75+
is RsResult.Err -> when (err) {
76+
is RsProcessExecutionException.FailedWithNonZeroExitCode -> RsResult.Ok(err.output)
77+
else -> this
78+
}
79+
}

0 commit comments

Comments
 (0)