diff --git a/.gitignore b/.gitignore index 997ec81335d8b059cd3a1265e62520d79a98692f..85d5a142703d8395a7c25d6aba7cdb6fedf710d5 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/README.md b/README.md index 821f3a4f70a98278a35287749766ee7ef2d542c0..170df3959b83af7b5ac0f205aec437a49aed109a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/config/base_model.json b/examples/config/base_model.json index b567319f97c5974c53a8610dc37910312fe4a6e3..50d68b61096b8e442310efca714241a63a417a73 100644 --- a/examples/config/base_model.json +++ b/examples/config/base_model.json @@ -1,10 +1,18 @@ { - "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 diff --git a/examples/generate.sh b/examples/generate.sh new file mode 100755 index 0000000000000000000000000000000000000000..af1778d754c82df633acddb169ac55986fab74b4 --- /dev/null +++ b/examples/generate.sh @@ -0,0 +1,5 @@ +pip install code-generator +code-gen render \ + --config=./config/base_model.json \ + --plugins=./plugins \ + --templates=./templates \ No newline at end of file diff --git a/examples/plugins/base.plugin.jinja2 b/examples/plugins/base.plugin.jinja2 index 075e8acb3fc1818bc118eb5787db3510bbbe044c..7af784b47a4c8a299537b2d1d4375b8148fe44ac 100644 --- a/examples/plugins/base.plugin.jinja2 +++ b/examples/plugins/base.plugin.jinja2 @@ -1,10 +1,9 @@ {%- macro number_input(column) -%} -

number_input

-

{{ column.name }}

+

number_input with {{ column.field }}

{% endmacro -%} - - {%- macro date_input(column) -%} -

date_input

-

{{ column.name }}

+

date_input with {{ column.field }}

{% endmacro -%} +{%- macro auto_complete(column) -%} +

auto_complete plugin with {{ column.field }}

+{% endmacro %} \ No newline at end of file diff --git a/examples/templates/index.html.jinja2 b/examples/templates/index.html.jinja2 deleted file mode 100644 index 346be00fef7e1f5e19eb3156bf7599b8eb8a4dbc..0000000000000000000000000000000000000000 --- a/examples/templates/index.html.jinja2 +++ /dev/null @@ -1,15 +0,0 @@ -

{{ model }}

- - \ No newline at end of file diff --git a/examples/templates/user_add.vue.jinja2 b/examples/templates/user_add.vue.jinja2 new file mode 100644 index 0000000000000000000000000000000000000000..a1226abc75c6df1db482334029fd3a8d422fa53c --- /dev/null +++ b/examples/templates/user_add.vue.jinja2 @@ -0,0 +1,80 @@ + + + \ No newline at end of file diff --git a/examples/templates/user_edit.vue.jinja2 b/examples/templates/user_edit.vue.jinja2 new file mode 100644 index 0000000000000000000000000000000000000000..93cae9400227f5452967f43740829f9cb0af0ee1 --- /dev/null +++ b/examples/templates/user_edit.vue.jinja2 @@ -0,0 +1,80 @@ + + + \ No newline at end of file diff --git a/src/code_generator/loader.py b/src/code_generator/loader.py index 1e08426708dd920e4282aafced34c9129592a99c..1d29eb1a5960312dd3c59af7f72467743d50cdfd 100644 --- a/src/code_generator/loader.py +++ b/src/code_generator/loader.py @@ -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): diff --git a/src/code_generator/main.py b/src/code_generator/main.py index d029fe1c503ee3e403e1296e7cac2b777accc30e..fad797d41a07f67ffc648f3b9b5fb694d5cb7a5d 100644 --- a/src/code_generator/main.py +++ b/src/code_generator/main.py @@ -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)