Skip to content

Commit

Permalink
Adds resource macro and tests. Fix #1525.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaLaGrotteria committed Oct 23, 2024
1 parent 464442c commit e72201d
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
47 changes: 47 additions & 0 deletions buildSrc/src/main/java/io/micronaut/guides/core/MacroUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,40 @@ public static List<String> addIncludes(@NonNull GuidesOption option,
return lines;
}

@NonNull
public static List<String> 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<String> tagNames = extractTags(str);
List<String> 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<String> 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('['));
Expand Down Expand Up @@ -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();
};
}
}
Original file line number Diff line number Diff line change
@@ -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<String> lines = addIncludesResources(str,slug,option,resourceDir,"resource");

return String.join("\n", lines);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit e72201d

Please sign in to comment.