Infer default link name when appropriate

This commit improves the HELP.md customizer for links to add a link even
if it does not have a description. Links are defined in three dedicated
areas so it is possible to infer a sensible name if only one link of a
certain type is present.

Closes gh-962
This commit is contained in:
Stephane Nicoll 2019-07-23 17:44:40 +02:00
parent 684258743f
commit 18b537b65f
2 changed files with 121 additions and 31 deletions

View File

@ -16,11 +16,20 @@
package io.spring.initializr.generator.spring.documentation;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import io.spring.initializr.generator.io.text.BulletedSection;
import io.spring.initializr.generator.project.ResolvedProjectDescription;
import io.spring.initializr.metadata.Dependency;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.Link;
import org.springframework.core.Ordered;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
/**
* A {@link HelpDocumentCustomizer} that register links for selected dependencies.
@ -56,19 +65,54 @@ public class RequestedDependenciesHelpDocumentCustomizer implements HelpDocument
private void handleDependency(HelpDocument document, Dependency dependency) {
GettingStartedSection gettingStartedSection = document.gettingStarted();
dependency.getLinks().forEach((link) -> {
if (link.getDescription() != null && link.getRel() != null) {
if ("reference".equals(link.getRel())) {
gettingStartedSection.addReferenceDocLink(link.getHref(), link.getDescription());
}
else if ("guide".equals(link.getRel())) {
gettingStartedSection.addGuideLink(link.getHref(), link.getDescription());
}
else {
gettingStartedSection.addAdditionalLink(link.getHref(), link.getDescription());
MultiValueMap<GuideType, Link> indexedLinks = indexLinks(dependency);
registerLinks(indexedLinks.get(GuideType.REFERENCE), defaultLinkDescription(dependency),
gettingStartedSection::referenceDocs);
registerLinks(indexedLinks.get(GuideType.GUIDE), defaultLinkDescription(dependency),
gettingStartedSection::guides);
registerLinks(indexedLinks.get(GuideType.OTHER), (links) -> null, gettingStartedSection::additionalLinks);
}
private void registerLinks(List<Link> links, Function<List<Link>, String> defaultDescription,
Supplier<BulletedSection<GettingStartedSection.Link>> section) {
if (ObjectUtils.isEmpty(links)) {
return;
}
links.forEach((link) -> {
if (link.getHref() != null) {
String description = (link.getDescription() != null) ? link.getDescription()
: defaultDescription.apply(links);
if (description != null) {
section.get().addItem(new GettingStartedSection.Link(link.getHref(), description));
}
}
});
}
private Function<List<Link>, String> defaultLinkDescription(Dependency dependency) {
return (links) -> (links.size() == 1) ? dependency.getName() : null;
}
private MultiValueMap<GuideType, Link> indexLinks(Dependency dependency) {
MultiValueMap<GuideType, Link> links = new LinkedMultiValueMap<>();
dependency.getLinks().forEach((link) -> {
if ("reference".equals(link.getRel())) {
links.add(GuideType.REFERENCE, link);
}
else if ("guide".equals(link.getRel())) {
links.add(GuideType.GUIDE, link);
}
else {
links.add(GuideType.OTHER, link);
}
});
return links;
}
private enum GuideType {
REFERENCE, GUIDE, OTHER
}
}

View File

@ -16,9 +16,10 @@
package io.spring.initializr.generator.spring.documentation;
import java.util.List;
import java.util.Arrays;
import io.spring.initializr.generator.io.template.MustacheTemplateRenderer;
import io.spring.initializr.generator.io.text.BulletedSection;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.project.ResolvedProjectDescription;
import io.spring.initializr.generator.spring.test.InitializrMetadataTestBuilder;
@ -39,48 +40,93 @@ class RequestedDependenciesHelpDocumentCustomizerTests {
private final InitializrMetadataTestBuilder metadataBuilder = InitializrMetadataTestBuilder.withDefaults();
@Test
void dependencyLinkWithNoDescriptionIsIgnored() {
Dependency dependency = Dependency.withId("example", "com.example", "example");
dependency.getLinks().add(Link.create("guide", "https://example.com/how-to"));
void dependencyWithReferenceDocLink() {
Dependency dependency = createDependency("example",
Link.create("reference", "https://example.com/doc", "Reference doc example"));
this.metadataBuilder.addDependencyGroup("test", dependency);
HelpDocument document = customizeHelp("example");
assertThat(document.gettingStarted().isEmpty()).isFalse();
assertSingleLink(document.gettingStarted().referenceDocs(), "https://example.com/doc", "Reference doc example");
}
@Test
void dependencyWithReferenceDocLinkGetDependencyNameByDefault() {
Dependency dependency = createDependency("example", Link.create("reference", "https://example.com/doc"));
dependency.setName("Example Library");
this.metadataBuilder.addDependencyGroup("test", dependency);
HelpDocument document = customizeHelp("example");
assertThat(document.gettingStarted().isEmpty()).isFalse();
assertSingleLink(document.gettingStarted().referenceDocs(), "https://example.com/doc", "Example Library");
}
@Test
void dependencyWithSeveralReferenceDocLinksDoNotGetDependencyNameByDefault() {
Dependency dependency = createDependency("example", Link.create("reference", "https://example.com/doc"),
Link.create("reference", "https://example.com/doc2"));
dependency.setName("Example Library");
this.metadataBuilder.addDependencyGroup("test", dependency);
HelpDocument document = customizeHelp("example");
assertThat(document.gettingStarted().isEmpty()).isTrue();
}
@Test
void dependencyWithReferenceDocLink() {
Dependency dependency = Dependency.withId("example", "com.example", "example");
dependency.getLinks().add(Link.create("reference", "https://example.com/doc", "Reference doc example"));
void dependencyWithGuideLink() {
Dependency dependency = createDependency("example",
Link.create("guide", "https://example.com/how-to", "How-to example"));
this.metadataBuilder.addDependencyGroup("test", dependency);
HelpDocument document = customizeHelp("example");
assertThat(document.gettingStarted().isEmpty()).isFalse();
List<GettingStartedSection.Link> links = document.gettingStarted().referenceDocs().getItems();
assertThat(links).hasSize(1);
assertLink(links.get(0), "https://example.com/doc", "Reference doc example");
assertSingleLink(document.gettingStarted().guides(), "https://example.com/how-to", "How-to example");
}
@Test
void dependencyWithGuideLink() {
Dependency dependency = Dependency.withId("example", "com.example", "example");
dependency.getLinks().add(Link.create("guide", "https://example.com/how-to", "How-to example"));
void dependencyWithGuideLinkGetDependencyNameByDefault() {
Dependency dependency = createDependency("example", Link.create("guide", "https://example.com/how-to"));
dependency.setName("Example Library");
this.metadataBuilder.addDependencyGroup("test", dependency);
HelpDocument document = customizeHelp("example");
assertThat(document.gettingStarted().isEmpty()).isFalse();
List<GettingStartedSection.Link> links = document.gettingStarted().guides().getItems();
assertThat(links).hasSize(1);
assertLink(links.get(0), "https://example.com/how-to", "How-to example");
assertSingleLink(document.gettingStarted().guides(), "https://example.com/how-to", "Example Library");
}
@Test
void dependencyWithSeveralGuideLinksDoNotGetDependencyNameByDefault() {
Dependency dependency = createDependency("example", Link.create("guide", "https://example.com/how-to"),
Link.create("guide", "https://example.com/anothero"));
dependency.setName("Example Library");
this.metadataBuilder.addDependencyGroup("test", dependency);
HelpDocument document = customizeHelp("example");
assertThat(document.gettingStarted().isEmpty()).isTrue();
}
@Test
void dependencyWithAdditionalLink() {
Dependency dependency = Dependency.withId("example", "com.example", "example");
dependency.getLinks().add(Link.create("something", "https://example.com/test", "Test App"));
Dependency dependency = createDependency("example",
Link.create("something", "https://example.com/test", "Test App"));
this.metadataBuilder.addDependencyGroup("test", dependency);
HelpDocument document = customizeHelp("example");
assertThat(document.gettingStarted().isEmpty()).isFalse();
List<GettingStartedSection.Link> links = document.gettingStarted().additionalLinks().getItems();
assertThat(links).hasSize(1);
assertLink(links.get(0), "https://example.com/test", "Test App");
assertSingleLink(document.gettingStarted().additionalLinks(), "https://example.com/test", "Test App");
}
@Test
void dependencyWithAdditionalLinkDoNotDependencyNameByDefault() {
Dependency dependency = createDependency("example", Link.create("something", "https://example.com/test"));
dependency.setName("Example Library");
this.metadataBuilder.addDependencyGroup("test", dependency);
HelpDocument document = customizeHelp("example");
assertThat(document.gettingStarted().isEmpty()).isTrue();
}
private Dependency createDependency(String id, Link... links) {
Dependency dependency = Dependency.withId(id, "com.example", "example");
dependency.getLinks().addAll(Arrays.asList(links));
return dependency;
}
private void assertSingleLink(BulletedSection<GettingStartedSection.Link> links, String href, String description) {
assertThat(links.getItems()).hasSize(1);
assertLink(links.getItems().get(0), href, description);
}
private void assertLink(GettingStartedSection.Link link, String href, String description) {