Skip to content

Commit

Permalink
Mapping and resolving JDK versions
Browse files Browse the repository at this point in the history
- if a jdk isn't found on the system a notification suggests the user to fix the JDK differently
  • Loading branch information
nailujx86 committed Jul 19, 2024
1 parent 5b8f43e commit 8469e20
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Autoconfig Plugin Properties
pluginVersion = 0.0.3
pluginVersion = 0.0.4
preRelease = false
hidden = false
pluginGroup = de.gebit.plugins.autoconfig
Expand Down
5 changes: 5 additions & 0 deletions metadata/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<title>Changelog</title>
</head>
<body>
<h2>0.0.4</h2>
<ul>
<li>map autoconfig jdk versions onto the versions already installed on the users machine</li>
<li>suggest user to download / manually install jdk if a jdk is not found</li>
</ul>
<h2>0.0.2</h2>
<ul>
<li>show notification in case the plugin made no config changes</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import de.gebit.plugins.autoconfig.UpdateHandler;
import de.gebit.plugins.autoconfig.handlers.AbstractHandler;
import de.gebit.plugins.autoconfig.model.MavenConfiguration;
import de.gebit.plugins.autoconfig.util.Notifications;
import de.gebit.plugins.autoconfig.sdk.JDKResolver;
import org.jetbrains.idea.maven.execution.MavenRunner;
import org.jetbrains.idea.maven.project.MavenProjectBundle;
import org.jetbrains.idea.maven.project.MavenProjectsManager;
Expand Down Expand Up @@ -53,35 +53,51 @@ public List<String> updateConfiguration(MavenConfiguration maven, Project projec
List<String> changedConfigs = new ArrayList<>();
var mavenProject = MavenProjectsManager.getInstance(project);
var mavenProjectSettings = mavenProject.getGeneralSettings();
applySetting(maven.getThreads(), mavenProjectSettings.getThreads(), mavenProjectSettings::setThreads, changedConfigs, "Maven thread parameter");
applySetting(maven.getThreads(), mavenProjectSettings.getThreads(), mavenProjectSettings::setThreads,
changedConfigs, "Maven thread parameter");

var settingsFilePath = maven.getSettingsFile();
if (settingsFilePath != null && project.getBasePath() != null) {
var projectBasePath = Paths.get(project.getBasePath());
var conversionContextImpl = new ConversionContextImpl(projectBasePath);
var expandPath = conversionContextImpl.expandPath(settingsFilePath);
applySetting(expandPath, mavenProjectSettings.getUserSettingsFile(), mavenProjectSettings::setUserSettingsFile, changedConfigs, "Maven user settings file");
applySetting(expandPath, mavenProjectSettings.getUserSettingsFile(),
mavenProjectSettings::setUserSettingsFile, changedConfigs, "Maven user settings file");
}

// https://youtrack.jetbrains.com/issue/IDEA-338870 setMavenHome und getMavenHome wird vsl. mit 2025.1 entfernt
applySetting(getMavenHome(maven.getUseMavenWrapper()), mavenProjectSettings.getMavenHome(), mavenProjectSettings::setMavenHome, changedConfigs, "Maven home");
applySetting(maven.getUseMavenConfig(), mavenProjectSettings.isUseMavenConfig(), mavenProjectSettings::setUseMavenConfig, changedConfigs, "Use maven config");
applySetting(getMavenHome(maven.getUseMavenWrapper()), mavenProjectSettings.getMavenHome(),
mavenProjectSettings::setMavenHome, changedConfigs, "Maven home");
applySetting(maven.getUseMavenConfig(), mavenProjectSettings.isUseMavenConfig(),
mavenProjectSettings::setUseMavenConfig, changedConfigs, "Use maven config");

var mavenImportingConfig = maven.getImporting();
if (mavenImportingConfig != null) {
var mavenImportingProjectSettings = mavenProject.getImportingSettings();
applySetting(mavenImportingConfig.getDetectCompiler(), mavenImportingProjectSettings.isAutoDetectCompiler(), mavenImportingProjectSettings::setAutoDetectCompiler, changedConfigs, "Autodetect compiler");
applySetting(mavenImportingConfig.getDownloadSources(), mavenImportingProjectSettings.isDownloadSourcesAutomatically(), mavenImportingProjectSettings::setDownloadSourcesAutomatically, changedConfigs, "Download sources");
applySetting(mavenImportingConfig.getDownloadDocumentation(), mavenImportingProjectSettings.isDownloadDocsAutomatically(), mavenImportingProjectSettings::setDownloadDocsAutomatically, changedConfigs, "Download documentation");
applySetting(mavenImportingConfig.getVmOptions(), mavenImportingProjectSettings.getVmOptionsForImporter(), mavenImportingProjectSettings::setVmOptionsForImporter, changedConfigs, "VM options for maven importer");
applySetting(findProjectSdk(mavenImportingConfig.getJdk(), project), mavenImportingProjectSettings.getJdkForImporter(), mavenImportingProjectSettings::setJdkForImporter, changedConfigs, "JDK for importer");
applySetting(mavenImportingConfig.getDetectCompiler(), mavenImportingProjectSettings.isAutoDetectCompiler(),
mavenImportingProjectSettings::setAutoDetectCompiler, changedConfigs, "Autodetect compiler");
applySetting(mavenImportingConfig.getDownloadSources(),
mavenImportingProjectSettings.isDownloadSourcesAutomatically(),
mavenImportingProjectSettings::setDownloadSourcesAutomatically, changedConfigs, "Download sources");
applySetting(mavenImportingConfig.getDownloadDocumentation(),
mavenImportingProjectSettings.isDownloadDocsAutomatically(),
mavenImportingProjectSettings::setDownloadDocsAutomatically, changedConfigs,
"Download documentation");
applySetting(mavenImportingConfig.getVmOptions(), mavenImportingProjectSettings.getVmOptionsForImporter(),
mavenImportingProjectSettings::setVmOptionsForImporter, changedConfigs,
"VM options for maven importer");
applySetting(findProjectSdk(mavenImportingConfig.getJdk(), project),
mavenImportingProjectSettings.getJdkForImporter(), mavenImportingProjectSettings::setJdkForImporter,
changedConfigs, "JDK for importer");
}

var mavenRunnerConfig = maven.getRunner();
if (mavenRunnerConfig != null) {
var mavenRunnerProjectSettings = MavenRunner.getInstance(project).getState();
applySetting(mavenRunnerConfig.getVmOptions(), mavenRunnerProjectSettings.getVmOptions(), mavenRunnerProjectSettings::setVmOptions, changedConfigs, "VM options for maven runner");
applySetting(findProjectSdk(mavenRunnerConfig.getJre(), project), mavenRunnerProjectSettings.getJreName(), mavenRunnerProjectSettings::setJreName, changedConfigs, "JRE for runner");
applySetting(mavenRunnerConfig.getVmOptions(), mavenRunnerProjectSettings.getVmOptions(),
mavenRunnerProjectSettings::setVmOptions, changedConfigs, "VM options for maven runner");
applySetting(findProjectSdk(mavenRunnerConfig.getJre(), project), mavenRunnerProjectSettings.getJreName(),
mavenRunnerProjectSettings::setJreName, changedConfigs, "JRE for runner");
}

return changedConfigs;
Expand All @@ -108,7 +124,9 @@ private String findProjectSdk(String sdkName, Project project) {
return jdk.getName();
}
}
Notifications.showError("SDK \"" + sdkName + " not found, please install it and add it in your SDK list.", project);
return null;

