Skip to content

Commit 146e176

Browse files
committed
项目根路径获取方式改进,支持独立部署和页面指定路径
1 parent ba30976 commit 146e176

File tree

5 files changed

+88
-18
lines changed

5 files changed

+88
-18
lines changed

src/frontend/src/pages/index/table/index.vue

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<div class="content-header"></div>
44
<div class="content">
55
<div class="container">
6-
<div style="margin-bottom:10px;">
6+
<div style="margin-bottom: 10px">
77
<el-input
88
clearable
9-
style="width:300px;margin-right:20px;"
9+
style="width: 300px; margin-right: 20px"
1010
placeholder="请输入表名进行查询"
1111
prefix-icon="el-icon-search"
1212
v-model="searchKey"
@@ -21,24 +21,33 @@
2121
@selection-change="handleTableSelection"
2222
>
2323
<el-table-column type="selection" width="55"></el-table-column>
24-
<el-table-column type="index" width="50" label="序号"></el-table-column>
24+
<el-table-column
25+
type="index"
26+
width="50"
27+
label="序号"
28+
></el-table-column>
2529
<el-table-column sortable prop="name" label="table名称">
2630
<template slot-scope="scope">{{ scope.row.name }}</template>
2731
</el-table-column>
2832
<el-table-column prop="comment" label="table注释"></el-table-column>
2933
</el-table>
3034
</div>
3135
</div>
32-
<el-dialog :visible.sync="showGenSettingDialog" title="输出配置" width="70%" top="5vh">
36+
<el-dialog
37+
:visible.sync="showGenSettingDialog"
38+
title="输出配置"
39+
width="70%"
40+
top="5vh"
41+
>
3342
<el-form label-width="220px">
3443
<el-form-item label="代码作者">
35-
<el-input v-model="genSetting.author" style="width:260px"></el-input>
44+
<el-input v-model="genSetting.author" style="width: 260px"></el-input>
3645
</el-form-item>
3746
<el-form-item label="功能模块名">
3847
<el-input
3948
v-model="genSetting.moduleName"
4049
placeholder="模块名将加入到输出包名之后,用于划分不同的模块"
41-
style="width:260px"
50+
style="width: 400px"
4251
></el-input>
4352
</el-form-item>
4453
<el-form-item label="本次需生成的文件">
@@ -47,11 +56,15 @@
4756
v-for="file in userConfig.outputFiles"
4857
:label="file.fileType"
4958
:key="file.fileType"
50-
>{{ file.fileType }}</el-checkbox>
59+
>{{ file.fileType }}</el-checkbox
60+
>
5161
</el-checkbox-group>
5262
</el-form-item>
5363
<el-form-item label="需生成的Controller方法" v-if="isControllerChecked">
54-
<el-alert title="注意:如果更换了Controller的模板,以下选项可能不会生效,需自行在模板中实现" type="warning"></el-alert>
64+
<el-alert
65+
title="注意:如果更换了Controller的模板,以下选项可能不会生效,需自行在模板中实现"
66+
type="warning"
67+
></el-alert>
5568
<el-checkbox-group v-model="genSetting.choosedControllerMethods">
5669
<el-checkbox label="list" key="list">列表查询</el-checkbox>
5770
<el-checkbox label="getById" key="getById">按ID查询</el-checkbox>
@@ -63,6 +76,15 @@
6376
<el-form-item label="存在同名文件时是否覆盖">
6477
<el-switch v-model="genSetting.override"></el-switch>
6578
</el-form-item>
79+
<el-form-item label="目标项目根目录">
80+
<el-input
81+
v-model="genSetting.rootPath"
82+
style="width: 400px"
83+
></el-input>
84+
<help-tip
85+
content="最终生成的代码位置等于:目标项目根目录 + 具体某个类别的源码设置的包目录"
86+
></help-tip>
87+
</el-form-item>
6688
<el-form-item>
6789
<el-button type="primary" @click="genCode()">开始生成</el-button>
6890
</el-form-item>
@@ -92,6 +114,7 @@ export default {
92114
override: false,
93115
moduleName: "",
94116
choosedControllerMethods: [],
117+
rootPath: "",
95118
},
96119
userConfig: {},
97120
outputFileInfos: [],
@@ -154,6 +177,11 @@ export default {
154177
this.genSetting.moduleName = "";
155178
this.genSetting.override = false;
156179
}
180+
if (!this.genSetting.rootPath) {
181+
axios.get("/api/output-file-info/project-root-path").then((res) => {
182+
this.genSetting.rootPath = res;
183+
});
184+
}
157185
} catch (e) {
158186
console.error(e);
159187
}

