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

Adds diffLink macro to core. Fix #1539. #1540

Merged
merged 9 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.micronaut.guides.core;

import io.micronaut.core.util.StringUtils;
import io.micronaut.http.uri.UriBuilder;
import jakarta.inject.Singleton;

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

@Singleton
public class BuildDiffLinkSubstitution implements MacroMetadataSubstitution {

private final GuidesConfiguration guidesConfiguration;

public BuildDiffLinkSubstitution(GuidesConfiguration config) {
this.guidesConfiguration = config;
}
@Override
public String substitute(String str, GuidesOption option, Guide guide) {
for(String line : findMacroLines(str, "diffLink")){
String appName;
if (StringUtils.isNotEmpty(extractAppName(line))) {
appName = extractAppName(line);
} else {
appName = guidesConfiguration.getDefaultAppName();
}
App app = guide.apps().stream()
.filter(a -> a.name().equals(appName))
.findFirst()
.orElse(null);

UriBuilder uriBuilder = UriBuilder.of(guidesConfiguration.getLauncherUrl())
.queryParam("lang", option.getLanguage().name())
.queryParam("build", option.getBuildTool().name())
.queryParam("test", option.getTestFramework().name())
.queryParam("name", appName.equals(guidesConfiguration.getDefaultAppName()) ? "micronautguide" : appName)
.queryParam("type", app.applicationType().name())
.queryParam("package", guidesConfiguration.getPackageName())
.queryParam("activity", "diff");
featureNames(line, app, option).forEach(f -> uriBuilder.queryParam("features", f));

String res = "NOTE: If you have an existing Micronaut application and want to add the functionality described here, you can "
sdelamo marked this conversation as resolved.
Show resolved Hide resolved
+ uriBuilder.build().toString()
+ "[view the dependency and configuration changes from the specified features, window=\"_blank\"]"
+ " and apply those changes to your application.";

str = str.replace(line,res);
}
return str;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ public interface GuidesConfiguration {
String getTitle();
String getLicensePath();
String getPackageName();
String getDefaultAppName();
String getLauncherUrl();
List<String> getFilesWithHeader();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class GuidesConfigurationProperties implements GuidesConfiguration {
private String licensePath = DEFAULT_LICENSEHEADER;
private String packageName = DEFAULT_PACKAGE_NAME;
private List<String> sourceFilesExtensions = List.of("java", "kotlin", "groovy");
private static final String DEFAULT_APP_NAME = "default";
private static final String HOMEPAGE_URL = "https://micronaut.io";
private static final String LAUNCHER_URL = HOMEPAGE_URL + "/launch";

@Override
public String getPackageName() {
Expand Down Expand Up @@ -52,6 +55,14 @@ public void setTitle(String title) {
this.title = title;
}

@Override
public String getDefaultAppName() {
return DEFAULT_APP_NAME;
}

@Override
public String getLauncherUrl() { return LAUNCHER_URL; }
sdelamo marked this conversation as resolved.
Show resolved Hide resolved

@Override
public List<String> getFilesWithHeader() {
return sourceFilesExtensions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.micronaut.guides.core;

public interface MacroMetadataSubstitution {
String substitute(String str, GuidesOption option, Guide guide);
}
47 changes: 47 additions & 0 deletions buildSrc/src/main/java/io/micronaut/guides/core/MacroUtils.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,61 @@
package io.micronaut.guides.core;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.util.StringUtils;
import io.micronaut.starter.options.Language;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public final class MacroUtils {
private MacroUtils() {
}

@NonNull
public static String extractAppName(@NonNull String line) {
return extractFromParametersLine(line, "app");
}

@NonNull
static String extractFromParametersLine(@NonNull String line, @NonNull String attributeName) {
String[] attrs = line.substring(line.indexOf("[") + 1, line.indexOf("]")).split(",");
return Arrays.stream(attrs)
.filter(attr -> attr.startsWith(attributeName))
.map(attr -> attr.split("="))
.map(parts -> parts[1])
.findFirst()
.orElse("");
}

@NonNull
static List<String> featureNames(@NonNull String line,
@NonNull App app,
@NonNull GuidesOption guidesOption) {
String features = extractFromParametersLine(line, "features");
List<String> featureNames;
if (StringUtils.isNotEmpty(features)) {
featureNames = Arrays.asList(features.split("\\|"));
} else {
featureNames = new ArrayList<>(GuideUtils.getAppVisibleFeatures(app, guidesOption.getLanguage()));
}

String featureExcludes = extractFromParametersLine(line, "featureExcludes");
List<String> excludedFeatureNames;
if (featureExcludes != null && !featureExcludes.isEmpty()) {
excludedFeatureNames = Arrays.asList(featureExcludes.split("\\|"));
} else {
excludedFeatureNames = new ArrayList<>();
}
featureNames.removeAll(excludedFeatureNames);

if (guidesOption.getLanguage() == Language.GROOVY) {
featureNames.remove("graalvm");
}
return featureNames;
}


@NonNull
static String getSourceDir(@NonNull String slug, @NonNull GuidesOption option) {
return slug + "-" + option.getBuildTool() + "-" + option.getLanguage();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.micronaut.guides.core;

import io.micronaut.core.beans.BeanIntrospection;
import io.micronaut.core.io.ResourceLoader;
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.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

@MicronautTest(startApplication = false)
public 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, new GuidesOption(BuildTool.GRADLE, Language.JAVA, TestFramework.JUNIT), guide);
String expectedJava = "NOTE: If you have an existing Micronaut application and want to add the functionality described here, you can https://micronaut.io/launch?lang=JAVA&build=GRADLE&test=JUNIT&name=cli&type=CLI&package=example.micronaut&activity=diff&features=yaml&features=graalvm&features=mqtt&features=awaitility[view the dependency and configuration changes from the specified features, window=\"_blank\"] and apply those changes to your application.";
assertEquals(expectedJava, resJava);
}
}
24 changes: 24 additions & 0 deletions buildSrc/src/test/resources/metadata-diff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"title": "Publishing and subscribing to MQTT Topics from a Micronaut Application",
"intro": "Learn how to use Mosquitto as an MQTT broker, create a Micronaut CLI application and publish an MQTT topic, and subscribe to the MQTT topic in a different Micronaut Messaging application.",
"authors": ["Sergio del Amo"],
"tags": ["messaging"],
"categories": ["Messaging"],
"publicationDate": "2022-03-14",
"apps": [
{
"applicationType": "messaging",
"name": "app",
"features": ["yaml","graalvm", "mqtt"],
"javaFeatures": [ "awaitility"],
"kotlinFeatures": [ "awaitility"]
},
{
"applicationType": "cli",
"name": "cli",
"features": ["yaml","graalvm", "mqtt"],
"javaFeatures": [ "awaitility"],
"kotlinFeatures": [ "awaitility", "kapt"]
}
]
}
Loading