JDKResolver.resolveMissingJDK(sdkName, project);

return sdkName;
}
}
104 changes: 104 additions & 0 deletions src/main/java/de/gebit/plugins/autoconfig/sdk/JDKResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package de.gebit.plugins.autoconfig.sdk;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.NotificationGroupManager;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.SdkType;
import com.intellij.openapi.projectRoots.impl.*;
import com.intellij.openapi.roots.ui.configuration.UnknownSdk;
import com.intellij.openapi.ui.MessageType;
import de.gebit.plugins.autoconfig.util.Notifications;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
* Utility class to resolve missing JDKs
*/
public class JDKResolver {

private JDKResolver() {
// nothing
}

/**
* Ensures, that the currently missing JDK is made available for the project. If the JDK is not found on the system
* it tries to assist the user in applying the IDEs suggestions.
*
* @param jdkName the name of the JDK to resolve
* @param project the project to resolve the JDK for
*/
public static void resolveMissingJDK(String jdkName, Project project) {
final NotificationGroup notifier = Notifications.getNotifier();
final NotificationGroup sdkNotification = NotificationGroupManager.getInstance()
.getNotificationGroup("AutoconfigSDK");

final Notification notification = notifier.createNotification(
"JDK \"" + jdkName + "\" not found. Autoconfig is trying to resolve this SDK automatically. If this fails, please configure the SDK manually.",
MessageType.WARNING);
notification.notify(project);

ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
final UnknownSdkCollector unknownSdkCollector = new UnknownSdkCollector(project) {
@NotNull
@Override
public UnknownSdkSnapshot collectSdksBlocking() {
return new UnknownSdkSnapshot(Set.of(), List.of(new UnknownSdk() {
@Override
public @NotNull String getSdkName() {
return jdkName;
}

@Override
public @NotNull SdkType getSdkType() {
return JavaSdk.getInstance();
}
}), Arrays.stream(ProjectJdkTable.getInstance().getAllJdks()).toList());
}
};

final List<UnknownSdkFix> unknownSdkFixes = UnknownSdkTracker.getInstance(project)
.collectUnknownSdks(unknownSdkCollector, progressIndicator);
final List<UnknownSdkFix> otherFixes = UnknownSdkTracker.getInstance(project)
.applyAutoFixesAndNotify(unknownSdkFixes, progressIndicator);

if (otherFixes.isEmpty()) {
// we've found an instant fix for this JDK
notification.expire();
}

for (UnknownSdkFix unknownSdkFix : otherFixes) {
final UnknownSdkFixAction suggestedFixAction = unknownSdkFix.getSuggestedFixAction();
if (suggestedFixAction != null) {
// it seems like we've found a fix for this JDK, but it's not an instant fix, e.g. downloading a jdk.
// we'll prompt the user to apply the fix
final Notification fixableSdkError = sdkNotification.createNotification("Fixable missing JDK",
suggestedFixAction.getActionDetailedText(), NotificationType.WARNING);
fixableSdkError.addAction(DumbAwareAction.create(suggestedFixAction.getActionShortText(), evt -> {
suggestedFixAction.applySuggestionAsync(project);
fixableSdkError.expire();
notification.expire();
}));
fixableSdkError.setImportantSuggestion(true);
fixableSdkError.setSuggestionType(true);
fixableSdkError.notify(project);
} else {
// neither we nor IntelliJ were able to find a fix for the requested JDK. This isn't going anywhere.
sdkNotification.createNotification("Unfixable missing JDK!",
unknownSdkFix.getNotificationText() + "\nPlease install it manually and add it to your SDK list.",
NotificationType.ERROR).notify(project);
}
}
}, "Autoconfig: Resolving JDK \"" + jdkName + "\"", true, project);
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<extensions defaultExtensionNs="com.intellij">
<postStartupActivity implementation="de.gebit.plugins.autoconfig.AutoconfigStartup"/>
<notificationGroup displayType="BALLOON" id="Autoconfig"/>
<notificationGroup displayType="STICKY_BALLOON" id="AutoconfigSDK"/>
<formatOnSaveOptions.defaultsProvider implementation="de.gebit.plugins.autoconfig.FormatOnSaveOptionsDefaultsProvider"/>
<updateSettingsProvider implementation="de.gebit.plugins.autoconfig.plugins.AutoconfigUpdateSettingsProvider"/>
</extensions>
Expand Down

0 comments on commit 8469e20

Please sign in to comment.