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

1533 move dependency and dependencies macros to core #1534

Merged
merged 27 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9e05a85
Adds rawTest macro and tests. Fix #1524.
AndreaLaGrotteria Oct 23, 2024
356ae11
Fixes spaces.
AndreaLaGrotteria Oct 23, 2024
cdff572
Fixes spaces.
AndreaLaGrotteria Oct 23, 2024
e72201d
Adds resource macro and tests. Fix #1525.
AndreaLaGrotteria Oct 23, 2024
e0ed37e
Apply requested changes.
AndreaLaGrotteria Oct 23, 2024
0633bde
Apply requested changes.
AndreaLaGrotteria Oct 23, 2024
c830ca7
Apply requested changes.
AndreaLaGrotteria Oct 23, 2024
f72d259
Apply requested changes.
AndreaLaGrotteria Oct 23, 2024
857651f
Fix for header lines offset.
AndreaLaGrotteria Oct 23, 2024
0a6b4c1
Implements suggestions.
AndreaLaGrotteria Oct 23, 2024
e972324
fix tests
sdelamo Oct 24, 2024
6a8e709
add @Nullability annotation
sdelamo Oct 24, 2024
adc0585
license lines is 0 don’t add lines attributes
sdelamo Oct 24, 2024
58fe4fb
Adds testResource macro to core. Fix #1526.
AndreaLaGrotteria Oct 24, 2024
d8c08e6
Adds supports for multi-line inputs. Makes code more modular.
AndreaLaGrotteria Oct 24, 2024
c645194
Adds supports for multi-line inputs. Makes code more modular.
AndreaLaGrotteria Oct 24, 2024
eb82a83
Adds supports for multi-line inputs. Makes code more modular.
AndreaLaGrotteria Oct 24, 2024
b295ee6
Improves path display.
AndreaLaGrotteria Oct 24, 2024
e9d59d2
Adds annotations.
AndreaLaGrotteria Oct 24, 2024
c0f3f79
Adds annotations.
AndreaLaGrotteria Oct 24, 2024
9d500ea
Adds resourceMacro and tests. Fix #1525.
AndreaLaGrotteria Oct 24, 2024
2329684
Merge branch '1525-move-resource-macro-to-core' into 1526-move-testre…
AndreaLaGrotteria Oct 24, 2024
60426d9
Adapts code to merged changes.
AndreaLaGrotteria Oct 24, 2024
df22d23
Fixes code warnings.
AndreaLaGrotteria Oct 24, 2024
0d725e1
Merge branch 'master' into 1526-move-testresource-macro-to-core
AndreaLaGrotteria Oct 25, 2024
f2a0a68
Bug fix.
AndreaLaGrotteria Oct 25, 2024
349c43d
Merge branch '1526-move-testresource-macro-to-core' into 1533-move-de…
AndreaLaGrotteria Oct 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.micronaut.context.ApplicationContext
import io.micronaut.core.annotation.NonNull
import io.micronaut.core.util.StringUtils
import io.micronaut.guides.core.App
import io.micronaut.guides.core.DependencyLines
import io.micronaut.guides.core.Guide
import io.micronaut.guides.core.GuideUtils
import io.micronaut.guides.core.GuidesOption
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.micronaut.guides;
package io.micronaut.guides.core;

import io.micronaut.core.util.StringUtils;
import io.micronaut.starter.options.BuildTool;
Expand Down Expand Up @@ -28,22 +28,13 @@ private static String toMavenScope(Map<String, String> attributes) {
if (s == null) {
return null;
}
switch (s) {
case "api":
case "implementation":
case "annotationProcessor":
return "compile";
case "testCompile":
case "testRuntimeOnly":
case "testImplementation":
return "test";
case "compileOnly":
return "provided";
case "runtimeOnly":
return "runtime";
default:
return s;
}
return switch (s) {
case "api", "implementation", "annotationProcessor" -> "compile";
case "testCompile", "testRuntimeOnly", "testImplementation" -> "test";
case "compileOnly" -> "provided";
case "runtimeOnly" -> "runtime";
default -> s;
};
}

