Skip to content

Commit

Permalink
Allow creation of language-specific metadata files
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Nov 20, 2023
1 parent e52fd18 commit 57d8775
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
15 changes: 15 additions & 0 deletions ecocode-rules-specifications/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
<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>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<optional>true</optional> <!-- Do not expose this dependency to children -->
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin><!--
Expand Down
45 changes: 42 additions & 3 deletions ecocode-rules-specifications/src/main/script/PrepareResources.jsh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//usr/bin/env jshell -v "$@" "$0"; exit $?

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -37,7 +41,7 @@ import static java.util.Optional.of;
@Override
public void run() {
getResourcesToCopy().forEach(rule -> {
copyFile(rule.metadata, rule.getMetadataTargetPath(targetDir));
mergeAndCopyFile(rule.metadata, rule.specificMetadata, rule.getMetadataTargetPath(targetDir));
copyFile(rule.htmlDescription, rule.getHtmlDescriptionTargetPath(targetDir));
});
}
Expand All @@ -55,6 +59,37 @@ import static java.util.Optional.of;
}
}

private void mergeAndCopyFile(Path source, Path merge, Path target) {
if (Files.isRegularFile(merge)) {
LOGGER.log(DEBUG, "Merge: {0} and {1} -> {2}", source, merge, target);
try (
FileReader sourceReader = new FileReader(source.toFile());
FileReader mergeReader = new FileReader(merge.toFile())
) {
JSONParser parser = new JSONParser();
JSONObject sourceJson = (JSONObject) parser.parse(sourceReader);
JSONObject mergeJson = (JSONObject) parser.parse(mergeReader);
mergeJsonObjects(sourceJson, mergeJson);
Files.write(target, sourceJson.toString().getBytes());
} catch (IOException | ParseException e) {
throw new RuntimeException("cannot process source " + source, e);
}
} else {
copyFile(source, target);
}
}

private void mergeJsonObjects(JSONObject object1, JSONObject object2) {
object2.forEach((key, value) -> {
Object element = object1.get(key);
if (element instanceof JSONObject && value instanceof JSONObject) {
mergeJsonObjects((JSONObject) element, (JSONObject) value);
} else {
object1.put(key, value);
}
});
}

private void copyFile(Path source, Path target) {
LOGGER.log(DEBUG, "Copy: {0} -> {1}", source, target);
try {
Expand All @@ -79,6 +114,7 @@ import static java.util.Optional.of;
}
final String ruleKey = matcher.group("ruleKey");
final Path metadata = htmlDescription.getParent().getParent().resolve(ruleKey + ".json");
final Path specificMetadata = htmlDescription.getParent().resolve(ruleKey + ".json");

if (!Files.isRegularFile(htmlDescription) || !Files.isRegularFile(metadata)) {
return empty();
Expand All @@ -87,18 +123,21 @@ import static java.util.Optional.of;
return of(new Rule(
matcher.group("language"),
htmlDescription,
metadata
metadata,
specificMetadata
));
}

private final String language;
private final Path htmlDescription;
private final Path metadata;
private final Path specificMetadata;

Rule(String language, Path htmlDescription, Path metadata) {
Rule(String language, Path htmlDescription, Path metadata, Path specificMetadata) {
this.language = language;
this.htmlDescription = htmlDescription;
this.metadata = metadata;
this.specificMetadata = specificMetadata;
}

Path getHtmlDescriptionTargetPath(Path targetDir) {
Expand Down

0 comments on commit 57d8775

Please sign in to comment.