|
7 | 7 | from colorama import Fore |
8 | 8 |
|
9 | 9 | from entari_cli import i18n_ |
10 | | -from entari_cli.config import EntariConfig |
| 10 | +from entari_cli.config import create_config |
11 | 11 | from entari_cli.consts import NO, YES |
12 | 12 | from entari_cli.project import ( |
13 | 13 | PYTHON_VERSION, |
14 | 14 | ensure_python, |
| 15 | + get_project_root, |
15 | 16 | get_user_email_from_git, |
16 | 17 | install_dependencies, |
17 | 18 | sanitize_project_name, |
@@ -64,10 +65,13 @@ def dispatch(self, result: Arparma, next_: Next): |
64 | 65 | is_application = result.find("new.application") |
65 | 66 | python = result.query[str]("new.python.path", "") |
66 | 67 | entari_version = "0.15.0" |
| 68 | + toml_path = Path.cwd() / "pyproject.toml" |
67 | 69 | if not is_application: |
68 | 70 | ans = ask(i18n_.commands.new.prompts.is_plugin_project(), "Y/n").strip().lower() |
69 | 71 | is_application = ans in NO |
70 | 72 | if not is_application: |
| 73 | + if toml_path.exists() or get_project_root().resolve() != Path.cwd().resolve(): |
| 74 | + return f"{Fore.RED}{i18n_.commands.new.messages.proj_exists()}{Fore.RESET}" |
71 | 75 | args = result.query[tuple[str, ...]]("new.install.params", ()) |
72 | 76 | python_path = sys.executable |
73 | 77 | if get_venv_like_prefix(sys.executable)[0] is None: |
@@ -139,39 +143,36 @@ def dispatch(self, result: Arparma, next_: Next): |
139 | 143 | ) |
140 | 144 | ) |
141 | 145 | if not is_application: |
142 | | - toml_path = Path.cwd() / "pyproject.toml" |
143 | | - if not toml_path.exists(): |
144 | | - with toml_path.open("w+", encoding="utf-8") as f: |
145 | | - f.write( |
146 | | - PLUGIN_PROJECT_TEMPLATE.format( |
147 | | - name=proj_name, |
148 | | - version=version, |
149 | | - description=description, |
150 | | - author=f'{{"name" = "{author}", "email" = "{email}"}}', |
151 | | - entari_version=entari_version, |
152 | | - python_requirement=f'"{python_requires}"', |
153 | | - license=f'{{"text" = "{licence}"}}', |
154 | | - ) |
| 146 | + with toml_path.open("w+", encoding="utf-8") as f: |
| 147 | + f.write( |
| 148 | + PLUGIN_PROJECT_TEMPLATE.format( |
| 149 | + name=proj_name, |
| 150 | + version=version, |
| 151 | + description=description, |
| 152 | + author=f'{{"name" = "{author}", "email" = "{email}"}}', |
| 153 | + entari_version=entari_version, |
| 154 | + python_requirement=f'"{python_requires}"', |
| 155 | + license=f'{{"text" = "{licence}"}}', |
155 | 156 | ) |
| 157 | + ) |
156 | 158 | readme_path = Path.cwd() / "README.md" |
157 | 159 | if not readme_path.exists(): |
158 | 160 | with readme_path.open("w+", encoding="utf-8") as f: |
159 | 161 | f.write(README_TEMPLATE.format(name=proj_name, description=description)) |
160 | | - cfg = EntariConfig.load(result.query[str]("cfg_path.path", None)) |
161 | | - if ( |
162 | | - file_name in cfg.plugin |
163 | | - or f"entari_plugin_{file_name}" in cfg.plugin |
164 | | - or file_name.removeprefix("entari_plugin_") in cfg.plugin |
165 | | - ): |
166 | | - return f"{Fore.RED}{i18n_.commands.new.messages.exists(name=file_name)}{Fore.RESET}" |
167 | | - cfg.plugin[file_name] = {} |
168 | | - if result.find("new.disabled"): |
169 | | - cfg.plugin[file_name]["$disable"] = True |
170 | | - if result.find("new.optional"): |
171 | | - cfg.plugin[file_name]["$optional"] = True |
172 | | - if result.find("new.priority"): |
173 | | - cfg.plugin[file_name]["priority"] = result.query[int]("new.priority.num", 16) |
174 | | - cfg.basic.setdefault("external_dirs", []).append("plugins" if is_application else "src") |
175 | | - cfg.save() |
| 162 | + with create_config(result.query[str]("cfg_path.path"), True) as cfg: |
| 163 | + if ( |
| 164 | + file_name in cfg.plugin |
| 165 | + or f"entari_plugin_{file_name}" in cfg.plugin |
| 166 | + or file_name.removeprefix("entari_plugin_") in cfg.plugin |
| 167 | + ): |
| 168 | + return f"{Fore.RED}{i18n_.commands.new.messages.exists(name=file_name)}{Fore.RESET}" |
| 169 | + cfg.plugin[file_name] = {} |
| 170 | + if result.find("new.disabled"): |
| 171 | + cfg.plugin[file_name]["$disable"] = True |
| 172 | + if result.find("new.optional"): |
| 173 | + cfg.plugin[file_name]["$optional"] = True |
| 174 | + if result.find("new.priority"): |
| 175 | + cfg.plugin[file_name]["priority"] = result.query[int]("new.priority.num", 16) |
| 176 | + cfg.basic.setdefault("external_dirs", []).append("plugins" if is_application else "src") |
176 | 177 | return f"{Fore.GREEN}{i18n_.commands.new.messages.created(path=str(path))}{Fore.RESET}" |
177 | 178 | return next_(None) |
0 commit comments