Skip to content

Commit

Permalink
Merge pull request #18761 from Postremus/#18676
Browse files Browse the repository at this point in the history
Improve IDE detection
  • Loading branch information
geoand authored Jul 20, 2021
2 parents c935245 + ec335b6 commit 4b44958
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
27 changes: 22 additions & 5 deletions core/deployment/src/main/java/io/quarkus/deployment/ide/Ide.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@

public enum Ide {

IDEA("idea", "--help"),
ECLIPSE("eclipse", (String[]) null),
VSCODE("code", "--version"),
NETBEANS("netbeans", "--help");
// see for cli syntax of idea https://www.jetbrains.com/help/idea/opening-files-from-command-line.html
IDEA("idea", "--line %s", "--help"),
ECLIPSE("eclipse", null, (String[]) null),
VSCODE("code", null, "--version"),
NETBEANS("netbeans", null, "--help");

private final String defaultCommand;
private final List<String> markerArgs;
private final String lineNumberArg;
private String machineSpecificCommand;

private String effectiveCommand;

Ide(String defaultCommand, String... markerArgs) {
Ide(String defaultCommand, String lineNumberArg, String... markerArgs) {
this.defaultCommand = defaultCommand;
this.lineNumberArg = lineNumberArg;
this.markerArgs = markerArgs != null ? Arrays.asList(markerArgs) : Collections.emptyList();
}

Expand Down Expand Up @@ -63,6 +66,20 @@ private String doGetEffectiveCommand() {
}
}

public List<String> createFileOpeningArgs(String fileName, String line) {
if (line == null || line.isEmpty()) {
return Collections.singletonList(fileName);
}

if (lineNumberArg == null) {
return Collections.singletonList(fileName + ":" + line);
}

String formattedLineArg = String.format(lineNumberArg, line);

return List.of(formattedLineArg, fileName);
}

public void setMachineSpecificCommand(String machineSpecificCommand) {
this.machineSpecificCommand = machineSpecificCommand;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public class IdeProcessor {

static {

IDE_PROCESSES.put((processInfo -> processInfo.containInCommand("idea") && processInfo.command.endsWith("java")),
IDE_PROCESSES.put(
(processInfo -> (processInfo.containInCommand("idea") || processInfo.containInCommand("IDEA"))
&& (processInfo.command.endsWith("java") || processInfo.command.endsWith("java.exe"))),
Ide.IDEA);
IDE_PROCESSES.put((processInfo -> processInfo.containInCommand("code")), Ide.VSCODE);
IDE_PROCESSES.put((processInfo -> processInfo.containInCommand("eclipse")), Ide.ECLIPSE);
Expand All @@ -62,9 +64,9 @@ public class IdeProcessor {
// into '/home/test/software/idea/ideaIU-203.5981.114/idea-IU-203.5981.114/bin/idea.sh'
String command = processInfo.getCommand();
int jbrIndex = command.indexOf("jbr");
if ((jbrIndex > -1) && command.endsWith("java")) {
if ((jbrIndex > -1) && (command.endsWith("java") || command.endsWith("java.exe"))) {
String ideaHome = command.substring(0, jbrIndex);
return (ideaHome + "bin" + File.separator + "idea") + (IdeUtil.isWindows() ? ".exe" : ".sh");
return (ideaHome + "bin" + File.separator + "idea") + (IdeUtil.isWindows() ? ".bat" : ".sh");
}
return null;
});
Expand Down Expand Up @@ -130,13 +132,26 @@ public IdeFileBuildItem detectIdeFiles(LaunchModeBuildItem launchModeBuildItem,
if (launchModeBuildItem.getDevModeType().orElse(null) != DevModeType.LOCAL) {
return null;
}

Set<Ide> result = new HashSet<>(2);
Path projectRoot = buildSystemTarget.getOutputDirectory().getParent();
IDE_MARKER_FILES.forEach((file, ides) -> {
if (Files.exists(projectRoot.resolve(file))) {
result.addAll(ides);
Path root = buildSystemTarget.getOutputDirectory();

// hack to try and guess the IDE when using a multi-module project
for (int i = 0; i < 3; i++) {
root = root.getParent();
if (root == null || !result.isEmpty()) {
break;
}
});

for (Map.Entry<String, List<Ide>> entry : IDE_MARKER_FILES.entrySet()) {
String file = entry.getKey();
List<Ide> ides = entry.getValue();
if (Files.exists(root.resolve(file))) {
result.addAll(ides);
}
}
}

return new IdeFileBuildItem(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ public Object apply(EvalContext ctx) {

switch (ctxName) {
case "srcMainPath": {
return srcMainPath.toAbsolutePath().toString();
return srcMainPath.toAbsolutePath().toString().replace("\\", "/");
}
case "ideLinkType":
if (!effectiveIdeBuildItem.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.quarkus.vertx.http.deployment.devmode.console;

import java.io.File;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -48,11 +49,9 @@ protected void dispatch(RoutingContext routingContext, MultiMap form) {

private void typicalProcessLaunch(RoutingContext routingContext, String className, String lang, String srcMainPath,
String line, Ide ide) {
String arg = toFileName(className, lang, srcMainPath);
if (!isNullOrEmpty(line)) {
arg = arg + ":" + line;
}
launchInIDE(ide, arg, routingContext);
String fileName = toFileName(className, lang, srcMainPath);
List<String> args = ide.createFileOpeningArgs(fileName, line);
launchInIDE(routingContext, ide, args);
}

private String toFileName(String className, String lang, String srcMainPath) {
Expand All @@ -68,7 +67,7 @@ private String toFileName(String className, String lang, String srcMainPath) {

}

protected void launchInIDE(Ide ide, String arg, RoutingContext routingContext) {
protected void launchInIDE(RoutingContext routingContext, Ide ide, List<String> args) {
new Thread(new Runnable() {
public void run() {
try {
Expand All @@ -78,7 +77,10 @@ public void run() {
routingContext.response().setStatusCode(500).end();
return;
}
new ProcessBuilder(Arrays.asList(effectiveCommand, arg)).inheritIO().start().waitFor(10,
List<String> command = new ArrayList<>();
command.add(effectiveCommand);
command.addAll(args);
new ProcessBuilder(command).inheritIO().start().waitFor(10,
TimeUnit.SECONDS);
routingContext.response().setStatusCode(200).end();
} catch (Exception e) {
Expand Down

0 comments on commit 4b44958

Please sign in to comment.