2013-08-04 13:50:06 +01:00
|
|
|
package app
|
|
|
|
|
2013-11-01 14:39:56 +00:00
|
|
|
@Grab("spring-boot-starter-actuator")
|
2014-05-28 19:25:35 +01:00
|
|
|
@Grab("org.codehaus.groovy:groovy-ant:2.3.2")
|
2013-06-06 08:40:10 +01:00
|
|
|
|
2014-05-28 19:25:35 +01:00
|
|
|
@Configuration
|
|
|
|
@EnableConfigurationProperties(Projects)
|
2013-06-06 08:40:10 +01:00
|
|
|
@Controller
|
|
|
|
@Log
|
|
|
|
class MainController {
|
|
|
|
|
|
|
|
@Value('${info.home:http://localhost:8080/}')
|
|
|
|
private String home
|
|
|
|
|
2014-04-11 09:30:31 -07:00
|
|
|
@Value('${info.spring-boot.version:1.0.1.RELEASE}')
|
2014-04-01 17:22:18 +01:00
|
|
|
private String bootVersion
|
|
|
|
|
2013-06-06 08:40:10 +01:00
|
|
|
@Value('${TMPDIR:.}')
|
|
|
|
private String tmpdir
|
|
|
|
|
2013-08-04 13:50:06 +01:00
|
|
|
@Autowired
|
|
|
|
private Reactor reactor
|
|
|
|
|
2014-05-28 19:25:35 +01:00
|
|
|
@Autowired
|
|
|
|
private Projects projects
|
|
|
|
|
2013-06-06 08:40:10 +01:00
|
|
|
@RequestMapping("/")
|
|
|
|
@ResponseBody
|
|
|
|
String home() {
|
|
|
|
def model = [:]
|
2014-05-28 19:25:35 +01:00
|
|
|
// sort lists
|
|
|
|
model["styles"] = projects.styles.sort { it.name }
|
|
|
|
model["types"] = projects.types.sort { it.name }
|
2013-06-06 08:40:10 +01:00
|
|
|
template "home.html", model
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping("/spring")
|
|
|
|
@ResponseBody
|
|
|
|
ResponseEntity<byte[]> spring() {
|
|
|
|
File download = new File(tmpdir, "spring.zip")
|
|
|
|
if (!download.exists()) {
|
|
|
|
log.info("Creating: " + download)
|
|
|
|
new AntBuilder().zip(destfile: download) {
|
|
|
|
zipfileset(dir:".", includes:"spring/bin/**", filemode:"775")
|
|
|
|
zipfileset(dir:".", includes:"spring/**", excludes:"spring/bin/**")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info("Downloading: " + download)
|
|
|
|
new ResponseEntity<byte[]>(download.bytes, ["Content-Type":"application/zip"] as HttpHeaders, HttpStatus.OK)
|
|
|
|
}
|
|
|
|
|
2014-04-01 19:46:00 +01:00
|
|
|
@RequestMapping(value="/starter.tgz", produces="application/x-compress")
|
2013-07-22 14:40:50 +01:00
|
|
|
@ResponseBody
|
2014-04-01 19:46:00 +01:00
|
|
|
ResponseEntity<byte[]> springTgz(PomRequest request) {
|
|
|
|
|
|
|
|
File dir = File.createTempFile("tmp","",new File(tmpdir));
|
|
|
|
def tempFiles = getProjectFiles(dir, request)
|
|
|
|
|
|
|
|
File download = new File(tmpdir, dir.name + ".tgz")
|
|
|
|
log.info("Creating: " + download)
|
|
|
|
tempFiles << download
|
|
|
|
|
|
|
|
new AntBuilder().tar(destfile: download, compression: "gzip") {
|
|
|
|
zipfileset(dir:dir, includes:"**")
|
|
|
|
}
|
|
|
|
log.info("Downloading: " + download)
|
|
|
|
def result = new ResponseEntity<byte[]>(download.bytes, ["Content-Type":"application/x-compress"] as HttpHeaders, HttpStatus.OK)
|
|
|
|
|
|
|
|
reactor.notify("tempfiles", Event.wrap(tempFiles))
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping("/starter.zip")
|
|
|
|
@ResponseBody
|
|
|
|
ResponseEntity<byte[]> springZip(PomRequest request) {
|
|
|
|
|
|
|
|
File dir = File.createTempFile("tmp","",new File(tmpdir));
|
|
|
|
def tempFiles = getProjectFiles(dir, request)
|
|
|
|
|
|
|
|
File download = new File(tmpdir, dir.name + ".zip")
|
|
|
|
log.info("Creating: " + download)
|
|
|
|
tempFiles << download
|
|
|
|
|
|
|
|
new AntBuilder().zip(destfile: download) {
|
|
|
|
zipfileset(dir:dir, includes:"**")
|
|
|
|
}
|
|
|
|
log.info("Downloading: " + download)
|
|
|
|
def result = new ResponseEntity<byte[]>(download.bytes, ["Content-Type":"application/zip"] as HttpHeaders, HttpStatus.OK)
|
|
|
|
|
|
|
|
reactor.notify("tempfiles", Event.wrap(tempFiles))
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
def getProjectFiles(File dir, PomRequest request) {
|
2013-07-22 14:40:50 +01:00
|
|
|
|
2013-08-04 13:50:06 +01:00
|
|
|
def tempFiles = []
|
|
|
|
|
2013-07-22 14:40:50 +01:00
|
|
|
def model = [:]
|
2013-08-04 13:50:06 +01:00
|
|
|
tempFiles << dir
|
2013-07-22 14:40:50 +01:00
|
|
|
dir.delete()
|
|
|
|
dir.mkdirs()
|
2014-04-01 19:46:00 +01:00
|
|
|
|
|
|
|
String pom = new String(pom(request, model).body)
|
2013-07-22 14:40:50 +01:00
|
|
|
new File(dir, "pom.xml").write(pom)
|
|
|
|
|
2014-04-01 19:46:00 +01:00
|
|
|
String gradle = new String(gradle(request, model).body)
|
|
|
|
new File(dir, "build.gradle").write(gradle)
|
|
|
|
|
2013-07-22 14:40:50 +01:00
|
|
|
File src = new File(new File(dir, "src/main/java"),request.packageName.replace(".", "/"))
|
|
|
|
src.mkdirs()
|
2013-11-01 14:39:56 +00:00
|
|
|
write(src, "Application.java", model)
|
2013-07-22 14:40:50 +01:00
|
|
|
|
2013-11-01 14:39:56 +00:00
|
|
|
File test = new File(new File(dir, "src/test/java"),request.packageName.replace(".", "/"))
|
|
|
|
test.mkdirs()
|
2014-03-18 10:21:31 +00:00
|
|
|
if (model.styles.contains("-web")) {
|
|
|
|
model.testAnnotations = "@WebAppConfiguration\n"
|
|
|
|
model.testImports = "import org.springframework.test.context.web.WebAppConfiguration;\n"
|
|
|
|
} else {
|
|
|
|
model.testAnnotations = ""
|
|
|
|
model.testImports = ""
|
|
|
|
}
|
2013-11-01 14:39:56 +00:00
|
|
|
write(test, "ApplicationTests.java", model)
|
|
|
|
|
2014-04-01 08:57:06 +01:00
|
|
|
File resources = new File(dir, "src/main/resources")
|
|
|
|
resources.mkdirs()
|
|
|
|
new File(resources, "application.properties").write("")
|
|
|
|
|
2014-04-01 19:46:00 +01:00
|
|
|
tempFiles
|
2013-07-22 14:40:50 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:39:56 +00:00
|
|
|
def write(File src, String name, def model) {
|
|
|
|
log.info("Creating: " + src + "/" + name)
|
2014-03-18 10:21:31 +00:00
|
|
|
def body = template name, model
|
2013-11-01 14:39:56 +00:00
|
|
|
new File(src, name).write(body)
|
|
|
|
}
|
|
|
|
|
2013-06-06 08:40:10 +01:00
|
|
|
@RequestMapping("/pom")
|
|
|
|
@ResponseBody
|
2013-07-22 14:40:50 +01:00
|
|
|
ResponseEntity<byte[]> pom(PomRequest request, Map model) {
|
2014-04-01 17:22:18 +01:00
|
|
|
model.bootVersion = bootVersion
|
2014-01-21 15:34:41 +00:00
|
|
|
new ResponseEntity<byte[]>(render("starter-pom.xml", request, model), ["Content-Type":"application/octet-stream"] as HttpHeaders, HttpStatus.OK)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping("/build")
|
|
|
|
@ResponseBody
|
|
|
|
ResponseEntity<byte[]> gradle(PomRequest request, Map model) {
|
2014-04-01 17:22:18 +01:00
|
|
|
model.bootVersion = bootVersion
|
2014-01-21 15:34:41 +00:00
|
|
|
new ResponseEntity<byte[]>(render("starter-build.gradle", request, model), ["Content-Type":"application/octet-stream"] as HttpHeaders, HttpStatus.OK)
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] render(String path, PomRequest request, Map model) {
|
2013-06-06 08:40:10 +01:00
|
|
|
|
|
|
|
def style = request.style
|
|
|
|
log.info("Styles requested: " + style)
|
|
|
|
|
2013-11-01 14:39:56 +00:00
|
|
|
def type = request.type
|
|
|
|
log.info("Type requested: " + type)
|
|
|
|
|
2013-06-06 08:40:10 +01:00
|
|
|
model.groupId = request.groupId
|
|
|
|
model.artifactId = request.artifactId
|
|
|
|
model.version = request.version
|
|
|
|
model.name = request.name
|
|
|
|
model.description = request.description
|
|
|
|
model.packageName = request.packageName
|
|
|
|
|
|
|
|
if (style==null || style.size()==0) {
|
|
|
|
style = [""]
|
|
|
|
}
|
2013-08-19 17:21:45 +01:00
|
|
|
if (!style.class.isArray() && !(style instanceof Collection)) {
|
|
|
|
style = [style]
|
|
|
|
}
|
2013-11-01 14:39:56 +00:00
|
|
|
style = style.collect{ it=="jpa" ? "data-jpa" : it }
|
2013-07-22 14:40:50 +01:00
|
|
|
model["styles"] = style.collect{ it=="" ? "" : "-" + it }
|
2013-06-06 08:40:10 +01:00
|
|
|
|
|
|
|
log.info("Model: " + model)
|
|
|
|
|
2014-01-21 15:34:41 +00:00
|
|
|
def body = template path, model
|
|
|
|
body
|
2013-06-06 08:40:10 +01:00
|
|
|
}
|
|
|
|
|
2013-06-06 12:19:15 +01:00
|
|
|
}
|
|
|
|
|
2013-08-04 13:50:06 +01:00
|
|
|
@Configuration
|
2014-05-28 17:26:53 +01:00
|
|
|
@EnableReactor
|
2013-08-04 13:50:06 +01:00
|
|
|
class ReactorConfiguration {
|
|
|
|
|
|
|
|
@Bean
|
2014-05-28 17:26:53 +01:00
|
|
|
public Reactor rootReactor(reactor.core.Environment reactorEnvironment) {
|
|
|
|
return reactorEnvironment.getRootReactor();
|
2013-08-04 13:50:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-28 19:25:35 +01:00
|
|
|
@Consumer
|
2013-08-04 13:50:06 +01:00
|
|
|
@Log
|
|
|
|
class TemporaryFileCleaner {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
Reactor reactor
|
|
|
|
|
2014-05-28 19:25:35 +01:00
|
|
|
@Selector('tempfiles')
|
|
|
|
void clean(def tempFiles) {
|
|
|
|
log.info "Tempfiles: " + tempFiles
|
|
|
|
if (tempFiles) {
|
|
|
|
tempFiles.each {
|
|
|
|
File file = it as File
|
|
|
|
if (file.directory) {
|
|
|
|
file.deleteDir()
|
|
|
|
} else {
|
|
|
|
file.delete()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-04 13:50:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-06 08:40:10 +01:00
|
|
|
class PomRequest {
|
|
|
|
def style = []
|
|
|
|
|
|
|
|
String name = "demo"
|
2013-11-01 14:39:56 +00:00
|
|
|
String type = "starter"
|
2013-08-04 13:50:06 +01:00
|
|
|
String description = "Demo project for Spring Boot"
|
2013-06-06 08:40:10 +01:00
|
|
|
String groupId = "org.test"
|
|
|
|
String artifactId
|
2013-11-01 14:39:56 +00:00
|
|
|
String version = "0.0.1-SNAPSHOT"
|
2013-06-06 08:40:10 +01:00
|
|
|
String packageName
|
2013-08-04 13:50:06 +01:00
|
|
|
String getArtifactId() {
|
2013-06-06 08:40:10 +01:00
|
|
|
artifactId == null ? name : artifactId
|
|
|
|
}
|
|
|
|
String getPackageName() {
|
2013-08-16 17:39:01 +01:00
|
|
|
packageName == null ? name.replace('-', '.') : packageName
|
2013-06-06 08:40:10 +01:00
|
|
|
}
|
2013-08-16 17:39:01 +01:00
|
|
|
}
|
2014-05-28 19:25:35 +01:00
|
|
|
|
|
|
|
@ConfigurationProperties(prefix='projects', ignoreUnknownFields=false)
|
|
|
|
class Projects {
|
|
|
|
List<Map<String,Object>> styles
|
|
|
|
List<Map<String,Object>> types
|
|
|
|
}
|