From 0c5f66b53fbc118579339a154e45df58e078534f Mon Sep 17 00:00:00 2001 From: wjmcat <1435130236@qq.com> Date: Sat, 27 Jun 2020 13:53:02 +0800 Subject: [PATCH] add examples --- .gitignore | 3 +- README.md | 21 +++++-- examples/config/base_model.json | 16 +++-- examples/generate.sh | 5 ++ examples/plugins/base.plugin.jinja2 | 11 ++-- examples/templates/index.html.jinja2 | 15 ----- examples/templates/user_add.vue.jinja2 | 80 +++++++++++++++++++++++++ examples/templates/user_edit.vue.jinja2 | 80 +++++++++++++++++++++++++ src/code_generator/loader.py | 1 + src/code_generator/main.py | 2 +- 10 files changed, 202 insertions(+), 32 deletions(-) create mode 100755 examples/generate.sh delete mode 100644 examples/templates/index.html.jinja2 create mode 100644 examples/templates/user_add.vue.jinja2 create mode 100644 examples/templates/user_edit.vue.jinja2 diff --git a/.gitignore b/.gitignore index 997ec81..85d5a14 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 821f3a4..170df39 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 b567319..50d68b6 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 0000000..af1778d --- /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 075e8ac..7af784b 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 346be00..0000000 --- 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 0000000..a1226ab --- /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 0000000..93cae94 --- /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 1e08426..1d29eb1 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 d029fe1..fad797d 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) -- GitLab