Skip to content

Commit

Permalink
Rebased to make use of module updater extension point
Browse files Browse the repository at this point in the history
  • Loading branch information
swesteme committed Dec 20, 2024
1 parent baf0634 commit 50eee31
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 186 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ jsonSchema2Pojo {
useOptionalForGetters = false

// properties to exclude from generated toString
toStringExcludes = ["someProperty"]
toStringExcludes = []

// What Java version to target with generated source code (1.6, 1.8, 9, 11, etc).
// By default, the version will be taken from the Gradle Java plugin's 'sourceCompatibility',
Expand Down
3 changes: 2 additions & 1 deletion metadata/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
<title>Changelog</title>
</head>
<body>
<h2>1.0.3</h2>
<h2>1.1.0</h2>
<ul>
<li>support setting of module SDK</li>
<li>support setting of module SonarQube project keys</li>
</ul>
<h2>1.0.2</h2>
<ul>
Expand Down
61 changes: 2 additions & 59 deletions src/main/java/de/gebit/plugins/autoconfig/AutoconfigStartup.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,81 +8,24 @@

package de.gebit.plugins.autoconfig;

import com.intellij.ide.impl.TrustedProjects;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.ProjectActivity;
import de.gebit.plugins.autoconfig.util.Notifications;
import de.gebit.plugins.autoconfig.service.ConfigurationUpdaterService;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static de.gebit.plugins.autoconfig.util.Notifications.showInfo;

/**
* Entry point for the opening of a project. The yaml configuration file is read here, the resulting configuration
* options object is passed to the CommonConfigurationHandler. At the end, a message is composed, displaying a list of
* all updated configuration options.
*/
public class AutoconfigStartup implements ProjectActivity {
private static final com.intellij.openapi.diagnostic.Logger LOG = Logger.getInstance(AutoconfigStartup.class);

public static final ExtensionPointName<UpdateHandler<?>> EP_NAME = ExtensionPointName.create(
"de.gebit.plugins.autoconfig.configurationUpdater");

@Nullable
@Override
public Object execute(@NotNull Project project, @NotNull Continuation<? super Unit> continuation) {
return runAutoconfig(project);
}

@SuppressWarnings("UnstableApiUsage")
public @Nullable List<String> runAutoconfig(@NotNull Project project) {
ConfigurationLoaderService projectService = project.getService(ConfigurationLoaderService.class);
if (projectService == null || !projectService.hasAutoconfigDir()) {
return null;
}

if (!TrustedProjects.isTrusted(project)) {
showInfo("Project configuration has not been updated, because project was opened in safe mode.", project);
return null;
}

List<String> changedConfigs = new ArrayList<>();

for (UpdateHandler<?> updateHandler : EP_NAME.getExtensionList()) {
changedConfigs.addAll(processUpdateHandler(project, updateHandler, projectService));
}

if (!changedConfigs.isEmpty()) {
String notification = String.join(", ", changedConfigs);
showInfo("New project configurations applied: " + notification, project);
}

return changedConfigs;
}

private <T> List<String> processUpdateHandler(@NotNull Project project, UpdateHandler<T> updateHandler, ConfigurationLoaderService projectService) {
Optional<T> extensionConfiguration = projectService.getConfiguration(updateHandler.getConfigurationClass(),
updateHandler.getFileName());
if (extensionConfiguration.isPresent()) {
try {
return updateHandler.updateConfiguration(extensionConfiguration.get(), project);
} catch (Exception e) {
String errorMessage = "An error occurred trying to process configuration updates for \"" + updateHandler.getUpdaterName() + "\"";
LOG.error(errorMessage, e);
Notifications.showError(errorMessage, project);
}
} else {
LOG.info("No configuration for " + updateHandler.getUpdaterName() + " found.");
}
return Collections.emptyList();
return project.getService(ConfigurationUpdaterService.class).runAutoconfig();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ default boolean acceptModule(T configuration, Module module) {
default UpdateTarget getUpdateTarget() {
return UpdateTarget.MODULE;
}

default boolean matchesAnyName(Module module, List<String> patterns) {
return patterns.stream().anyMatch(p -> module.getName().matches(p));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
* Allow setting module SDK for a limited number of matching modules.
Expand Down Expand Up @@ -54,10 +55,6 @@ public boolean acceptModule(GeneralModuleConfiguration configuration, Module mod
return matchesAnyName(module, configuration.getModuleFilter());
}

private static boolean matchesAnyName(Module module, List<String> patterns) {
return patterns.stream().anyMatch(p -> module.getName().matches(p));
}

@Override
public List<String> updateConfiguration(GeneralModuleConfiguration configuration, Module module) {
List<String> updatedConfigs = new ArrayList<>();
Expand All @@ -78,17 +75,12 @@ private void applyModuleSDKOptions(ModuleSDK sdkOptions, Module module, List<Str
if (projectSdk != null && projectSdk.equals(moduleSdk)) {
// reset module SDK in case project SDK and designated module SDK are identical
if (!modifiableModel.isSdkInherited()) {
WriteAction.runAndWait(() -> {
modifiableModel.inheritSdk();
modifiableModel.commit();
});
commitModifiableModelChange(modifiableModel, ModifiableRootModel::inheritSdk);
updatedConfigs.add("Module SDK");
}
} else {
applySetting(moduleSdk, currentModuleSdk, s -> WriteAction.runAndWait(() -> {
modifiableModel.setSdk(s);
modifiableModel.commit();
}), updatedConfigs, "Module SDK");
applySetting(moduleSdk, currentModuleSdk, s -> commitModifiableModelChange(modifiableModel,
modifiableRootModel -> modifiableRootModel.setSdk(s)), updatedConfigs, "Module SDK");
}
}
} else {
Expand All @@ -98,4 +90,11 @@ private void applyModuleSDKOptions(ModuleSDK sdkOptions, Module module, List<Str
}
}
}

private void commitModifiableModelChange(ModifiableRootModel modifiableModel, Consumer<ModifiableRootModel> consumer) {
WriteAction.runAndWait(() -> {
consumer.accept(modifiableModel);
modifiableModel.commit();
});
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.gebit.plugins.autoconfig.handlers.sonarqube;

import com.intellij.openapi.module.Module;
import de.gebit.plugins.autoconfig.UpdateModuleHandler;
import de.gebit.plugins.autoconfig.handlers.AbstractHandler;
import de.gebit.plugins.autoconfig.model.SonarQubeConfiguration;
import org.jetbrains.annotations.NonNls;
import org.sonarlint.intellij.config.Settings;
import org.sonarlint.intellij.config.module.SonarLintModuleSettings;
import org.sonarlint.intellij.config.project.SonarLintProjectSettings;

import java.util.ArrayList;
import java.util.List;

/**
* Configuration handler used to synchronise SonarQube module bindings in projects.
*/
public class SonarQubeModuleHandler extends AbstractHandler implements UpdateModuleHandler<SonarQubeConfiguration> {
private static final @NonNls String CONFIG_SCHEMA_JSON = "/schema/sonarqubeModule.schema.json";

public static final @NonNls String CONFIG_FILE_NAME = "autoconfigSonarQubeModule.yaml";

@Override
public String getFileName() {
return CONFIG_FILE_NAME;
}

@Override
public String getJsonSchema() {
return CONFIG_SCHEMA_JSON;
}

@Override
public String getUpdaterName() {
return "SonarQube module configuration updater";
}

@Override
public Class<SonarQubeConfiguration> getConfigurationClass() {
return SonarQubeConfiguration.class;
}

@Override
public boolean acceptModule(SonarQubeConfiguration configuration, Module module) {
return matchesAnyName(module, configuration.getModuleFilter());
}

@Override
public List<String> updateConfiguration(SonarQubeConfiguration configuration, Module module) {
List<String> updatedConfigs = new ArrayList<>();
SonarLintProjectSettings projectSettings = Settings.getSettingsFor(module.getProject());
if (projectSettings.isBindingEnabled() && configuration.getProjectKey() != null) {
SonarLintModuleSettings moduleSettings = Settings.getSettingsFor(module);
applySetting(configuration.getProjectKey(), moduleSettings.getProjectKey(), moduleSettings::setProjectKey,
updatedConfigs, "SonarQube module key");
}
return updatedConfigs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,9 @@ private <T> List<String> processUpdateHandler(@NotNull Project project, UpdateHa
try {
return updateHandler.updateConfiguration(c, project);
} catch (Exception e) {
LOG.error("An error occurred trying to process configuration updates for \"{}\"", e,
updateHandler.getUpdaterName());
Notifications.showError(
"An error occurred trying to process configuration updates for \"" + updateHandler.getUpdaterName() + "\"",
project);
String errorMessage = "An error occurred trying to process configuration updates for \"" + updateHandler.getUpdaterName() + "\"";
LOG.error(errorMessage, e);
Notifications.showError(errorMessage, project);
}
return emptyList;
}).orElseGet(() -> {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin-git.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<idea-plugin>
<depends>de.gebit.plugins.autoconfig</depends>
<extensions defaultExtensionNs="de.gebit.plugins.autoconfig">
<configurationUpdater implementation="de.gebit.plugins.autoconfig.handlers.git.GitHandler"/>
</extensions>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin-java.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<idea-plugin>
<depends>de.gebit.plugins.autoconfig</depends>
<extensions defaultExtensionNs="de.gebit.plugins.autoconfig">
<configurationUpdater implementation="de.gebit.plugins.autoconfig.handlers.java.JavaHandler"/>
</extensions>
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin-maven.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<idea-plugin>
<depends>de.gebit.plugins.autoconfig</depends>
<extensions defaultExtensionNs="de.gebit.plugins.autoconfig">
<configurationUpdater implementation="de.gebit.plugins.autoconfig.handlers.maven.MavenHandler"/>
</extensions>
Expand Down
5 changes: 0 additions & 5 deletions src/main/resources/META-INF/plugin-sonarlint.xml

This file was deleted.

6 changes: 6 additions & 0 deletions src/main/resources/META-INF/plugin-sonarqube.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<idea-plugin>
<depends>de.gebit.plugins.autoconfig</depends>
<extensions defaultExtensionNs="de.gebit.plugins.autoconfig">
<moduleConfigurationUpdater implementation="de.gebit.plugins.autoconfig.handlers.sonarqube.SonarQubeModuleHandler"/>
</extensions>
</idea-plugin>
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<depends optional="true" config-file="plugin-git.xml">Git4Idea</depends>
<depends optional="true" config-file="plugin-maven.xml">org.jetbrains.idea.maven</depends>
<depends optional="true" config-file="plugin-java.xml">com.intellij.modules.java</depends>
<depends optional="true" config-file="plugin-sonarlint.xml">org.sonarlint.idea</depends>
<depends optional="true" config-file="plugin-sonarqube.xml">org.sonarlint.idea</depends>

<extensionPoints>
<extensionPoint name="configurationUpdater" interface="de.gebit.plugins.autoconfig.UpdateHandler" dynamic="true"/>
Expand Down
30 changes: 0 additions & 30 deletions src/main/resources/schema/sonarlint.schema.json

This file was deleted.

Loading

0 comments on commit 50eee31

Please sign in to comment.