-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
bbcc6cd
commit ff50863
Showing
37 changed files
with
307 additions
and
47 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
76 changes: 76 additions & 0 deletions
76
buildSrc/src/main/java/io/micronaut/guides/core/BuildDiffLinkSubstitution.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,76 @@ | ||
package io.micronaut.guides.core; | ||
|
||
import io.micronaut.guides.core.asciidoc.AsciidocMacro; | ||
import io.micronaut.guides.core.asciidoc.Attribute; | ||
import io.micronaut.http.uri.UriBuilder; | ||
import io.micronaut.starter.application.ApplicationType; | ||
import jakarta.inject.Singleton; | ||
import java.net.URI; | ||
import java.util.*; | ||
import static io.micronaut.guides.core.MacroUtils.*; | ||
|
||
@Singleton | ||
public class BuildDiffLinkSubstitution implements MacroSubstitution { | ||
private static final String QUERY_PARAMLANG = "lang"; | ||
private static final String QUERY_PARAM_BUILD = "build"; | ||
private static final String QUERY_PARAM_TEST = "test"; | ||
private static final String QUERY_PARAM_NAME = "name"; | ||
private static final String QUERY_PARAM_TYPE = "type"; | ||
private static final String QUERY_PARAM_PACKAGE = "package"; | ||
private static final String QUERY_PARAM_ACTIVITY = "activity"; | ||
private static final String QUERY_PARAM_FEATURES = "features"; | ||
public static final String MACRO_DIFF_LINK = "diffLink"; | ||
private static final String ATTRIBUTE_FEATURES = "features"; | ||
private static final String ATTRIBUTE_EXCLUDE_FEATURES = "featureExcludes"; | ||
private final GuidesConfiguration guidesConfiguration; | ||
|
||
public BuildDiffLinkSubstitution(GuidesConfiguration config) { | ||
this.guidesConfiguration = config; | ||
} | ||
|
||
@Override | ||
public String substitute(String str, Guide guide, GuidesOption option) { | ||
for(String line : findMacroLines(str, MACRO_DIFF_LINK)) { | ||
Optional<AsciidocMacro> asciidocMacroOptional = AsciidocMacro.of(MACRO_DIFF_LINK, line); | ||
if (asciidocMacroOptional.isEmpty()) { | ||
continue; | ||
} | ||
AsciidocMacro asciidocMacro = asciidocMacroOptional.get(); | ||
String res = buildDiffLink(asciidocMacro, guide, option).toString() + "[Diff]"; | ||
str = str.replace(line,res); | ||
} | ||
return str; | ||
} | ||
|
||
private URI buildDiffLink(AsciidocMacro asciidocMacro, Guide guide, GuidesOption option) { | ||
String appName = appName(asciidocMacro); | ||
App app = app(guide, asciidocMacro); | ||
Set<String> features = features(app, asciidocMacro, option); | ||
UriBuilder uriBuilder = UriBuilder.of(guidesConfiguration.getProjectGeneratorUrl()) | ||
.queryParam(QUERY_PARAMLANG, option.getLanguage().name()) | ||
.queryParam(QUERY_PARAM_BUILD, option.getBuildTool().name()) | ||
.queryParam(QUERY_PARAM_TEST, option.getTestFramework().name()) | ||
.queryParam(QUERY_PARAM_NAME, appName.equals(guidesConfiguration.getDefaultAppName()) ? "micronautguide" : appName) | ||
.queryParam(QUERY_PARAM_TYPE, app != null ? app.applicationType().name() : ApplicationType.DEFAULT.name()) | ||
.queryParam(QUERY_PARAM_PACKAGE, guidesConfiguration.getPackageName()) | ||
.queryParam(QUERY_PARAM_ACTIVITY, "diff"); | ||
features.forEach(f -> uriBuilder.queryParam(QUERY_PARAM_FEATURES, f)); | ||
return uriBuilder.build(); | ||
} | ||
|
||
private static Set<String> features(App app, AsciidocMacro asciidocMacro, GuidesOption option) { | ||
Set<String> features = new HashSet<>(); | ||
if (app != null) { | ||
features.addAll(GuideUtils.getAppVisibleFeatures(app, option.getLanguage())); | ||
} | ||
asciidocMacro.attributes().stream() | ||
.filter(attribute -> attribute.key().equals(ATTRIBUTE_FEATURES)) | ||
.map(Attribute::values) | ||
.forEach(features::addAll); | ||
asciidocMacro.attributes().stream() | ||
.filter(attribute -> attribute.key().equals(ATTRIBUTE_EXCLUDE_FEATURES)) | ||
.map(Attribute::values) | ||
.forEach(features::removeAll); | ||
return features; | ||
} | ||
} |
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
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
23 changes: 22 additions & 1 deletion
23
buildSrc/src/main/java/io/micronaut/guides/core/MacroSubstitution.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 |
---|---|---|
@@ -1,10 +1,31 @@ | ||
package io.micronaut.guides.core; | ||
|
||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.guides.core.asciidoc.AsciidocMacro; | ||
import io.micronaut.guides.core.asciidoc.Attribute; | ||
|
||
public interface MacroSubstitution { | ||
String APP = "app"; | ||
String APP_NAME_DEFAULT = "default"; | ||
|
||
@NonNull | ||
String substitute(@NonNull String str, @NonNull String slug, @NonNull GuidesOption option); | ||
String substitute(@NonNull String str, @NonNull Guide guide, @NonNull GuidesOption option); | ||
|
||
default App app(Guide guide, AsciidocMacro asciidocMacro) { | ||
final String appName = appName(asciidocMacro); | ||
return guide.apps().stream() | ||
.filter(a -> a.name().equals(appName)) | ||
.findFirst() | ||
.orElseThrow(() -> new RuntimeException("app not found for app name" + appName)); | ||
} | ||
|
||
default String appName(AsciidocMacro asciidocMacro) { | ||
return asciidocMacro.attributes().stream() | ||
.filter(attribute -> attribute.key().equals(APP)) | ||
.map(Attribute::values) | ||
.filter(l -> !l.isEmpty()) | ||
.map(l -> l.get(0)) | ||
.findFirst() | ||
.orElse(APP_NAME_DEFAULT); | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
buildSrc/src/main/java/io/micronaut/guides/core/MacroUtils.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
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
76 changes: 76 additions & 0 deletions
76
buildSrc/src/test/java/io/micronaut/guides/core/BuildDiffLinkSubstitutionTest.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,76 @@ | ||
package io.micronaut.guides.core; | ||
|
||
import io.micronaut.core.beans.BeanIntrospection; | ||
import io.micronaut.core.io.ResourceLoader; | ||
import io.micronaut.http.uri.UriBuilder; | ||
import io.micronaut.json.JsonMapper; | ||
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 java.io.InputStream; | ||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@MicronautTest(startApplication = false) | ||
class BuildDiffLinkSubstitutionTest { | ||
@Inject | ||
BuildDiffLinkSubstitution buildDiffLinkSubstitution; | ||
|
||
@Inject | ||
JsonMapper jsonMapper; | ||
|
||
@Inject | ||
ResourceLoader resourceLoader; | ||
|
||
@Test | ||
void testSubstitute(){ | ||
assertDoesNotThrow(() -> BeanIntrospection.getIntrospection(Guide.class)); | ||
Optional<InputStream> inputStreamOptional = resourceLoader.getResourceAsStream("classpath:metadata-diff.json"); | ||
assertTrue(inputStreamOptional.isPresent()); | ||
final InputStream inputStreamBase = inputStreamOptional.get(); | ||
Guide guide = assertDoesNotThrow(() -> jsonMapper.readValue(inputStreamBase, Guide.class)); | ||
String str = "diffLink:[app=cli]"; | ||
String resJava = buildDiffLinkSubstitution.substitute(str, guide, new GuidesOption(BuildTool.GRADLE, Language.JAVA, TestFramework.JUNIT)); | ||
URI expectedURI = UriBuilder.of("https://micronaut.io") | ||
.path("launch") | ||
.queryParam("lang", "JAVA") | ||
.queryParam("build", "GRADLE") | ||
.queryParam("test", "JUNIT") | ||
.queryParam("name", "cli") | ||
.queryParam("type", "CLI") | ||
.queryParam("package", "example.micronaut") | ||
.queryParam("activity", "diff") | ||
.queryParam("features", "awaitility") | ||
.queryParam("features", "graalvm") | ||
.queryParam("features", "mqtt") | ||
.queryParam("features", "yaml") | ||
.build(); | ||
String expectedJava = expectedURI.toString() + "[Diff]"; | ||
assertEquals(expectedJava, resJava); | ||
|
||
str = "diffLink:[app=cli,featureExcludes=graalvm]"; | ||
resJava = buildDiffLinkSubstitution.substitute(str, guide, new GuidesOption(BuildTool.GRADLE, Language.JAVA, TestFramework.JUNIT)); | ||
expectedURI = UriBuilder.of("https://micronaut.io") | ||
.path("launch") | ||
.queryParam("lang", "JAVA") | ||
.queryParam("build", "GRADLE") | ||
.queryParam("test", "JUNIT") | ||
.queryParam("name", "cli") | ||
.queryParam("type", "CLI") | ||
.queryParam("package", "example.micronaut") | ||
.queryParam("activity", "diff") | ||
.queryParam("features", "awaitility") | ||
.queryParam("features", "mqtt") | ||
.queryParam("features", "yaml") | ||
.build(); | ||
expectedJava = expectedURI.toString() + "[Diff]"; | ||
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
13 changes: 13 additions & 0 deletions
13
buildSrc/src/test/java/io/micronaut/guides/core/GuideTestUtils.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,13 @@ | ||
package io.micronaut.guides.core; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public final class GuideTestUtils { | ||
private GuideTestUtils() { | ||
} | ||
|
||
public static Guide guideWithSlug(String slug) { | ||
return new Guide("", "", List.of(), List.of(), null, 0, 0, null, null, null, "", List.of(), List.of(), List.of(), null, List.of(), slug, null, "", Map.of(), List.of()); | ||
} | ||
} |
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
Oops, something went wrong.