Skip to content

Commit

Permalink
🐛 Fix: 修复 cookiecutter 2.2.0 渲染项目模板出错 (#89)
Browse files Browse the repository at this point in the history
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 8, 2023
1 parent ec7c1b9 commit eec7fc9
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 103 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ ipython_config.py
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
pdm.toml
.pdm-python

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
Expand Down
9 changes: 7 additions & 2 deletions nb_cli/cli/commands/project.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import sys
import json
from pathlib import Path
from logging import Logger
from functools import partial
Expand Down Expand Up @@ -94,7 +95,9 @@ async def prompt_common_context(context: ProjectContext) -> ProjectContext:
validator=bool,
error_message=_("Chosen drivers is not valid!"),
).prompt_async(style=CLI_DEFAULT_STYLE)
context.variables["drivers"] = [d.data.dict() for d in drivers]
context.variables["drivers"] = json.dumps(
{d.data.project_link: d.data.dict() for d in drivers}
)
context.packages.extend(
[d.data.project_link for d in drivers if d.data.project_link]
)
Expand All @@ -118,7 +121,9 @@ async def prompt_common_context(context: ProjectContext) -> ProjectContext:
).prompt_async(style=CLI_DEFAULT_STYLE)
)

context.variables["adapters"] = [a.data.dict() for a in adapters]
context.variables["adapters"] = json.dumps(
{a.data.project_link: a.data.dict() for a in adapters}
)
context.packages.extend([a.data.project_link for a in adapters])

return context
Expand Down
9 changes: 9 additions & 0 deletions nb_cli/extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import json

from jinja2.ext import Extension


class UnJsonifyExtension(Extension):
def __init__(self, environment):
super().__init__(environment)
environment.filters["unjsonify"] = json.loads
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% if cookiecutter.sub_plugin == "True" %}from pathlib import Path
{% if cookiecutter.sub_plugin %}from pathlib import Path

import nonebot
{% endif %}from nonebot import get_driver
Expand All @@ -7,7 +7,7 @@ from .config import Config

global_config = get_driver().config
config = Config.parse_obj(global_config)
{% if cookiecutter.sub_plugin == "True" %}
{% if cookiecutter.sub_plugin %}
sub_plugins = nonebot.load_plugins(
str(Path(__file__).parent.joinpath("plugins").resolve())
)
Expand Down
7 changes: 4 additions & 3 deletions nb_cli/template/project/bootstrap/cookiecutter.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"nonebot": {
"project_name": "AwesomeBot",
"drivers": [],
"adapters": []
"drivers": "",
"adapters": ""
},
"computed": {
"project_slug": "{{ cookiecutter.nonebot.project_name|replace(' ', '-') }}",
"project_desc": "{{ cookiecutter.nonebot.project_name }}"
}
},
"_extensions": ["nb_cli.extensions.UnJsonifyExtension"]
}
7 changes: 7 additions & 0 deletions nb_cli/template/project/bootstrap/hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
{{ cookiecutter.nonebot.update({ "drivers": cookiecutter.nonebot.drivers|unjsonify }) }}
{{ cookiecutter.nonebot.update({ "adapters": cookiecutter.nonebot.adapters|unjsonify }) }}
"""
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{% if cookiecutter.nonebot.drivers -%}DRIVER={{ cookiecutter.nonebot.drivers|map(attribute="module_name")|join("+") }}
{% if cookiecutter.nonebot.drivers -%}DRIVER={{ cookiecutter.nonebot.drivers.values()|map(attribute="module_name")|join("+") }}
{%- endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ requires-python = ">=3.8, <4.0"

[tool.nonebot]
{% set adapters = [] -%}
{% for adapter in cookiecutter.nonebot.adapters -%}
{% for adapter in cookiecutter.nonebot.adapters.values() -%}
{% set name = adapter.name -%}
{% set module_name = adapter.module_name -%}
{% set adapter_str = '{ name = "%s", module_name = "%s" }'|format(name, module_name) -%}
Expand Down
9 changes: 5 additions & 4 deletions nb_cli/template/project/simple/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
"py": "py",
"nonebot": {
"project_name": "AwesomeBot",
"drivers": [],
"adapters": [],
"drivers": "",
"adapters": "",
"use_src": false
},
"computed": {
"project_slug": "{{ cookiecutter.nonebot.project_name|replace(' ', '-') }}",
"project_desc": "{{ cookiecutter.nonebot.project_name }}"
},
"custom": {
"source_dir": "{{ 'src' if cookiecutter.nonebot.use_src == 'True' else cookiecutter.computed.project_slug|lower|replace('-', '_') }}"
}
"source_dir": "{{ 'src' if cookiecutter.nonebot.use_src else cookiecutter.computed.project_slug|lower|replace('-', '_') }}"
},
"_extensions": ["nb_cli.extensions.UnJsonifyExtension"]
}
7 changes: 7 additions & 0 deletions nb_cli/template/project/simple/hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
{{ cookiecutter.nonebot.update({ "drivers": cookiecutter.nonebot.drivers|unjsonify }) }}
{{ cookiecutter.nonebot.update({ "adapters": cookiecutter.nonebot.adapters|unjsonify }) }}
"""
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ENVIRONMENT=dev
{% if cookiecutter.nonebot.drivers -%}DRIVER={{ cookiecutter.nonebot.drivers|map(attribute="module_name")|join("+") }}
{% if cookiecutter.nonebot.drivers -%}DRIVER={{ cookiecutter.nonebot.drivers.values()|map(attribute="module_name")|join("+") }}
{%- endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ requires-python = ">=3.8, <4.0"

[tool.nonebot]
{% set adapters = [] -%}
{% for adapter in cookiecutter.nonebot.adapters -%}
{% for adapter in cookiecutter.nonebot.adapters.values() -%}
{% set name = adapter.name -%}
{% set module_name = adapter.module_name -%}
{% set adapter_str = '{ name = "%s", module_name = "%s" }'|format(name, module_name) -%}
Expand Down
Loading

0 comments on commit eec7fc9

Please sign in to comment.