private static String toGradleScope(Map<String, String> attributes, Language language) {
Expand Down Expand Up @@ -170,10 +161,10 @@ private static String extractCallout(Map<String, String> attributes) {
}

private static String getMavenAnnotationScopeXMLPath(Language language) {
switch (language) {
case JAVA: return "path";
case KOTLIN: return "annotationProcessorPath";
default: return ""; // not used for Groovy
}
return switch (language) {
case JAVA -> "path";
case KOTLIN -> "annotationProcessorPath";
default -> ""; // not used for Groovy
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.micronaut.guides.core;

import jakarta.inject.Singleton;
import java.util.List;

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

@Singleton
public class DependencyMacroSubstitution implements MacroSubstitution{
@Override
public String substitute(String str, String slug, GuidesOption option) {
for(String block : findMacroGroups(str, "dependencies")){
List<String> lines = DependencyLines.asciidoc(block.replace(":dependencies:","").strip().lines().toList(), option.getBuildTool(), option.getLanguage());
str = str.replace(block,String.join("\n", lines));
}
for(String line : findMacroLines(str, "dependency")){
List<String> lines = DependencyLines.asciidoc(line, option.getBuildTool(), option.getLanguage());
str = str.replace(line,String.join("\n", lines));
}
return str;
}
}
21 changes: 21 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 @@ -155,6 +155,27 @@ static List<String> findMacroLines(@NonNull String str, @NonNull String macro) {
.toList();
}

static List<String> findMacroGroups(@NonNull String str, @NonNull String macro) {
List<String> matches = new ArrayList<>();
String pattern = ":"+macro+":";
int startIndex = 0;

while (true) {
startIndex = str.indexOf(pattern, startIndex);
if (startIndex == -1) break;

int endIndex = str.indexOf(pattern, startIndex + pattern.length());
if (endIndex == -1) break;

String match = str.substring(startIndex, endIndex + pattern.length());
matches.add(match);

startIndex = endIndex + pattern.length();
}

return matches;
}

@NonNull
static String resolveAsciidoctorLanguage(@NonNull String fileName) {
String extension = fileName.indexOf(".") > 0 ?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 TestResourceMacroSubstitution implements MacroSubstitution {
private final GuidesConfiguration guidesConfiguration;
private final LicenseLoader licenseLoader;
public TestResourceMacroSubstitution(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, "testResource")){

String name = extractName(line, "testResource");
String appName = extractAppName(line);
List<String> tags = extractTags(line);
String indent = extractIndent(line);
String sourcePath = testResourcePath(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 testResourcePath(@NonNull String appName, @NonNull String fileName) {
String module = appName.isEmpty() ? "" : appName + "/";
return module + "src/" + "test/resources/" + fileName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private List<String> attributes() {
if (getTags().size() > 1) {
attributes.add("tags=" + String.join(";", getTags()));
} else if (getTags().size() == 1) {
attributes.add("tag=" + getTags().getFirst());
attributes.add("tag=" + getTags().get(0));
}
}
if (getIndent() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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.*;

@MicronautTest(startApplication = false)
public class DependencyMacroSubstitutionTest {
@Inject
DependencyMacroSubstitution dependencyMacroSubstitution;

@Test
void testSubstitute(){
String str = """
==== Dependencies

When you add a `jax-rs` feature, the generated application includes the following dependencies:

:dependencies:
dependency:micronaut-jaxrs-processor[groupId=io.micronaut.jaxrs,scope=annotationProcessor]
dependency:micronaut-jaxrs-server[groupId=io.micronaut.jaxrs]
:dependencies:

==== Resource

:dependencies:
dependency:micronaut-flyway[groupId=io.micronaut.flyway]
:dependencies:

Test

dependency:micronaut-security-session[groupId=io.micronaut.security]
""";

String resJava = dependencyMacroSubstitution.substitute(str, "micronaut-jaxrs", new GuidesOption(BuildTool.GRADLE, Language.JAVA, TestFramework.SPOCK));
String expectedJava = """
==== Dependencies

When you add a `jax-rs` feature, the generated application includes the following dependencies:

[source, groovy]
.build.gradle
----
annotationProcessor("io.micronaut.jaxrs:micronaut-jaxrs-processor")
implementation("io.micronaut.jaxrs:micronaut-jaxrs-server")
----

==== Resource

[source, groovy]
.build.gradle
----
implementation("io.micronaut.flyway:micronaut-flyway")
----

Test

[source, groovy]
.build.gradle
----
implementation("io.micronaut.security:micronaut-security-session")
----
""";
assertEquals(expectedJava, resJava);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,73 @@ void testAddIncludesWithoutTags() {
assertEquals("include::{sourceDir}/exampleSlug/exampleSlug-gradle-java/exampleSourcePath[lines=16..-1;indent=4]", lines.get(3));
assertEquals("----", lines.get(4));
}

@Test
void testFindMacroGroups(){
String str = """
https://micronaut-projects.github.io/micronaut-validation/snapshot/guide/[Micronaut validation] is built on the standard framework – https://www.jcp.org/en/jsr/detail?id=380[JSR 380], also known as Bean Validation 2.0. Micronaut Validation has built-in support for validation of beans that are annotated with `jakarta.validation` annotations.

To use Micronaut Validation, you need the following dependencies:

:dependencies:

dependency:micronaut-validation-processor[groupId=io.micronaut.validation,scope=annotationProcessor]
dependency:micronaut-validation[groupId=io.micronaut.validation]

:dependencies:

Alternatively, you can use https://micronaut-projects.github.io/micronaut-hibernate-validator/latest/guide/[Micronaut Hibernate Validator], which uses https://hibernate.org/validator/[Hibernate Validator]; a reference implementation of the validation API.
""";
List<String> result = MacroUtils.findMacroGroups(str,"dependencies");
List<String> expected = List.of("""
:dependencies:

dependency:micronaut-validation-processor[groupId=io.micronaut.validation,scope=annotationProcessor]
dependency:micronaut-validation[groupId=io.micronaut.validation]

:dependencies:""");
assertEquals(expected, result);
}

@Test
void testFindMacroGroupsMultiple(){
String str = """
https://micronaut-projects.github.io/micronaut-validation/snapshot/guide/[Micronaut validation] is built on the standard framework – https://www.jcp.org/en/jsr/detail?id=380[JSR 380], also known as Bean Validation 2.0. Micronaut Validation has built-in support for validation of beans that are annotated with `jakarta.validation` annotations.

To use Micronaut Validation, you need the following dependencies:

:dependencies:

dependency:micronaut-validation-processor[groupId=io.micronaut.validation,scope=annotationProcessor]
dependency:micronaut-validation[groupId=io.micronaut.validation]

:dependencies:

Alternatively, you can use https://micronaut-projects.github.io/micronaut-hibernate-validator/latest/guide/[Micronaut Hibernate Validator], which uses https://hibernate.org/validator/[Hibernate Validator]; a reference implementation of the validation API.

:dependencies:

dependency:geb-spock[groupId=org.gebish,scope=testImplementation,version=@geb-spockVersion@]
dependency:htmlunit-driver[groupId=org.seleniumhq.selenium,scope=testImplementation,version=@htmlunit-driverVersion@]

:dependencies:

Test
""";
List<String> result = MacroUtils.findMacroGroups(str,"dependencies");
List<String> expected = List.of("""
:dependencies:

dependency:micronaut-validation-processor[groupId=io.micronaut.validation,scope=annotationProcessor]
dependency:micronaut-validation[groupId=io.micronaut.validation]

:dependencies:""", """
:dependencies:

dependency:geb-spock[groupId=org.gebish,scope=testImplementation,version=@geb-spockVersion@]
dependency:htmlunit-driver[groupId=org.seleniumhq.selenium,scope=testImplementation,version=@htmlunit-driverVersion@]

:dependencies:""");
assertEquals(expected, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 TestResourceMacroSubstitutionTest {

@Inject
TestResourceMacroSubstitution testResourceMacroSubstitution;

@Test
void testSubstitute() {
String str = "testResource:application-test.yml[tag=testcontainers]";
String resJava = testResourceMacroSubstitution.substitute(str, "micronaut-metrics-oci", new GuidesOption(BuildTool.GRADLE, Language.JAVA, TestFramework.JUNIT));
String expectedJava = """
[source,yaml]
.src/test/resources/application-test.yml
----
include::{sourceDir}/micronaut-metrics-oci/micronaut-metrics-oci-gradle-java/src/test/resources/application-test.yml[tag=testcontainers]
----""";
assertEquals(expectedJava, resJava);
}
}
Loading