Skip to content

Commit

Permalink
version 2.1 with support of environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
turbanoff committed Sep 10, 2023
1 parent 853cb18 commit d8f170b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
8 changes: 6 additions & 2 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>org.turbanov.execution.cmd</id>
<name>RunInCmd</name>
<version>2.0</version>
<version>2.1</version>
<vendor email="[email protected]" url="https://github.com/turbanoff/RunInCmdPlugin">Turbanov Andrey</vendor>

<description><![CDATA[
Expand All @@ -12,6 +12,10 @@
<!--Add change notes here.<br>-->
<!--<em>most HTML tags may be used</em>-->
<change-notes><![CDATA[
<h3>2.1</h3>
<ul>
<li>Support environment variables</li>
</ul>
<h3>2.0</h3>
<ul>
<li>Support macOS</li>
Expand Down Expand Up @@ -66,7 +70,7 @@
</change-notes>

<!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="191"/>
<idea-version since-build="201"/>

<!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
Expand Down
26 changes: 19 additions & 7 deletions src/org/turbanov/execution/cmd/InCmdRunner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.turbanov.execution.cmd;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.JavaTestConfigurationBase;
import com.intellij.execution.application.ApplicationConfiguration;
Expand Down Expand Up @@ -32,8 +34,6 @@
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.PathsList;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
Expand All @@ -47,6 +47,7 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
* @author Andrey Turbanov
Expand Down Expand Up @@ -114,30 +115,41 @@ protected RunContentDescriptor doExecute(@NotNull RunProfileState runProfileStat
newCommandLine = original;
}

boolean passParentEnvs = javaParameters.isPassParentEnvs();
Map<String, String> env = javaParameters.getEnv();

if (options.isRunInsideTerminal && isTerminalPluginEnabled()) {
try {
String[] command = createCommand(false, newCommandLine, null, null);
TerminalRunner.runInIdeaTerminal(environment.getProject(), command, classPathPathsString, workingDirectory);
TerminalRunner.runInIdeaTerminal(environment.getProject(), command, classPathPathsString, workingDirectory, passParentEnvs, env);
} catch (Throwable e) {
showWarning("Unable to run in internal IDEA Terminal due to '" + e.getMessage() + "'<br>Run in external cmd instead", environment);
runInExternalCmd(classPathPathsString, generalCommandLine, workingDirectory, newCommandLine);
runInExternalCmd(classPathPathsString, generalCommandLine, workingDirectory, newCommandLine, passParentEnvs, env);
}
} else {
if (!isTerminalPluginEnabled()) {
showWarning("Terminal plugin disabled<br>Run in external cmd instead", environment);
}
runInExternalCmd(classPathPathsString, generalCommandLine, workingDirectory, newCommandLine);
runInExternalCmd(classPathPathsString, generalCommandLine, workingDirectory, newCommandLine, passParentEnvs, env);
}
return null;
}

private static void runInExternalCmd(String classPathPathsString, GeneralCommandLine generalCommandLine,
String workingDirectory, String commandLine) throws ProcessNotCreatedException {
private static void runInExternalCmd(@NotNull String classPathPathsString,
@NotNull GeneralCommandLine generalCommandLine,
@NotNull String workingDirectory,
@NotNull String commandLine,
boolean passParentEnv,
@NotNull Map<String, String> env) throws ProcessNotCreatedException {
Process process;
try {
String[] command = createCommand(true, commandLine, workingDirectory, classPathPathsString);
ProcessBuilder builder = new ProcessBuilder().command(command);
builder.directory(new File(workingDirectory));
if (!passParentEnv) {
builder.environment().clear();
}
builder.environment().putAll(env);
builder.environment().put("CLASSPATH", classPathPathsString);
LOG.info(builder.command().toString());
process = builder.start();
Expand Down
24 changes: 17 additions & 7 deletions src/org/turbanov/execution/cmd/TerminalRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,38 @@
* @author Andrey Turbanov
*/
public class TerminalRunner {
public static void runInIdeaTerminal(@NotNull Project project, @NotNull String[] command, @NotNull String classPath, @NotNull String workingDirectory) {
public static void runInIdeaTerminal(@NotNull Project project, @NotNull String[] command, @NotNull String classPath, @NotNull String workingDirectory, boolean passParentEnvs, @NotNull Map<String, String> env) {
TerminalView terminalView = TerminalView.getInstance(project);
LocalTerminalDirectRunner runner = new LocalTerminalDirectRunner(project) {
@Override
protected PtyProcess createProcess(@Nullable String directory, @Nullable String commandHistoryFilePath) throws ExecutionException {
return createProcessImpl(classPath, command, workingDirectory);
return createProcessImpl(classPath, command, workingDirectory, passParentEnvs, env);
}


@Override
public @NotNull PtyProcess createProcess(@NotNull TerminalProcessOptions options, @Nullable JBTerminalWidget widget) throws ExecutionException {
return createProcessImpl(classPath, command, workingDirectory);
return createProcessImpl(classPath, command, workingDirectory, passParentEnvs, env);
}
};
terminalView.createNewSession(runner);
}

private static PtyProcess createProcessImpl(@NotNull String classPath, @NotNull String[] command, @NotNull String workingDirectory) throws ExecutionException {
Map<String, String> envs = new HashMap<>(System.getenv());
envs.put("CLASSPATH", classPath);
private static PtyProcess createProcessImpl(@NotNull String classPath,
@NotNull String[] command,
@NotNull String workingDirectory,
boolean passParentEnvs,
@NotNull Map<String, String> env)
throws ExecutionException
{
Map<String, String> finalEnv = new HashMap<>();
if (passParentEnvs) {
finalEnv.putAll(System.getenv());
}
finalEnv.putAll(env);
finalEnv.put("CLASSPATH", classPath);
try {
return PtyProcess.exec(command, envs, workingDirectory);
return PtyProcess.exec(command, finalEnv, workingDirectory);
} catch (IOException e) {
throw new ExecutionException(e);
}
Expand Down

0 comments on commit d8f170b

Please sign in to comment.