From ae71a445138a0adb5c7ecb479fd68e5bddeaf5c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E5=A4=A9=E5=90=8C?= Date: Fri, 16 Jul 2021 16:52:16 +0800 Subject: [PATCH 1/2] fix new problem in windows --- sea/cmds.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/sea/cmds.py b/sea/cmds.py index 0d76a9f..e1c9ea6 100644 --- a/sea/cmds.py +++ b/sea/cmds.py @@ -81,22 +81,25 @@ def new(project, **extra): PACKAGE_DIR = os.path.dirname(__file__) TMPLPATH = os.path.join(PACKAGE_DIR, 'template') IGNORED_FILES = { - 'git': ['gitignore'], - 'peewee': ['configs/default/peewee.py.tmpl', - 'app/models.py.tmpl'], - 'cache': ['configs/default/cache.py.tmpl'], + 'git': [['gitignore']], + 'peewee': [['configs', 'default', 'peewee.py.tmpl'], + ['app', 'models.py.tmpl']], + 'cache': [['configs', 'default', 'cache.py.tmpl']], 'sentry': [], - 'async_task': ['configs/default/async_task.py.tmpl', - 'app/tasks.py.tmpl'], - 'bus': ['configs/default/bus.py.tmpl', 'app/buses.py.tmpl'], + 'async_task': [['configs', 'default', 'async_task.py.tmpl'], + ['app', 'async_tasks.py.tmpl']], + 'bus': [['configs', 'default', 'bus.py.tmpl'], ['app', 'buses.py.tmpl']], } def _build_skip_files(extra): skipped = set() for ignore_key in IGNORED_FILES.keys(): if extra[('skip_' + ignore_key)]: - for f in IGNORED_FILES[ignore_key]: - skipped.add(os.path.join(TMPLPATH, f)) + for fp in IGNORED_FILES[ignore_key]: + f = TMPLPATH + for c in fp: + f = os.path.join(f, c) + skipped.add(f) return skipped def _gen_project(path, skip=set(), ctx={}): @@ -114,7 +117,8 @@ def _gen_project(path, skip=set(), ctx={}): r, ext = os.path.splitext(dst) if ext == '.tmpl': with open(r, 'w') as f: - tmpl = env.get_template(relfn) + # jinja2 always expect forward slashes here + tmpl = env.get_template(relfn.replace(os.path.sep, '/')) f.write(tmpl.render(**ctx)) else: shutil.copyfile(src, dst) From 7ca983d153062481d36fbb7a255d6c866544d444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E5=A4=A9=E5=90=8C?= Date: Fri, 16 Jul 2021 17:11:31 +0800 Subject: [PATCH 2/2] refactor, topo fix --- docs/quickstart.md | 2 +- docs/structure.md | 2 +- sea/cmds.py | 33 ++++++++++++++++++--------------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/docs/quickstart.md b/docs/quickstart.md index 8aa1ccb..2896ef9 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -5,7 +5,7 @@ 让我们来创建第一个项目 helloworld: ``` -$ sea new --skip-peewee --skip-cache --skip-async_task --skip-bus --skip-sentry helloworld +$ sea new --skip-peewee --skip-cache --skip-async-task --skip-bus --skip-sentry helloworld ``` 这条命令会自动生成以下目录及文件: diff --git a/docs/structure.md b/docs/structure.md index 1a3d915..741c0d0 100644 --- a/docs/structure.md +++ b/docs/structure.md @@ -51,7 +51,7 @@ > `extensions.py`: 声明了项目需要的 "扩展",`sea` 支持通过 "扩展" 的方式来集成第三方的库,例如 orm, cache 等等。 -每个扩展往往包括一个“扩展类”和需要的相应的配置,在 `extensions.py`中实力化这个“扩展类”,并在项目中设置好这些配置,就可以使用了。 +每个扩展往往包括一个“扩展类”和需要的相应的配置,在 `extensions.py`中实例化这个“扩展类”,并在项目中设置好这些配置,就可以使用了。 以 [`cache`](https://github.com/shanbay/cachext) 扩展为例: diff --git a/sea/cmds.py b/sea/cmds.py index e1c9ea6..dbd74ea 100644 --- a/sea/cmds.py +++ b/sea/cmds.py @@ -102,28 +102,31 @@ def _build_skip_files(extra): skipped.add(f) return skipped - def _gen_project(path, skip=set(), ctx={}): + def _gen_project_file(path, tmpl_file, env, ctx): import shutil + relfn = os.path.relpath(tmpl_file, TMPLPATH) + dst = os.path.join(path, relfn) + # create the parentdir if not exists + os.makedirs(os.path.dirname(dst), exist_ok=True) + r, ext = os.path.splitext(dst) + if ext == '.tmpl': + with open(r, 'w') as f: + # jinja2 always expect forward slashes here + tmpl = env.get_template(relfn.replace(os.path.sep, '/')) + f.write(tmpl.render(**ctx)) + else: + shutil.copyfile(tmpl_file, dst) + + print('created: {}'.format(dst)) + + def _gen_project(path, skip=set(), ctx={}): from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader(TMPLPATH)) for dirpath, dirnames, filenames in os.walk(TMPLPATH): for fn in filenames: src = os.path.join(dirpath, fn) if src not in skip: - relfn = os.path.relpath(src, TMPLPATH) - dst = os.path.join(path, relfn) - # create the parentdir if not exists - os.makedirs(os.path.dirname(dst), exist_ok=True) - r, ext = os.path.splitext(dst) - if ext == '.tmpl': - with open(r, 'w') as f: - # jinja2 always expect forward slashes here - tmpl = env.get_template(relfn.replace(os.path.sep, '/')) - f.write(tmpl.render(**ctx)) - else: - shutil.copyfile(src, dst) - - print('created: {}'.format(dst)) + _gen_project_file(path, src, env, ctx) path = os.path.join(os.getcwd(), project) if os.path.exists(path):