Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement looking for manually installed idea on windows #193

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading