Skip to content
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

1525 move resource macro to core #1530

Merged
merged 19 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion buildSrc/src/main/java/io/micronaut/guides/core/MacroUtils.java
Original file line number Diff line number Diff line change
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
@@ -0,0 +1,78 @@
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]

sdelamo marked this conversation as resolved.
Show resolved Hide resolved
----""";
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]

AndreaLaGrotteria marked this conversation as resolved.
Show resolved Hide resolved
----

[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]

AndreaLaGrotteria marked this conversation as resolved.
Show resolved Hide resolved
----
""";
assertEquals(expectedJava, resJava);
}
}
Loading