mirror of
https://gitee.com/dcren/initializr.git
synced 2025-04-24 18:04:58 +08:00
Remove magic string comparison for build systems
This commit removes the magic string comparision to identify the build system in use. Rather than just returning the `Path` to the project, a `ProjectGenerationResult` is now returned that contains the description of the generated project. Closes gh-817
This commit is contained in:
parent
1485d9af82
commit
5ea041744c
@ -28,7 +28,10 @@ import java.util.concurrent.TimeUnit;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.samskivert.mustache.Mustache;
|
||||
import io.spring.initializr.generator.buildsystem.BuildSystem;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
|
||||
import io.spring.initializr.generator.io.template.TemplateRenderer;
|
||||
import io.spring.initializr.generator.project.ResolvedProjectDescription;
|
||||
import io.spring.initializr.generator.version.Version;
|
||||
import io.spring.initializr.metadata.DependencyMetadata;
|
||||
import io.spring.initializr.metadata.DependencyMetadataProvider;
|
||||
@ -261,10 +264,11 @@ public class MainController extends AbstractInitializrController {
|
||||
@RequestMapping("/starter.zip")
|
||||
@ResponseBody
|
||||
public ResponseEntity<byte[]> springZip(ProjectRequest request) throws IOException {
|
||||
File dir = this.projectGenerationInvoker
|
||||
ProjectGenerationResult result = this.projectGenerationInvoker
|
||||
.invokeProjectStructureGeneration(request);
|
||||
File dir = result.getRootDirectory().toFile();
|
||||
File download = this.projectGenerationInvoker.createDistributionFile(dir, ".zip");
|
||||
String wrapperScript = getWrapperScript(request);
|
||||
String wrapperScript = getWrapperScript(result.getProjectDescription());
|
||||
Zip zip = new Zip();
|
||||
zip.setProject(new Project());
|
||||
zip.setDefaultexcludes(false);
|
||||
@ -288,11 +292,12 @@ public class MainController extends AbstractInitializrController {
|
||||
@RequestMapping(path = "/starter.tgz", produces = "application/x-compress")
|
||||
@ResponseBody
|
||||
public ResponseEntity<byte[]> springTgz(ProjectRequest request) throws IOException {
|
||||
File dir = this.projectGenerationInvoker
|
||||
ProjectGenerationResult result = this.projectGenerationInvoker
|
||||
.invokeProjectStructureGeneration(request);
|
||||
File dir = result.getRootDirectory().toFile();
|
||||
File download = this.projectGenerationInvoker.createDistributionFile(dir,
|
||||
".tar.gz");
|
||||
String wrapperScript = getWrapperScript(request);
|
||||
String wrapperScript = getWrapperScript(result.getProjectDescription());
|
||||
Tar zip = new Tar();
|
||||
zip.setProject(new Project());
|
||||
zip.setDefaultexcludes(false);
|
||||
@ -325,11 +330,12 @@ public class MainController extends AbstractInitializrController {
|
||||
}
|
||||
}
|
||||
|
||||
private static String getWrapperScript(ProjectRequest request) {
|
||||
String script = (request.getType() != null
|
||||
&& request.getType().startsWith("gradle")) ? "gradlew" : "mvnw";
|
||||
return (request.getBaseDir() != null) ? request.getBaseDir() + "/" + script
|
||||
: script;
|
||||
private static String getWrapperScript(ResolvedProjectDescription description) {
|
||||
BuildSystem buildSystem = description.getBuildSystem();
|
||||
String script = buildSystem.id().equals(GradleBuildSystem.ID) ? "gradlew"
|
||||
: "mvnw";
|
||||
return (description.getBaseDirectory() != null)
|
||||
? description.getBaseDirectory() + "/" + script : script;
|
||||
}
|
||||
|
||||
private ResponseEntity<byte[]> upload(File download, File dir, String fileName,
|
||||
|
@ -70,12 +70,12 @@ public class ProjectGenerationInvoker {
|
||||
|
||||
/**
|
||||
* Invokes the project generation API that generates the entire project structure for
|
||||
* the specified {@link WebProjectRequest}. Returns a directory containing the
|
||||
* project.
|
||||
* the specified {@link WebProjectRequest}.
|
||||
* @param request the project request
|
||||
* @return the generated project structure
|
||||
* @return the {@link ProjectGenerationResult}
|
||||
*/
|
||||
public File invokeProjectStructureGeneration(ProjectRequest request) {
|
||||
public ProjectGenerationResult invokeProjectStructureGeneration(
|
||||
ProjectRequest request) {
|
||||
InitializrMetadata metadata = this.parentApplicationContext
|
||||
.getBean(InitializrMetadataProvider.class).get();
|
||||
try {
|
||||
@ -84,12 +84,12 @@ public class ProjectGenerationInvoker {
|
||||
ProjectGenerator projectGenerator = new ProjectGenerator(
|
||||
(projectGenerationContext) -> customizeProjectGenerationContext(
|
||||
projectGenerationContext, metadata));
|
||||
Path path = projectGenerator.generate(projectDescription,
|
||||
ProjectGenerationResult result = projectGenerator.generate(projectDescription,
|
||||
generateProject(request));
|
||||
File file = path.toFile();
|
||||
File file = result.getRootDirectory().toFile();
|
||||
String name = file.getName();
|
||||
addTempFile(name, file);
|
||||
return file;
|
||||
return result;
|
||||
}
|
||||
catch (ProjectGenerationException ex) {
|
||||
publishProjectFailedEvent(request, metadata, ex);
|
||||
@ -97,11 +97,13 @@ public class ProjectGenerationInvoker {
|
||||
}
|
||||
}
|
||||
|
||||
private ProjectAssetGenerator<Path> generateProject(ProjectRequest request) {
|
||||
private ProjectAssetGenerator<ProjectGenerationResult> generateProject(
|
||||
ProjectRequest request) {
|
||||
return (context) -> {
|
||||
Path projectDir = new DefaultProjectAssetGenerator().generate(context);
|
||||
publishProjectGeneratedEvent(request, context);
|
||||
return projectDir;
|
||||
return new ProjectGenerationResult(
|
||||
context.getBean(ResolvedProjectDescription.class), projectDir);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.web.project;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import io.spring.initializr.generator.project.ResolvedProjectDescription;
|
||||
|
||||
/**
|
||||
* Result of project generation.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class ProjectGenerationResult {
|
||||
|
||||
private final ResolvedProjectDescription projectDescription;
|
||||
|
||||
private final Path rootDirectory;
|
||||
|
||||
ProjectGenerationResult(ResolvedProjectDescription projectDescription,
|
||||
Path rootDirectory) {
|
||||
this.projectDescription = projectDescription;
|
||||
this.rootDirectory = rootDirectory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link ResolvedProjectDescription} that was used to generate the
|
||||
* project.
|
||||
* @return the project description
|
||||
*/
|
||||
public ResolvedProjectDescription getProjectDescription() {
|
||||
return this.projectDescription;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the root directory.
|
||||
* @return the root directory
|
||||
* @see ResolvedProjectDescription#getBaseDirectory()
|
||||
*/
|
||||
public Path getRootDirectory() {
|
||||
return this.rootDirectory;
|
||||
}
|
||||
|
||||
}
|
@ -88,7 +88,9 @@ public class ProjectGenerationInvokerTests {
|
||||
WebProjectRequest request = new WebProjectRequest();
|
||||
request.setType("maven-project");
|
||||
request.initialize(metadata);
|
||||
File file = this.invoker.invokeProjectStructureGeneration(request);
|
||||
ProjectGenerationResult result = this.invoker
|
||||
.invokeProjectStructureGeneration(request);
|
||||
File file = result.getRootDirectory().toFile();
|
||||
new ProjectAssert(file).isJavaProject();
|
||||
Map<String, List<File>> tempFiles = (Map<String, List<File>>) ReflectionTestUtils
|
||||
.getField(this.invoker, "temporaryFiles");
|
||||
@ -169,7 +171,9 @@ public class ProjectGenerationInvokerTests {
|
||||
WebProjectRequest request = new WebProjectRequest();
|
||||
request.initialize(metadata);
|
||||
request.setType("gradle-project");
|
||||
File file = this.invoker.invokeProjectStructureGeneration(request);
|
||||
ProjectGenerationResult result = this.invoker
|
||||
.invokeProjectStructureGeneration(request);
|
||||
File file = result.getRootDirectory().toFile();
|
||||
this.invoker.cleanTempFiles(file);
|
||||
assertThat(file.listFiles()).isNull();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user