Skip to content

Commit

Permalink
Merge branch 'feature/generate-java-code' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuSugar committed Jul 27, 2021
2 parents bec0a0e + 5d060bd commit ce07cf4
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Randomly generated data tool
## 程序界面
### web界面
![主界面](https://cdn.jsdelivr.net/gh/mikusugar/PictureBed@master/uPic/2021/06/2W0rwN.png)
![主界面](docs/assets/web.png)
### CLI界面
![CLI](docs/assets/cli_new.gif)
## 程序文档
Expand Down
Binary file added docs/assets/cli_code.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/web_code.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions docs/usage_cli/generate_code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 生成代码

> 生成造数的Java Code,理论上其他JVM语言也可以用
⚠️目前依赖在GitHub package,并没有在中央仓库。[github 仓库使用参考](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry)

## 使用

执行`code`命令即可。


如下图
![](../assets/cli_code.gif)

## 注意事项

+ **请手动修改 依赖版本为你使用的版本**

+ **使用方式参考 生成代码的main方法**

19 changes: 19 additions & 0 deletions docs/usage_web/generate_code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 生成代码

> 生成造数的Java Code,理论上其他JVM语言也可以用
⚠️目前依赖在GitHub package,并没有在中央仓库。[github 仓库使用参考](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry)

## 使用
直接点击`生成Java代码`按钮即可。

如下图:

![](../assets/web_code.gif)

## 注意事项

+ **请手动修改 依赖版本为你使用的版本**

+ **使用方式参考 生成代码的main方法**

2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ nav:
- WEB使用:
- 快速使用: usage_web/index.md
- 配置: usage_web/config.md
- 生成代码: usage_web/generate_code.md
- CLI使用:
- 快速使用: usage_cli/quick_cli.md
- 新版命令: usage_cli/quick_cli_new.md
- 生成代码: usage_cli/generate_code.md
- 开发:
- 简介: develop/index.md
- 新的随机类型: develop/newtype.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import me.mikusugar.random.core.constant.ServiceNameValues;
import me.mikusugar.random.core.service.AbstractRandomService;
import me.mikusugar.random.core.service.ConfigSavaRepository;
import me.mikusugar.random.core.utils.GenerateCodeUtil;
import me.mikusugar.random.core.utils.SugarJsonNodeSerialization;
import me.mikusugar.random.core.utils.SugarJsonUtils;
import me.mikusugar.randomsugar.app.utils.NotionUtils;
Expand Down Expand Up @@ -78,6 +79,11 @@ public class SugarRandomView extends HorizontalLayout {
private TextArea resPreTextArea;
private Button resPreButton;

//生成代码
private Dialog generateCodeDialog;
private TextArea generateCodeArea;
private Button generateCodeButton;

@Autowired
private Map<String, AbstractRandomService> randomServiceMap;

Expand Down Expand Up @@ -220,6 +226,15 @@ private boolean checkNext() {
resPreTextArea.setValue(prettyFormatJson);
});

generateCodeButton.addClickListener(e->{
try {
generateCodeArea.setValue(GenerateCodeUtil.getCode(rootNode));
} catch (Exception exception) {
exception.printStackTrace();
NotionUtils.defaultNotion(exception.toString());
}
});

}


