Skip to content

Commit 70daf1b

Browse files
committed
💥 merge config new into init and new
1 parent faf314c commit 70daf1b

File tree

17 files changed

+225
-448
lines changed

17 files changed

+225
-448
lines changed

src/entari_cli/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import importlib
22
import pkgutil
33

4+
# import traceback
45
from clilte import CommandLine
56
from colorama.ansi import Fore
67

@@ -22,6 +23,7 @@
2223

2324
def printer(exc: Exception) -> None:
2425
print(f"{Fore.RED}[Error: {exc.__class__.__name__}]{Fore.RESET}: {exc!s}")
26+
# traceback.print_exception(type(exc), exc, exc.__traceback__)
2527

2628

2729
cli.exception_printer = printer
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
@register("entari_cli.plugins")
99
class ConfigPath(BasePlugin):
1010
def init(self):
11-
return Option("-c|--config", Args["path/", str], help_text=i18n_.commands.config.path(), dest="cfg_path"), True
11+
return Option("-c|--config", Args["path/", str], help_text=i18n_.commands.config_path(), dest="cfg_path"), True
1212

1313
def meta(self) -> PluginMetadata:
1414
return PluginMetadata(
1515
name="cfg_path",
16-
description=i18n_.commands.config.path(),
16+
description=i18n_.commands.config_path(),
1717
version="0.1.0",
1818
)
1919

src/entari_cli/commands/config/__init__.py

Lines changed: 0 additions & 102 deletions
This file was deleted.

src/entari_cli/commands/init.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
from clilte import BasePlugin, CommandLine, PluginMetadata, register
66
from clilte.core import Next
77
from colorama import Fore
8+
import tomlkit
89

910
from entari_cli import i18n_
11+
from entari_cli.config import create_config
1012
from entari_cli.project import ensure_python, install_dependencies
1113
from entari_cli.py_info import PythonInfo, check_package_installed, get_package_version
14+
from entari_cli.setting import set_item
1215
from entari_cli.template import WORKSPACE_PROJECT_TEMPLATE
1316
from entari_cli.utils import ask
1417
from entari_cli.venv import get_venv_like_prefix
@@ -19,7 +22,7 @@ class InitEnv(BasePlugin):
1922
def init(self):
2023
return Alconna(
2124
"init",
22-
Option("-d|--develop", help_text=i18n_.commands.init.options.develop()),
25+
Option("-d|--dev", help_text=i18n_.commands.init.options.develop()),
2326
Option("-py|--python", Args["path/", str], help_text=i18n_.commands.init.options.python()),
2427
Option(
2528
"--install-args",
@@ -43,7 +46,7 @@ def dispatch(self, result: Arparma, next_: Next):
4346
if result.find("init"):
4447
python = result.query[str]("init.python.path", "")
4548
args = result.query[tuple[str, ...]]("init.install.params", ())
46-
is_dev = result.find("init.develop")
49+
is_dev = result.find("init.dev")
4750
extra = ["yaml", "cron"]
4851
if is_dev:
4952
extra += ["reload", "dotenv"]
@@ -54,8 +57,20 @@ def dispatch(self, result: Arparma, next_: Next):
5457
use_venv = ans in {"yes", "true", "t", "1", "y", "yea", "yeah", "yep", "sure", "ok", "okay", "", "y/n"}
5558
if use_venv:
5659
python_path = str(ensure_python(Path.cwd(), python).executable)
60+
print(f"{Fore.GREEN}{i18n_.commands.init.messages.success()}{Fore.RESET}")
61+
toml_file = Path.cwd() / "pyproject.toml"
62+
if not toml_file.exists():
63+
info = PythonInfo.from_path(python_path)
64+
with toml_file.open("w", encoding="utf-8") as f:
65+
f.write(
66+
WORKSPACE_PROJECT_TEMPLATE.format(
67+
extra=extras,
68+
entari_version="0.15.1",
69+
python_requirement=f'">= {info.major}.{info.minor}"',
70+
)
71+
)
5772
if check_package_installed("arclet.entari", python_path):
58-
return f"{Fore.YELLOW}{i18n_.commands.init.messages.initialized()}{Fore.RESET}"
73+
print(f"{Fore.YELLOW}{i18n_.commands.init.messages.initialized()}{Fore.RESET}")
5974
else:
6075
ret_code = install_dependencies(
6176
CommandLine.current().get_plugin(SelfSetting), # type: ignore
@@ -65,16 +80,14 @@ def dispatch(self, result: Arparma, next_: Next):
6580
)
6681
if ret_code != 0:
6782
return
68-
toml_file = Path.cwd() / "pyproject.toml"
69-
if not toml_file.exists():
70-
info = PythonInfo.from_path(python_path)
71-
with toml_file.open("w", encoding="utf-8") as f:
72-
f.write(
73-
WORKSPACE_PROJECT_TEMPLATE.format(
74-
extra=extras,
75-
entari_version=get_package_version("arclet.entari", python_path) or "0.15.0",
76-
python_requirement=f">= {info.major}.{info.minor}",
77-
)
78-
)
79-
return f"{Fore.GREEN}{i18n_.commands.init.messages.success()}{Fore.RESET}"
83+
with toml_file.open("a+", encoding="utf-8") as f:
84+
f.seek(0)
85+
proj = tomlkit.load(f)
86+
entari_version = get_package_version("arclet.entari", python_path) or "0.15.1"
87+
set_item(proj, "project.dependencies", [f"arclet.entari[{extras}] >= {entari_version}"])
88+
f.truncate(0)
89+
tomlkit.dump(proj, f)
90+
with create_config(result.query[str]("cfg_path.path"), is_dev):
91+
pass
92+
return
8093
return next_(None)

src/entari_cli/commands/new.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
from colorama import Fore
88

99
from entari_cli import i18n_
10-
from entari_cli.config import EntariConfig
10+
from entari_cli.config import create_config
1111
from entari_cli.consts import NO, YES
1212
from entari_cli.project import (
1313
PYTHON_VERSION,
1414
ensure_python,
15+
get_project_root,
1516
get_user_email_from_git,
1617
install_dependencies,
1718
sanitize_project_name,
@@ -64,10 +65,13 @@ def dispatch(self, result: Arparma, next_: Next):
6465
is_application = result.find("new.application")
6566
python = result.query[str]("new.python.path", "")
6667
entari_version = "0.15.0"
68+
toml_path = Path.cwd() / "pyproject.toml"
6769
if not is_application:
6870
ans = ask(i18n_.commands.new.prompts.is_plugin_project(), "Y/n").strip().lower()
6971
is_application = ans in NO
7072
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}"
7175
args = result.query[tuple[str, ...]]("new.install.params", ())
7276
python_path = sys.executable
7377
if get_venv_like_prefix(sys.executable)[0] is None:
@@ -139,39 +143,36 @@ def dispatch(self, result: Arparma, next_: Next):
139143
)
140144
)
141145
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}"}}',
155156
)
157+
)
156158
readme_path = Path.cwd() / "README.md"
157159
if not readme_path.exists():
158160
with readme_path.open("w+", encoding="utf-8") as f:
159161
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")
176177
return f"{Fore.GREEN}{i18n_.commands.new.messages.created(path=str(path))}{Fore.RESET}"
177178
return next_(None)

src/entari_cli/commands/run.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class RunApplication(BasePlugin):
1515
def init(self):
1616
return Alconna(
1717
"run",
18-
Option("-py|--python", Args["path/", str], help_text=i18n_.commands.run.options.python()),
1918
meta=CommandMeta(i18n_.commands.run.description()),
2019
)
2120

0 commit comments

Comments
 (0)