From 85ebbe7239e97ce99d04dceecb77b18854eff3db Mon Sep 17 00:00:00 2001 From: jycr Date: Mon, 10 Jul 2023 16:32:45 +0200 Subject: [PATCH 1/6] Removes some warnings in build --- pom.xml | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f30d9ec81..2055c4142 100644 --- a/pom.xml +++ b/pom.xml @@ -99,6 +99,8 @@ 11 ${java.version} ${java.version} + + ${java.version} UTF-8 ${encoding} @@ -218,6 +220,14 @@ shade + + + + + ${project.version} + + + commons-*:* @@ -245,8 +255,14 @@ META-INF/** + + io.ecocode:ecocode-rules-specifications:* + + META-INF/** + + - + From 8f46e4197b7e9d0ec1dac21525f446f6bfe99716 Mon Sep 17 00:00:00 2001 From: jycr Date: Mon, 10 Jul 2023 16:33:39 +0200 Subject: [PATCH 2/6] Removes metadata for rules not available in corresponding language --- ecocode-rules-specifications/pom.xml | 65 ++++++------------- .../src/main/script/prepare-resources.groovy | 33 ++++++++++ 2 files changed, 52 insertions(+), 46 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/script/prepare-resources.groovy diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index 81977d3d7..adfef2d6a 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -65,6 +65,11 @@ - syntax highlighting (see code blocks on ASCIIDOC rules) - inclusions (see: php/EC74.asciidoc) - table data generation from CSV (see: php/EC34.asciidoc) + + NB: Current version has a bug which display following false positive warning : + [WARNING] Duplicated destination found: overwriting file ... + + This issue is fixed in 3.x release of asciidoctor-maven-plugin --> org.asciidoctor asciidoctor-maven-plugin @@ -78,12 +83,13 @@ ${project.basedir}/src/main/rules - ${project.build.directory}/rules + ${project.build.directory}/rules-html coderay style true + true false true @@ -97,57 +103,24 @@ - org.apache.maven.plugins - maven-antrun-plugin - 3.1.0 + org.codehaus.gmaven + groovy-maven-plugin + 2.1.1 - process-resources + generate-resources - run + execute - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + ${project.basedir}/src/main/script/prepare-resources.groovy + + ${project.build.directory}/rules-html + ${project.build.outputDirectory}/io/ecocode/rules + diff --git a/ecocode-rules-specifications/src/main/script/prepare-resources.groovy b/ecocode-rules-specifications/src/main/script/prepare-resources.groovy new file mode 100644 index 000000000..15eb9bee7 --- /dev/null +++ b/ecocode-rules-specifications/src/main/script/prepare-resources.groovy @@ -0,0 +1,33 @@ +#!/usr/bin/env groovy +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.StandardCopyOption + +Path projectBasedir = Path.of(project.basedir.absolutePath) + +Path sourceDir = Path.of(Objects.requireNonNull(properties.sourceDir, "property must be set: sourceDir")) +Path targetDir = Path.of(Objects.requireNonNull(properties.targetDir, "property must be set: targetDir")) + +def copyFile(Path basedirPath, Path source, Path target) { + log.debug("Copy: {} -> {}", basedirPath.relativize(source), basedirPath.relativize(target)) + Files.createDirectories(target.parent) + Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING) +} + +sourceDir.traverse(type: groovy.io.FileType.FILES, nameFilter: ~/.*\.html$/) { Path htmlPath -> + def language = htmlPath.parent.fileName.toString() + def languageDir = targetDir.resolve(language) + + def ruleDir = htmlPath.parent.parent + def ruleKey = ruleDir.fileName.toString() + + def metadataFileSourcePath = ruleDir.resolve(ruleKey + ".json") + + def htmlTargetPath = languageDir.resolve(htmlPath.fileName) + def metadataFileTargetPath = languageDir.resolve(metadataFileSourcePath.fileName) + + if(Files.exists(metadataFileSourcePath)) { + copyFile(projectBasedir, htmlPath, htmlTargetPath) + copyFile(projectBasedir, metadataFileSourcePath, metadataFileTargetPath) + } +} From e577d18e6a8179906e6114a872eaa8fad1889ae2 Mon Sep 17 00:00:00 2001 From: jycr Date: Mon, 10 Jul 2023 16:40:36 +0200 Subject: [PATCH 3/6] Renames "js" and "ts" languages to "javascript" See: * https://github.com/green-code-initiative/ecoCode-javascript/pull/14#discussion_r1257548418 * https://github.com/green-code-initiative/ecoCode-javascript/pull/14#discussion_r1257564032 --- ecocode-rules-specifications/pom.xml | 16 ++-------------- .../main/assembly/{js.xml => javascript.xml} | 4 ++-- .../src/main/assembly/ts.xml | 18 ------------------ .../EC11/{js => javascript}/EC11.asciidoc | 0 .../EC12/{js => javascript}/EC12.asciidoc | 0 .../EC13/{ts => javascript}/EC13.asciidoc | 0 .../rules/EC8/{js => javascript}/EC8.asciidoc | 0 .../rules/EC9/{js => javascript}/EC9.asciidoc | 0 8 files changed, 4 insertions(+), 34 deletions(-) rename ecocode-rules-specifications/src/main/assembly/{js.xml => javascript.xml} (87%) delete mode 100644 ecocode-rules-specifications/src/main/assembly/ts.xml rename ecocode-rules-specifications/src/main/rules/EC11/{js => javascript}/EC11.asciidoc (100%) rename ecocode-rules-specifications/src/main/rules/EC12/{js => javascript}/EC12.asciidoc (100%) rename ecocode-rules-specifications/src/main/rules/EC13/{ts => javascript}/EC13.asciidoc (100%) rename ecocode-rules-specifications/src/main/rules/EC8/{js => javascript}/EC8.asciidoc (100%) rename ecocode-rules-specifications/src/main/rules/EC9/{js => javascript}/EC9.asciidoc (100%) diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index adfef2d6a..805538401 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -178,26 +178,14 @@ - assembly-js + assembly-javascript prepare-package single - ${project.basedir}/src/main/assembly/js.xml - - - - - assembly-ts - prepare-package - - single - - - - ${project.basedir}/src/main/assembly/ts.xml + ${project.basedir}/src/main/assembly/javascript.xml diff --git a/ecocode-rules-specifications/src/main/assembly/js.xml b/ecocode-rules-specifications/src/main/assembly/javascript.xml similarity index 87% rename from ecocode-rules-specifications/src/main/assembly/js.xml rename to ecocode-rules-specifications/src/main/assembly/javascript.xml index d5d72ca1e..9b7dbd84d 100644 --- a/ecocode-rules-specifications/src/main/assembly/js.xml +++ b/ecocode-rules-specifications/src/main/assembly/javascript.xml @@ -1,7 +1,7 @@ - js + javascript jar @@ -10,7 +10,7 @@ ${project.build.outputDirectory} - io/ecocode/rules/js/*.* + io/ecocode/rules/javascript/*.* diff --git a/ecocode-rules-specifications/src/main/assembly/ts.xml b/ecocode-rules-specifications/src/main/assembly/ts.xml deleted file mode 100644 index ec0100661..000000000 --- a/ecocode-rules-specifications/src/main/assembly/ts.xml +++ /dev/null @@ -1,18 +0,0 @@ - - ts - - jar - - false - - - ${project.build.outputDirectory} - - io/ecocode/rules/ts/*.* - - - - - diff --git a/ecocode-rules-specifications/src/main/rules/EC11/js/EC11.asciidoc b/ecocode-rules-specifications/src/main/rules/EC11/javascript/EC11.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules/EC11/js/EC11.asciidoc rename to ecocode-rules-specifications/src/main/rules/EC11/javascript/EC11.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC12/js/EC12.asciidoc b/ecocode-rules-specifications/src/main/rules/EC12/javascript/EC12.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules/EC12/js/EC12.asciidoc rename to ecocode-rules-specifications/src/main/rules/EC12/javascript/EC12.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC13/ts/EC13.asciidoc b/ecocode-rules-specifications/src/main/rules/EC13/javascript/EC13.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules/EC13/ts/EC13.asciidoc rename to ecocode-rules-specifications/src/main/rules/EC13/javascript/EC13.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC8/js/EC8.asciidoc b/ecocode-rules-specifications/src/main/rules/EC8/javascript/EC8.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules/EC8/js/EC8.asciidoc rename to ecocode-rules-specifications/src/main/rules/EC8/javascript/EC8.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC9/js/EC9.asciidoc b/ecocode-rules-specifications/src/main/rules/EC9/javascript/EC9.asciidoc similarity index 100% rename from ecocode-rules-specifications/src/main/rules/EC9/js/EC9.asciidoc rename to ecocode-rules-specifications/src/main/rules/EC9/javascript/EC9.asciidoc From d23fcab56e79b77b9ce03c42e7055849fd83190a Mon Sep 17 00:00:00 2001 From: jycr Date: Mon, 10 Jul 2023 16:53:19 +0200 Subject: [PATCH 4/6] Removes unnecessary dependencies and plugins --- ecocode-rules-specifications/pom.xml | 51 ++-------------------------- 1 file changed, 3 insertions(+), 48 deletions(-) diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index 805538401..2bfbada5f 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -14,52 +14,9 @@ Repository that contains the specifications of every static-analysis rules available in ecoCode plugins. https://github.com/green-code-initiative/ecoCode/tree/main/ecocode-rules-specifications - - - org.sonarsource.sonarqube - sonar-plugin-api - provided - - - org.sonarsource.analyzer-commons - sonar-analyzer-commons - - - - org.junit.jupiter - junit-jupiter - test - - - - org.assertj - assertj-core - test - - - - - org.jacoco - jacoco-maven-plugin - - - prepare-agent - - prepare-agent - - - - report - - report - - - - - - @@ -125,8 +81,7 @@ - - - org.codehaus.gmaven - groovy-maven-plugin - 2.1.1 + com.github.johnpoth + jshell-maven-plugin + 1.4 + prepare-rules-resources generate-resources - execute + run - ${project.basedir}/src/main/script/prepare-resources.groovy - - ${project.build.directory}/rules-html - ${project.build.outputDirectory}/io/ecocode/rules - + + + + + + + diff --git a/ecocode-rules-specifications/src/main/script/PrepareResources.jsh b/ecocode-rules-specifications/src/main/script/PrepareResources.jsh new file mode 100644 index 000000000..04f15b3f6 --- /dev/null +++ b/ecocode-rules-specifications/src/main/script/PrepareResources.jsh @@ -0,0 +1,117 @@ +//usr/bin/env jshell -v "$@" "$0"; exit $? + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static java.lang.System.Logger.Level.DEBUG; +import static java.util.Optional.empty; +import static java.util.Optional.of; + + class PrepareResources implements Runnable { + private static final System.Logger LOGGER = System.getLogger("PrepareResources"); + + private final Path sourceDir; + private final Path targetDir; + + public static void main(String... args) throws Exception { + new PrepareResources( + Path.of(Objects.requireNonNull(System.getProperty("sourceDir"), "system property: sourceDir")), + Path.of(Objects.requireNonNull(System.getProperty("targetDir"), "system property: targetDir")) + ).run(); + } + + PrepareResources(Path sourceDir, Path targetDir) { + this.sourceDir = sourceDir; + this.targetDir = targetDir; + } + + @Override + public void run() { + getResourcesToCopy().forEach(rule -> { + copyFile(rule.metadata, rule.getMetadataTargetPath(targetDir)); + copyFile(rule.htmlDescription, rule.getHtmlDescriptionTargetPath(targetDir)); + }); + } + + private List getResourcesToCopy() { + try (Stream stream = Files.walk(sourceDir)) { + return stream + .filter(Files::isRegularFile) + .map(Rule::createFromHtmlDescription) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toList()); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + private void copyFile(Path source, Path target) { + LOGGER.log(DEBUG, "Copy: {0} -> {1}", source, target); + try { + Files.createDirectories(target.getParent()); + Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + + private static class Rule { + /** + * Resources to include + */ + private static final Pattern TARGET_RESOURCES = Pattern.compile("^.*/(?EC\\d+)/(?[^/]*)/.*\\.html$"); + + static Optional createFromHtmlDescription(Path htmlDescription) { + final Matcher matcher = TARGET_RESOURCES.matcher(htmlDescription.toString().replace('\\', '/')); + if (!matcher.find()) { + return empty(); + } + final String ruleKey = matcher.group("ruleKey"); + final Path metadata = htmlDescription.getParent().getParent().resolve(ruleKey + ".json"); + + if (!Files.isRegularFile(htmlDescription) || !Files.isRegularFile(metadata)) { + return empty(); + } + + return of(new Rule( + matcher.group("language"), + htmlDescription, + metadata + )); + } + + private final String language; + private final Path htmlDescription; + private final Path metadata; + + Rule(String language, Path htmlDescription, Path metadata) { + this.language = language; + this.htmlDescription = htmlDescription; + this.metadata = metadata; + } + + Path getHtmlDescriptionTargetPath(Path targetDir) { + return targetDir.resolve(language).resolve(htmlDescription.getFileName()); + } + + Path getMetadataTargetPath(Path targetDir) { + return targetDir.resolve(language).resolve(metadata.getFileName()); + } + } + } + + PrepareResources.main(); + + // @formatter:off +/exit diff --git a/ecocode-rules-specifications/src/main/script/prepare-resources.groovy b/ecocode-rules-specifications/src/main/script/prepare-resources.groovy deleted file mode 100644 index 15eb9bee7..000000000 --- a/ecocode-rules-specifications/src/main/script/prepare-resources.groovy +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env groovy -import java.nio.file.Files -import java.nio.file.Path -import java.nio.file.StandardCopyOption - -Path projectBasedir = Path.of(project.basedir.absolutePath) - -Path sourceDir = Path.of(Objects.requireNonNull(properties.sourceDir, "property must be set: sourceDir")) -Path targetDir = Path.of(Objects.requireNonNull(properties.targetDir, "property must be set: targetDir")) - -def copyFile(Path basedirPath, Path source, Path target) { - log.debug("Copy: {} -> {}", basedirPath.relativize(source), basedirPath.relativize(target)) - Files.createDirectories(target.parent) - Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING) -} - -sourceDir.traverse(type: groovy.io.FileType.FILES, nameFilter: ~/.*\.html$/) { Path htmlPath -> - def language = htmlPath.parent.fileName.toString() - def languageDir = targetDir.resolve(language) - - def ruleDir = htmlPath.parent.parent - def ruleKey = ruleDir.fileName.toString() - - def metadataFileSourcePath = ruleDir.resolve(ruleKey + ".json") - - def htmlTargetPath = languageDir.resolve(htmlPath.fileName) - def metadataFileTargetPath = languageDir.resolve(metadataFileSourcePath.fileName) - - if(Files.exists(metadataFileSourcePath)) { - copyFile(projectBasedir, htmlPath, htmlTargetPath) - copyFile(projectBasedir, metadataFileSourcePath, metadataFileTargetPath) - } -} diff --git a/pom.xml b/pom.xml index 2055c4142..1beefaee1 100644 --- a/pom.xml +++ b/pom.xml @@ -9,8 +9,8 @@ ecoCode Sonar Plugins Project Provides rules to reduce the environmental footprint of your programs - 2023 https://github.com/green-code-initiative/ecocode + 2023 green-code-initiative https://github.com/green-code-initiative @@ -262,7 +262,7 @@ - + From 175a2d36aca99b6f5a322bd6ddd553867780f9c1 Mon Sep 17 00:00:00 2001 From: jycr Date: Fri, 27 Oct 2023 17:26:51 +0200 Subject: [PATCH 6/6] [ISSUE 209] fix: "java.lang.NoClassDefFoundError: org/sonarsource/analyzer/commons/RuleMetadataLoader" --- java-plugin/pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/java-plugin/pom.xml b/java-plugin/pom.xml index f5f780e25..20bf75d59 100644 --- a/java-plugin/pom.xml +++ b/java-plugin/pom.xml @@ -36,6 +36,11 @@ provided + + org.sonarsource.analyzer-commons + sonar-analyzer-commons + + com.google.re2j