From e72201d9805b133714c573588d569eb596ed9321 Mon Sep 17 00:00:00 2001 From: Andrea La Grotteria Date: Wed, 23 Oct 2024 15:47:39 +0200 Subject: [PATCH] Adds resource macro and tests. Fix #1525. --- .../io/micronaut/guides/core/MacroUtils.java | 47 +++++++++++++++++++ .../core/ResourceMacroSubstitution.java | 19 ++++++++ .../core/ResourceMacroSubstitutionTest.java | 46 ++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 buildSrc/src/main/java/io/micronaut/guides/core/ResourceMacroSubstitution.java create mode 100644 buildSrc/src/test/java/io/micronaut/guides/core/ResourceMacroSubstitutionTest.java diff --git a/buildSrc/src/main/java/io/micronaut/guides/core/MacroUtils.java b/buildSrc/src/main/java/io/micronaut/guides/core/MacroUtils.java index 53b6647efd..033642c435 100644 --- a/buildSrc/src/main/java/io/micronaut/guides/core/MacroUtils.java +++ b/buildSrc/src/main/java/io/micronaut/guides/core/MacroUtils.java @@ -54,6 +54,40 @@ public static List addIncludes(@NonNull GuidesOption option, return lines; } + @NonNull + public static List addIncludesResources(@NonNull String str, @NonNull String slug, @NonNull GuidesOption option, @NonNull String resourceDir, @NonNull String macro) { + String name = extractName(str, macro); + String appName = extractAppName(str); + + List tagNames = extractTags(str); + List tags = (!tagNames.isEmpty()) + ? tagNames.stream().map(it -> "tag=" + it).toList() + : Collections.emptyList(); + + String sourceDir = getSourceDir(slug, option); + String asciidoctorLang = resolveAsciidoctorLanguage(name); + String module = !appName.isEmpty() ? appName + "/" : ""; + + List lines = new ArrayList<>(); + String pathcallout = name.startsWith("../") ? + "." + module + "src/" + resourceDir + "/" + name.substring("../".length()) : + "." + module + "src/" + resourceDir + "/resources/" + name; + + lines.add("[source," + asciidoctorLang + "]"); + lines.add(pathcallout); + lines.add("----"); + + if (!tags.isEmpty()) { + for (String tag : tags) { + lines.add("include::{sourceDir}/" + slug + "/"+sourceDir+"/" + module + "src/" + resourceDir + "/resources/" + name + "[" + tag + "]\n"); + } + } else { + lines.add("include::{sourceDir}/" + slug + "/"+sourceDir+"/" + module + "src/" + resourceDir + "/resources/" + name + "[]"); + } + lines.add("----\n"); + return lines; + } + @NonNull public static String extractName(@NonNull String line, @NonNull String macro) { return line.substring(macro.length() + 1, line.indexOf('[')); @@ -129,4 +163,17 @@ static String extractFromParametersLine(@NonNull String line, @NonNull String at .findFirst() .orElse(""); } + + @NonNull + static String resolveAsciidoctorLanguage(@NonNull String fileName) { + String extension = fileName.indexOf(".") > 0 ? + fileName.substring(fileName.lastIndexOf(".") + 1) : ""; + + return switch (extension.toLowerCase()) { + case "yml", "yaml" -> "yaml"; + case "html", "vm", "hbs" -> "html"; + case "xml" -> "xml"; + default -> extension.toLowerCase(); + }; + } } diff --git a/buildSrc/src/main/java/io/micronaut/guides/core/ResourceMacroSubstitution.java b/buildSrc/src/main/java/io/micronaut/guides/core/ResourceMacroSubstitution.java new file mode 100644 index 0000000000..73b5647cc9 --- /dev/null +++ b/buildSrc/src/main/java/io/micronaut/guides/core/ResourceMacroSubstitution.java @@ -0,0 +1,19 @@ +package io.micronaut.guides.core; + +import jakarta.inject.Singleton; + +import java.util.List; + +import static io.micronaut.guides.core.MacroUtils.*; + +@Singleton +public class ResourceMacroSubstitution implements MacroSubstitution{ + @Override + public String substitute(String str, String slug, GuidesOption option) { + String resourceDir = "main"; + + List lines = addIncludesResources(str,slug,option,resourceDir,"resource"); + + return String.join("\n", lines); + } +} diff --git a/buildSrc/src/test/java/io/micronaut/guides/core/ResourceMacroSubstitutionTest.java b/buildSrc/src/test/java/io/micronaut/guides/core/ResourceMacroSubstitutionTest.java new file mode 100644 index 0000000000..f70b51b64e --- /dev/null +++ b/buildSrc/src/test/java/io/micronaut/guides/core/ResourceMacroSubstitutionTest.java @@ -0,0 +1,46 @@ +package io.micronaut.guides.core; + +import io.micronaut.starter.api.TestFramework; +import io.micronaut.starter.options.BuildTool; +import io.micronaut.starter.options.Language; +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@MicronautTest(startApplication = false) +public class ResourceMacroSubstitutionTest { + + @Inject + ResourceMacroSubstitution resourceMacroSubstitution; + + @Test + void testSubstitute() { + String str = "resource:../../../ttfr.sh[]"; + String resJava = resourceMacroSubstitution.substitute(str, "executable-jar", new GuidesOption(BuildTool.GRADLE, Language.JAVA, TestFramework.JUNIT)); + String expectedJava = """ + [source,] + .src/main/../../ttfr.sh + ---- + include::{sourceDir}/executable-jar/executable-jar-gradle-java/src/main/resources/../../../ttfr.sh[] + ---- + """; + assertEquals(expectedJava, resJava); + } + + @Test + void testSubstituteWithTags() { + String str = "resource:application.yml[tag=endpoints]"; + String resJava = resourceMacroSubstitution.substitute(str, "adding-commit-info", new GuidesOption(BuildTool.GRADLE, Language.JAVA, TestFramework.JUNIT)); + String expectedJava = """ + [source,yaml] + .src/main/resources/application.yml + ---- + include::{sourceDir}/adding-commit-info/adding-commit-info-gradle-java/src/main/resources/application.yml[tag=endpoints] + + ---- + """; + assertEquals(expectedJava, resJava); + } +}