Skip to content

Commit

Permalink
fix: Quarkus Dev Mode is having trouble running main module by using
Browse files Browse the repository at this point in the history
Gradle with Kotlin DSL

Fixes #1374

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr authored and fbricon committed Aug 28, 2024
1 parent 9520627 commit ac4e0c2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -52,6 +49,7 @@ public class QuarkusModuleUtil {

public static final Pattern APPLICATION_YAML = Pattern.compile("application(-.+)?\\.ya?ml");

private static final Comparator<VirtualFile> ROOT_COMPARATOR = Comparator.comparingInt(r -> r.getPath().length());

/**
* Check if the module is a Quarkus project. Should check if some class if present
Expand Down Expand Up @@ -152,6 +150,34 @@ private static boolean isQuarkusModule(VirtualFile file, Project project) {
}

public static @Nullable VirtualFile getModuleDirPath(@NotNull Module module) {
return LocalFileSystem.getInstance().findFileByPath(ModuleUtilCore.getModuleDirPath(module));
VirtualFile[] roots = getContentRoots(module);
if (roots.length > 0) {
return roots[0];
}
return VfsUtil.findFileByIoFile(new File(ModuleUtilCore.getModuleDirPath(module)), true);
}

/**
* Returns an array of content roots of the given module sorted with smallest path first (to eliminate generated sources roots) from all content entries.
*
* @param module the module
* @return the array of content roots.
*/
public static VirtualFile[] getContentRoots(Module module) {
VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots();
if (roots.length <= 1) {
return roots;
}
// put root with smallest path first (eliminates generated sources roots)
sortRoot(roots);
return roots;
}

public static void sortRoot(List<VirtualFile> roots) {
Collections.sort(roots, ROOT_COMPARATOR); // put root with smallest path first (eliminates generated sources roots)
}

public static void sortRoot(VirtualFile[] roots) {
Arrays.sort(roots, ROOT_COMPARATOR); // put root with smallest path first (eliminates generated sources roots)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@
package com.redhat.devtools.intellij.qute.psi.core.command;

import com.google.gson.JsonPrimitive;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.redhat.devtools.lsp4ij.commands.CommandExecutor;
import com.redhat.devtools.lsp4ij.commands.LSPCommand;
import com.redhat.devtools.lsp4ij.commands.LSPCommandAction;

import java.util.List;

public abstract class QuteAction extends LSPCommandAction {

protected String getURL(LSPCommand command) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.lsp4ij.LanguageServiceAccessor;
import com.redhat.devtools.lsp4ij.commands.CommandExecutor;
import com.redhat.devtools.lsp4ij.commands.LSPCommand;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.ExecuteCommandOptions;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.eclipse.lsp4j.services.LanguageServer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class PsiQuteProjectUtils {
* Value for Qute annotations indicating behaviour should be using the default
*/
private static final String DEFAULTED = "<<defaulted>>";
private static final Comparator<VirtualFile> ROOT_COMPARATOR = Comparator.comparingInt(r -> r.getPath().length());

private PsiQuteProjectUtils() {
}
Expand Down Expand Up @@ -104,7 +103,7 @@ private static String getTemplateBaseDir(Module javaProject) {
public static @Nullable VirtualFile findBestResourcesDir(@NotNull Module javaProject) {
List<VirtualFile> resourcesDirs = ModuleRootManager.getInstance(javaProject).getSourceRoots(JavaResourceRootType.RESOURCE);
if (!resourcesDirs.isEmpty()) {
Collections.sort(resourcesDirs, ROOT_COMPARATOR); // put root with smallest path first (eliminates generated sources roots)
QuarkusModuleUtil.sortRoot(resourcesDirs); // put root with smallest path first (eliminates generated sources roots)
// The module configure 'Resources folder'
// 1) loop for each configured resources dir and returns the first which contains 'templates' folder.
for (var dir : resourcesDirs) {
Expand All @@ -117,10 +116,6 @@ private static String getTemplateBaseDir(Module javaProject) {
return resourcesDirs.get(0);
}
// Corner usecase, the module doesn't configure 'Resources folder', use the first content roots
VirtualFile[] roots = getContentRoots(javaProject);
if (roots.length > 0) {
return roots[0];
}
return QuarkusModuleUtil.getModuleDirPath(javaProject);
}

Expand All @@ -131,12 +126,7 @@ private static String getTemplateBaseDir(Module javaProject) {
* @return the array of content roots.
*/
public static VirtualFile[] getContentRoots(Module module) {
VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots();
if (roots.length <= 1) {
return roots;
}
Arrays.sort(roots, ROOT_COMPARATOR); // put root with smallest path first (eliminates generated sources roots)
return roots;
return QuarkusModuleUtil.getContentRoots(module);
}

/**
Expand Down

0 comments on commit ac4e0c2

Please sign in to comment.