From e2114d24d943dcbfefff27a87a2a9b7a7d5ef02a Mon Sep 17 00:00:00 2001 From: jycr Date: Fri, 4 Aug 2023 00:48:40 +0200 Subject: [PATCH] Changes resources processing from Groovy script to java/jshell script --- ecocode-rules-specifications/pom.xml | 21 ++-- .../src/main/script/PrepareResources.jsh | 117 ++++++++++++++++++ .../src/main/script/prepare-resources.groovy | 33 ----- 3 files changed, 129 insertions(+), 42 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/script/PrepareResources.jsh delete mode 100644 ecocode-rules-specifications/src/main/script/prepare-resources.groovy diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index 2bfbada5f..9d896478d 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -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. --> - org.codehaus.gmaven - groovy-maven-plugin - 2.1.1 + com.github.johnpoth + jshell-maven-plugin + 1.4 + prepare-rules-resources generate-resources - execute + run - ${project.basedir}/src/main/script/prepare-resources.groovy - - ${project.build.directory}/rules-html - ${project.build.outputDirectory}/io/ecocode/rules - + + + + + + + diff --git a/ecocode-rules-specifications/src/main/script/PrepareResources.jsh b/ecocode-rules-specifications/src/main/script/PrepareResources.jsh new file mode 100644 index 000000000..04f15b3f6 --- /dev/null +++ b/ecocode-rules-specifications/src/main/script/PrepareResources.jsh @@ -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 getResourcesToCopy() { + try (Stream 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("^.*/(?EC\\d+)/(?[^/]*)/.*\\.html$"); + + static Optional 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 diff --git a/ecocode-rules-specifications/src/main/script/prepare-resources.groovy b/ecocode-rules-specifications/src/main/script/prepare-resources.groovy deleted file mode 100644 index 15eb9bee7..000000000 --- a/ecocode-rules-specifications/src/main/script/prepare-resources.groovy +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env groovy -import java.nio.file.Files -import java.nio.file.Path -import java.nio.file.StandardCopyOption - -Path projectBasedir = Path.of(project.basedir.absolutePath) - -Path sourceDir = Path.of(Objects.requireNonNull(properties.sourceDir, "property must be set: sourceDir")) -Path targetDir = Path.of(Objects.requireNonNull(properties.targetDir, "property must be set: targetDir")) - -def copyFile(Path basedirPath, Path source, Path target) { - log.debug("Copy: {} -> {}", basedirPath.relativize(source), basedirPath.relativize(target)) - Files.createDirectories(target.parent) - Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING) -} - -sourceDir.traverse(type: groovy.io.FileType.FILES, nameFilter: ~/.*\.html$/) { Path htmlPath -> - def language = htmlPath.parent.fileName.toString() - def languageDir = targetDir.resolve(language) - - def ruleDir = htmlPath.parent.parent - def ruleKey = ruleDir.fileName.toString() - - def metadataFileSourcePath = ruleDir.resolve(ruleKey + ".json") - - def htmlTargetPath = languageDir.resolve(htmlPath.fileName) - def metadataFileTargetPath = languageDir.resolve(metadataFileSourcePath.fileName) - - if(Files.exists(metadataFileSourcePath)) { - copyFile(projectBasedir, htmlPath, htmlTargetPath) - copyFile(projectBasedir, metadataFileSourcePath, metadataFileTargetPath) - } -}