Skip to content

Commit

Permalink
Changes resources processing from Groovy script to java/jshell script
Browse files Browse the repository at this point in the history
  • Loading branch information
jycr committed Oct 22, 2023
1 parent d23fcab commit 45ab514
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 44 deletions.
21 changes: 12 additions & 9 deletions ecocode-rules-specifications/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,24 @@
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.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.1.1</version>
<groupId>com.github.johnpoth</groupId>
<artifactId>jshell-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>prepare-rules-resources</id>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
<goal>run</goal>
</goals>
<configuration>
<source>${project.basedir}/src/main/script/prepare-resources.groovy</source>
<properties>
<sourceDir>${project.build.directory}/rules-html</sourceDir>
<targetDir>${project.build.outputDirectory}/io/ecocode/rules</targetDir>
</properties>
<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>
Expand Down
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

This file was deleted.

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

<name>ecoCode Sonar Plugins Project</name>
<description>Provides rules to reduce the environmental footprint of your programs</description>
<inceptionYear>2023</inceptionYear>
<url>https://github.com/green-code-initiative/ecocode</url>
<inceptionYear>2023</inceptionYear>
<organization>
<name>green-code-initiative</name>
<url>https://github.com/green-code-initiative</url>
Expand Down Expand Up @@ -262,7 +262,7 @@
</excludes>
</filter>
</filters>
</configuration>
</configuration>
</execution>
</executions>
</plugin>
Expand Down

0 comments on commit 45ab514

Please sign in to comment.