Polish "Allow generation of custom files"

Closes gh-552
This commit is contained in:
Stephane Nicoll
2017-12-03 02:00:42 +00:00
parent f224c6a38a
commit baf1022e6f
7 changed files with 96 additions and 82 deletions

View File

@@ -186,7 +186,8 @@ public class ProjectGenerator {
*/ */
public File generateProjectStructure(ProjectRequest request) { public File generateProjectStructure(ProjectRequest request) {
try { try {
File rootDir = doGenerateProjectStructure(request); Map<String, Object> model = resolveModel(request);
File rootDir = generateProjectStructure(request, model);
publishProjectGeneratedEvent(request); publishProjectGeneratedEvent(request);
return rootDir; 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; File rootDir;
try { try {
rootDir = File.createTempFile("tmp", "", getTemporaryDirectory()); rootDir = File.createTempFile("tmp", "", getTemporaryDirectory());
@@ -566,11 +570,11 @@ public class ProjectGenerator {
&& !request.getJavaVersion().equals("1.7"); && !request.getJavaVersion().equals("1.7");
} }
protected static boolean isGradleBuild(ProjectRequest request) { private static boolean isGradleBuild(ProjectRequest request) {
return "gradle".equals(request.getBuild()); return "gradle".equals(request.getBuild());
} }
protected static boolean isMavenBuild(ProjectRequest request) { private static boolean isMavenBuild(ProjectRequest request) {
return "maven".equals(request.getBuild()); return "maven".equals(request.getBuild());
} }

View File

@@ -53,11 +53,11 @@ public abstract class AbstractProjectGeneratorTests {
protected final ApplicationEventPublisher eventPublisher = mock( protected final ApplicationEventPublisher eventPublisher = mock(
ApplicationEventPublisher.class); ApplicationEventPublisher.class);
public AbstractProjectGeneratorTests() { protected AbstractProjectGeneratorTests() {
this(new ProjectGenerator()); this(new ProjectGenerator());
} }
public AbstractProjectGeneratorTests(ProjectGenerator projectGenerator) { protected AbstractProjectGeneratorTests(ProjectGenerator projectGenerator) {
this.projectGenerator = projectGenerator; this.projectGenerator = projectGenerator;
} }

View File

@@ -1,63 +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; package io.spring.initializr.generator;
import java.io.File;
import java.util.Map;
import io.spring.initializr.test.generator.ProjectAssert; import io.spring.initializr.test.generator.ProjectAssert;
import org.junit.Test; import org.junit.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import java.io.File;
import static org.mockito.Matchers.argThat; import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
/**
* Test for custom {@link ProjectGenerator}.
*
* @author Torsten Walter
* @author Stephane Nicoll
*/
public class CustomProjectGeneratorTests extends AbstractProjectGeneratorTests { public class CustomProjectGeneratorTests extends AbstractProjectGeneratorTests {
public CustomProjectGeneratorTests() { public CustomProjectGeneratorTests() {
super(new MyProjectGenerator()); super(new MyProjectGenerator());
} }
@Test @Test
public void jenkinsfileMaven() { public void generateCustomResource() {
ProjectRequest request = createProjectRequest(); ProjectRequest request = createProjectRequest();
request.setType("maven-project"); request.setType("maven-project");
ProjectAssert project = generateProject(request); request.setGroupId("com.example.custom");
project.sourceCodeAssert("Jenkinsfile") ProjectAssert project = generateProject(request);
.equalsTo(new ClassPathResource("project/maven/Jenkinsfile")); project.sourceCodeAssert("custom.txt")
} .equalsTo(new ClassPathResource("project/custom/custom.txt"));
}
@Test @Test
public void jenkinsfileGradle() { public void generateCustomResourceDisabled() {
ProjectRequest request = createProjectRequest(); ProjectRequest request = createProjectRequest();
request.setType("gradle-build"); request.setType("gradle-build");
ProjectAssert project = generateProject(request); request.setGroupId("com.example.custom");
project.hasNoFile("Jenkinsfile"); ProjectAssert project = generateProject(request);
} project.hasNoFile("custom.txt");
}
@Test @Test
public void projectGenerationEventAfterGeneratingAllFiles() throws Exception { public void projectGenerationEventFiredAfterCustomization() {
ProjectRequest request = createProjectRequest("web"); ProjectRequest request = createProjectRequest();
generateProject(request); request.setType("maven-project");
verifyProjectSuccessfulEventFor(request); request.setGroupId("com.example.custom");
generateProject(request);
verifyProjectSuccessfulEventFor(request);
Runnable jenkinsfileGenerated = ((MyProjectGenerator) projectGenerator).jenkinsfileGenerated; Runnable customFileGenerated = ((MyProjectGenerator) projectGenerator).customFileGenerated;
InOrder inOrder = Mockito.inOrder(eventPublisher, jenkinsfileGenerated); InOrder inOrder = Mockito.inOrder(eventPublisher, customFileGenerated);
inOrder.verify(jenkinsfileGenerated, times(1)).run(); inOrder.verify(customFileGenerated, times(1)).run();
inOrder.verify(eventPublisher, times(1)).publishEvent(argThat(new ProjectGeneratedEventMatcher(request))); inOrder.verify(eventPublisher, times(1)).publishEvent(
} argThat(new ProjectGeneratedEventMatcher(request)));
}
private static class MyProjectGenerator extends ProjectGenerator {
protected final Runnable jenkinsfileGenerated = Mockito.mock(Runnable.class);
@Override private static class MyProjectGenerator extends ProjectGenerator {
protected File doGenerateProjectStructure(ProjectRequest request) { private Runnable customFileGenerated = mock(Runnable.class);
File dir = super.doGenerateProjectStructure(request);
if (isMavenBuild(request)) { @Override
write(new File(dir, "Jenkinsfile"), "Jenkinsfile.tmpl", resolveModel(request)); protected File generateProjectStructure(ProjectRequest request,
jenkinsfileGenerated.run(); Map<String, Object> model) {
} model.put("customValue", 42);
return dir; File dir = super.generateProjectStructure(request, model);
} if ("maven".equals(request.getBuild())) {
} write(new File(dir, "custom.txt"), "custom.txt", model);
customFileGenerated.run();
}
return dir;
}
}
} }

View File

@@ -0,0 +1,4 @@
Custom resources
com.example.custom
42

View File

@@ -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'
}
}
}
}
}

View File

@@ -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'
}
}
}
}
}

View File

@@ -0,0 +1,4 @@
Custom resources
{{groupId}}
{{customValue}}