Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lwydyby/generator-cli

spring boot 项目命令行生成代码工具(顺便学习Go)
https://github.com/lwydyby/generator-cli

Last synced: about 2 months ago
JSON representation

spring boot 项目命令行生成代码工具(顺便学习Go)

Awesome Lists containing this project

README

        

# LCli
[![Go Report Card](https://goreportcard.com/badge/github.com/lwydyby/generator-cli)](https://goreportcard.com/report/github.com/lwydyby/generator-cli)

spring boot + vue项目代码生成命令行工具

## 简介

``` bash
$ genratorv1 --help
A code generator for spring boot program.

For example:
$ genratorv1 -f ./data.json
-t ./templates \
-o ./output

Usage:
genratorv1 [flags]

Flags:
-f, --file string setting文件及data文件所在位置

-o, --output string 输出到的文件夹.
-t, --template string 模板文件所在文件夹
-v, --version 版本信息
```

## 安装及使用

项目根目录存在mac上编译好的二进制文件(genratorv1) 其他系统请下载源码自行安装

### 源码安装步骤

``` bash
$ git clone [email protected]:lwydyby/generator-cli.git
$ glide install
$ go install
```

### 说明

1. setting.yaml

配置了模板名称和相应的输出文件名

```yaml
template:
format: Go
template_files:
web:
file_path: web.tmpl
output_file_naming: index.vue
dao:
file_path: dao.tmpl
output_file_naming: userRepository.java
api:
file_path: api.tmpl
output_file_naming: user.js
service:
file_path: service.tmpl
output_file_naming: UserService.java
controller:
file_path: controller.tmpl
output_file_naming: UserController.java
entity:
file_path: entity.tmpl
output_file_naming: User.java
search:
file_path: search.tmpl
output_file_naming: UserSearch.java

```

2. data.yaml

模板中所依赖的数据

```yaml
data:
format: mongo
# 提供搜索功能的字段
search_column:
- prop: "name"
label: "名称"
type: "input"
- prop: "type"
label: "类型"
type: "select"
#创建时所需的字段
create_column:
- prop: "name"
type: "String"
label: "名称"
#列表展示的字段
table_column:
- prop: "name"
label: "名称"
type: "String"
#数据库中的名称
column_name: "name"
#除列表展示外数据库要保存的字段
extra_column:
- prop: "createDate"
type: "Date"
label: "创建日期"
column_name: "create_date"
#按钮及表单名
button_name: "用户"
#生成的英文名
name: "User"
package: "com.example"

```

3. *.tmpl

下面展示了生成vue的模板,其余模板请参照templates/example

```javascript
{{$Data := .Data}}
{{$pre:= "{{"}}
{{$end:= "}}"}}




{{ range $Data.SearchColumn }}

{{if eq .Type "input"}}

{{else if eq .Type "select"}}



{{end}}

{{- end }}


查询{{$Data.ButtonName}}




创建{{$Data.ButtonName}}






{{ range $Data.TableColumn }}


{{ $pre }}scope.row.{{.Prop}}{{$end}}


{{- end }}



编辑{{$Data.ButtonName}}
删除{{$Data.ButtonName}}









{{ range $Data.CreateColumn }}

{{if eq .Type "input"}}

{{else if eq .Type "select"}}



{{end}}

{{- end }}



import {findPage, create{{$Data.Name}}, modify{{$Data.Name}}, delete{{$Data.Name}}} from '@/api/{{$Data.Name}}'
export default {
name: '{{$Data.Name}}',
data() {
return {
page: {
current: 1,
size: 100,
total: 0
},
temp: {
id: null,
{{ range $Data.CreateColumn }}
{{.Prop}}:null,
{{- end}}

},
list: null,
total: 0,
listLoading: true,
listQuery: {
page: 1,
limit: 5,
{{ range $Data.SearchColumn }}
{{.Prop}}:null,
{{- end}}
},
dialogFormVisible: false,
editFlag: 0,
rules: {
{{ range $Data.CreateColumn }}
{{.Prop}}: [
{required: true, message: '请输入{{.Label}}', trigger: 'blur'}
],
{{- end}}

}
}
},
created() {
this.getList()
},
methods: {
handleSizeChange(val) {
this.page.size = val;
this.getList();
},
handleCurrentChange(val) {
this.page.current = val;
this.getList();
},
handleFilter() {
this.listQuery.page = 1
this.getList()
},
save{{$Data.Name}}(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
if (this.editFlag === 0) {
create{{$Data.Name}}(
this.temp
).then(response => {
this.$message({
message: '保存成功',
type: 'success'
});
this.getList();
this.reset{{$Data.Name}}Form('ruleForm')
})
} else {
modify{{$Data.Name}}(
this.temp
).then(response => {
this.$message({
message: '保存成功',
type: 'success'
});
this.getList();
this.reset{{$Data.Name}}Form('ruleForm')
})

}

} else {
this.$message.error('保存失败,请重试');
return false;
}
});
},
reset{{$Data.Name}}Form(formName) {
this.$refs[formName].resetFields();
this.dialogFormVisible = false;
},
getList() {
this.listLoading = true
this.listQuery.page=this.page.current
this.listQuery.limit=this.page.size
findPage(this.listQuery).then(response => {
this.list = response.content;
this.page.total = response.totalElements;
this.listLoading = false
})
},
create{{$Data.Name}}() {
this.resetTemp()
this.dialogFormVisible = true;
this.editFlag = 0;
},
edit{{$Data.Name}}(item) {
this.resetTemp()
this.temp.id = item.id;
this.temp$Data.Name = item$Data.Name;
this.temp.rpId=item.rpId
this.dialogFormVisible = true;
this.editFlag = 1;
},

resetTemp() {
this.temp = {
id: null,
name: null,
rpId:null
}
},
delete{{$Data.Name}}(row) {
this.$confirm('此操作将永久删除该{{$Data.ButtonName}}, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
delete{{$Data.Name}}(row.id).then(response => {
this.getList();
this.$message({
type: 'success',
message: '删除成功!'
});
})

}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
}

}

}

```

4. 使用实例

```bash
./generatorv1 -f ./templates/example/ -o ./templates/example/test/ -t ./templates/example/

```