src/main/java/com/github/davidfantasy/mybatisplus/generatorui/ProjectPathResolver.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,28 @@ public class ProjectPathResolver {
3030
public ProjectPathResolver(String basePackage) {
3131
this.basePackage = basePackage;
3232
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
33-
String projectDir = System.getProperty("user.dir");
33+
String rootPath = "";
3434
if (contextLoader.getResource(".") != null) {
35-
projectDir = contextLoader.getResource(".").getPath();
35+
String projectDir = contextLoader.getResource(".").getPath();
3636
projectDir = getUTF8String(projectDir);
3737
String[] paths = projectDir.split("/");
3838
StringBuilder temp = new StringBuilder();
39+
//去掉目录的最后两个子目录,通常是/target/classes
3940
for (int i = 0; i < paths.length; i++) {
4041
String path = paths[i];
42+
if (Strings.isNullOrEmpty(path)) {
43+
continue;
44+
}
4145
if (i < paths.length - 2) {
4246
temp.append(path);
43-
temp.append("/");
47+
temp.append(File.separator);
4448
}
4549
}
46-
baseProjectPath = temp.toString();
50+
rootPath = temp.toString();
4751
} else {
48-
projectDir = getUTF8String(projectDir).replace("\\", "/") + "/";
49-
baseProjectPath = projectDir;
52+
rootPath = getUTF8String(System.getProperty("user.dir")) + File.separator;
5053
}
51-
sourcePath = new File(baseProjectPath + "src/main/java").toString();
52-
resourcePath = new File(baseProjectPath + "src/main/resources").toString();
54+
refreshBaseProjectPath(rootPath);
5355
}
5456

5557
/**
@@ -58,7 +60,7 @@ public ProjectPathResolver(String basePackage) {
5860
* @param basePath
5961
* @return
6062
*/
61-
private String getUTF8String(String basePath) {
63+
public String getUTF8String(String basePath) {
6264
try {
6365
basePath = URLDecoder.decode(basePath, "UTF-8");
6466
} catch (UnsupportedEncodingException e) {
@@ -135,4 +137,12 @@ public String resolveMapperXmlPackage() {
135137
return PACKAGE_RESOURCES_PREFIX + "mapper";
136138
}
137139

140+
public synchronized void refreshBaseProjectPath(String rootPath) {
141+
if (baseProjectPath == null || !baseProjectPath.equals(rootPath)) {
142+
this.baseProjectPath = rootPath;
143+
sourcePath = new File(baseProjectPath + "src/main/java".replace("/", File.separator)).toString();
144+
resourcePath = new File(baseProjectPath + "src/main/resources".replace("/", File.separator)).toString();
145+
}
146+
}
147+
138148
}

src/main/java/com/github/davidfantasy/mybatisplus/generatorui/controller/OutputFileInfoController.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.davidfantasy.mybatisplus.generatorui.controller;
22

3+
import com.github.davidfantasy.mybatisplus.generatorui.ProjectPathResolver;
34
import com.github.davidfantasy.mybatisplus.generatorui.common.Result;
45
import com.github.davidfantasy.mybatisplus.generatorui.common.ResultGenerator;
56
import com.github.davidfantasy.mybatisplus.generatorui.dto.OutputFileInfo;
@@ -21,14 +22,21 @@ public class OutputFileInfoController {
2122
@Autowired
2223
private UserConfigStore userConfigStore;
2324

25+
@Autowired
26+
private ProjectPathResolver projectPathResolver;
27+
2428
@GetMapping("/user-config")
2529
public Result getUserConfig() {
2630
return ResultGenerator.genSuccessResult(userConfigStore.getDefaultUserConfig());
2731
}
2832

33+
@GetMapping("/project-root-path")
34+
public Result getRootPath() {
35+
return ResultGenerator.genSuccessResult(projectPathResolver.getBaseProjectPath());
36+
}
37+
2938
@PostMapping("/delete")
3039
public Result deleteOutputInfos(@RequestBody OutputFileInfo outputFileInfo) throws IOException {
31-
3240
outputFileInfoService.deleteOutputFileInfo(outputFileInfo);
3341
return ResultGenerator.genSuccessResult();
3442
}

src/main/java/com/github/davidfantasy/mybatisplus/generatorui/dto/GenSetting.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ public class GenSetting {
3636
*/
3737
private List<String> choosedControllerMethods;
3838

39+
/**
40+
* 目标项目根目录
41+
*/
42+
private String rootPath;
43+
3944
}

src/main/java/com/github/davidfantasy/mybatisplus/generatorui/service/MbpGeneratorService.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.github.davidfantasy.mybatisplus.generatorui.service;
22

3+
import cn.hutool.core.io.FileUtil;
34
import com.baomidou.mybatisplus.generator.AutoGenerator;
45
import com.baomidou.mybatisplus.generator.config.*;
56
import com.baomidou.mybatisplus.generator.config.po.TableField;
67
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
78
import com.github.davidfantasy.mybatisplus.generatorui.GeneratorConfig;
89
import com.github.davidfantasy.mybatisplus.generatorui.ProjectPathResolver;
10+
import com.github.davidfantasy.mybatisplus.generatorui.common.ServiceException;
911
import com.github.davidfantasy.mybatisplus.generatorui.dto.GenSetting;
1012
import com.github.davidfantasy.mybatisplus.generatorui.dto.OutputFileInfo;
1113
import com.github.davidfantasy.mybatisplus.generatorui.dto.UserConfig;
@@ -21,6 +23,7 @@
2123
import org.springframework.stereotype.Service;
2224

2325
import javax.annotation.PostConstruct;
26+
import java.io.File;
2427
import java.util.ArrayList;
2528
import java.util.List;
2629

@@ -66,6 +69,8 @@ public void initGenerator() {
6669
}
6770

6871
public void genCodeBatch(GenSetting genSetting, List<String> tables) {
72+
checkGenSetting(genSetting);
73+
projectPathResolver.refreshBaseProjectPath(genSetting.getRootPath());
6974
//自定义参数配置
7075
mpg.setCfg(new TableInjectionConfig(generatorConfig, genSetting));
7176
//生成策略配置
@@ -152,4 +157,18 @@ private StrategyConfig getCurrentStrategy(UserConfig userConfig) {
152157
}
153158

154159

160+
private void checkGenSetting(GenSetting genSetting) {
161+
if (Strings.isNullOrEmpty(genSetting.getRootPath())) {
162+
throw new ServiceException("目标项目根目录不能为空");
163+
}
164+
genSetting.setRootPath(projectPathResolver.getUTF8String(genSetting.getRootPath()));
165+
if (!FileUtil.isDirectory(genSetting.getRootPath())) {
166+
throw new ServiceException("目标项目根目录错误,请确认目录有效且存在:" + genSetting.getRootPath());
167+
}
168+
if (!genSetting.getRootPath().endsWith(File.separator)) {
169+
genSetting.setRootPath(genSetting.getRootPath() + File.separator);
170+
}
171+
}
172+
173+
155174
}

0 commit comments

Comments
 (0)