Skip to content

Commit

Permalink
Merge pull request #2216 from Haehnchen/feature/assets-configure-exclude
Browse files Browse the repository at this point in the history
compiled assets folder can be excluded via plugin autoconfigure
  • Loading branch information
Haehnchen authored Aug 26, 2023
2 parents f60fce8 + 194312b commit fdde7e9
Showing 1 changed file with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*/
public class IdeHelper {

private static final String DIRECTORY_EXCLUDE_MESSAGE = "Directory 'var' marked as excluded for indexing";
private static final String DIRECTORY_EXCLUDE_MESSAGE = "Directory '%s' marked as excluded for indexing";

public static void openUrl(String url) {
if(java.awt.Desktop.isDesktopSupported() ) {
Expand Down Expand Up @@ -231,44 +231,65 @@ public static Collection<String> enablePluginAndConfigure(@NotNull Project proje
Module moduleForFile = ModuleUtilCore.findModuleForFile(ProjectUtil.getProjectDir(project), project);
if (moduleForFile != null) {
ModifiableRootModel modifiableRootModel = ModuleRootManager.getInstance(moduleForFile).getModifiableModel();
boolean needsCommit = false;

for (ContentEntry contentEntry : modifiableRootModel.getContentEntries()) {
VirtualFile projectDir = ProjectUtil.getProjectDir(modifiableRootModel.getProject());

VirtualFile var = VfsUtil.findRelativeFile(projectDir, "var");
if (var == null) {
continue;
}

// second check for valid exclude
if (VfsUtil.findRelativeFile(projectDir, "var", "cache") == null && VfsUtil.findRelativeFile(projectDir, "var", "log") == null) {
continue;
}
if (var != null && (VfsUtil.findRelativeFile(projectDir, "var", "cache") != null || VfsUtil.findRelativeFile(projectDir, "var", "log") != null) && !hasAlreadyAnExclude(contentEntry, projectDir, "var")) {
contentEntry.addExcludeFolder(var);
needsCommit = true;

boolean hasVarDirectoryInExclude = Arrays.stream(contentEntry.getExcludeFolders()).anyMatch(excludeFolder -> {
VirtualFile file = excludeFolder.getFile();
if (file == null) {
return false;
if (!messages.contains(String.format(DIRECTORY_EXCLUDE_MESSAGE, "var"))) {
messages.add(String.format(DIRECTORY_EXCLUDE_MESSAGE, "var"));
}
}

String path = VfsUtil.getRelativePath(file, projectDir, '/');
// encore build path + "var" are Symfony structure root
VirtualFile publicBuild = VfsUtil.findRelativeFile(projectDir, "public", "build");
if (publicBuild != null && var != null && !hasAlreadyAnExclude(contentEntry, projectDir, "public/build")) {
contentEntry.addExcludeFolder(publicBuild);
needsCommit = true;

return "var".equals(path);
});
if (!messages.contains(String.format(DIRECTORY_EXCLUDE_MESSAGE, "public/build"))) {
messages.add(String.format(DIRECTORY_EXCLUDE_MESSAGE, "public/build"));
}
}

if (!hasVarDirectoryInExclude) {
contentEntry.addExcludeFolder(var);
ApplicationManager.getApplication().runWriteAction(modifiableRootModel::commit);
// assetic bundle + "var" are Symfony structure root
VirtualFile publicBundles = VfsUtil.findRelativeFile(projectDir, "public", "bundles");
if (publicBundles != null && var != null && !hasAlreadyAnExclude(contentEntry, projectDir, "public/bundles")) {
contentEntry.addExcludeFolder(publicBundles);
needsCommit = true;

if (!messages.contains(DIRECTORY_EXCLUDE_MESSAGE)) {
messages.add(DIRECTORY_EXCLUDE_MESSAGE);
if (!messages.contains(String.format(DIRECTORY_EXCLUDE_MESSAGE, "public/bundles"))) {
messages.add(String.format(DIRECTORY_EXCLUDE_MESSAGE, "public/bundles"));
}
}
}

if (needsCommit) {
ApplicationManager.getApplication().runWriteAction(modifiableRootModel::commit);
}
}

return messages;
}

private static boolean hasAlreadyAnExclude(@NotNull ContentEntry contentEntry, @NotNull VirtualFile projectDir, @NotNull String path) {
return Arrays.stream(contentEntry.getExcludeFolders()).anyMatch(excludeFolder -> {
VirtualFile file = excludeFolder.getFile();
if (file == null) {
return false;
}

return path.equals(VfsUtil.getRelativePath(file, projectDir, '/'));
});
}

public static void navigateToPsiElement(@NotNull PsiElement psiElement) {
final Navigatable descriptor = PsiNavigationSupport.getInstance().getDescriptor(psiElement);
if (descriptor != null) {
Expand Down

0 comments on commit fdde7e9

Please sign in to comment.