Skip to content

Commit

Permalink
get rid of deprecated API usages
Browse files Browse the repository at this point in the history
fix plugin.xml according to new inspections
fix terminal runner
fix "Synchronous execution on EDT"
now minimum required version is 2019.1
  • Loading branch information
turbanoff committed Oct 2, 2019
1 parent cb3e75e commit 13a5fe1
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
out/
.idea/copyright/
.idea/workspace.xml
.idea/kotlinc.xml
.idea/uiDesigner.xml
.idea/dictionaries/
runInCmdPlugin.jar
10 changes: 6 additions & 4 deletions .idea/runConfigurations/RunInCmdPlugin.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin version="2">
<idea-plugin>
<id>org.turbanov.execution.cmd</id>
<name>RunInCmd</name>
<version>1.6</version>
<version>1.7</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>1.7</h3>
<ul>
<li>Update to support IntelliJ IDEA 2019.x</li>
</ul>
<h3>1.6</h3>
<ul>
<li>Support running JUnit tests</li>
Expand Down Expand Up @@ -51,13 +55,13 @@
]]>
</change-notes>

<!-- please see https://confluence.jetbrains.com/display/IDEADEV/Build+Number+Ranges for description -->
<idea-version since-build="139.0"/>
<!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="191"/>

<!-- please see https://confluence.jetbrains.com/display/IDEADEV/Plugin+Compatibility+with+IntelliJ+Platform+Products
<!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<depends>com.intellij.modules.java</depends>
<depends optional="true">org.jetbrains.plugins.terminal</depends>
<depends optional="true" config-file="terminal.plugin.support.xml">org.jetbrains.plugins.terminal</depends>

<extensions defaultExtensionNs="com.intellij">
<executor implementation="org.turbanov.execution.cmd.InCmdExecutor"/>
Expand Down
1 change: 1 addition & 0 deletions META-INF/terminal.plugin.support.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<idea-plugin/>
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Plugin for IntelliJ IDEA. It allows to run your RunConfiguration inside cmd.exe
Also it provides ability to modify program and VM options on-the-fly before run

# Repository link
https://plugins.jetbrains.com/plugin/7976?pr=idea
https://plugins.jetbrains.com/plugin/7976-runincmd


Supported IDEA versions: 14.x - 2017.2
Supported IDEA versions: 2019.1+


