Add dialect support to Type metadata

Closes gh-1344
This commit is contained in:
Stephane Nicoll
2022-11-15 15:15:16 +01:00
parent 3ccc2011cd
commit 6b843967b2
5 changed files with 41 additions and 12 deletions

View File

@@ -421,8 +421,10 @@ By default, Spring Initializr exposes the following resources (all accessed via
The build system must be defined with a `build` tag providing the name of the
`BuildSystem` to use (e.g. `maven`, `gradle`).
Additional tags can be provided to further qualify the entry. Besides the mandatory `build`
tag, a `format` tag is also available to define the format of the project (e.g. `project`
Additional tags can be provided to further qualify the entry. If the build system supports
multiple dialects, the chosen dialect can be specified using the `dialect` tag.
A `format` tag is also available to define the format of the project (e.g. `project`
for a full project, `build` for just a build file). By default, the HTML UI filters all
the available types to only display the ones that have a `format` tag with value
`project`.

View File

@@ -89,14 +89,14 @@ public class InitializrMetadataTestBuilder {
}
public InitializrMetadataTestBuilder addDefaultTypes() {
return addType("maven-build", false, "/pom.xml", "maven", "build")
.addType("maven-project", true, "/starter.zip", "maven", "project")
.addType("gradle-build", false, "/build.gradle", "gradle", "build")
.addType("gradle-project", false, "/starter.zip", "gradle", "project");
return addType("maven-build", false, "/pom.xml", "maven", null, "build")
.addType("maven-project", true, "/starter.zip", "maven", null, "project")
.addType("gradle-build", false, "/build.gradle", "gradle", null, "build")
.addType("gradle-project", false, "/starter.zip", "gradle", null, "project");
}
public InitializrMetadataTestBuilder addType(String id, boolean defaultValue, String action, String build,
String format) {
String dialect, String format) {
Type type = new Type();
type.setId(id);
type.setName(id);
@@ -105,6 +105,9 @@ public class InitializrMetadataTestBuilder {
if (StringUtils.hasText(build)) {
type.getTags().put("build", build);
}
if (StringUtils.hasText(dialect)) {
type.getTags().put("dialect", dialect);
}
if (StringUtils.hasText(format)) {
type.getTags().put("format", format);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package io.spring.initializr.web.project;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import io.spring.initializr.generator.buildsystem.BuildSystem;
@@ -165,8 +166,10 @@ public class DefaultProjectRequestToDescriptionConverter
}
private BuildSystem getBuildSystem(ProjectRequest request, InitializrMetadata metadata) {
Type typeFromMetadata = metadata.getTypes().get(request.getType());
return BuildSystem.forId(typeFromMetadata.getTags().get("build"));
Map<String, String> typeTags = metadata.getTypes().get(request.getType()).getTags();
String id = typeTags.get("build");
String dialect = typeTags.get("dialect");
return BuildSystem.forIdAndDialect(id, dialect);
}
private Version getPlatformVersion(ProjectRequest request, InitializrMetadata metadata) {

View File

@@ -44,7 +44,7 @@ class InitializrMetadataV21JsonMapperTests {
@Test
void withNoAppUrl() throws IOException {
InitializrMetadata metadata = new InitializrMetadataTestBuilder()
.addType("foo", true, "/foo.zip", "none", "test").addDependencyGroup("foo", "one", "two").build();
.addType("foo", true, "/foo.zip", "none", null, "test").addDependencyGroup("foo", "one", "two").build();
String json = this.jsonMapper.write(metadata, null);
JsonNode result = objectMapper.readTree(json);
assertThat(get(result, "_links.foo.href"))
@@ -55,7 +55,7 @@ class InitializrMetadataV21JsonMapperTests {
@Test
void withAppUrl() throws IOException {
InitializrMetadata metadata = new InitializrMetadataTestBuilder()
.addType("foo", true, "/foo.zip", "none", "test").addDependencyGroup("foo", "one", "two").build();
.addType("foo", true, "/foo.zip", "none", null, "test").addDependencyGroup("foo", "one", "two").build();
String json = this.jsonMapper.write(metadata, "http://server:8080/my-app");
JsonNode result = objectMapper.readTree(json);
assertThat(get(result, "_links.foo.href"))

View File

@@ -68,6 +68,27 @@ class DefaultProjectRequestToDescriptionConverterTests {
.withMessage("Invalid type 'example-project' (missing build tag) check project metadata");
}
@Test
void convertWhenTypeDoesNotDefineDialectTagShouldUseDefaultDialect() {
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addType("foo", true, "/foo.zip", GradleBuildSystem.ID, null, "test").build();
ProjectRequest request = createProjectRequest();
request.setType("foo");
assertThat(this.converter.convert(request, metadata).getBuildSystem().dialect())
.isEqualTo(GradleBuildSystem.DIALECT_GROOVY);
}
@Test
void convertWhenTypeDefinesDialectTagShouldUseDialect() {
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addType("foo", true, "/foo.zip", GradleBuildSystem.ID, GradleBuildSystem.DIALECT_KOTLIN, "test")
.build();
ProjectRequest request = createProjectRequest();
request.setType("foo");
assertThat(this.converter.convert(request, metadata).getBuildSystem().dialect())
.isEqualTo(GradleBuildSystem.DIALECT_KOTLIN);
}
@Test
void convertWhenPlatformCompatibilityRangeIsNotSetShouldNotThrowException() {
this.metadata = InitializrMetadataTestBuilder.withDefaults().setPlatformCompatibilityRange(null).build();