-
Notifications
You must be signed in to change notification settings - Fork 54
feat(init): 重构初始化流程以确保项目结构完整并支持交互式配置 #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,28 +3,30 @@ | |
| import getpass | ||
| import re | ||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| import click | ||
| import yaml | ||
|
|
||
| from ..utils.colors import success, warning, info | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.option("--dir", "target_dir", default=".", help="目标目录") | ||
| def init(target_dir: str): | ||
| """初始化 NcatBot 项目(创建 config.yaml + plugins/ + 模板插件)""" | ||
| def ensure_project_initialized(target_dir: str = ".") -> Optional[Path]: | ||
| """确保项目基础结构存在,必要时执行交互式初始化。 | ||
|
|
||
| 返回创建/确认后的 config.yaml 路径;若用户取消初始化则返回 None。 | ||
| """ | ||
| target = Path(target_dir).resolve() | ||
| config_path = target / "config.yaml" | ||
| plugins_path = target / "plugins" | ||
|
|
||
| if config_path.exists(): | ||
| click.echo(warning(f"config.yaml 已存在: {config_path}")) | ||
| if not click.confirm("是否覆盖?"): | ||
| click.echo(info("已跳过 config.yaml")) | ||
| _ensure_plugins_dir(plugins_path) | ||
| _generate_template_plugin(plugins_path) | ||
| return | ||
| _ensure_plugins_dir(plugins_path) | ||
| _generate_template_plugin(plugins_path) | ||
| return config_path | ||
|
|
||
| click.echo(warning(f"未检测到 config.yaml: {config_path}")) | ||
| click.echo(info("将执行初始化流程。")) | ||
|
|
||
| bot_uin = click.prompt("请输入机器人 QQ 号", type=str) | ||
| root = click.prompt("请输入管理员 QQ 号", type=str) | ||
|
|
@@ -44,6 +46,40 @@ def init(target_dir: str): | |
| click.echo(success(f"config.yaml 已创建: {config_path}")) | ||
| _ensure_plugins_dir(plugins_path) | ||
| _generate_template_plugin(plugins_path) | ||
| return config_path | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.option("--dir", "target_dir", default=".", help="目标目录") | ||
| def init(target_dir: str): | ||
| """初始化 NcatBot 项目(创建 config.yaml + plugins/ + 模板插件)""" | ||
| target = Path(target_dir).resolve() | ||
| config_path = target / "config.yaml" | ||
|
|
||
| if config_path.exists(): | ||
| click.echo(warning(f"config.yaml 已存在: {config_path}")) | ||
| if click.confirm("是否覆盖?"): | ||
| bot_uin = click.prompt("请输入机器人 QQ 号", type=str) | ||
| root = click.prompt("请输入管理员 QQ 号", type=str) | ||
| config_data = _build_default_config(bot_uin=bot_uin, root=root) | ||
| with open(config_path, "w", encoding="utf-8") as f: | ||
| yaml.dump( | ||
| config_data, | ||
| f, | ||
| allow_unicode=True, | ||
| default_flow_style=False, | ||
| sort_keys=False, | ||
| ) | ||
| click.echo(success(f"config.yaml 已覆盖: {config_path}")) | ||
| _ensure_plugins_dir(target / "plugins") | ||
| _generate_template_plugin(target / "plugins") | ||
| click.echo() | ||
|
Comment on lines
+59
to
+76
|
||
| click.echo(info("下一步: 运行 'ncatbot run' 启动机器人")) | ||
| return | ||
|
|
||
| click.echo(info("已跳过 config.yaml")) | ||
|
|
||
| ensure_project_initialized(target_dir=target_dir) | ||
| click.echo() | ||
| click.echo(info("下一步: 运行 'ncatbot run' 启动机器人")) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| import click | ||
|
|
||
| from .init import ensure_project_initialized | ||
|
|
||
|
|
||
| @click.command() | ||
| @click.option("--debug", is_flag=True, help="启用调试模式") | ||
|
|
@@ -11,6 +13,7 @@ def run(debug: bool, no_hot_reload: bool, plugin_dir: str): | |
| """启动 NcatBot(连接 NapCat + 加载插件 + 监听事件)""" | ||
| from ncatbot.app import BotClient | ||
|
|
||
| ensure_project_initialized(".") | ||
| bot = BotClient(debug=debug, plugin_dir=plugin_dir) | ||
| bot.run() | ||
|
Comment on lines
+16
to
18
|
||
|
|
||
|
|
@@ -21,5 +24,6 @@ def dev(plugin_dir: str): | |
| """以开发模式启动(debug=True + 热重载)""" | ||
| from ncatbot.app import BotClient | ||
|
|
||
| ensure_project_initialized(".") | ||
| bot = BotClient(debug=True, plugin_dir=plugin_dir) | ||
| bot.run() | ||
|
Comment on lines
+27
to
29
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ensure_project_initialized在 config 已存在时也会无条件生成模板插件(_generate_template_plugin)。这会导致ncatbot run/dev仅仅启动机器人也可能在工作目录写入新插件文件,属于比较意外的副作用。建议将“确保 plugins/ 存在”和“生成模板插件”拆分,或仅在显式init命令中生成模板插件。