Commit f85d5e0a authored by liaozan's avatar liaozan 🏀

init

parent 4fb7f5a6
...@@ -16,6 +16,8 @@ public class WebConfiguration implements WebMvcConfigurer { ...@@ -16,6 +16,8 @@ public class WebConfiguration implements WebMvcConfigurer {
registry.addMapping("/**") registry.addMapping("/**")
.allowedOrigins("*") .allowedOrigins("*")
.allowedMethods("*") .allowedMethods("*")
.allowedHeaders("*")
.exposedHeaders("*")
.maxAge(3600); .maxAge(3600);
} }
......
...@@ -2,11 +2,21 @@ package com.schbrain.archetype.initializer.backend.controller; ...@@ -2,11 +2,21 @@ package com.schbrain.archetype.initializer.backend.controller;
import com.schbrain.archetype.initializer.backend.maven.MavenUtils; import com.schbrain.archetype.initializer.backend.maven.MavenUtils;
import com.schbrain.archetype.initializer.backend.param.ArchetypeGenerateParam; import com.schbrain.archetype.initializer.backend.param.ArchetypeGenerateParam;
import com.schbrain.common.util.ServletUtils;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/** /**
* @author liaozan * @author liaozan
* @since 2022/3/20 * @since 2022/3/20
...@@ -15,8 +25,24 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -15,8 +25,24 @@ import org.springframework.web.bind.annotation.RestController;
public class InitializerController { public class InitializerController {
@PostMapping("/archetype/generate") @PostMapping("/archetype/generate")
public void generateArchetype(@RequestBody @Validated ArchetypeGenerateParam param) { public void generateArchetype(@RequestBody @Validated ArchetypeGenerateParam param) throws IOException {
MavenUtils.generate(param.getGroup(), param.getArtifact()); File generatedProject = MavenUtils.generate(param);
FileInputStream inputStream = new FileInputStream(generatedProject);
HttpServletResponse response = ServletUtils.getResponse();
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, contentDisposition(generatedProject));
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
StreamUtils.copy(inputStream, response.getOutputStream());
}
private String contentDisposition(File file) {
return ContentDisposition
.attachment()
.filename(file.getName())
.build()
.toString();
} }
} }
\ No newline at end of file
package com.schbrain.archetype.initializer.backend.maven; package com.schbrain.archetype.initializer.backend.maven;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.schbrain.archetype.initializer.backend.param.ArchetypeGenerateParam;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.maven.cli.MavenCli; import org.apache.maven.cli.MavenCli;
...@@ -13,18 +15,26 @@ import java.io.File; ...@@ -13,18 +15,26 @@ import java.io.File;
@Slf4j @Slf4j
public class MavenUtils { public class MavenUtils {
public static void generate(String groupId, String artifactId) { public static File generate(ArchetypeGenerateParam param) {
MavenCli cli = new MavenCli(); MavenCli cli = new MavenCli();
System.getProperties().setProperty("maven.multiModuleProjectDirectory", "$M2_HOME"); System.getProperties().setProperty("maven.multiModuleProjectDirectory", "$M2_HOME");
String tmpDirPath = FileUtil.getTmpDirPath(); File archetype = getArchetypeDirectory(param.getArtifact());
File archetype = new File(tmpDirPath, artifactId); String[] args = getArgs(param, archetype.getAbsolutePath());
String[] args = getArgs(groupId, artifactId, archetype.getAbsolutePath());
cli.doMain(args, null, System.out, System.err); cli.doMain(args, null, System.out, System.err);
log.info("Generate archetype project at {}", archetype.getAbsolutePath()); log.info("Generate archetype project at {}", archetype.getAbsolutePath());
return ZipUtil.zip(archetype);
}
private static File getArchetypeDirectory(String artifactId) {
String tmpDirPath = FileUtil.getTmpDirPath();
File archetype = new File(tmpDirPath, artifactId);
// clean cache
FileUtil.del(archetype);
return archetype;
} }
@SuppressWarnings("SpellCheckingInspection") @SuppressWarnings("SpellCheckingInspection")
private static String[] getArgs(String groupId, String artifactId, String outputDirectory) { private static String[] getArgs(ArchetypeGenerateParam param, String outputDirectory) {
return new String[]{ return new String[]{
"-B", "-B",
"archetype:generate", "archetype:generate",
...@@ -32,8 +42,10 @@ public class MavenUtils { ...@@ -32,8 +42,10 @@ public class MavenUtils {
"-DarchetypeArtifactId=schbrain-archetype", "-DarchetypeArtifactId=schbrain-archetype",
"-DarchetypeVersion=1.0-SNAPSHOT", "-DarchetypeVersion=1.0-SNAPSHOT",
String.format("-DoutputDirectory=%s", outputDirectory), String.format("-DoutputDirectory=%s", outputDirectory),
String.format("-DgroupId=%s", groupId), String.format("-DgroupId=%s", param.getGroup()),
String.format("-DartifactId=%S", artifactId) String.format("-DartifactId=%s", param.getArtifact()),
String.format("-DpackageName=%s", param.getPackageName()),
String.format("-DprojectName=%s", param.getProjectName()),
}; };
} }
......
...@@ -38,11 +38,11 @@ export default { ...@@ -38,11 +38,11 @@ export default {
data() { data() {
return { return {
generateForm: { generateForm: {
projectName: 'schbrain-alarm', projectName: '',
gitlabUrl: 'http://gitlab.schbrain.com/gitlab/tools/schbrain-archetype.git', gitlabUrl: '',
group: 'com.schbrain.kp', group: '',
artifact: 'schbrain-alarm', artifact: '',
packageName: 'com.schbrain.kp.alarm' packageName: ''
} }
} }
}, },
...@@ -51,16 +51,15 @@ export default { ...@@ -51,16 +51,15 @@ export default {
axios.post('/archetype/generate', this.generateForm, { axios.post('/archetype/generate', this.generateForm, {
responseType: 'blob' responseType: 'blob'
}).then(res => { }).then(res => {
let fileName = window.decodeURI(res.headers['Content-Disposition'].split('=')[1]) const {headers, data} = res
let blob = new Blob([res.data]) const fileName = headers['content-disposition'].replace(/\w+;\sfilename="(.*)"/, '$1')
const blob = new Blob([data])
let link = document.createElement('a'); const link = document.createElement('a');
link.style.display = "none"; link.style.display = "none";
link.href = window.URL.createObjectURL(blob); link.href = window.URL.createObjectURL(blob);
link.download = fileName; link.download = fileName;
document.body.appendChild(link); document.body.appendChild(link);
link.click(); link.click();
//释放内存
window.URL.revokeObjectURL(link.href); window.URL.revokeObjectURL(link.href);
document.body.removeChild(link); document.body.removeChild(link);
}) })
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment