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

#621: Allow additional bundled configs #622

Merged
merged 2 commits into from
Sep 24, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.openapi.project.Project;
import org.infernus.idea.checkstyle.VersionListReader;
import org.infernus.idea.checkstyle.csapi.BundledConfig;
import org.infernus.idea.checkstyle.model.BundledConfigurationLocation;
import org.infernus.idea.checkstyle.model.ConfigurationLocation;
import org.infernus.idea.checkstyle.model.ConfigurationLocationFactory;
import org.infernus.idea.checkstyle.model.ScanScope;
Expand Down Expand Up @@ -216,13 +217,13 @@ private boolean booleanValueOfWithDefault(@NotNull final Map<String, String> pro
}

private void ensureBundledConfigs(@NotNull final List<ConfigurationLocation> configurationLocations) {
final ConfigurationLocation sunChecks = configurationLocationFactory().create(BundledConfig.SUN_CHECKS, project);
final ConfigurationLocation googleChecks = configurationLocationFactory().create(BundledConfig.GOOGLE_CHECKS, project);
if (!configurationLocations.contains(sunChecks)) {
configurationLocations.add(sunChecks);
}
if (!configurationLocations.contains(googleChecks)) {
configurationLocations.add(googleChecks);

List<BundledConfigurationLocation> bundledConfigurationLocations = BundledConfig.getAllBundledConfigs().stream().map(bc -> configurationLocationFactory().create(bc, project)).collect(Collectors.toList());

for (BundledConfigurationLocation bundledConfigurationLocation : bundledConfigurationLocations) {
if (!configurationLocations.contains(bundledConfigurationLocation)) {
configurationLocations.add(bundledConfigurationLocation);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public static PluginConfigurationBuilder defaultConfiguration(final Project proj
final String csDefaultVersion = new VersionListReader().getDefaultVersion();

final SortedSet<ConfigurationLocation> defaultLocations = new TreeSet<>();
defaultLocations.add(configurationLocationFactory(project).create(BundledConfig.SUN_CHECKS, project));
defaultLocations.add(configurationLocationFactory(project).create(BundledConfig.GOOGLE_CHECKS, project));

BundledConfig.getAllBundledConfigs().stream().map(bc -> configurationLocationFactory(project).create(bc, project)).forEach(defaultLocations::add);

final boolean copyLibs = OS.isWindows();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private TreeSet<org.infernus.idea.checkstyle.model.ConfigurationLocation> deseri
.map(location -> deserialiseLocation(project, location))
.filter(Objects::nonNull)
.collect(Collectors.toCollection(TreeSet::new));
List.of(BundledConfig.values()).forEach(bundleConfig -> {
BundledConfig.getAllBundledConfigs().forEach(bundleConfig -> {
if (configurationLocations.stream().noneMatch(bundleConfig::matches)) {
configurationLocations.add(configurationLocationFactory(project).create(bundleConfig, project));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package org.infernus.idea.checkstyle.csapi;

import com.intellij.openapi.diagnostic.Logger;
import org.infernus.idea.checkstyle.model.ConfigurationLocation;
import org.infernus.idea.checkstyle.model.ConfigurationType;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;


/**
* The configuration files bundled with Checkstyle-IDEA as provided by Checkstyle.
*/
public enum BundledConfig {
public class BundledConfig {

private static final Logger LOG = Logger.getInstance(BundledConfig.class);

private static final String BUNDLED_LOCATION = "(bundled)";

/** the Sun checks */
SUN_CHECKS("bundled-sun-checks", "(bundled)", "Sun Checks", "/sun_checks.xml"),
public static final BundledConfig SUN_CHECKS = new BundledConfig(0, "bundled-sun-checks", BUNDLED_LOCATION, "Sun Checks", "/sun_checks.xml");

/** the Google checks */
GOOGLE_CHECKS("bundled-google-checks", "(bundled)", "Google Checks", "/google_checks.xml");

public static final BundledConfig GOOGLE_CHECKS = new BundledConfig(1, "bundled-google-checks", BUNDLED_LOCATION, "Google Checks", "/google_checks.xml");

private final int sortOrder;
private final String id;

private final String location;
Expand All @@ -28,16 +40,31 @@ public enum BundledConfig {
private final String path;


BundledConfig(@NotNull final String id,
private BundledConfig(final int sortOrder,
@NotNull final String id,
@NotNull final String location,
@NotNull final String description,
@NotNull final String path) {
this.sortOrder = sortOrder;
this.id = id;
this.location = location;
this.description = description;
this.path = path;
}

private BundledConfig(final int sortOrder, final String id, final BundledConfigProvider.BasicConfig baseConfig) {
this.sortOrder = sortOrder;
this.id = id;
this.description = baseConfig.getDescription();
this.location = BUNDLED_LOCATION;
this.path = baseConfig.getPath();
}


public int getSortOrder() {
return sortOrder;
}

public String getId() {
return id;
}
Expand Down Expand Up @@ -72,4 +99,44 @@ public static BundledConfig fromDescription(@NotNull final String pDescription)
}
return result;
}

public static BundledConfig getDefault() {
return SUN_CHECKS;
}

public static Collection<BundledConfig> getAllBundledConfigs() {

Map<String,BundledConfig> map = new HashMap<>();

map.put(SUN_CHECKS.getId(), SUN_CHECKS);
map.put(GOOGLE_CHECKS.getId(), GOOGLE_CHECKS);

LOG.info("Loading additional BundledConfigs");


for (BundledConfigProvider bundledConfigProvider : ServiceLoader.load(BundledConfigProvider.class, BundledConfig.class.getClassLoader())) {

LOG.info("Loading additional BundledConfig " + bundledConfigProvider.getClass() + " from " + bundledConfigProvider.getClass().getProtectionDomain().getCodeSource());
for (BundledConfigProvider.BasicConfig config : bundledConfigProvider.getConfigs()) {

int i = 0;
String id = config.getId();
while(map.containsKey(id)) {
id = config.getId() + (++i);
}
BundledConfig bundledConfig = new BundledConfig(map.size(), id, config);
map.put(id, bundledConfig);
}
}
List<BundledConfig> ret = new ArrayList<>(map.values());
ret.sort((bc1, bc2) -> Integer.compare(bc1.getSortOrder(), bc2.getSortOrder()));
return ret;
}



public static Optional<BundledConfig> getById(final String id) {

return getAllBundledConfigs().stream().filter(bc -> bc.getId().equals(id)).findAny();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.infernus.idea.checkstyle.csapi;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;

/**
* Provider that providers the additional bundled checkstyle configs
*/
public interface BundledConfigProvider {

/**
* @return the configs
*/
@NotNull
Collection<BasicConfig> getConfigs();

public class BasicConfig {

private final String id;
private final String path;
private final String description;

public BasicConfig(final String id, final String description, final String path) {
this.id = requiredNotBlank(id, "id");
this.path = requiredNotBlank(path, "path");
this.description = requiredNotBlank(description, "description");
}

static String requiredNotBlank(final String val, final String name) {
if(StringUtils.isBlank(val)) {
throw new IllegalArgumentException(name + " must not be blank");
}
return val;
}

public String getId() {
return id;
}

public String getPath() {
return path;
}

public String getDescription() {
return description;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,14 @@ public String toString() {
return description;
}


@Override
public final int compareTo(@NotNull final ConfigurationLocation other) {
int result;
// bundled configs go first, ordered by their position in the BundledConfig enum
if (other instanceof BundledConfigurationLocation) {
if (this instanceof BundledConfigurationLocation) {
final int o1 = ((BundledConfigurationLocation) this).getBundledConfig().ordinal();
final int o2 = ((BundledConfigurationLocation) other).getBundledConfig().ordinal();
final int o1 = ((BundledConfigurationLocation) this).getBundledConfig().getSortOrder();
final int o2 = ((BundledConfigurationLocation) other).getBundledConfig().getSortOrder();
result = Integer.compare(o1, o2);
} else {
result = 1;
Expand Down