Commit f3599e6e authored by liaozan's avatar liaozan 🏀

feat: support init git repo

parent a145ed1b
......@@ -2,6 +2,7 @@ package com.schbrain.archetype.initializer.maven;
import cn.hutool.core.io.FileUtil;
import com.schbrain.archetype.initializer.param.ArchetypeGenerateParam;
import com.schbrain.common.exception.BaseException;
import com.schbrain.common.util.IdWorker;
import com.schbrain.common.util.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
......@@ -10,9 +11,13 @@ import org.apache.maven.settings.Mirror;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.io.DefaultSettingsWriter;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import static org.apache.maven.cli.MavenCli.MULTIMODULE_PROJECT_DIRECTORY;
......@@ -47,6 +52,9 @@ public class MavenUtils {
String[] args = getArchetypeGenerateArgs(param, getSettingsFile().getAbsolutePath(), outputDirectory);
mavenCli.doMain(args, null, System.out, System.err);
log.info("Generate archetype project at {}", outputDirectory);
if (param.isInitGitRepo()) {
initGitRepo(Paths.get(archetype.getAbsolutePath(), param.getArtifactId()));
}
return id;
}
......@@ -55,6 +63,18 @@ public class MavenUtils {
return new File(tmpDirPath, id);
}
private static void initGitRepo(Path directory) {
try {
Git git = Git.init().setDirectory(directory.toFile()).call();
git.add().addFilepattern(".").call();
git.commit().setAllowEmpty(true).setAuthor("initializer", "no-reply@schbrain.com").setMessage("Initial Commit").call();
git.close();
} catch (GitAPIException e) {
log.warn("Git repo init failed", e);
throw new BaseException("Git仓库初始化失败");
}
}
private static File getSettingsFile() {
if (SETTINGS_FILE.exists()) {
return SETTINGS_FILE;
......
......@@ -36,6 +36,10 @@ public class ArchetypeGenerateParam {
* 子模块前缀
*/
private String subModuleNamePrefix;
/**
* 是否初始化 git 仓库
*/
private boolean initGitRepo;
public String getSubModuleNamePrefix() {
if (!StringUtils.hasText(this.subModuleNamePrefix)) {
......
package com.schbrain.archetype.initializer.service;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ZipUtil;
import com.schbrain.archetype.initializer.maven.MavenUtils;
......@@ -13,17 +14,17 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
......@@ -33,7 +34,7 @@ import java.util.stream.Collectors;
@Service
public class ArchetypeService {
private final Map<String, String> archetypeNameCache = new ConcurrentHashMap<>();
private final TimedCache<String, String> archetypeNameCache = new TimedCache<>(Duration.ofDays(1).toMillis());
@Autowired
private ArchetypePreparer archetypePreparer;
......@@ -89,6 +90,9 @@ public class ArchetypeService {
throw new FileNotFoundException(archetypeDirectory.getAbsolutePath());
}
String artifactId = archetypeNameCache.get(id);
if (!StringUtils.hasText(artifactId)) {
throw new FileNotFoundException("文件已过期,请重新生成");
}
return new File(archetypeDirectory, artifactId);
}
......
......@@ -6,7 +6,7 @@
"build": "vite build --mode production"
},
"dependencies": {
"vue": "^3.2.31",
"vue": "^3.2.32",
"axios": "^0.26.1",
"ant-design-vue": "^3.1.1",
"highlight.js": "^11.5.1",
......@@ -15,7 +15,7 @@
"devDependencies": {
"@vitejs/plugin-vue": "^2.3.1",
"typescript": "^4.6.3",
"vite": "^2.9.1",
"vue-tsc": "^0.34.5"
"vite": "^2.9.2",
"vue-tsc": "^0.34.6"
}
}
\ No newline at end of file
......@@ -15,6 +15,9 @@
<a-form-item has-feedback label="PackageName" name="packageName">
<a-input v-model:value="formState.packageName" placeholder="默认为GroupId"/>
</a-form-item>
<a-form-item has-feedback name="initGitRepo">
<a-checkbox v-model:checked="formState.initGitRepo">初始化 Git 仓库</a-checkbox>
</a-form-item>
</a-form>
<div class="button-group">
<a-button class="generic-btn" type="primary" @click="onGenerate">生成</a-button>
......@@ -46,7 +49,8 @@ interface FormState {
groupId: string,
artifactId: string,
version: string,
packageName: string
packageName: string,
initGitRepo: boolean
}
interface FileNode {
......@@ -70,7 +74,8 @@ const formState = reactive<FormState>({
groupId: '',
artifactId: '',
version: '1.0.0-SNAPSHOT',
packageName: ''
packageName: '',
initGitRepo: true
})
const formRules = {
......
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