提交 0c5f66b5 编写于 作者: W wjmcat

add examples

上级 2d91eab4
......@@ -128,4 +128,5 @@ dmypy.json
# Pyre type checker
.pyre/
build.sh
.idea/
\ No newline at end of file
.idea/
./src/code_generator.egg-info/
\ No newline at end of file
......@@ -16,17 +16,28 @@
## quick start
1 . install command
1 . clone the repo
```shell script
pip install code-generator
git clone https://github.com/wj-Mcat/code-generator
```
2 . prepare `templates`, `plugins`, `config`
2 . run the example generation
you should set the prepared
```shell script
./examples/generate.sh
```
3 . [Optional] change the template to generate your own code
> you can change the code in `examples/config`, `examples/plugins`, `examples/templates`
3 . generate codes
## command description
> you don't need to write any code to run `code-generator`, only for configuration, templates, plugins
```shell script
```
## changelog
......
{
"model": "基础模型",
"model": "user",
"description": "用户管理",
"columns": [{
"name": "1"
"field": "name",
"description": "姓名",
"type": "str"
},{
"name": "22"
"field": "sex",
"description": "性别",
"type": "int"
},{
"name": "33"
"field": "role_id",
"description": "角色",
"type": "str",
"plugin": "auto_complete"
}]
}
\ No newline at end of file
pip install code-generator
code-gen render \
--config=./config/base_model.json \
--plugins=./plugins \
--templates=./templates
\ No newline at end of file
{%- macro number_input(column) -%}
<h1>number_input</h1>
<h2>{{ column.name }}</h2>
<h1>number_input with {{ column.field }}</h1>
{% endmacro -%}
{%- macro date_input(column) -%}
<h1>date_input</h1>
<h3>{{ column.name }}</h3>
<h1>date_input with {{ column.field }}</h1>
{% endmacro -%}
{%- macro auto_complete(column) -%}
<h1>auto_complete plugin with {{ column.field }}</h1>
{% endmacro %}
\ No newline at end of file
<h1>{{ model }}</h1>
<ul>
{%- for column in columns %}
<li>
{%- if column.name == "1" %}
{{ number_input(column) }}
{%- elif column.name == "22" %}
{{ date_input(column) }}
{%- else %}
<h1>ssss</h1>
{%- endif %}
</li>
{%- endfor %}
</ul>
\ No newline at end of file
<template>
<div class="container">
<el-card class="box-card margin-space" style="margin:20px;">
<el-form :model="form" label-width="120px" :rules="rules" ref="form">
{%- for column in columns -%}
{% if column.type == "str" %}
<el-form-item label="{{ column.description }}" prop="{{ column.field }}">
<el-input v-model="form.{{ column.field }}" placeholder="请输入{{ column.description }}" />
</el-form-item>
{%- elif column.type == "int" %}
<el-input-number style="width:100%;" v-model="form.{{ column.field }}"
:min="0" :max="100" label="请输入{{ column.description }}">
</el-input-number>
{% else %}
{% endif %}
{%- endfor -%}
<el-form-item>
<el-button type="primary" @click="submit">提 交</el-button>
<el-button @click="reset_form">重 置</el-button>
<el-button @click="show=false">取 消</el-button>
</el-form-item>
</el-form>
</el-card>
</div>
</template>
<script>
import { add_news } from "./api_result";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import { quillEditor } from "vue-quill-editor";
export default {
name: "add",
components: { quillEditor },
data() {
return {
loading: false,
show: false,
form: {},
rules: {
{% for column in columns %}
{{ column.field }}: [{ required: true, message: "请输入{{ column.description }}", trigger: ["blur"] }],
{% endfor %}
}
};
},
methods: {
show_dialog() {
this.show = true;
this.loading = false;
this.form = {
};
},
reset_form() {
this.form = {};
},
submit() {
this.$refs["form"].validate(valid => {
if (valid) {
this.loading = true;
let $this = this;
add_news(this.form).then(function(res) {
$this.loading = false;
if (res.code === 200) {
$this.show = false;
$this.$emit("fresh");
} else {
alert(res.message);
}
});
} else {
return false;
}
});
}
}
};
</script>
\ No newline at end of file
<template>
<div class="container">
<el-card class="box-card margin-space" style="margin:20px;">
<el-form :model="form" label-width="120px" :rules="rules" ref="form">
{%- for column in columns -%}
{% if column.type == "str" %}
<el-form-item label="{{ column.description }}" prop="{{ column.field }}">
<el-input v-model="form.{{ column.field }}" placeholder="请输入{{ column.description }}" />
</el-form-item>
{%- elif column.type == "int" %}
<el-input-number style="width:100%;" v-model="form.{{ column.field }}"
:min="0" :max="100" label="请输入{{ column.description }}">
</el-input-number>
{% else %}
{% endif %}
{%- endfor -%}
<el-form-item>
<el-button type="primary" @click="submit">提 交</el-button>
<el-button @click="reset_form">重 置</el-button>
<el-button @click="show=false">取 消</el-button>
</el-form-item>
</el-form>
</el-card>
</div>
</template>
<script>
import { add_news } from "./api_result";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import { quillEditor } from "vue-quill-editor";
export default {
name: "edit",
components: { quillEditor },
data() {
return {
loading: false,
show: false,
form: {},
rules: {
{% for column in columns %}
{{ column.field }}: [{ required: true, message: "请输入{{ column.description }}", trigger: ["blur"] }],
{% endfor %}
}
};
},
methods: {
show_dialog() {
this.show = true;
this.loading = false;
this.form = {
};
},
reset_form() {
this.form = {};
},
submit() {
this.$refs["form"].validate(valid => {
if (valid) {
this.loading = true;
let $this = this;
add_news(this.form).then(function(res) {
$this.loading = false;
if (res.code === 200) {
$this.show = false;
$this.$emit("fresh");
} else {
alert(res.message);
}
});
} else {
return false;
}
});
}
}
};
</script>
\ No newline at end of file
......@@ -39,6 +39,7 @@ class Loader(metaclass=ABCMeta):
init the plugin files path, not load the file content,
lazy load plugins
"""
log.info('load file/dir <{%s}>', file_or_dir)
self.files: Dict[str, str] = {}
log.info('load the files from : %s', file_or_dir)
if not os.path.exists(file_or_dir):
......
......@@ -62,7 +62,7 @@ def render_by_config():
log.info(plugins)
templates = TemplateLoader(args['templates']).load_templates(plugins)
for name, template in templates.items():
result = template.render(config)
result = template.render(**config)
base_name = os.path.basename(args['templates']) + '_result'
_save_to_file(base_name, name, result)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册