mirror of
https://gitee.com/dcren/initializr.git
synced 2025-12-02 11:24:04 +08:00
Polish "Allow generation of custom files"
Closes gh-552
This commit is contained in:
@@ -186,7 +186,8 @@ public class ProjectGenerator {
|
||||
*/
|
||||
public File generateProjectStructure(ProjectRequest request) {
|
||||
try {
|
||||
File rootDir = doGenerateProjectStructure(request);
|
||||
Map<String, Object> model = resolveModel(request);
|
||||
File rootDir = generateProjectStructure(request, model);
|
||||
publishProjectGeneratedEvent(request);
|
||||
return rootDir;
|
||||
}
|
||||
@@ -196,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;
|
||||
try {
|
||||
rootDir = File.createTempFile("tmp", "", getTemporaryDirectory());
|
||||
@@ -566,11 +570,11 @@ public class ProjectGenerator {
|
||||
&& !request.getJavaVersion().equals("1.7");
|
||||
}
|
||||
|
||||
protected static boolean isGradleBuild(ProjectRequest request) {
|
||||
private static boolean isGradleBuild(ProjectRequest request) {
|
||||
return "gradle".equals(request.getBuild());
|
||||
}
|
||||
|
||||
protected static boolean isMavenBuild(ProjectRequest request) {
|
||||
private static boolean isMavenBuild(ProjectRequest request) {
|
||||
return "maven".equals(request.getBuild());
|
||||
}
|
||||
|
||||
|
||||
@@ -53,11 +53,11 @@ public abstract class AbstractProjectGeneratorTests {
|
||||
protected final ApplicationEventPublisher eventPublisher = mock(
|
||||
ApplicationEventPublisher.class);
|
||||
|
||||
public AbstractProjectGeneratorTests() {
|
||||
protected AbstractProjectGeneratorTests() {
|
||||
this(new ProjectGenerator());
|
||||
}
|
||||
|
||||
public AbstractProjectGeneratorTests(ProjectGenerator projectGenerator) {
|
||||
protected AbstractProjectGeneratorTests(ProjectGenerator projectGenerator) {
|
||||
this.projectGenerator = projectGenerator;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,41 @@
|
||||
/*
|
||||
* 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 java.io.File;
|
||||
|
||||
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() {
|
||||
@@ -18,46 +43,55 @@ public class CustomProjectGeneratorTests extends AbstractProjectGeneratorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jenkinsfileMaven() {
|
||||
public void generateCustomResource() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
request.setType("maven-project");
|
||||
request.setGroupId("com.example.custom");
|
||||
ProjectAssert project = generateProject(request);
|
||||
project.sourceCodeAssert("Jenkinsfile")
|
||||
.equalsTo(new ClassPathResource("project/maven/Jenkinsfile"));
|
||||
project.sourceCodeAssert("custom.txt")
|
||||
.equalsTo(new ClassPathResource("project/custom/custom.txt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void jenkinsfileGradle() {
|
||||
public void generateCustomResourceDisabled() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
request.setType("gradle-build");
|
||||
request.setGroupId("com.example.custom");
|
||||
ProjectAssert project = generateProject(request);
|
||||
project.hasNoFile("Jenkinsfile");
|
||||
project.hasNoFile("custom.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectGenerationEventAfterGeneratingAllFiles() throws Exception {
|
||||
ProjectRequest request = createProjectRequest("web");
|
||||
public void projectGenerationEventFiredAfterCustomization() {
|
||||
ProjectRequest request = createProjectRequest();
|
||||
request.setType("maven-project");
|
||||
request.setGroupId("com.example.custom");
|
||||
generateProject(request);
|
||||
verifyProjectSuccessfulEventFor(request);
|
||||
|
||||
Runnable jenkinsfileGenerated = ((MyProjectGenerator) projectGenerator).jenkinsfileGenerated;
|
||||
InOrder inOrder = Mockito.inOrder(eventPublisher, jenkinsfileGenerated);
|
||||
Runnable customFileGenerated = ((MyProjectGenerator) projectGenerator).customFileGenerated;
|
||||
InOrder inOrder = Mockito.inOrder(eventPublisher, customFileGenerated);
|
||||
|
||||
inOrder.verify(jenkinsfileGenerated, times(1)).run();
|
||||
inOrder.verify(eventPublisher, times(1)).publishEvent(argThat(new ProjectGeneratedEventMatcher(request)));
|
||||
inOrder.verify(customFileGenerated, times(1)).run();
|
||||
inOrder.verify(eventPublisher, times(1)).publishEvent(
|
||||
argThat(new ProjectGeneratedEventMatcher(request)));
|
||||
}
|
||||
|
||||
|
||||
private static class MyProjectGenerator extends ProjectGenerator {
|
||||
protected final Runnable jenkinsfileGenerated = Mockito.mock(Runnable.class);
|
||||
private Runnable customFileGenerated = mock(Runnable.class);
|
||||
|
||||
@Override
|
||||
protected File doGenerateProjectStructure(ProjectRequest request) {
|
||||
File dir = super.doGenerateProjectStructure(request);
|
||||
if (isMavenBuild(request)) {
|
||||
write(new File(dir, "Jenkinsfile"), "Jenkinsfile.tmpl", resolveModel(request));
|
||||
jenkinsfileGenerated.run();
|
||||
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
|
||||
@@ -1,16 +0,0 @@
|
||||
pipeline {
|
||||
agent any
|
||||
stages {
|
||||
stage ('Build') {
|
||||
steps {
|
||||
echo "building com.example:demo:0.0.1-SNAPSHOT"
|
||||
sh 'mvn -Dmaven.test.failure.ignore=true -B clean install'
|
||||
}
|
||||
post {
|
||||
success {
|
||||
junit 'target/surefire-reports/**/*.xml'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
pipeline {
|
||||
agent any
|
||||
stages {
|
||||
stage ('Build') {
|
||||
steps {
|
||||
echo "building {{groupId}}:{{artifactId}}:{{version}}"
|
||||
sh 'mvn -Dmaven.test.failure.ignore=true -B clean install'
|
||||
}
|
||||
post {
|
||||
success {
|
||||
junit 'target/surefire-reports/**/*.xml'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
Custom resources
|
||||
|
||||
{{groupId}}
|
||||
{{customValue}}
|
||||
Reference in New Issue
Block a user