Skip to content

Commit 4d44a70

Browse files
committed
Rough draft of configuring the plugin based on the Maven plugin configuration
1 parent 8c610f8 commit 4d44a70

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ dependencies {
8686
intellijIdeaCommunity("2024.1.7")
8787

8888
bundledPlugin("com.intellij.java")
89+
bundledPlugin("org.jetbrains.idea.maven")
8990

9091
testFramework(TestFrameworkType.Platform)
9192
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package org.infernus.idea.checkstyle.maven;
2+
3+
import java.util.ArrayList;
4+
import java.util.TreeSet;
5+
import org.infernus.idea.checkstyle.config.PluginConfigurationBuilder;
6+
import org.infernus.idea.checkstyle.config.PluginConfigurationManager;
7+
import org.infernus.idea.checkstyle.model.ConfigurationLocationFactory;
8+
import org.infernus.idea.checkstyle.model.ConfigurationType;
9+
import org.infernus.idea.checkstyle.model.NamedScopeHelper;
10+
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.idea.maven.importing.MavenAfterImportConfigurator;
12+
import org.jetbrains.idea.maven.model.MavenId;
13+
14+
@SuppressWarnings("UnstableApiUsage")
15+
public class MavenCheckstyleConfigurator implements MavenAfterImportConfigurator {
16+
17+
private static final MavenId CHECKSTYLE_MAVEN_ID =
18+
new MavenId("com.puppycrawl.tools", "checkstyle", null);
19+
private static final MavenId MAVEN_CHECKSTYLE_PLUGIN_MAVEN_ID =
20+
new MavenId("org.apache.maven.plugins", "maven-checkstyle-plugin", null);
21+
private static final String MAVEN_CONFIG_LOCATION_ID = "maven-config-location";
22+
23+
@Override
24+
public void afterImport(@NotNull final MavenAfterImportConfigurator.Context context) {
25+
final var project = context.getProject();
26+
context
27+
.getMavenProjectsWithModules()
28+
.iterator()
29+
.forEachRemaining(
30+
mavenProjectWithModules -> {
31+
final var mavenProject = mavenProjectWithModules.getMavenProject();
32+
final var checkstyleMavenPlugin =
33+
mavenProject.findPlugin(
34+
MAVEN_CHECKSTYLE_PLUGIN_MAVEN_ID.getGroupId(),
35+
MAVEN_CHECKSTYLE_PLUGIN_MAVEN_ID.getArtifactId());
36+
37+
if (checkstyleMavenPlugin == null) {
38+
return;
39+
}
40+
41+
final var checkstyleDependencyMavenId =
42+
checkstyleMavenPlugin.getDependencies().stream()
43+
.filter(
44+
dependency ->
45+
CHECKSTYLE_MAVEN_ID.equals(
46+
dependency.getGroupId(), dependency.getArtifactId()))
47+
.findFirst()
48+
.orElse(null);
49+
50+
final var pluginConfigurationManager =
51+
project.getService(PluginConfigurationManager.class);
52+
final var currentPluginConfiguration = pluginConfigurationManager.getCurrent();
53+
final var pluginConfigurationBuilder =
54+
PluginConfigurationBuilder.from(currentPluginConfiguration);
55+
// TODO: This will be null if checkstyle isn't declared explicitly.
56+
// Meaning the transitive dependency version won't be found.
57+
// Would be great to resolve that transitive version somehow.
58+
if (checkstyleDependencyMavenId != null
59+
&& checkstyleDependencyMavenId.getVersion() != null) {
60+
// Checkstyle Version
61+
pluginConfigurationBuilder.withCheckstyleVersion(
62+
checkstyleDependencyMavenId.getVersion());
63+
}
64+
65+
final var checkstyleMavenPluginConfiguration =
66+
checkstyleMavenPlugin.getConfigurationElement();
67+
68+
if (checkstyleMavenPluginConfiguration != null) {
69+
// Checkstyle Config File
70+
final var configLocationElement =
71+
checkstyleMavenPluginConfiguration.getChild("configLocation");
72+
if (configLocationElement != null && configLocationElement.getText() != null) {
73+
// TODO: configLocation can be a relative path, absolute path, URL, or classpath
74+
// resource.
75+
final String configLocation = configLocationElement.getText();
76+
final var configurationLocationFactory =
77+
project.getService(ConfigurationLocationFactory.class);
78+
// TODO: Should there be special handling if the user selects "sun_checks.xml" or
79+
// "google_checks.xml"?
80+
final var configurationLocation =
81+
configurationLocationFactory.create(
82+
project,
83+
MAVEN_CONFIG_LOCATION_ID,
84+
// TODO: This needs to be based on the configLocation.
85+
ConfigurationType.PROJECT_RELATIVE,
86+
configLocation,
87+
"Maven Config Location",
88+
// TODO: What should this be?
89+
NamedScopeHelper.getDefaultScope(project));
90+
91+
final var configLocations =
92+
new TreeSet<>(currentPluginConfiguration.getLocations());
93+
configLocations.removeIf(
94+
location -> MAVEN_CONFIG_LOCATION_ID.equals(location.getId()));
95+
configLocations.add(configurationLocation);
96+
pluginConfigurationBuilder.withLocations(configLocations);
97+
98+
final var activeConfigLocationIds =
99+
new TreeSet<>(currentPluginConfiguration.getActiveLocationIds());
100+
activeConfigLocationIds.removeIf(MAVEN_CONFIG_LOCATION_ID::equals);
101+
activeConfigLocationIds.add(configurationLocation.getId());
102+
pluginConfigurationBuilder.withActiveLocationIds(activeConfigLocationIds);
103+
}
104+
}
105+
106+
// Checkstyle Third Party Rules
107+
final var additionalDependencies =
108+
checkstyleMavenPlugin.getDependencies().stream()
109+
.filter(
110+
dependency ->
111+
!CHECKSTYLE_MAVEN_ID.equals(
112+
dependency.getGroupId(), dependency.getArtifactId()))
113+
.toList();
114+
if (!additionalDependencies.isEmpty()) {
115+
final var thirdPartyClassPath =
116+
new ArrayList<>(currentPluginConfiguration.getThirdPartyClasspath());
117+
118+
// TODO: Need to identify how to get the actual jar for a given MavenId.
119+
pluginConfigurationBuilder.withThirdPartyClassPath(thirdPartyClassPath);
120+
}
121+
122+
// TODO:
123+
// Checkstyle Scan Scope
124+
125+
final var newPluginConfiguration = pluginConfigurationBuilder.build();
126+
if (!currentPluginConfiguration.equals(newPluginConfiguration)) {
127+
pluginConfigurationManager.setCurrent(pluginConfigurationBuilder.build(), true);
128+
}
129+
});
130+
}
131+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<depends>com.intellij.modules.vcs</depends>
2222

2323
<depends>com.intellij.modules.java</depends>
24+
<!-- TODO: This should be optional, so that maven or gradle can still be used without forcing one or the other. -->
25+
<depends>org.jetbrains.idea.maven</depends>
2426

2527
<change-notes>
2628
<![CDATA[
@@ -105,6 +107,10 @@
105107
key="plugin.notification.logging"/>
106108
</extensions>
107109

110+
<extensions defaultExtensionNs="org.jetbrains.idea.maven">
111+
<importing.afterImportConfigurator implementation="org.infernus.idea.checkstyle.maven.MavenCheckstyleConfigurator" />
112+
</extensions>
113+
108114
<actions>
109115
<group id="CheckStylePluginTreeActions" text="Filter" popup="true">
110116
<action id="CheckStyleScrollToSourceAction"

0 commit comments

Comments
 (0)