Skip to content

Commit

Permalink
Merge pull request #193 from lyze237-forks/findideamanually
Browse files Browse the repository at this point in the history
Implement looking for manually installed IDEA on Windows.
  • Loading branch information
tommyettinger authored Aug 15, 2024
2 parents 8f7b1af + 915d429 commit 2f34479
Showing 1 changed file with 64 additions and 12 deletions.
76 changes: 64 additions & 12 deletions src/main/java/gdx/liftoff/ui/panels/CompleteButtonsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import com.ray3k.stripe.PopTable;
import gdx.liftoff.ui.UserData;
import gdx.liftoff.ui.dialogs.FullscreenDialog;
import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

import static gdx.liftoff.Main.*;

Expand All @@ -28,6 +29,7 @@ public class CompleteButtonsPanel extends Table implements Panel {
PopTable popTable;

String intellijPath = null;
boolean intellijIsFlatpak = false;

public CompleteButtonsPanel(boolean fullscreen) {
this(null, fullscreen);
Expand Down Expand Up @@ -66,22 +68,36 @@ public void populate(boolean fullscreen) {
table.add(ideaButton);
addHandListener(ideaButton);
try {
List<String> findIntellij = (UIUtils.isWindows) ? Arrays.asList("where.exe", "idea") : Arrays.asList("which", "idea");

Process process = new ProcessBuilder(findIntellij).start();
if (process.waitFor() != 0) {
throw new Exception("IntelliJ not found");
List<String> findIntellijExecutable = (UIUtils.isWindows) ? Arrays.asList("where.exe", "idea") : Arrays.asList("which", "idea");

Process whereProcess = new ProcessBuilder(findIntellijExecutable).start();
if (whereProcess.waitFor() == 0) {
intellijPath = new BufferedReader(new InputStreamReader(whereProcess.getInputStream())).readLine();
ideaButton.setDisabled(false);
} else {
if (UIUtils.isLinux) {
intellijPath = findFlatpakIntellij();
intellijIsFlatpak = true;
ideaButton.setDisabled(false);
} else if (UIUtils.isWindows) {
intellijPath = findManuallyInstalledIntellijOnWindows();
ideaButton.setDisabled(false);
} else {
throw new Exception("IntelliJ not found");
}
}

intellijPath = new BufferedReader(new InputStreamReader(process.getInputStream())).readLine();
ideaButton.setDisabled(false);
} catch (Exception e) {
addTooltip(ideaButton, Align.top, TOOLTIP_WIDTH, prop.getProperty("ideaNotFoundTip"));
ideaButton.setDisabled(true);
}

onChange(ideaButton, () -> {
try {
new ProcessBuilder(intellijPath, ".").directory(Gdx.files.absolute(UserData.projectPath).file()).start();
if (intellijIsFlatpak) {
new ProcessBuilder("flatpak", "run", intellijPath, ".").directory(Gdx.files.absolute(UserData.projectPath).file()).start();
} else {
new ProcessBuilder(intellijPath, ".").directory(Gdx.files.absolute(UserData.projectPath).file()).start();
}
} catch (IOException e) {
ideaButton.setText("WHOOPS");
}
Expand All @@ -95,6 +111,42 @@ public void populate(boolean fullscreen) {
onChange(textButton, () -> Gdx.app.exit());
}

private static String findManuallyInstalledIntellijOnWindows() throws Exception {
String programFilesFolder = System.getenv("PROGRAMFILES");
File jetbrainsFolder = new File(programFilesFolder, "JetBrains");
File[] ideaFolders = jetbrainsFolder.listFiles((dir, name) -> name.contains("IDEA"));
if (ideaFolders == null) {
throw new Exception("IntelliJ not found");
}

File intellijFolder = Stream.of(ideaFolders)
.max(Comparator.comparingLong(File::lastModified))
.orElseThrow(() -> new Exception("IntelliJ not found"));

return new File(intellijFolder, "bin/idea64.exe").getAbsolutePath();
}

@NotNull
private static String findFlatpakIntellij() throws Exception {
List<String> findFlatpakIntelliJ = Arrays.asList("flatpak", "list", "--app", "--columns=application");

Process flatpakProcess = new ProcessBuilder(findFlatpakIntelliJ).start();
if (flatpakProcess.waitFor() != 0) {
throw new Exception("Flatpak not found");
}

List<String> intellijIds = Arrays.asList("com.jetbrains.IntelliJ-IDEA-Ultimate", "com.jetbrains.IntelliJ-IDEA-Community");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(flatpakProcess.getInputStream()));

String line;
while ((line = bufferedReader.readLine()) != null) {
if (intellijIds.contains(line)) {
return line;
}
}
throw new Exception("Flatpak IntelliJ not installed");
}

@Override
public void captureKeyboardFocus() {

Expand Down

0 comments on commit 2f34479

Please sign in to comment.