Add .groovy lang support

This commit is contained in:
Dave Syer
2014-05-30 09:50:24 +01:00
parent eeb6a762a8
commit 113b864e4b
9 changed files with 128 additions and 12 deletions

View File

@@ -22,7 +22,13 @@ class MainController {
@Autowired @Autowired
private Projects projects private Projects projects
@RequestMapping('/') @RequestMapping(value='/')
@ResponseBody
Projects projects() {
projects
}
@RequestMapping(value='/', produces='text/html')
@ResponseBody @ResponseBody
String home() { String home() {
def model = [:] def model = [:]
@@ -31,6 +37,7 @@ class MainController {
model['types'] = projects.types.sort { it.name } model['types'] = projects.types.sort { it.name }
model['packagings'] = projects.packagings.sort { it.name } model['packagings'] = projects.packagings.sort { it.name }
model['javaVersions'] = projects.javaVersions model['javaVersions'] = projects.javaVersions
model['languages'] = projects.languages
template 'home.html', model template 'home.html', model
} }
@@ -112,15 +119,17 @@ class MainController {
new File(dir, 'pom.xml').write(pom) new File(dir, 'pom.xml').write(pom)
} }
File src = new File(new File(dir, 'src/main/java'),request.packageName.replace('.', '/')) String language = request.language
File src = new File(new File(dir, 'src/main/' + language),request.packageName.replace('.', '/'))
src.mkdirs() src.mkdirs()
write(src, 'Application.java', model) write(src, 'Application.' + language, model)
if (request.packaging=='war') { if (request.packaging=='war') {
write(src, 'ServletInitializer.java', model) write(src, 'ServletInitializer.' + language, model)
} }
File test = new File(new File(dir, 'src/test/java'),request.packageName.replace('.', '/')) File test = new File(new File(dir, 'src/test/' + language),request.packageName.replace('.', '/'))
test.mkdirs() test.mkdirs()
if (model.styles.contains('-web')) { if (model.styles.contains('-web')) {
model.testAnnotations = '@WebAppConfiguration\n' model.testAnnotations = '@WebAppConfiguration\n'
@@ -129,7 +138,7 @@ class MainController {
model.testAnnotations = '' model.testAnnotations = ''
model.testImports = '' model.testImports = ''
} }
write(test, 'ApplicationTests.java', model) write(test, 'ApplicationTests.' + language, model)
File resources = new File(dir, 'src/main/resources') File resources = new File(dir, 'src/main/resources')
resources.mkdirs() resources.mkdirs()
@@ -180,6 +189,7 @@ class MainController {
model.packageName = request.packageName model.packageName = request.packageName
model.packaging = request.packaging model.packaging = request.packaging
model.javaVersion = request.javaVersion model.javaVersion = request.javaVersion
model.language = request.language
if (style==null || style.size()==0) { if (style==null || style.size()==0) {
style = [''] style = ['']
@@ -237,6 +247,7 @@ class PomRequest {
String artifactId String artifactId
String version = '0.0.1-SNAPSHOT' String version = '0.0.1-SNAPSHOT'
String packaging = 'jar' String packaging = 'jar'
String language = 'java'
String packageName String packageName
String javaVersion = '1.7' String javaVersion = '1.7'
String getArtifactId() { String getArtifactId() {
@@ -254,6 +265,12 @@ class Projects {
List<Type> types List<Type> types
List<Packaging> packagings List<Packaging> packagings
List<JavaVersion> javaVersions List<JavaVersion> javaVersions
List<Language> languages
static class Language {
String name
String value
boolean selected
}
static class JavaVersion { static class JavaVersion {
String value String value
boolean selected boolean selected

View File

@@ -87,3 +87,10 @@ projects:
selected: true selected: true
- value: 1.8 - value: 1.8
selected: false selected: false
languages:
- name: Groovy
value: groovy
selected: false
- name: Java
value: java
selected: true

View File

@@ -0,0 +1,16 @@
package ${packageName}
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
@Configuration
@ComponentScan
@EnableAutoConfiguration
class Application {
static void main(String[] args) {
SpringApplication.run Application, args
}
}

View File

@@ -0,0 +1,16 @@
package ${packageName}
import org.junit.Test
import org.junit.runner.RunWith
${testImports}import org.springframework.boot.test.SpringApplicationConfiguration
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
@RunWith(SpringJUnit4ClassRunner)
@SpringApplicationConfiguration(classes = Application)
${testAnnotations}class ApplicationTests {
@Test
void contextLoads() {
}
}

View File

@@ -0,0 +1,13 @@
package ${packageName}
import org.springframework.boot.builder.SpringApplicationBuilder
import org.springframework.boot.context.web.SpringBootServletInitializer
class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
application.sources(Application)
}
}

View File

@@ -64,6 +64,12 @@
<input type="radio" name="javaVersion" value="${it.value}"${it.selected==true ? ' checked="true"' : ''}/> <input type="radio" name="javaVersion" value="${it.value}"${it.selected==true ? ' checked="true"' : ''}/>
${it.value} ${it.value}
</label><% } %> </label><% } %>
<label>Language:</label>
<% languages.each { %>
<label class="radio">
<input type="radio" name="language" value="${it.value}"${it.selected==true ? ' checked="true"' : ''}/>
${it.name}
</label><% } %>
<button type="submit" class="btn">Generate</button> <button type="submit" class="btn">Generate</button>
</form> </form>
</div> </div>

View File

@@ -13,7 +13,7 @@ buildscript {
} }
} }
apply plugin: 'java' apply plugin: '${language}'
apply plugin: 'eclipse' apply plugin: 'eclipse'
apply plugin: 'idea' apply plugin: 'idea'
apply plugin: 'spring-boot' <% if (packaging=='war') { %> apply plugin: 'spring-boot' <% if (packaging=='war') { %>
@@ -35,8 +35,9 @@ repositories {
} }
dependencies {<% styles.each { %> dependencies {<% styles.each { %>
compile("org.springframework.boot:spring-boot-starter${it}:\${springBootVersion}")<% } %> compile("org.springframework.boot:spring-boot-starter${it}")<% } %>
testCompile("org.springframework.boot:spring-boot-starter-test:\${springBootVersion}") <% if (language=='groovy') { %>compile("org.codehaus.groovy:groovy")<% } %>
testCompile("org.springframework.boot:spring-boot-starter-test")
} }
task wrapper(type: Wrapper) { task wrapper(type: Wrapper) {

View File

@@ -22,6 +22,10 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter${it}</artifactId> <artifactId>spring-boot-starter${it}</artifactId>
</dependency><% } %><% if (language=='groovy') { %>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency><% } %> </dependency><% } %>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@@ -41,7 +45,31 @@
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin><% if (language=='groovy') { %>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.8.0-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.1.8-01</version>
</dependency>
</dependencies>
</plugin> </plugin>
<plugin>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.8.0-01</version>
<extensions>true</extensions>
</plugin><% } %>
</plugins> </plugins>
</build><% if (!bootVersion.contains("RELEASE")) { %> </build><% if (!bootVersion.contains("RELEASE")) { %>

View File

@@ -9,12 +9,24 @@ class IntegrationTests {
@Value('${local.server.port}') @Value('${local.server.port}')
int port int port
private String home() {
HttpHeaders headers = new HttpHeaders()
headers.setAccept([MediaType.TEXT_HTML])
new TestRestTemplate().exchange('http://localhost:' + port, HttpMethod.GET, new HttpEntity<Void>(headers), String).body
}
@Test @Test
void homeIsForm() { void homeIsForm() {
String body = new TestRestTemplate().getForObject('http://localhost:' + port, String) String body = home()
assertTrue('Wrong body:\n' + body, body.contains('action="/starter.zip"')) assertTrue('Wrong body:\n' + body, body.contains('action="/starter.zip"'))
} }
@Test
void homeIsJson() {
String body = new TestRestTemplate().getForObject('http://localhost:' + port, String)
assertTrue('Wrong body:\n' + body, body.contains('{"styles"'))
}
@Test @Test
void webIsAdded() { void webIsAdded() {
String body = new TestRestTemplate().getForObject('http://localhost:' + port + '/pom.xml?packaging=war', String) String body = new TestRestTemplate().getForObject('http://localhost:' + port + '/pom.xml?packaging=war', String)
@@ -29,7 +41,7 @@ class IntegrationTests {
@Test @Test
void homeHasWebStyle() { void homeHasWebStyle() {
String body = new TestRestTemplate().getForObject('http://localhost:' + port, String) String body = home()
assertTrue('Wrong body:\n' + body, body.contains('name="style" value="web"')) assertTrue('Wrong body:\n' + body, body.contains('name="style" value="web"'))
} }