Skip to content

Commit 3b74b51

Browse files
committed
(#22) Run configuration: update the default working directory
From now on, the plugin will try to get the default working directory from the script path.
1 parent ac9f2a4 commit 3b74b51

File tree

5 files changed

+101
-23
lines changed

5 files changed

+101
-23
lines changed

src/main/java/com/intellij/plugin/powershell/ide/run/PowerShellRunSettingsEditor.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import javax.swing.*;
2020
import java.io.File;
21+
import java.util.Objects;
22+
23+
import static com.intellij.openapi.util.io.NioFiles.toPath;
2124

2225
public class PowerShellRunSettingsEditor extends SettingsEditor<PowerShellRunConfiguration> {
2326

@@ -42,14 +45,16 @@ public PowerShellRunSettingsEditor(Project project, PowerShellRunConfiguration r
4245
protected void resetEditorFrom(@NotNull PowerShellRunConfiguration configuration) {
4346
String configName = configuration.getName();
4447
String scriptPath = configuration.getScriptPath();
45-
String workingDir = configuration.getWorkingDirectory();
48+
String customWorkingDir = configuration.getCustomWorkingDirectory();
4649
EnvironmentVariablesData envVars = configuration.getEnvironmentVariables();
4750
String executablePath = configuration.getExecutablePath();
4851

49-
if (workingDir.equals(PSExecutionUtilKt.getDefaultWorkingDirectory())) {
50-
workingDirectoryTextField.getEmptyText().setText(workingDir);
52+
var path = toPath(scriptPath);
53+
var customWorkingDirPath = customWorkingDir == null ? null : toPath(customWorkingDir);
54+
if (Objects.equals(customWorkingDirPath, PSExecutionUtilKt.getDefaultWorkingDirectory(path))) {
55+
workingDirectoryTextField.getEmptyText().setText(customWorkingDir);
5156
} else {
52-
workingDirectoryTextFieldWithBrowseBtn.setText(workingDir);
57+
workingDirectoryTextFieldWithBrowseBtn.setText(customWorkingDir);
5358
}
5459
String scriptParameters = configuration.getScriptParameters();
5560
String commandOptions = configuration.getCommandOptions();
@@ -77,7 +82,7 @@ protected void resetEditorFrom(@NotNull PowerShellRunConfiguration configuration
7782
@Override
7883
protected void applyEditorTo(@NotNull PowerShellRunConfiguration configuration) {
7984
configuration.setScriptPath(scriptTextField.getText().trim());
80-
configuration.setWorkingDirectory(workingDirectoryTextFieldWithBrowseBtn.getText().trim());
85+
configuration.setCustomWorkingDirectory(workingDirectoryTextFieldWithBrowseBtn.getText().trim());
8186
configuration.setScriptParameters(parametersTextField.getText().trim());
8287
configuration.setCommandOptions(commandOptionsTextField.getText().trim());
8388
configuration.setEnvironmentVariables(environmentVariablesTextFieldWithBrowseButton.getData());

src/main/kotlin/com/intellij/plugin/powershell/ide/run/PSExecutionUtil.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import com.intellij.openapi.util.io.FileUtil
55
import com.intellij.plugin.powershell.lang.lsp.languagehost.PowerShellExtensionError
66
import com.intellij.plugin.powershell.lang.lsp.languagehost.PowerShellNotInstalled
77
import com.intellij.util.EnvironmentUtil
8-
8+
import com.intellij.util.text.nullize
9+
import java.nio.file.Path
10+
import kotlin.io.path.Path
911

1012
@Throws(PowerShellNotInstalled::class)
1113
fun findPsExecutable(): String {
@@ -46,6 +48,11 @@ fun join(vararg pathPart: String): String {
4648
return FileUtil.toCanonicalPath(FileUtil.join(*pathPart))
4749
}
4850

49-
fun getDefaultWorkingDirectory(): String {
50-
return EnvironmentUtil.getValue("HOME") ?: System.getProperty("user.home") ?: System.getProperty("user.dir") ?: ""
51+
fun getDefaultWorkingDirectory(scriptPath: Path?): Path {
52+
val workingDir = scriptPath?.parent
53+
if (workingDir != null) return workingDir
54+
55+
val directory = (System.getProperty("user.home") ?: System.getProperty("user.dir"))
56+
.nullize(nullizeSpaces = true) ?: "."
57+
return Path(directory)
5158
}

src/main/kotlin/com/intellij/plugin/powershell/ide/run/PowerShellRunConfiguration.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.intellij.execution.configuration.EnvironmentVariablesData
66
import com.intellij.execution.configurations.ConfigurationFactory
77
import com.intellij.execution.configurations.LocatableConfigurationBase
88
import com.intellij.execution.configurations.RunConfiguration
9-
import com.intellij.execution.configurations.RunProfileState
109
import com.intellij.execution.runners.ExecutionEnvironment
1110
import com.intellij.openapi.options.SettingsEditor
1211
import com.intellij.openapi.project.Project
@@ -26,8 +25,7 @@ class PowerShellRunConfiguration(project: Project, configurationFactory: Configu
2625
private val EXE_PATH = "executablePath"
2726

2827
var scriptPath: String = ""
29-
var workingDirectory: String = getDefaultWorkingDirectory()
30-
get() = if (StringUtil.isEmptyOrSpaces(field)) getDefaultWorkingDirectory() else field
28+
var customWorkingDirectory: String? = null
3129
var scriptParameters: String = ""
3230
private var commandOptions: String? = null
3331
var environmentVariables: EnvironmentVariablesData = EnvironmentVariablesData.DEFAULT
@@ -36,7 +34,7 @@ class PowerShellRunConfiguration(project: Project, configurationFactory: Configu
3634
override fun getConfigurationEditor(): SettingsEditor<out RunConfiguration> = PowerShellRunSettingsEditor(project, this)
3735

3836
@Throws(ExecutionException::class)
39-
override fun getState(executor: Executor, environment: ExecutionEnvironment): RunProfileState =
37+
override fun getState(executor: Executor, environment: ExecutionEnvironment): PowerShellScriptCommandLineState =
4038
PowerShellScriptCommandLineState(this, environment)
4139

4240
@Throws(InvalidDataException::class)
@@ -57,7 +55,7 @@ class PowerShellRunConfiguration(project: Project, configurationFactory: Configu
5755
this.commandOptions = commandOptions
5856
}
5957
if (!StringUtil.isEmpty(workingDirectory)) {
60-
this.workingDirectory = workingDirectory
58+
this.customWorkingDirectory = workingDirectory
6159
}
6260
environmentVariables = EnvironmentVariablesData.readExternal(element)
6361
executablePath = if (StringUtil.isEmpty(exePath)) FormUIUtil.globalSettingsExecutablePath else exePath
@@ -75,8 +73,8 @@ class PowerShellRunConfiguration(project: Project, configurationFactory: Configu
7573
if (!StringUtil.isEmpty(commandOptions)) {
7674
element.setAttribute(COMMAND_OPTIONS, commandOptions)
7775
}
78-
if (!StringUtil.isEmpty(workingDirectory)) {
79-
element.setAttribute(WORKING_DIRECTORY, workingDirectory)
76+
if (!StringUtil.isEmpty(customWorkingDirectory)) {
77+
element.setAttribute(WORKING_DIRECTORY, customWorkingDirectory)
8078
}
8179
environmentVariables.writeExternal(element)
8280

@@ -91,4 +89,4 @@ class PowerShellRunConfiguration(project: Project, configurationFactory: Configu
9189
fun setCommandOptions(commandOptions: String) {
9290
this.commandOptions = commandOptions
9391
}
94-
}
92+
}

src/main/kotlin/com/intellij/plugin/powershell/ide/run/PowerShellScriptCommandLineState.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,27 @@ import com.intellij.openapi.diagnostic.logger
1616
import com.intellij.openapi.diagnostic.runAndLogException
1717
import com.intellij.openapi.options.advanced.AdvancedSettings
1818
import com.intellij.openapi.roots.ProjectRootManager
19-
import com.intellij.openapi.util.Key
19+
import com.intellij.openapi.util.io.NioFiles.toPath
2020
import com.intellij.openapi.util.text.StringUtil
2121
import com.intellij.openapi.vfs.LocalFileSystem
2222
import com.intellij.plugin.powershell.lang.lsp.LSPInitMain
2323
import com.intellij.plugin.powershell.lang.lsp.languagehost.PowerShellNotInstalled
2424
import com.intellij.terminal.TerminalExecutionConsole
2525
import kotlinx.coroutines.Dispatchers
2626
import kotlinx.coroutines.withContext
27+
import org.jetbrains.annotations.TestOnly
2728
import java.io.File
2829
import java.nio.charset.Charset
2930
import java.nio.file.Path
3031
import java.util.regex.Pattern
31-
import kotlin.io.path.Path
3232

3333
class PowerShellScriptCommandLineState(
3434
private val runConfiguration: PowerShellRunConfiguration,
35-
private val environment: ExecutionEnvironment) : RunProfileState {
35+
private val environment: ExecutionEnvironment
36+
) : RunProfileState {
3637

37-
private lateinit var workingDirectory: Path
38+
@TestOnly
39+
lateinit var workingDirectory: Path
3840
suspend fun prepareExecution() {
3941
val project = runConfiguration.project
4042
val file = withContext(Dispatchers.IO) { LocalFileSystem.getInstance().findFileByIoFile(File(runConfiguration.scriptPath)) }
@@ -43,9 +45,9 @@ class PowerShellScriptCommandLineState(
4345
ProjectRootManager.getInstance(project).fileIndex.getModuleForFile(it)
4446
}
4547
}
46-
workingDirectory = Path(
47-
ProgramParametersUtil.expandPathAndMacros(runConfiguration.workingDirectory, module, project)
48-
)
48+
workingDirectory = runConfiguration.customWorkingDirectory?.let {
49+
toPath(ProgramParametersUtil.expandPathAndMacros(it, module, project))
50+
} ?: getDefaultWorkingDirectory(toPath(runConfiguration.scriptPath))
4951
}
5052

5153
private fun startProcess(): ProcessHandler {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.intellij.plugin.powershell.ide.run
2+
3+
import com.intellij.execution.executors.DefaultRunExecutor
4+
import com.intellij.execution.impl.RunManagerImpl
5+
import com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl
6+
import com.intellij.execution.runners.ExecutionEnvironment
7+
import com.intellij.execution.runners.ProgramRunner
8+
import com.intellij.testFramework.fixtures.BasePlatformTestCase
9+
import kotlinx.coroutines.runBlocking
10+
import java.nio.file.Path
11+
import kotlin.io.path.Path
12+
13+
class PowerShellRunConfigurationTests : BasePlatformTestCase() {
14+
15+
private val projectPath: Path
16+
get() = Path(project.basePath!!)
17+
private val defaultWorkingDirectory
18+
get() = projectPath.resolve("scripts")
19+
20+
fun testCustomWorkingDirectoryPath() {
21+
val customDir = projectPath.resolve("ttt")
22+
assertWorkingDirectory(custom = customDir.toString(), expected = customDir)
23+
}
24+
25+
fun testNoCustomWorkingDirectory() {
26+
assertWorkingDirectory(custom = null, expected = projectPath.resolve("scripts"))
27+
}
28+
29+
fun testCustomWorkingDirectoryPathVariable() {
30+
assertWorkingDirectory(custom = "\$PROJECT_DIR$/foobar", expected = projectPath.resolve("foobar"))
31+
}
32+
33+
fun testInvalidWorkingDir() {
34+
assertWorkingDirectory(custom = """\\\""", expected = defaultWorkingDirectory)
35+
}
36+
fun testInvalidScriptPath() {
37+
assertWorkingDirectory(custom = null, scriptPath = """\\\""", expected = Path(System.getProperty("user.home")))
38+
}
39+
40+
private fun assertWorkingDirectory(custom: String?, scriptPath: String? = null, expected: Path) {
41+
val configuration = createConfiguration(custom, scriptPath)
42+
val executor = DefaultRunExecutor.getRunExecutorInstance()
43+
val runner = ProgramRunner.getRunner(executor.id, configuration) as PowerShellProgramRunner
44+
val environment = ExecutionEnvironment(
45+
executor,
46+
runner,
47+
RunnerAndConfigurationSettingsImpl(RunManagerImpl.getInstanceImpl(project), configuration),
48+
project
49+
)
50+
val state = configuration.getState(executor, environment)
51+
runBlocking { state.prepareExecution() }
52+
assertEquals(expected, state.workingDirectory)
53+
}
54+
55+
private fun createConfiguration(
56+
customWorkingDirectory: String?,
57+
customScriptPath: String?
58+
): PowerShellRunConfiguration {
59+
val type = PowerShellConfigurationType()
60+
val factory = type.configurationFactories.single()
61+
return PowerShellRunConfiguration(project, factory, "Test").apply {
62+
scriptPath = customScriptPath ?: projectPath.resolve("scripts/MyScript.ps1").toString()
63+
this.customWorkingDirectory = customWorkingDirectory
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)