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

fix: Creates rule metadata parent directory if needed #263

Merged
merged 1 commit into from
Dec 15, 2023
Merged
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
46 changes: 27 additions & 19 deletions ecocode-rules-specifications/src/main/script/PrepareResources.jsh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import static java.util.Optional.of;
@Override
public void run() {
getResourcesToCopy().forEach(rule -> {
mergeAndCopyJsonMetadata(rule.metadata, rule.specificMetadata, rule.getMetadataTargetPath(targetDir));
mergeOrCopyJsonMetadata(rule.metadata, rule.specificMetadata, rule.getMetadataTargetPath(targetDir));
copyFile(rule.htmlDescription, rule.getHtmlDescriptionTargetPath(targetDir));
});
}
Expand All @@ -63,39 +63,47 @@ import static java.util.Optional.of;
}
}

private void mergeAndCopyJsonMetadata(Path source, Path merge, Path target) {
private void mergeOrCopyJsonMetadata(Path source, Path merge, Path target) {
try {
Files.createDirectories(target.getParent());
} catch (IOException e) {
throw new RuntimeException(e);
}
if (Files.isRegularFile(merge)) {
LOGGER.log(DEBUG, "Merge: {0} and {1} -> {2}", source, merge, target);
mergeJsonFile(source, merge, target);
} else {
copyFile(source, target);
}
}

try (
private void mergeJsonFile(Path source, Path merge, Path target) {
LOGGER.log(DEBUG, "Merge: {0} and {1} -> {2}", source, merge, target);

try (
JsonReader sourceJsonReader = Json.createReader(Files.newBufferedReader(source));
JsonReader mergeJsonReader = Json.createReader(Files.newBufferedReader(merge));
JsonWriter resultJsonWriter = Json.createWriter(Files.newBufferedWriter(target));
) {
Files.createDirectories(target.getParent());
) {
Files.createDirectories(target.getParent());

JsonObject sourceJson = sourceJsonReader.readObject();
JsonObject mergeJson = mergeJsonReader.readObject();
JsonObject sourceJson = sourceJsonReader.readObject();
JsonObject mergeJson = mergeJsonReader.readObject();

JsonMergePatch mergePatch = Json.createMergePatch(mergeJson);
JsonValue result = mergePatch.apply(sourceJson);
JsonMergePatch mergePatch = Json.createMergePatch(mergeJson);
JsonValue result = mergePatch.apply(sourceJson);

resultJsonWriter.write(result);
} catch (IOException e) {
throw new RuntimeException("cannot process source " + source, e);
}
} else {
copyFile(source, target);
resultJsonWriter.write(result);
} catch (IOException e) {
throw new RuntimeException("cannot process source " + source, e);
}
}

private void copyFile(Path source, Path target) {
LOGGER.log(DEBUG, "Copy: {0} -> {1}", source, target);
try {
Files.createDirectories(target.getParent());
LOGGER.log(DEBUG, "Copy: {0} -> {1}", source, target);
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new IllegalStateException(e);
throw new RuntimeException("unable to copy '" + source + "' to '"+ target +"'", e);
}
}

Expand Down