-
Notifications
You must be signed in to change notification settings - Fork 105
Add a global JSON index file inside Multi-language Rules Specification archive #375
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
base: main
Are you sure you want to change the base?
Changes from all commits
7a16174
26fb39b
39bce64
a64429e
7c77bd3
e092b02
cd3cc4f
2663c5a
73db5d0
169a45e
235e4dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,3 +13,6 @@ dependency-reduced-pom.xml | |
|
|
||
| # Ignore IDE files | ||
| *.iml | ||
|
|
||
| # Data directory is only for `gh-pages` branch | ||
| /data/ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. What name would you suggest? As far as I know, there is no convention for naming the directory that should contain resources used only by the build process itself. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package org.greencodeinitiative.tools.exporter; | ||
|
|
||
| import org.greencodeinitiative.tools.exporter.infra.MetadataWriter; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
| import static java.util.Optional.empty; | ||
| import static java.util.Optional.ofNullable; | ||
|
|
||
| public class Main implements Runnable { | ||
| private final List<String> args; | ||
|
|
||
| public Main(String[] args) { | ||
| this.args = ofNullable(args).map(List::of).orElse(emptyList()); | ||
| } | ||
|
|
||
| public static void main(String... args) { | ||
| new Main(args).run(); | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| Path sourceDir = argAsPath(0, "sourceDir"); | ||
| Path targetDir = argAsPath(1, "targetDir"); | ||
| new MetadataWriter( | ||
| sourceDir, | ||
| targetDir, | ||
| Map.of( | ||
| "title", arg(2, "specificationTitle"), | ||
| "version", arg(3, "specificationVersion"), | ||
| "scmRevisionNumber", arg(4, "specificationSCMRevisionNumber"), | ||
| "scmRevisionDate", arg(5, "specificationSCMRevisionDate") | ||
| ), | ||
| // indexFile | ||
| optionalArg(6) | ||
| .map(Path::of) | ||
| .orElseGet(() -> targetDir.resolve("index.json")), | ||
jycr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // minTermLength | ||
| optionalArg(7) | ||
| .map(Integer::parseInt) | ||
| .orElse(4) | ||
| ).run(); | ||
| } | ||
|
|
||
| private Optional<String> optionalArg(int index) { | ||
| if (args.size() <= index) { | ||
| return empty(); | ||
| } | ||
| return Optional.of(args.get(index)); | ||
| } | ||
|
|
||
| private String arg(int index, String description) { | ||
| if (args.size() <= index) { | ||
| throw new IllegalArgumentException("argument " + (index + 1) + " is required: " + description); | ||
| } | ||
| return optionalArg(index).orElseThrow(() -> new IllegalArgumentException("argument " + (index + 1) + " is required: " + description)); | ||
| } | ||
|
|
||
| private Path argAsPath(int index, String description) { | ||
| return Path.of(arg(index, description)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package org.greencodeinitiative.tools.exporter.domain; | ||
|
|
||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Optional; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import static java.util.Optional.empty; | ||
| import static java.util.Optional.of; | ||
|
|
||
| public class Rule { | ||
| /** | ||
| * Resources to include | ||
| */ | ||
| private static final Pattern TARGET_RESOURCES = Pattern.compile("[^/]{1,1000}/(?<ruleKey>GCI\\d{1,20})/(?<language>[^/]{1,100})/[^/]{3,20}\\.html$"); | ||
|
|
||
| public 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"); | ||
| final Path specificMetadata = htmlDescription.getParent().resolve(ruleKey + ".json"); | ||
|
|
||
| if (!Files.isRegularFile(htmlDescription) || !Files.isRegularFile(metadata)) { | ||
| return empty(); | ||
| } | ||
|
|
||
| return of(new Rule( | ||
| ruleKey, | ||
| matcher.group("language"), | ||
| htmlDescription, | ||
| metadata, | ||
| specificMetadata | ||
| )); | ||
| } | ||
|
|
||
| private final String ruleKey; | ||
| private final String language; | ||
| private final Path htmlDescription; | ||
| private final Path metadata; | ||
| private final Path specificMetadata; | ||
|
|
||
| Rule( | ||
| String ruleKey, | ||
| String language, | ||
| Path htmlDescription, | ||
| Path metadata, | ||
| Path specificMetadata | ||
| ) { | ||
| this.ruleKey = ruleKey; | ||
| this.language = language; | ||
| this.htmlDescription = htmlDescription; | ||
| this.metadata = metadata; | ||
| this.specificMetadata = specificMetadata; | ||
| } | ||
|
|
||
| public Path getHtmlDescriptionTargetPath(Path targetDir) { | ||
| return targetDir.resolve(language).resolve(htmlDescription.getFileName()); | ||
| } | ||
|
|
||
| public Path getMetadataTargetPath(Path targetDir) { | ||
| return targetDir.resolve(language).resolve(metadata.getFileName()); | ||
| } | ||
|
|
||
| public String ruleKey() { | ||
| return ruleKey; | ||
| } | ||
|
|
||
| public String language() { | ||
| return language; | ||
| } | ||
|
|
||
| public Path htmlDescription() { | ||
| return htmlDescription; | ||
| } | ||
|
|
||
| public Path metadata() { | ||
| return metadata; | ||
| } | ||
|
|
||
| public Path specificMetadata() { | ||
| return specificMetadata; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also use the "GitHub action" approach to reduce complexity in this workflow. Check the official doc here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In all transparency, I'm completely new to GitHub Actions.
My proposal is therefore very much open to improvement.
Please feel free to suggest concrete improvements by adding commits directly to this branch.