![screen](https://cloud.githubusercontent.com/assets/741251/10416757/4080e210-702c-11e5-915c-f5df58583719.png)
Expand Down
17 changes: 12 additions & 5 deletions src/org/turbanov/execution/cmd/InCmdConfigurable.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package org.turbanov.execution.cmd;

import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;

import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.LabeledComponent;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.BorderLayout;
import java.util.Objects;

/**
* @author Andrey Turbanov
*/
public class InCmdConfigurable implements Configurable {
public class InCmdConfigurable implements Configurable {

private final Project myProject;
private OptionsPatchConfiguration myState;
Expand Down
8 changes: 5 additions & 3 deletions src/org/turbanov/execution/cmd/InCmdExecutor.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.turbanov.execution.cmd;

import javax.swing.Icon;

import org.jetbrains.annotations.NotNull;
import com.intellij.execution.Executor;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.wm.ToolWindowId;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

/**
* @author Andrey Turbanov
Expand All @@ -14,11 +14,13 @@ public class InCmdExecutor extends Executor {
public static final Icon cmdExecutorIcon = IconLoader.getIcon("/cmd.png");
public static final String executorId = "RunInCmdExecutor";

@NotNull
@Override
public String getToolWindowId() {
return ToolWindowId.DEBUG;
}

@NotNull
@Override
public Icon getToolWindowIcon() {
return cmdExecutorIcon;
Expand Down
64 changes: 32 additions & 32 deletions src/org/turbanov/execution/cmd/InCmdRunner.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
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;
import com.intellij.execution.configurations.CommandLineBuilder;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.JavaCommandLineState;
import com.intellij.execution.configurations.JavaParameters;
import com.intellij.execution.configurations.ParametersList;
import com.intellij.execution.configurations.RunProfile;
import com.intellij.execution.configurations.RunProfileState;
import com.intellij.execution.configurations.RunnerSettings;
import com.intellij.execution.process.CapturingProcessHandler;
import com.intellij.execution.process.ProcessNotCreatedException;
import com.intellij.execution.process.ProcessOutput;
Expand All @@ -18,6 +20,7 @@
import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.PluginId;
Expand All @@ -30,8 +33,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 @@ -43,7 +44,7 @@
/**
* @author Andrey Turbanov
*/
public class InCmdRunner extends GenericProgramRunner {
public class InCmdRunner extends GenericProgramRunner<RunnerSettings> {

private static final Logger LOG = Logger.getInstance(InCmdRunner.class);
private Boolean terminalPluginEnabled;
Expand Down Expand Up @@ -73,8 +74,8 @@ protected RunContentDescriptor doExecute(@NotNull RunProfileState runProfileStat
FileDocumentManager.getInstance().saveAllDocuments();
JavaCommandLineState state = (JavaCommandLineState) runProfileState;
JavaParameters javaParameters = state.getJavaParameters();

GeneralCommandLine oldCommandLine = CommandLineBuilder.createFromJavaParameters(javaParameters, environment.getProject(), false);
javaParameters.setUseDynamicClasspath(false);
GeneralCommandLine oldCommandLine = javaParameters.toCommandLine();
LOG.info("Old command line: " + oldCommandLine);

OptionsPatchConfiguration options = ServiceManager.getService(environment.getProject(), OptionsPatchConfiguration.class);
Expand All @@ -87,7 +88,7 @@ protected RunContentDescriptor doExecute(@NotNull RunProfileState runProfileStat
String classPathPathsString = classPath.getPathsString();
clear(classPath);

GeneralCommandLine generalCommandLine = CommandLineBuilder.createFromJavaParameters(javaParameters, environment.getProject(), false);
GeneralCommandLine generalCommandLine = javaParameters.toCommandLine();
String original = generalCommandLine.getCommandLineString();
String newCommandLine = original
.replace("^", "^^") //replace ^ first
Expand All @@ -104,22 +105,12 @@ protected RunContentDescriptor doExecute(@NotNull RunProfileState runProfileStat
String[] command = {"cmd.exe", "/K", newCommandLine};
TerminalRunner.runInIdeaTerminal(environment.getProject(), command, classPathPathsString, workingDirectory);
} catch (Throwable e) {
StatusBar statusBar = WindowManager.getInstance().getStatusBar(environment.getProject());
JBPopupFactory.getInstance()
.createHtmlTextBalloonBuilder("Unable to run in internal IDEA Terminal due to '" + e.getMessage() + "'<br>Run in external cmd instead", MessageType.WARNING, null)
.setHideOnClickOutside(true)
.createBalloon()
.show(RelativePoint.getCenterOf(statusBar.getComponent()), Balloon.Position.above);
showWarning("Unable to run in internal IDEA Terminal due to '" + e.getMessage() + "'<br>Run in external cmd instead", environment);
runInExternalCmd(classPathPathsString, generalCommandLine, workingDirectory, newCommandLine);
}
} else {
if (!isTerminalPluginEnabled()) {
StatusBar statusBar = WindowManager.getInstance().getStatusBar(environment.getProject());
JBPopupFactory.getInstance()
.createHtmlTextBalloonBuilder("Terminal plugin disabled<br>Run in external cmd instead", MessageType.WARNING, null)
.setHideOnClickOutside(true)
.createBalloon()
.show(RelativePoint.getCenterOf(statusBar.getComponent()), Balloon.Position.above);
showWarning("Terminal plugin disabled<br>Run in external cmd instead", environment);
}
runInExternalCmd(classPathPathsString, generalCommandLine, workingDirectory, newCommandLine);
}
Expand All @@ -128,25 +119,36 @@ protected RunContentDescriptor doExecute(@NotNull RunProfileState runProfileStat

private static void runInExternalCmd(String classPathPathsString, GeneralCommandLine generalCommandLine, String workingDirectory, String commandLine) throws ProcessNotCreatedException {
String[] command = {"cmd.exe", "/C", "\"start cmd.exe /K \"" + commandLine + "\"\""};
Process start;
Process process;
try {
ProcessBuilder builder = new ProcessBuilder().command(command);
builder.directory(new File(workingDirectory));
builder.environment().put("CLASSPATH", classPathPathsString);
LOG.info(builder.command().toString());
start = builder.start();
process = builder.start();
} catch (IOException e) {
LOG.info(e);
throw new ProcessNotCreatedException(e.getMessage(), e, generalCommandLine);
}

CapturingProcessHandler processHandler = new CapturingProcessHandler(start, Charset.forName("cp866"), commandLine);
ProcessOutput output = processHandler.runProcess();
LOG.debug("Process output: " + output.getStdout());
String processErrors = output.getStderr();
if (!processErrors.isEmpty()) {
LOG.info("Process error: " + processErrors);
}
ApplicationManager.getApplication().executeOnPooledThread(() -> {
CapturingProcessHandler processHandler = new CapturingProcessHandler(process, Charset.forName("cp866"), commandLine);
ProcessOutput output = processHandler.runProcess();
LOG.debug("Process output: " + output.getStdout());
String processErrors = output.getStderr();
if (!processErrors.isEmpty()) {
LOG.info("Process error: " + processErrors);
}
});
}

private void showWarning(@NotNull String htmlContent, @NotNull ExecutionEnvironment environment) {
StatusBar statusBar = WindowManager.getInstance().getStatusBar(environment.getProject());
JBPopupFactory.getInstance()
.createHtmlTextBalloonBuilder(htmlContent, MessageType.WARNING, null)
.setHideOnClickOutside(true)
.createBalloon()
.show(RelativePoint.getCenterOf(statusBar.getComponent()), Balloon.Position.above);
}

private static void clear(PathsList classPath) {
Expand Down Expand Up @@ -192,11 +194,9 @@ private static Integer findFreePort(int startFrom) {
if (tryPort < 0) {
return null;
}
try (ServerSocket serverSocket = new ServerSocket(tryPort)) {
try (ServerSocket ignored = new ServerSocket(tryPort)) {
// calling close() immediately after opening socket may result that socket is not closed
synchronized (serverSocket) {
serverSocket.wait(1);
}
Thread.sleep(1);
}
return tryPort;
} catch (IOException | InterruptedException ignored) {
Expand Down
5 changes: 2 additions & 3 deletions src/org/turbanov/execution/cmd/OptionsPatchConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.turbanov.execution.cmd;

import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.components.StoragePathMacros;
import org.jetbrains.annotations.NotNull;

/**
* @author Andrey Turbanov
*/
@State(name = "RunInCmdPluginSettings", storages = {@Storage(file = StoragePathMacros.PROJECT_CONFIG_DIR + "/RunInCmd.xml")})
@State(name = "RunInCmdPluginSettings", storages = {@Storage(value = "RunInCmd.xml")})
public class OptionsPatchConfiguration implements PersistentStateComponent<OptionsPatchConfiguration> {

public String toAddVmOptions = "";
Expand Down
18 changes: 10 additions & 8 deletions src/org/turbanov/execution/cmd/TerminalRunner.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
package org.turbanov.execution.cmd;

import com.intellij.openapi.project.Project;
import com.intellij.util.containers.HashMap;
import com.pty4j.PtyProcess;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.terminal.LocalTerminalDirectRunner;
import org.jetbrains.plugins.terminal.TerminalView;
import com.intellij.openapi.project.Project;
import com.pty4j.PtyProcess;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
* @author Andrey Turbanov
*/
public class TerminalRunner {
public static void runInIdeaTerminal(Project project, final String[] command, final String classPath, final String workingDirectory) {
public static void runInIdeaTerminal(@NotNull Project project, @NotNull String[] command, @NotNull String classPath, @NotNull String workingDirectory) {
TerminalView terminalView = TerminalView.getInstance(project);
terminalView.createNewSession(project, new LocalTerminalDirectRunner(project) {
LocalTerminalDirectRunner runner = new LocalTerminalDirectRunner(project) {
@Override
protected PtyProcess createProcess(@Nullable String directory) throws ExecutionException {
Map<String, String> envs = new HashMap<String, String>(System.getenv());
protected PtyProcess createProcess(@Nullable String directory, @Nullable String commandHistoryFilePath) throws ExecutionException {
Map<String, String> envs = new HashMap<>(System.getenv());
envs.put("CLASSPATH", classPath);
try {
return PtyProcess.exec(command, envs, workingDirectory);
} catch (IOException e) {
throw new ExecutionException(e);
}
}
});
};
terminalView.createNewSession(runner);
}
}

0 comments on commit 13a5fe1

Please sign in to comment.