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

Allow setting project SDK (closes #11) #12

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
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
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 = 1.0.0
pluginVersion = 1.0.1
preRelease = false
hidden = false
pluginGroup = de.gebit.plugins.autoconfig
Expand Down
3 changes: 2 additions & 1 deletion metadata/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
<title>Changelog</title>
</head>
<body>
<h2>1.0.0</h2>
<h2>1.0.1</h2>
<ul>
<li>initial public release</li>
<li>allow disabling of async stack traces debugger agent</li>
<li>allow setting project SDK</li>
</ul>
<h2>0.0.6</h2>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public boolean hasAutoconfigDir() {

private Optional<VirtualFile> getConfigDirectory() {
var projectFile = project.getProjectFile();
ProjectKt.getStateStore(project).getDirectoryStorePath();
if (projectFile == null) {
// Fallback to find autoconfig directory. May happen when project is first opened and no misc.xml can be found
Path directoryStorePath = ProjectKt.getStateStore(project).getDirectoryStorePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@
import com.intellij.codeInsight.actions.onSave.OptimizeImportsOnSaveOptions;
import com.intellij.externalDependencies.ExternalDependenciesManager;
import com.intellij.externalDependencies.impl.ExternalDependenciesManagerImpl;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.externalSystem.autoimport.ExternalSystemProjectTrackerSettings;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.SdkType;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.updateSettings.impl.UpdateSettings;
import com.intellij.openapi.vcs.IssueNavigationConfiguration;
import com.intellij.openapi.vcs.IssueNavigationLink;
import de.gebit.plugins.autoconfig.UpdateHandler;
import de.gebit.plugins.autoconfig.model.*;
import de.gebit.plugins.autoconfig.model.Formatting;
import de.gebit.plugins.autoconfig.model.GeneralConfiguration;
import de.gebit.plugins.autoconfig.model.IssueNavigation;
import de.gebit.plugins.autoconfig.model.OnSave;
import de.gebit.plugins.autoconfig.model.ProjectSDK;
import de.gebit.plugins.autoconfig.model.ReloadProjectAutomatically;
import de.gebit.plugins.autoconfig.sdk.JDKResolver;
import de.gebit.plugins.autoconfig.state.TransientPluginStateService;
import de.gebit.plugins.autoconfig.util.Notifications;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

Expand All @@ -29,7 +39,8 @@
import java.util.List;

/**
* The main class used to update the IntelliJ configuration. All supported options are checked, updated and logged in a list of changed configs.
* The main class used to update the IntelliJ configuration. All supported options are checked, updated and logged in a
* list of changed configs.
*/
public class CommonConfigurationHandler extends AbstractHandler implements UpdateHandler<GeneralConfiguration> {
private static final @NonNls String CONFIG_SCHEMA_JSON = "/schema/config.schema.json";
Expand Down Expand Up @@ -65,20 +76,24 @@ public List<String> updateConfiguration(GeneralConfiguration options, Project pr
applyPluginHosts(options.getGlobalPluginRepositories(), project, updatedConfigs);
applyOnSaveOptions(options.getOnSave(), project, updatedConfigs);
applyReloadConfiguration(options.getReloadProjectAutomatically(), project, updatedConfigs);
applyProjectSDKOptions(options.getProjectSDK(), project, updatedConfigs);
return updatedConfigs;
}

private void applyReloadConfiguration(ReloadProjectAutomatically reloadProjectAutomatically, Project project, List<String> updatedConfigs) {
if (reloadProjectAutomatically != null) {
final ExternalSystemProjectTrackerSettings instance = ExternalSystemProjectTrackerSettings.getInstance(project);
final ExternalSystemProjectTrackerSettings instance = ExternalSystemProjectTrackerSettings.getInstance(
project);
if (Boolean.FALSE.equals(reloadProjectAutomatically.getEnabled())) {
applySetting(ExternalSystemProjectTrackerSettings.AutoReloadType.NONE, instance.getAutoReloadType(), instance::setAutoReloadType, updatedConfigs, "Automatic project reload deactivated");
applySetting(ExternalSystemProjectTrackerSettings.AutoReloadType.NONE, instance.getAutoReloadType(),
instance::setAutoReloadType, updatedConfigs, "Automatic project reload deactivated");
instance.setAutoReloadType(ExternalSystemProjectTrackerSettings.AutoReloadType.NONE);
} else if (reloadProjectAutomatically.getMode() != null) {
applySetting(switch (reloadProjectAutomatically.getMode()) {
case ANY_CHANGES -> ExternalSystemProjectTrackerSettings.AutoReloadType.ALL;
case EXTERNAL_CHANGES -> ExternalSystemProjectTrackerSettings.AutoReloadType.SELECTIVE;
}, instance.getAutoReloadType(), instance::setAutoReloadType, updatedConfigs, "Automatic project reload activated");
case ANY_CHANGES -> ExternalSystemProjectTrackerSettings.AutoReloadType.ALL;
case EXTERNAL_CHANGES -> ExternalSystemProjectTrackerSettings.AutoReloadType.SELECTIVE;
}, instance.getAutoReloadType(), instance::setAutoReloadType, updatedConfigs,
"Automatic project reload activated");
}
}
}
Expand All @@ -89,9 +104,11 @@ private void applyIssueNavigationConfiguration(List<IssueNavigation> issueNaviga
var newSettingsList = new ArrayList<IssueNavigationLink>();

for (IssueNavigation navigationConfig : issueNavigationConfig) {
newSettingsList.add(new IssueNavigationLink(navigationConfig.getExpression(), navigationConfig.getUrl()));
newSettingsList.add(
new IssueNavigationLink(navigationConfig.getExpression(), navigationConfig.getUrl()));
}
applySetting(newSettingsList, issueNavigationSettings.getLinks(), issueNavigationSettings::setLinks, updatedConfigs, "Issue navigation links");
applySetting(newSettingsList, issueNavigationSettings.getLinks(), issueNavigationSettings::setLinks,
updatedConfigs, "Issue navigation links");
}
}

Expand All @@ -100,15 +117,37 @@ private void applyOnSaveOptions(OnSave options, Project project, List<String> up
// first, initialise the application state service used for code formatter options
Formatting optionsFormat = options.getFormat();
Formatting optionsOptimizeImports = options.getOptimizeImports();
TransientPluginStateService.getInstance().initFormatterSettings(getFileTypes(optionsFormat), getFileTypes(optionsOptimizeImports));
TransientPluginStateService.getInstance()
.initFormatterSettings(getFileTypes(optionsFormat), getFileTypes(optionsOptimizeImports));

setFormattingOptions(updatedConfigs, optionsFormat, "Code format on save", FormatOnSaveOptions.getInstance(project));
setFormattingOptions(updatedConfigs, optionsOptimizeImports, "Optimize imports on save", OptimizeImportsOnSaveOptions.getInstance(project));
setFormattingOptions(updatedConfigs, optionsFormat, "Code format on save",
FormatOnSaveOptions.getInstance(project));
setFormattingOptions(updatedConfigs, optionsOptimizeImports, "Optimize imports on save",
OptimizeImportsOnSaveOptions.getInstance(project));
}
}

private void applyProjectSDKOptions(ProjectSDK sdkOptions, Project project, List<String> updatedConfigs) {
if (sdkOptions != null) {
SdkType sdk = SdkType.findByName(sdkOptions.getType());
if (sdk != null) {
String projectSdk = JDKResolver.findProjectSdk(sdkOptions.getName(), project);
if (projectSdk != null) {
ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
applySetting(projectSdk, projectRootManager.getProjectSdkName(), sdkName -> WriteAction.runAndWait(
() -> projectRootManager.setProjectSdkName(sdkName, sdkOptions.getType())), updatedConfigs,
"Project SDK");
}
} else {
Notifications.showWarning(
"SDK type \"" + sdkOptions.getType() + "\" has not been found for auto configuration", project);
nailujx86 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

private void setFormattingOptions(List<String> updatedConfigs, Formatting optionsFormat, String description, FormatOnSaveOptionsBase<?> format) {
applySetting(optionsFormat != null, format.isRunOnSaveEnabled(), format::setRunOnSaveEnabled, updatedConfigs, description);
applySetting(optionsFormat != null, format.isRunOnSaveEnabled(), format::setRunOnSaveEnabled, updatedConfigs,
description);
}

private List<String> getFileTypes(@Nullable Formatting formattingOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@

import com.intellij.conversion.impl.ConversionContextImpl;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
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.sdk.JDKResolver;
import org.jetbrains.idea.maven.execution.MavenRunner;
import org.jetbrains.idea.maven.project.MavenProjectBundle;
import org.jetbrains.idea.maven.project.MavenProjectsManager;
Expand All @@ -24,6 +21,8 @@
import java.util.ArrayList;
import java.util.List;

import static de.gebit.plugins.autoconfig.sdk.JDKResolver.findProjectSdk;

/**
* Maven configuration update handler.
*/
Expand Down Expand Up @@ -114,19 +113,4 @@ private String getMavenHome(Boolean useMavenWrapperConfig) {
}
}
}

private String findProjectSdk(String sdkName, Project project) {
if (sdkName == null) {
return null;
}
for (Sdk jdk : ProjectJdkTable.getInstance().getAllJdks()) {
if (jdk.getName().equals(sdkName)) {
return jdk.getName();
}
}

JDKResolver.resolveMissingJDK(sdkName, project);

return sdkName;
}
}
22 changes: 21 additions & 1 deletion src/main/java/de/gebit/plugins/autoconfig/sdk/JDKResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JavaSdk;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkType;
import com.intellij.openapi.projectRoots.impl.*;
import com.intellij.openapi.projectRoots.impl.UnknownSdkCollector;
import com.intellij.openapi.projectRoots.impl.UnknownSdkFix;
import com.intellij.openapi.projectRoots.impl.UnknownSdkFixAction;
import com.intellij.openapi.projectRoots.impl.UnknownSdkSnapshot;
import com.intellij.openapi.projectRoots.impl.UnknownSdkTracker;
import com.intellij.openapi.roots.ui.configuration.UnknownSdk;
import com.intellij.openapi.ui.MessageType;
import de.gebit.plugins.autoconfig.util.Notifications;
Expand All @@ -30,6 +35,21 @@ private JDKResolver() {
// nothing
}

public static String findProjectSdk(String sdkName, Project project) {
if (sdkName == null) {
return null;
}
for (Sdk jdk : ProjectJdkTable.getInstance().getAllJdks()) {
if (jdk.getName().equals(sdkName)) {
return jdk.getName();
}
}

resolveMissingJDK(sdkName, project);

return sdkName;
}

/**
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public static void showInfo(String text, Project project) {
getNotifier().createNotification(text, MessageType.INFO).notify(project);
}

public static void showWarning(String text, Project project) {
getNotifier().createNotification(text, MessageType.WARNING).notify(project);
}

public static void showError(String text, Project project) {
getNotifier().createNotification(text, MessageType.ERROR).notify(project);
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/resources/schema/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@
}
}
}
},
"projectSDK": {
"type": "object",
"description": "The project SDK settings.",
"required": [
"name",
"type"
],
"properties": {
"name": {
"type": "string",
"description": "The SDK name, e.g. a version or a distributor"
},
"type": {
"type": "string",
"description": "The SDK type, e.g. \"JavaSDK\", \"IDEA JDK\", \"KotlinSDK\" or \"Android SDK\"."
}
}
}
}
}