From 26cbd35c08fb41856da29c8bf0352b1dab32790b Mon Sep 17 00:00:00 2001 From: piyush kumar sadangi Date: Tue, 12 Nov 2024 23:16:10 +0530 Subject: [PATCH] Issue 214: Support of config bundles with extra configuration files --- RegexpHeader/Example2/config.xml | 2 +- RegexpHeader/Example2/extra-config-files.txt | 1 + .../extractor/CheckstyleExampleExtractor.java | 41 ++++++++++++++++--- 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 RegexpHeader/Example2/extra-config-files.txt diff --git a/RegexpHeader/Example2/config.xml b/RegexpHeader/Example2/config.xml index a43216ec1..4786176d8 100644 --- a/RegexpHeader/Example2/config.xml +++ b/RegexpHeader/Example2/config.xml @@ -17,7 +17,7 @@ - + diff --git a/RegexpHeader/Example2/extra-config-files.txt b/RegexpHeader/Example2/extra-config-files.txt new file mode 100644 index 000000000..93925e67c --- /dev/null +++ b/RegexpHeader/Example2/extra-config-files.txt @@ -0,0 +1 @@ +java.header \ No newline at end of file diff --git a/extractor/src/main/java/com/example/extractor/CheckstyleExampleExtractor.java b/extractor/src/main/java/com/example/extractor/CheckstyleExampleExtractor.java index de14e8acd..2fb8786cc 100644 --- a/extractor/src/main/java/com/example/extractor/CheckstyleExampleExtractor.java +++ b/extractor/src/main/java/com/example/extractor/CheckstyleExampleExtractor.java @@ -32,6 +32,7 @@ import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; @@ -57,6 +58,12 @@ public final class CheckstyleExampleExtractor { /** The root directory of the project. */ private static final Path PROJECT_ROOT = Paths.get("").toAbsolutePath().getParent(); + /** The constant for RegexHeader module. */ + private static final String REGEXP_HEADER_MODULE = "regexpheader/Example2"; + + /** The constant for extra config file for RegexHeader. */ + private static final String JAVA_HEADER_FILE = "java.header"; + /** The filename for project properties. */ private static final String PROJ_PROP_FILENAME = "list-of-projects.properties"; @@ -428,8 +435,17 @@ private static void processFile( try { final String templateFilePath = getTemplateFilePathForExamples(exampleFile); if (templateFilePath != null) { - final String generatedContent = + String generatedContent = ConfigSerializer.serializeConfigToString(exampleFile, templateFilePath); + + // Special case handling for RegexpHeader/Example2 having external config + if (exampleFile.contains(REGEXP_HEADER_MODULE)) { + generatedContent = generatedContent.replace( + "config/java.header", + JAVA_HEADER_FILE + ); + } + writeConfigFile(outputPath, generatedContent); copyPropertiesFile(outputPath); generateReadme(outputPath); @@ -565,13 +581,28 @@ private static List getAllExampleFiles(final List exampleDirs) */ private static void generateAllInOneContent( final List allExampleFiles, - final Path allInOneSubfolderPath) - throws Exception { + final Path allInOneSubfolderPath + ) throws Exception { + + // Fail fast if null is not allowed + Objects.requireNonNull(allInOneSubfolderPath, "allInOneSubfolderPath must not be null"); + final String templateFilePath = getTemplateFilePathForExamples(allExampleFiles.get(0)); final Path outputFilePath = allInOneSubfolderPath.resolve("config.xml"); - final String generatedContent = ConfigSerializer.serializeAllInOneConfigToString( - allExampleFiles.toArray(new String[0]), templateFilePath); + String generatedContent = ConfigSerializer.serializeAllInOneConfigToString( + allExampleFiles.toArray(new String[0]), + templateFilePath + ); + + final Path parent = allInOneSubfolderPath.getParent(); + if (parent != null) { + final String parentPath = parent.toString(); + if (parentPath.contains(REGEXP_HEADER_MODULE)) { + generatedContent = generatedContent.replace("config/java.header", JAVA_HEADER_FILE); + } + } + Files.writeString(outputFilePath, generatedContent); }