-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1525 move resource macro to core (#1530)
- Loading branch information
1 parent
aeb98f2
commit 2f28426
Showing
6 changed files
with
135 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
buildSrc/src/main/java/io/micronaut/guides/core/ResourceMacroSubstitution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
buildSrc/src/test/java/io/micronaut/guides/core/ResourceMacroSubstitutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters