Simplify KotlinVersionResolver arrangement

This commit makes sure KotlinVersionResolver works with the actual
metadata instance used by the project to generate rather than
potentially using a different instance via the provider.

Rather than requiring such a bean to be present, the configuration
fallbacks to the metadata implementation if none is provided.
This commit is contained in:
Stephane Nicoll 2019-08-14 11:27:05 +02:00
parent e343e3c0fc
commit 9c30905003
7 changed files with 28 additions and 61 deletions

View File

@ -17,7 +17,6 @@
package io.spring.initializr.stub;
import io.spring.initializr.actuate.autoconfigure.InitializrActuatorEndpointsAutoConfiguration;
import io.spring.initializr.generator.spring.autoconfigure.GeneratorSpringAutoConfiguration;
import io.spring.initializr.web.autoconfigure.InitializrAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -27,8 +26,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
*
* @author Stephane Nicoll
*/
@SpringBootApplication(exclude = { InitializrAutoConfiguration.class,
InitializrActuatorEndpointsAutoConfiguration.class, GeneratorSpringAutoConfiguration.class })
@SpringBootApplication(
exclude = { InitializrAutoConfiguration.class, InitializrActuatorEndpointsAutoConfiguration.class })
public class SampleApp {
}

View File

@ -1,41 +0,0 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.generator.spring.autoconfigure;
import io.spring.initializr.generator.spring.code.kotlin.InitializrMetadataKotlinVersionResolver;
import io.spring.initializr.generator.spring.code.kotlin.KotlinVersionResolver;
import io.spring.initializr.metadata.InitializrMetadataProvider;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
/**
* {@link EnableAutoConfiguration Auto-configuration} for generation of Spring-based
* projects.
*
* @author Andy Wilkinson
*/
public class GeneratorSpringAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public KotlinVersionResolver kotlinVersionResolver(InitializrMetadataProvider metadataProvider) {
return new InitializrMetadataKotlinVersionResolver(metadataProvider);
}
}

View File

@ -18,7 +18,6 @@ package io.spring.initializr.generator.spring.code.kotlin;
import io.spring.initializr.generator.project.ResolvedProjectDescription;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.InitializrMetadataProvider;
/**
* {@link KotlinVersionResolver} that resolves the version from the
@ -28,15 +27,15 @@ import io.spring.initializr.metadata.InitializrMetadataProvider;
*/
public class InitializrMetadataKotlinVersionResolver implements KotlinVersionResolver {
private final InitializrMetadataProvider metadataProvider;
private final InitializrMetadata metadata;
public InitializrMetadataKotlinVersionResolver(InitializrMetadataProvider metadataProvider) {
this.metadataProvider = metadataProvider;
public InitializrMetadataKotlinVersionResolver(InitializrMetadata metadata) {
this.metadata = metadata;
}
@Override
public String resolveKotlinVersion(ResolvedProjectDescription description) {
return this.metadataProvider.get().getConfiguration().getEnv().getKotlin()
return this.metadata.getConfiguration().getEnv().getKotlin()
.resolveKotlinVersion(description.getPlatformVersion());
}

View File

@ -80,8 +80,12 @@ public class KotlinProjectGenerationConfiguration {
}
@Bean
public KotlinProjectSettings kotlinProjectSettings(KotlinVersionResolver kotlinVersionResolver) {
return new SimpleKotlinProjectSettings(kotlinVersionResolver.resolveKotlinVersion(this.projectDescription));
public KotlinProjectSettings kotlinProjectSettings(ObjectProvider<KotlinVersionResolver> kotlinVersionResolver,
InitializrMetadata metadata) {
String kotlinVersion = kotlinVersionResolver
.getIfAvailable(() -> new InitializrMetadataKotlinVersionResolver(metadata))
.resolveKotlinVersion(this.projectDescription);
return new SimpleKotlinProjectSettings(kotlinVersion);
}
@Bean

View File

@ -9,6 +9,3 @@ io.spring.initializr.generator.spring.code.kotlin.KotlinProjectGenerationConfigu
io.spring.initializr.generator.spring.configuration.ApplicationConfigurationProjectGenerationConfiguration,\
io.spring.initializr.generator.spring.documentation.HelpDocumentProjectGenerationConfiguration,\
io.spring.initializr.generator.spring.scm.git.GitProjectGenerationConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
io.spring.initializr.generator.spring.autoconfigure.GeneratorSpringAutoConfiguration

View File

@ -27,8 +27,6 @@ import io.spring.initializr.generator.language.Language;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.project.ProjectGenerationContext;
import io.spring.initializr.generator.project.ResolvedProjectDescription;
import io.spring.initializr.generator.spring.code.kotlin.InitializrMetadataKotlinVersionResolver;
import io.spring.initializr.generator.spring.code.kotlin.KotlinVersionResolver;
import io.spring.initializr.generator.spring.test.InitializrMetadataTestBuilder;
import io.spring.initializr.generator.spring.test.ProjectAssert;
import io.spring.initializr.generator.test.project.ProjectGeneratorTester;
@ -104,8 +102,6 @@ public abstract class AbstractComplianceTests {
context.getBean(ResolvedProjectDescription.class).getPlatformVersion()));
context.registerBean(IndentingWriterFactory.class,
() -> IndentingWriterFactory.create(new SimpleIndentStrategy("\t")));
context.registerBean(KotlinVersionResolver.class,
() -> new InitializrMetadataKotlinVersionResolver(() -> metadata));
}
private void setupProjectDescription(Language language, String version, BuildSystem buildSystem,

View File

@ -54,9 +54,7 @@ class KotlinProjectGenerationConfigurationTests {
.withConfiguration(SourceCodeProjectGenerationConfiguration.class,
KotlinProjectGenerationConfiguration.class, BuildProjectGenerationConfiguration.class,
MavenProjectGenerationConfiguration.class)
.withDirectory(directory)
.withBean(KotlinProjectSettings.class, () -> new SimpleKotlinProjectSettings("1.2.70"))
.withBean(InitializrMetadata.class, () -> {
.withDirectory(directory).withBean(InitializrMetadata.class, () -> {
io.spring.initializr.metadata.Dependency dependency = io.spring.initializr.metadata.Dependency
.withId("foo");
dependency.setFacets(Collections.singletonList("json"));
@ -70,6 +68,21 @@ class KotlinProjectGenerationConfigurationTests {
});
}
@Test
void kotlinVersionFallbacksToMetadataIfNotPresent() {
KotlinProjectSettings settings = this.projectTester.generate(new ProjectDescription(),
(context) -> context.getBean(KotlinProjectSettings.class));
assertThat(settings.getVersion()).isEqualTo("1.1.1");
}
@Test
void kotlinVersionResolverIsUsedIfPresent() {
KotlinProjectSettings settings = this.projectTester
.withBean(KotlinProjectSettings.class, () -> new SimpleKotlinProjectSettings("0.9.12"))
.generate(new ProjectDescription(), (context) -> context.getBean(KotlinProjectSettings.class));
assertThat(settings.getVersion()).isEqualTo("0.9.12");
}
@Test
void mainClassIsContributedWhenGeneratingProject() {
ProjectStructure projectStructure = this.projectTester.generate(new ProjectDescription());