Skip to content

Commit 9b5a8c7

Browse files
committed
Issue #50: Accept propertypath as input param
1 parent 86d6fd4 commit 9b5a8c7

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/main/java/org/checkstyle/autofix/CheckstyleAutoFix.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public class CheckstyleAutoFix extends Recipe {
4242
example = "config/checkstyle.xml")
4343
private String configurationPath;
4444

45+
@Option(displayName = "Checkstyle properties file path",
46+
description = "Path to the file containing the Checkstyle Properties.",
47+
example = "config/checkstyle.properties",
48+
required = false)
49+
private String propertiesPath;
50+
4551
@Override
4652
public String getDisplayName() {
4753
return "Checkstyle autoFix";
@@ -60,6 +66,10 @@ public String getConfigurationPath() {
6066
return configurationPath;
6167
}
6268

69+
public String getPropertiesPath() {
70+
return propertiesPath;
71+
}
72+
6373
@Override
6474
public List<Recipe> getRecipeList() {
6575
final List<CheckstyleViolation> violations = CheckstyleReportParser
@@ -69,6 +79,6 @@ public List<Recipe> getRecipeList() {
6979
}
7080

7181
private CheckConfiguration loadCheckstyleConfiguration() {
72-
return ConfigurationLoader.loadConfiguration(getConfigurationPath());
82+
return ConfigurationLoader.loadConfiguration(getConfigurationPath(), getPropertiesPath());
7383
}
7484
}

src/main/java/org/checkstyle/autofix/parser/ConfigurationLoader.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.checkstyle.autofix.parser;
1919

20+
import java.io.FileInputStream;
21+
import java.io.IOException;
2022
import java.util.HashMap;
2123
import java.util.List;
2224
import java.util.Map;
@@ -57,13 +59,26 @@ private static CheckConfiguration mapConfiguration(Configuration config) {
5759
return new CheckConfiguration(config.getName(), properties, List.of(simpleChildren));
5860
}
5961

60-
public static CheckConfiguration loadConfiguration(String checkstyleConfigurationPath) {
62+
public static CheckConfiguration loadConfiguration(String checkstyleConfigurationPath,
63+
String propFile) {
64+
Properties props = new Properties();
65+
if (propFile == null) {
66+
props = System.getProperties();
67+
}
68+
else {
69+
try (FileInputStream input = new FileInputStream(propFile)) {
70+
props.load(input);
71+
}
72+
catch (IOException exception) {
73+
throw new IllegalStateException("Failed to read: " + propFile, exception);
74+
}
75+
}
6176

6277
final Configuration checkstyleConfig;
6378
try {
6479
checkstyleConfig = com.puppycrawl.tools.checkstyle.ConfigurationLoader
6580
.loadConfiguration(checkstyleConfigurationPath,
66-
new PropertiesExpander(new Properties()));
81+
new PropertiesExpander(props));
6782
}
6883
catch (CheckstyleException exception) {
6984
throw new IllegalStateException("Failed to load configuration:"

src/main/resources/META-INF/rewrite/recipes.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ recipeList:
1616
- org.checkstyle.autofix.CheckstyleAutoFix:
1717
violationReportPath: "target/checkstyle/checkstyle-report.xml"
1818
configurationPath: "https://raw.githubusercontent.com/checkstyle/checkstyle/checkstyle-${checkstyle.version}/config/checkstyle-checks.xml"
19+
propertiesPath: "config/checkstyle.properties"

0 commit comments

Comments
 (0)