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

Feature/optimize rules specifications artifact #209

Merged
merged 7 commits into from
Oct 27, 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
133 changes: 26 additions & 107 deletions ecocode-rules-specifications/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,19 @@
<description>Repository that contains the specifications of every static-analysis rules available in ecoCode plugins.</description>
<url>https://github.com/green-code-initiative/ecoCode/tree/main/ecocode-rules-specifications</url>

<dependencies>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.analyzer-commons</groupId>
<artifactId>sonar-analyzer-commons</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<!--
<plugin><!--
This plugin convert ASCIIDOC rule specification in HTML format
ASCIIDOC format is used with custom features such as :
- 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
-->
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
Expand All @@ -78,12 +40,13 @@
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/rules</sourceDirectory>
<outputDirectory>${project.build.directory}/rules</outputDirectory>
<outputDirectory>${project.build.directory}/rules-html</outputDirectory>
<attributes>
<source-highlighter>coderay</source-highlighter>
<coderay-css>style</coderay-css>
</attributes>
<preserveDirectories>true</preserveDirectories>
<embedAssets>true</embedAssets>
<headerFooter>false</headerFooter>
<relativeBaseDir>true</relativeBaseDir>
<logHandler>
Expand All @@ -95,65 +58,33 @@
</execution>
</executions>
</plugin>
<plugin>
<!--
Target resources tree need to be "flat".
Each metadata JSON file must be in the same tree as the HTML description file for the corresponding language.
The only way currently found to generate the correct file tree is to use antrun-plugin with `flattenmapper`
<plugin><!--
Prepare resources tree needed by language.
Each metadata JSON file must be in the same folder as the HTML description file for the corresponding language.
-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<groupId>com.github.johnpoth</groupId>
<artifactId>jshell-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>process-resources</phase>
<id>prepare-rules-resources</id>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/java">
<fileset dir="${project.build.directory}/rules">
<include name="*/java/EC*.html" />
<include name="*/EC*.json" />
</fileset>
<flattenmapper />
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/php">
<fileset dir="${project.build.directory}/rules">
<include name="*/php/EC*.html" />
<include name="*/EC*.json" />
</fileset>
<flattenmapper />
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/python">
<fileset dir="${project.build.directory}/rules">
<include name="*/python/EC*.html" />
<include name="*/EC*.json" />
</fileset>
<flattenmapper />
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/js">
<fileset dir="${project.build.directory}/rules">
<include name="*/js/EC*.html" />
<include name="*/EC*.json" />
</fileset>
<flattenmapper />
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/ts">
<fileset dir="${project.build.directory}/rules">
<include name="*/ts/EC*.html" />
<include name="*/EC*.json" />
</fileset>
<flattenmapper />
</copy>
</target>
<scripts>
<script>${project.basedir}/src/main/script/PrepareResources.jsh</script>
</scripts>
<options>
<option>-R "-DsourceDir=${project.build.directory}/rules-html"</option>
<option>-R "-DtargetDir=${project.build.outputDirectory}/io/ecocode/rules"</option>
</options>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!--
<plugin><!--
This module produce one artifact by language (with corresponding classifier)
For example, to add rule specifications for Python language, add following dependency in python sonarqube plugin:
Expand Down Expand Up @@ -205,26 +136,14 @@
</configuration>
</execution>
<execution>
<id>assembly-js</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/js.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>assembly-ts</id>
<id>assembly-javascript</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/ts.xml</descriptor>
<descriptor>${project.basedir}/src/main/assembly/javascript.xml</descriptor>
</descriptors>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
<id>js</id>
<id>javascript</id>
<formats>
<format>jar</format>
</formats>
Expand All @@ -10,7 +10,7 @@
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>io/ecocode/rules/js/*.*</include>
<include>io/ecocode/rules/javascript/*.*</include>
</includes>
<outputDirectory/>
</fileSet>
Expand Down
18 changes: 0 additions & 18 deletions ecocode-rules-specifications/src/main/assembly/ts.xml

This file was deleted.

117 changes: 117 additions & 0 deletions ecocode-rules-specifications/src/main/script/PrepareResources.jsh
Original file line number Diff line number Diff line change
@@ -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<Rule> getResourcesToCopy() {
try (Stream<Path> 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("^.*/(?<ruleKey>EC\\d+)/(?<language>[^/]*)/.*\\.html$");

static Optional<Rule> 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
5 changes: 5 additions & 0 deletions java-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.sonarsource.analyzer-commons</groupId>
<artifactId>sonar-analyzer-commons</artifactId>
</dependency>

<!-- for security on regex patterns : https://github.com/google/re2j -->
<dependency>
<groupId>com.google.re2j</groupId>
Expand Down
Loading