Skip to content

Commit

Permalink
1525 move resource macro to core (#1530)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaLaGrotteria authored Oct 25, 2024
1 parent aeb98f2 commit 2f28426
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 14 deletions.
16 changes: 14 additions & 2 deletions buildSrc/src/main/java/io/micronaut/guides/core/MacroUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static List<String> addIncludes(@NonNull GuidesOption option,
if (StringUtils.isNotEmpty(indent)) {
attrs += "," + indent;
}
lines.add("include::{sourceDir}/" + path + "[" + attrs + "]\n");
lines.add("include::{sourceDir}/" + path + "[" + attrs + "]");
}
} else {
List<String> attributes = new ArrayList<>();
Expand Down Expand Up @@ -112,7 +112,6 @@ public static List<String> extractTags(@NonNull String line) {
return attributeValue.isEmpty() ? Collections.emptyList() : Collections.singletonList("tag="+attributeValue);
}


@NonNull
static String pathByFolder(@NonNull GuidesConfiguration guidesConfiguration,
@NonNull String appName,
Expand Down Expand Up @@ -155,4 +154,17 @@ static List<String> findMacroLines(@NonNull String str, @NonNull String macro) {
.filter(line -> line.startsWith(macro+":"))
.toList();
}

@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,44 @@
package io.micronaut.guides.core;

import io.micronaut.core.annotation.NonNull;
import jakarta.inject.Singleton;

import java.util.List;

import static io.micronaut.guides.core.MacroUtils.*;
import static io.micronaut.guides.core.MacroUtils.addIncludes;

@Singleton
public class ResourceMacroSubstitution implements MacroSubstitution {
private final GuidesConfiguration guidesConfiguration;
private final LicenseLoader licenseLoader;

public ResourceMacroSubstitution(GuidesConfiguration guidesConfiguration, LicenseLoader licenseLoader) {
this.guidesConfiguration = guidesConfiguration;
this.licenseLoader = licenseLoader;
}

@Override
public String substitute(String str, String slug, GuidesOption option) {
for(String line : findMacroLines(str, "resource")){

String name = extractName(line, "resource");
String appName = extractAppName(line);
List<String> tags = extractTags(line);
String indent = extractIndent(line);
String sourcePath = resourcePath(appName, name);
String extension = resolveAsciidoctorLanguage(name);

List<String> lines = addIncludes(option, licenseLoader, guidesConfiguration, slug, sourcePath, extension, indent, tags);

str = str.replace(line,String.join("\n", lines));
}
return str;
}

@NonNull
private static String resourcePath(@NonNull String appName, @NonNull String fileName) {
String module = appName.isEmpty() ? "" : appName + "/";
return module + "src/" + "main/resources/" + fileName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ void testAddIncludesWithTags() {
assertEquals("[source,java]", lines.get(0));
assertEquals(".exampleSourcePath", lines.get(1));
assertEquals("----", lines.get(2));
assertEquals("include::{sourceDir}/exampleSlug/exampleSlug-gradle-java/exampleSourcePath[tag=tag1,indent=4]\n", lines.get(3));
assertEquals("include::{sourceDir}/exampleSlug/exampleSlug-gradle-java/exampleSourcePath[tag=tag2,indent=4]\n", lines.get(4));
assertEquals("include::{sourceDir}/exampleSlug/exampleSlug-gradle-java/exampleSourcePath[tag=tag1,indent=4]", lines.get(3));
assertEquals("include::{sourceDir}/exampleSlug/exampleSlug-gradle-java/exampleSourcePath[tag=tag2,indent=4]", lines.get(4));
assertEquals("----", lines.get(5));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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,]
.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);
}

@Test
void testMultiLineInput(){
String str = """
The `git.properties` file that is generated by the `gradle-git-properties` plugin
will not be accessible from the native executable unless access to the file is
configured in `resource-config.json`:
resource:application.yml[tag=endpoints]
resource:test.yml[tag=security,app=testApp]
""";
String resJava = resourceMacroSubstitution.substitute(str, "adding-commit-info", new GuidesOption(BuildTool.GRADLE, Language.JAVA, TestFramework.JUNIT));
String expectedJava = """
The `git.properties` file that is generated by the `gradle-git-properties` plugin
will not be accessible from the native executable unless access to the file is
configured in `resource-config.json`:
[source,yaml]
.src/main/resources/application.yml
----
include::{sourceDir}/adding-commit-info/adding-commit-info-gradle-java/src/main/resources/application.yml[tag=endpoints]
----
[source,yaml]
.testApp/src/main/resources/test.yml
----
include::{sourceDir}/adding-commit-info/adding-commit-info-gradle-java/testApp/src/main/resources/test.yml[tag=security]
----
""";
assertEquals(expectedJava, resJava);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ void TestSubstituteWithTags(){
.src/main/java/example/micronaut/TeamConfiguration.java
----
include::{sourceDir}/micronaut-configuration/micronaut-configuration-gradle-java/src/main/java/example/micronaut/TeamConfiguration.java[tag=teamConfigClassNoBuilder]
include::{sourceDir}/micronaut-configuration/micronaut-configuration-gradle-java/src/main/java/example/micronaut/TeamConfiguration.java[tag=gettersandsetters]
----
""";
assertEquals(expectedJava, resJava);
Expand All @@ -87,9 +85,7 @@ void TestSubstituteWithMultiple(){
.springboot/src/main/java/example/micronaut/TeamConfiguration.java
----
include::{sourceDir}/micronaut-configuration/micronaut-configuration-gradle-java/springboot/src/main/java/example/micronaut/TeamConfiguration.java[tag=teamConfigClassNoBuilder,indent=0]
include::{sourceDir}/micronaut-configuration/micronaut-configuration-gradle-java/springboot/src/main/java/example/micronaut/TeamConfiguration.java[tag=gettersandsetters,indent=0]
----
""";
assertEquals(expectedJava, resJava);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ void TestSubstituteWithTags(){
.src/test/java/example/micronaut/TeamConfigurationTest.java
----
include::{sourceDir}/micronaut-configuration/micronaut-configuration-gradle-java/src/test/java/example/micronaut/TeamConfigurationTest.java[tag=teamConfigClassNoBuilder]
include::{sourceDir}/micronaut-configuration/micronaut-configuration-gradle-java/src/test/java/example/micronaut/TeamConfigurationTest.java[tag=gettersandsetters]
----""";
assertEquals(expectedJava, resJava);
}
Expand All @@ -82,9 +80,7 @@ void TestSubstituteWithMultiple(){
.springboot/src/test/java/example/micronaut/TeamConfigurationTest.java
----
include::{sourceDir}/micronaut-configuration/micronaut-configuration-gradle-java/springboot/src/test/java/example/micronaut/TeamConfigurationTest.java[tag=teamConfigClassNoBuilder,indent=0]
include::{sourceDir}/micronaut-configuration/micronaut-configuration-gradle-java/springboot/src/test/java/example/micronaut/TeamConfigurationTest.java[tag=gettersandsetters,indent=0]
----""";
assertEquals(expectedJava, resJava);
}
Expand All @@ -104,9 +100,7 @@ void TestSubstituteMultipleLines(){
.springboot/src/test/java/example/micronaut/TeamConfigurationTest.java
----
include::{sourceDir}/micronaut-configuration/micronaut-configuration-gradle-java/springboot/src/test/java/example/micronaut/TeamConfigurationTest.java[tag=teamConfigClassNoBuilder,indent=0]
include::{sourceDir}/micronaut-configuration/micronaut-configuration-gradle-java/springboot/src/test/java/example/micronaut/TeamConfigurationTest.java[tag=gettersandsetters,indent=0]
----
""";
assertEquals(expectedJava, resJava);
Expand Down

0 comments on commit 2f28426

Please sign in to comment.