Skip to content

Commit

Permalink
Implemented fallback project directory resolving mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
swesteme committed Sep 6, 2024
1 parent 4796a66 commit 05b2ff9
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.impl.ProjectImpl;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import de.gebit.plugins.autoconfig.util.Notifications;

import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -26,6 +29,11 @@
*/
@Service(Service.Level.PROJECT)
public final class ConfigurationLoaderService {
/**
* Configuration directory containing yaml sources.
*/
public static final String AUTOCONFIG_DIRECTORY = "autoconfig";

private final Project project;

private final Map<String, Object> configurationOptions = new HashMap<>();
Expand Down Expand Up @@ -68,12 +76,21 @@ public boolean hasAutoconfigDir() {
return getConfigDirectory().isPresent();
}

@SuppressWarnings("UnstableApiUsage")
private Optional<VirtualFile> getConfigDirectory() {
var projectFile = project.getProjectFile();
if (projectFile == null) {
// Fallback to find autoconfig directory. May happen when project is first opened and no misc.xml can be found
if (project instanceof ProjectImpl projectImpl) {
Path directoryStorePath = projectImpl.getComponentStore().getDirectoryStorePath();
if (directoryStorePath != null) {
return Optional.ofNullable(VirtualFileManager.getInstance().findFileByNioPath(directoryStorePath))
.map(m -> m.findChild(AUTOCONFIG_DIRECTORY));
}
}
return Optional.empty();
}
return Optional.ofNullable(projectFile.getParent().findChild("autoconfig"));
return Optional.ofNullable(projectFile.getParent().findChild(AUTOCONFIG_DIRECTORY));
}

public void resetConfigurationCache() {
Expand Down

0 comments on commit 05b2ff9

Please sign in to comment.