Skip to content

Commit

Permalink
Version 1.5
Browse files Browse the repository at this point in the history
Add $freePort macros. Closes #2
  • Loading branch information
turbanoff committed Jan 19, 2017
1 parent e9b575f commit 813b7c6
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 24 deletions.
15 changes: 1 addition & 14 deletions .idea/misc.xml

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

10 changes: 9 additions & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin version="2">
<id>org.turbanov.execution.cmd</id>
<name>RunInCmd</name>
<version>1.4</version>
<version>1.5</version>
<vendor email="turbanoff@gmail.com" url="https://github.com/turbanoff/RunInCmdPlugin">Turbanov Andrey</vendor>

<description><![CDATA[
Expand All @@ -12,6 +12,14 @@
<!--Add change notes here.<br>-->
<!--<em>most HTML tags may be used</em>-->
<change-notes><![CDATA[
<h3>1.5</h3>
<ul>
<li>
Added $freePort macros available in "To add" options. Plugin will replace it with some free port.<br>
This is very useful when you want to debug application launched via this plugin.<br>
Just add <div style="font-family:monospace">-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$freePort</div> to "To add VM options".
</li>
</ul>
<h3>1.4</h3>
<ul>
<li>Fix run inside 2016.x IDEA terminal</li>
Expand Down
2 changes: 1 addition & 1 deletion runInCmdPlugin.iml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PLUGIN_MODULE" version="4">
<component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/META-INF/plugin.xml" />
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="true">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
Expand Down
33 changes: 28 additions & 5 deletions src/org/turbanov/execution/cmd/InCmdConfigurable.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

/**
* @author Andrey Turbanov
Expand All @@ -23,6 +24,8 @@ public class InCmdConfigurable implements Configurable {
private JTextArea toAddProgramOptions;
private JTextArea toRemoveProgramOptions;
private JCheckBox runInTerminal;
private SpinnerNumberModel freePortSpinnerModel;
private JCheckBox findFreePort;

public InCmdConfigurable(Project project) {
myProject = project;
Expand Down Expand Up @@ -59,19 +62,32 @@ public JComponent createComponent() {

runInTerminal = new JCheckBox("Run inside IDEA Terminal", myState.isRunInsideTerminal);

Box bottomBox = Box.createHorizontalBox();
freePortSpinnerModel = new SpinnerNumberModel(60000, 1024, 65535, 1);
JSpinner freePortSpinner = new JSpinner(freePortSpinnerModel);
findFreePort = new JCheckBox("Try to find free port");
findFreePort.setToolTipText("At start plugin will try to find free port\n" +
"This port will be available as $freePort macros in program/VM options");
bottomBox.add(findFreePort);
bottomBox.add(LabeledComponent.create(freePortSpinner, "Start value to check"));

JPanel result = new JPanel(new BorderLayout());
result.add(runInTerminal, BorderLayout.PAGE_START);
result.add(mainBox, BorderLayout.CENTER);
result.add(bottomBox, BorderLayout.PAGE_END);
return result;
}

@Override
public boolean isModified() {
return !toAddVmOptions.getText().equals(myState.toAddVmOptions)
|| !toRemoveVmOptions.getText().equals(myState.toRemoveVmOptions)
|| !toAddProgramOptions.getText().equals(myState.toAddProgramOptions)
|| !toRemoveProgramOptions.getText().equals(myState.toRemoveProgramOptions)
|| runInTerminal.isSelected() != myState.isRunInsideTerminal;
boolean simpleModified = !toAddVmOptions.getText().equals(myState.toAddVmOptions)
|| !toRemoveVmOptions.getText().equals(myState.toRemoveVmOptions)
|| !toAddProgramOptions.getText().equals(myState.toAddProgramOptions)
|| !toRemoveProgramOptions.getText().equals(myState.toRemoveProgramOptions)
|| runInTerminal.isSelected() != myState.isRunInsideTerminal;
if (simpleModified) return true;
Integer currentValue = findFreePort.isSelected() ? freePortSpinnerModel.getNumber().intValue() : null;
return !Objects.equals(currentValue, myState.startPort);
}

@Override
Expand All @@ -82,6 +98,7 @@ public void apply() throws ConfigurationException {
state.toAddProgramOptions = toAddProgramOptions.getText().trim();
state.toRemoveProgramOptions = toRemoveProgramOptions.getText().trim();
state.isRunInsideTerminal = runInTerminal.isSelected();
state.startPort = findFreePort.isSelected() ? freePortSpinnerModel.getNumber().intValue() : null;
ServiceManager.getService(myProject, OptionsPatchConfiguration.class).loadState(state);
}

Expand All @@ -92,6 +109,12 @@ public void reset() {
toAddProgramOptions.setText(myState.toAddProgramOptions);
toRemoveProgramOptions.setText(myState.toRemoveProgramOptions);
runInTerminal.setSelected(myState.isRunInsideTerminal);
if (myState.startPort == null) {
findFreePort.setSelected(false);
} else {
findFreePort.setSelected(true);
freePortSpinnerModel.setValue(myState.startPort);
}
}

@Override
Expand Down
34 changes: 31 additions & 3 deletions src/org/turbanov/execution/cmd/InCmdRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -70,8 +71,8 @@ protected RunContentDescriptor doExecute(@NotNull RunProfileState runProfileStat
LOG.info("Old command line: " + oldCommandLine);

OptionsPatchConfiguration options = ServiceManager.getService(environment.getProject(), OptionsPatchConfiguration.class);
patchParameterList(javaParameters.getVMParametersList(), options.toAddVmOptions, options.toRemoveVmOptions);
patchParameterList(javaParameters.getProgramParametersList(), options.toAddProgramOptions, options.toRemoveProgramOptions);
patchParameterList(javaParameters.getVMParametersList(), options.toAddVmOptions, options.toRemoveVmOptions, options.startPort);
patchParameterList(javaParameters.getProgramParametersList(), options.toAddProgramOptions, options.toRemoveProgramOptions, options.startPort);

String workingDirectory = state.getJavaParameters().getWorkingDirectory();

Expand Down Expand Up @@ -148,8 +149,14 @@ private static void clear(PathsList classPath) {
}
}

private static void patchParameterList(ParametersList parametersList, String toAdd, String toRemove) {
private static void patchParameterList(ParametersList parametersList, String toAdd, String toRemove, Integer startPort) {
if (!toAdd.isEmpty()) {
if (startPort != null && toAdd.contains("$freePort")) {
Integer freePort = findFreePort(startPort);
if (freePort != null) {
toAdd = toAdd.replace("$freePort", freePort.toString());
}
}
String[] toAddParams = ParametersList.parse(toAdd);
for (String toAddParam : toAddParams) {
if (!parametersList.hasParameter(toAddParam)) {
Expand All @@ -169,4 +176,25 @@ private static void patchParameterList(ParametersList parametersList, String toA
}
}
}

//Adapted from org.jetbrains.jps.incremental.java.JavaBuilder.findFreePort()
private static Integer findFreePort(int startFrom) {
for (int i = 0; i < 100; i++) {
try {
int tryPort = startFrom + i;
if (tryPort < 0) {
return null;
}
try (ServerSocket serverSocket = new ServerSocket(tryPort)) {
// calling close() immediately after opening socket may result that socket is not closed
synchronized (serverSocket) {
serverSocket.wait(1);
}
}
return tryPort;
} catch (IOException | InterruptedException ignored) {
}
}
return null;
}
}
2 changes: 2 additions & 0 deletions src/org/turbanov/execution/cmd/OptionsPatchConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class OptionsPatchConfiguration implements PersistentStateComponent<Optio
public String toAddProgramOptions = "";
public String toRemoveProgramOptions = "";
public boolean isRunInsideTerminal = false;
public Integer startPort;

@NotNull
@Override
Expand All @@ -31,5 +32,6 @@ public void loadState(OptionsPatchConfiguration state) {
toAddProgramOptions = state.toAddProgramOptions;
toRemoveProgramOptions = state.toRemoveProgramOptions;
isRunInsideTerminal = state.isRunInsideTerminal;
startPort = state.startPort;
}
}

0 comments on commit 813b7c6

Please sign in to comment.