mirror of
https://gitee.com/dcren/initializr.git
synced 2026-09-19 06:17:42 +08:00
Merge pull request #552 from torstenwalter:allowGenerationOfCustomFiles
* pr/552: Polish "Allow generation of custom files" Allow generation of custom files
This commit is contained in:
+10
-6
@@ -186,7 +186,10 @@ public class ProjectGenerator {
|
|||||||
*/
|
*/
|
||||||
public File generateProjectStructure(ProjectRequest request) {
|
public File generateProjectStructure(ProjectRequest request) {
|
||||||
try {
|
try {
|
||||||
return doGenerateProjectStructure(request);
|
Map<String, Object> model = resolveModel(request);
|
||||||
|
File rootDir = generateProjectStructure(request, model);
|
||||||
|
publishProjectGeneratedEvent(request);
|
||||||
|
return rootDir;
|
||||||
}
|
}
|
||||||
catch (InitializrException ex) {
|
catch (InitializrException ex) {
|
||||||
publishProjectFailedEvent(request, ex);
|
publishProjectFailedEvent(request, ex);
|
||||||
@@ -194,9 +197,12 @@ public class ProjectGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected File doGenerateProjectStructure(ProjectRequest request) {
|
/**
|
||||||
Map<String, Object> model = resolveModel(request);
|
* Generate a project structure for the specified {@link ProjectRequest} and resolved
|
||||||
|
* model.
|
||||||
|
*/
|
||||||
|
protected File generateProjectStructure(ProjectRequest request,
|
||||||
|
Map<String, Object> model) {
|
||||||
File rootDir;
|
File rootDir;
|
||||||
try {
|
try {
|
||||||
rootDir = File.createTempFile("tmp", "", getTemporaryDirectory());
|
rootDir = File.createTempFile("tmp", "", getTemporaryDirectory());
|
||||||
@@ -254,9 +260,7 @@ public class ProjectGenerator {
|
|||||||
new File(dir, "src/main/resources/templates").mkdirs();
|
new File(dir, "src/main/resources/templates").mkdirs();
|
||||||
new File(dir, "src/main/resources/static").mkdirs();
|
new File(dir, "src/main/resources/static").mkdirs();
|
||||||
}
|
}
|
||||||
publishProjectGeneratedEvent(request);
|
|
||||||
return rootDir;
|
return rootDir;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+11
-3
@@ -48,11 +48,19 @@ public abstract class AbstractProjectGeneratorTests {
|
|||||||
@Rule
|
@Rule
|
||||||
public final TemporaryFolder folder = new TemporaryFolder();
|
public final TemporaryFolder folder = new TemporaryFolder();
|
||||||
|
|
||||||
protected final ProjectGenerator projectGenerator = new ProjectGenerator();
|
protected final ProjectGenerator projectGenerator;
|
||||||
|
|
||||||
private final ApplicationEventPublisher eventPublisher = mock(
|
protected final ApplicationEventPublisher eventPublisher = mock(
|
||||||
ApplicationEventPublisher.class);
|
ApplicationEventPublisher.class);
|
||||||
|
|
||||||
|
protected AbstractProjectGeneratorTests() {
|
||||||
|
this(new ProjectGenerator());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected AbstractProjectGeneratorTests(ProjectGenerator projectGenerator) {
|
||||||
|
this.projectGenerator = projectGenerator;
|
||||||
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() throws IOException {
|
public void setup() throws IOException {
|
||||||
Dependency web = Dependency.withId("web");
|
Dependency web = Dependency.withId("web");
|
||||||
@@ -104,7 +112,7 @@ public abstract class AbstractProjectGeneratorTests {
|
|||||||
verify(eventPublisher, times(1)).publishEvent(argThat(new ProjectFailedEventMatcher(request, ex)));
|
verify(eventPublisher, times(1)).publishEvent(argThat(new ProjectFailedEventMatcher(request, ex)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ProjectGeneratedEventMatcher
|
protected static class ProjectGeneratedEventMatcher
|
||||||
extends ArgumentMatcher<ProjectGeneratedEvent> {
|
extends ArgumentMatcher<ProjectGeneratedEvent> {
|
||||||
|
|
||||||
private final ProjectRequest request;
|
private final ProjectRequest request;
|
||||||
|
|||||||
+97
@@ -0,0 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 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.generator;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import io.spring.initializr.test.generator.ProjectAssert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.InOrder;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.argThat;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for custom {@link ProjectGenerator}.
|
||||||
|
*
|
||||||
|
* @author Torsten Walter
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
*/
|
||||||
|
public class CustomProjectGeneratorTests extends AbstractProjectGeneratorTests {
|
||||||
|
|
||||||
|
public CustomProjectGeneratorTests() {
|
||||||
|
super(new MyProjectGenerator());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateCustomResource() {
|
||||||
|
ProjectRequest request = createProjectRequest();
|
||||||
|
request.setType("maven-project");
|
||||||
|
request.setGroupId("com.example.custom");
|
||||||
|
ProjectAssert project = generateProject(request);
|
||||||
|
project.sourceCodeAssert("custom.txt")
|
||||||
|
.equalsTo(new ClassPathResource("project/custom/custom.txt"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateCustomResourceDisabled() {
|
||||||
|
ProjectRequest request = createProjectRequest();
|
||||||
|
request.setType("gradle-build");
|
||||||
|
request.setGroupId("com.example.custom");
|
||||||
|
ProjectAssert project = generateProject(request);
|
||||||
|
project.hasNoFile("custom.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void projectGenerationEventFiredAfterCustomization() {
|
||||||
|
ProjectRequest request = createProjectRequest();
|
||||||
|
request.setType("maven-project");
|
||||||
|
request.setGroupId("com.example.custom");
|
||||||
|
generateProject(request);
|
||||||
|
verifyProjectSuccessfulEventFor(request);
|
||||||
|
|
||||||
|
Runnable customFileGenerated = ((MyProjectGenerator) projectGenerator).customFileGenerated;
|
||||||
|
InOrder inOrder = Mockito.inOrder(eventPublisher, customFileGenerated);
|
||||||
|
|
||||||
|
inOrder.verify(customFileGenerated, times(1)).run();
|
||||||
|
inOrder.verify(eventPublisher, times(1)).publishEvent(
|
||||||
|
argThat(new ProjectGeneratedEventMatcher(request)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static class MyProjectGenerator extends ProjectGenerator {
|
||||||
|
private Runnable customFileGenerated = mock(Runnable.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected File generateProjectStructure(ProjectRequest request,
|
||||||
|
Map<String, Object> model) {
|
||||||
|
model.put("customValue", 42);
|
||||||
|
File dir = super.generateProjectStructure(request, model);
|
||||||
|
if ("maven".equals(request.getBuild())) {
|
||||||
|
write(new File(dir, "custom.txt"), "custom.txt", model);
|
||||||
|
customFileGenerated.run();
|
||||||
|
}
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
Custom resources
|
||||||
|
|
||||||
|
com.example.custom
|
||||||
|
42
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
Custom resources
|
||||||
|
|
||||||
|
{{groupId}}
|
||||||
|
{{customValue}}
|
||||||
Reference in New Issue
Block a user