Expand Down Expand Up @@ -292,7 +307,7 @@ private void initView() {
number.setLabel("生成条数");
start = new Button("开始");
area = new TextArea("参数配置说明");
area.setWidthFull();
area.setWidth("60%");
area.setReadOnly(true);
StreamResource href = new StreamResource("download.json", this::getInputStream);
href.setCacheTime(0);
Expand All @@ -301,17 +316,16 @@ private void initView() {
download.add(start);
resPreDialog = new Dialog();
resPreTextArea = new TextArea("结果:");
resPreTextArea.setWidthFull();
resPreTextArea.setHeight("90%");
resPreDialog.add(resPreTextArea);
final Button closePreButton = new Button("关闭", e -> resPreDialog.close());
closePreButton.setWidthFull();
resPreDialog.add(closePreButton);
resPreDialog.setHeight("80%");
resPreDialog.setWidth("80%");
helpDialogAndTextArea(resPreTextArea, resPreDialog);
resPreButton = new Button("预览", e -> resPreDialog.open());
startLayout.add(area, resPreButton, number, download);
startLayout.setVerticalComponentAlignment(Alignment.END, area, resPreButton, number, download);

generateCodeDialog = new Dialog();
generateCodeArea = new TextArea("Code");
helpDialogAndTextArea(generateCodeArea, generateCodeDialog);
generateCodeButton = new Button("生成Java代码", e -> generateCodeDialog.open());

startLayout.add(area, resPreButton, number, download, generateCodeButton);
startLayout.setVerticalComponentAlignment(Alignment.END, area, resPreButton, number, download, generateCodeButton);
add(startLayout);

addClassName("sugar-random-view");
Expand All @@ -327,10 +341,22 @@ private void initView() {
flushTree();
}


///////////////////////////////////////////////////////////////////////////
// 工具方法
///////////////////////////////////////////////////////////////////////////

private void helpDialogAndTextArea(TextArea textArea, Dialog dialog) {
textArea.setWidthFull();
textArea.setHeight("90%");
dialog.add(textArea);
final Button closePreButton = new Button("关闭", e -> dialog.close());
closePreButton.setWidthFull();
dialog.add(closePreButton);
dialog.setHeight("80%");
dialog.setWidth("80%");
}

private InputStream getInputStream() {
val res = new StringBuilder();
int size = number.getValue().intValue();
Expand Down Expand Up @@ -376,7 +402,7 @@ private void dfsAddMap(SugarJsonNode node) {

private HorizontalLayout createHorizontalLayout() {
HorizontalLayout res = new HorizontalLayout();
res.setWidthFull();
res.setWidth("98%");
return res;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import me.mikusugar.random.core.service.AbstractRandomService;
import me.mikusugar.random.core.service.ConfigSavaRepository;
import me.mikusugar.random.core.service.RandomCoreService;
import me.mikusugar.random.core.utils.GenerateCodeUtil;
import me.mikusugar.random.core.utils.SugarJsonNodeSerialization;
import me.mikusugar.random.core.utils.SugarJsonUtils;
import me.mikusugar.sugar.random.cli.utils.CliUtils;
Expand Down Expand Up @@ -76,8 +77,7 @@ public String showAllR() {
StringBuilder res = new StringBuilder();
randomServiceMap.forEach(
(k, v) ->
res.append("随机类型:")
.append(k)
res.append(k)
.append("\t 描述:")
.append(v.helpText().replace(System.lineSeparator(), " "))
.append(System.lineSeparator())
Expand Down Expand Up @@ -204,6 +204,11 @@ public String outFile(int num, String path) throws IOException {
return "文件已经生成";
}

@ShellMethod(value = "生成Java代码",group = "random")
public String code() throws JsonProcessingException {
return GenerateCodeUtil.getCode(rootNode);
}

///////////////////////////////////////////////////////////////////////////
// group config
///////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 5 additions & 0 deletions sugar_random_core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<artifactId>jackson-annotations</artifactId>
<version>2.11.4</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.12</version>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.mikusugar.random.core.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import me.mikusugar.random.core.bean.SugarJsonNode;
import me.mikusugar.random.core.utils.GetAllService;
import me.mikusugar.random.core.utils.SugarJsonNodeSerialization;
import me.mikusugar.random.core.utils.SugarJsonUtils;

import java.util.Map;

/**
* 给生成的造数代码调研
*
* @author mikusugar
*/
public class GenerateCodeService {
private SugarJsonNode sugarJsonNode;

private Map<String, AbstractRandomService> randomServiceMap;

public GenerateCodeService(String configJson)
throws InstantiationException, IllegalAccessException, JsonProcessingException {

this.randomServiceMap = GetAllService.getAllService();
this.sugarJsonNode = SugarJsonNodeSerialization.read(configJson, randomServiceMap);
}

/**
* @return 生成一条随机json
*/
public String getJson(){
final StringBuilder res = new StringBuilder();
SugarJsonUtils.toJsonStr(null,sugarJsonNode,res);
return res.toString();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package me.mikusugar.random.core.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import me.mikusugar.random.core.bean.SugarJsonNode;

/**
* @author mikusugar
*/
public class GenerateCodeUtil {

public static String getCode(SugarJsonNode sugarJsonNode) throws JsonProcessingException {
final String json = SugarJsonNodeSerialization.write(sugarJsonNode);

return "import com.fasterxml.jackson.core.JsonProcessingException;\n" +
"import me.mikusugar.random.core.service.GenerateCodeService;\n" +
"\n" +
"/**\n" +
" * 请添加以下依赖\n" +
" * <dependency>\n" +
" * <groupId>me.mikusugar.random</groupId>\n" +
" * <artifactId>sugar_random_core</artifactId>\n" +
" * <version>xxx</version>\n" +
" * </dependency>\n" +
" *\n" +
" * @author mikusugar\n" +
" */\n" +
"public class SugarRandom {\n" +
"\n" +
" public static void main(String[] args) throws JsonProcessingException, InstantiationException, IllegalAccessException {\n" +
" System.out.println(new SugarRandom().next());\n" +
" }\n" +
"\n" +
" private final GenerateCodeService generateCodeService;\n" +
"\n" +
" public SugarRandom() throws JsonProcessingException, InstantiationException, IllegalAccessException {\n" +
" String json=\""+helpJson(json)+"\";\n" +
" this.generateCodeService = new GenerateCodeService(json);\n" +
" }\n" +
"\n" +
" public String next() {\n" +
" return this.generateCodeService.getJson();\n" +
" }\n" +
"\n" +
"}\n";
}

private static String helpJson(String json) {
final StringBuilder res = new StringBuilder();
for (char c:json.toCharArray()){
if(c=='"'){
res.append("\\");
}
res.append(c);
}
return res.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package me.mikusugar.random.core.utils;

import me.mikusugar.random.core.constant.ServiceName;
import me.mikusugar.random.core.service.AbstractRandomService;
import me.mikusugar.random.core.service.RandomCoreService;
import org.reflections.Reflections;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* 得到me.mikusugar.random.core.service 包下的所有实例
*
* @author mikusugar
*/
@SuppressWarnings("ALL")
public class GetAllService {

public final static String SERVICE_PACKAGE = "me.mikusugar.random.core.service.impl";


//TEST
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
final Map<String, AbstractRandomService> allService = getAllService();
final AbstractRandomService abstractRandomService = allService.get(ServiceName.UUID);
final RandomCoreService randomCoreService = abstractRandomService.createRandomCoreService("");
int x=10;
while (x-->0) System.out.println(randomCoreService.getRandomUtilInterface().next());
}


/**
* 获取service实例
* 也许有更好的实现
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static Map<String, AbstractRandomService> getAllService() throws InstantiationException, IllegalAccessException {
final Reflections reflections = new Reflections(SERVICE_PACKAGE);
final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Service.class);
final Map<String, AbstractRandomService> res = new HashMap<>();
for (Class<?> clz : classes) {
final String name = clz.getAnnotationsByType(Service.class)[0].value();
final AbstractRandomService randomService = (AbstractRandomService) clz.newInstance();
res.put(name, randomService);
}
return res;
}

}
31 changes: 31 additions & 0 deletions sugar_random_core/src/test/java/SugarRandom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import me.mikusugar.random.core.service.GenerateCodeService;
//生成代码的示例
/**
* 请添加以下依赖
* <dependency>
* <groupId>me.mikusugar.random</groupId>
* <artifactId>sugar_random_core</artifactId>
* <version>xxx</version>
* </dependency>
*
* @author mikusugar
*/
public class SugarRandom {

public static void main(String[] args) throws JsonProcessingException, InstantiationException, IllegalAccessException {
System.out.println(new SugarRandom().next());
}

private final GenerateCodeService generateCodeService;

public SugarRandom() throws JsonProcessingException, InstantiationException, IllegalAccessException {
String json = "{\"name\":\"root\",\"type\":\"OBJECT\",\"desc\":\"默认根节点\",\"randomServiceName\":\"随机对象\",\"input\":\"\",\"nexts\":[{\"name\":\"name\",\"type\":\"STRING\",\"desc\":\"输入0随机生成女性名字,输入1随机生成男性名字,不输入都生成\",\"randomServiceName\":\"随机中文姓名\",\"input\":\"\",\"nexts\":[]},{\"name\":\"uuid\",\"type\":\"STRING\",\"desc\":\"java 原生的UUID,无需输入\",\"randomServiceName\":\"UUID\",\"input\":\"\",\"nexts\":[]},{\"name\":\"citys\",\"type\":\"ARRAY\",\"desc\":\"设置数组长度,输入 2 代表数组长度为2 ,输入 2,3 代表数组长度为 2到3随机\",\"randomServiceName\":\"随机数组长度\",\"input\":\"1,5\",\"nexts\":[{\"name\":\"city\",\"type\":\"STRING\",\"desc\":\"随机生成国内城市,无需输入~ (北上广等概率大)\",\"randomServiceName\":\"默认国内城市\",\"input\":\"\",\"nexts\":[]}]},{\"name\":\"other\",\"type\":\"OBJECT\",\"desc\":\"什么都不需要输入\",\"randomServiceName\":\"随机对象\",\"input\":\"\",\"nexts\":[{\"name\":\"phone\",\"type\":\"STRING\",\"desc\":\"随机生成国内手机号,无需输入\",\"randomServiceName\":\"随机中国大陆手机号\",\"input\":\"\",\"nexts\":[]},{\"name\":\"mac\",\"type\":\"STRING\",\"desc\":\"随机生成MAC地址,无需输入\",\"randomServiceName\":\"MAC\",\"input\":\"\",\"nexts\":[]}]}]}";
this.generateCodeService = new GenerateCodeService(json);
}

public String next() {
return this.generateCodeService.getJson();
}

}
5 changes: 0 additions & 5 deletions sugar_random_core/target/maven-archiver/pom.properties

This file was deleted.

0 comments on commit ce07cf4

Please sign in to comment.