Skip to content

Commit

Permalink
Add dialect support to Type metadata
Browse files Browse the repository at this point in the history
Closes gh-1344
  • Loading branch information
snicoll committed Nov 15, 2022
1 parent 3ccc201 commit 6b84396
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
6 changes: 4 additions & 2 deletions initializr-docs/src/main/asciidoc/configuration-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ public InitializrMetadataTestBuilder addBasicDefaults() {
}

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);
Expand All @@ -105,6 +105,9 @@ public InitializrMetadataTestBuilder addType(String id, boolean defaultValue, St
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -165,8 +166,10 @@ private void validateDependencyRange(Version platformVersion, List<Dependency> r
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -55,7 +55,7 @@ void withNoAppUrl() throws IOException {
@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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ void convertWhenTypeDoesNotDefineBuildTagShouldThrowException() {
.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();
Expand Down

0 comments on commit 6b84396

Please sign in to comment.