mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
new command new)
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
from typer import Typer
|
from typer import Typer
|
||||||
|
|
||||||
from .commands import run_handler, init_handler
|
from .commands import run_handler, init_handler, new_handler
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
app = Typer()
|
app = Typer()
|
||||||
app.command("run")(run_handler)
|
app.command("run")(run_handler)
|
||||||
app.command("init")(init_handler)
|
app.command("init")(init_handler)
|
||||||
|
app.command("new")(new_handler)
|
||||||
app()
|
app()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
from .run import run_handler as run_handler
|
from .run import run_handler as run_handler
|
||||||
from .init import init_handler as init_handler
|
from .init import init_handler as init_handler
|
||||||
|
from .new import new_handler as new_handler
|
||||||
@@ -78,7 +78,6 @@ def create_file(path: Path, content: str) -> None:
|
|||||||
if not path.exists():
|
if not path.exists():
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
path.write_text(content.strip(), encoding="utf-8")
|
path.write_text(content.strip(), encoding="utf-8")
|
||||||
print(f"Created: {path}")
|
|
||||||
else:
|
else:
|
||||||
print(f"Skipped: {path} (already exists)")
|
print(f"Skipped: {path} (already exists)")
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,115 @@
|
|||||||
|
__all__ = ["new_handler"]
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
|
|
||||||
|
GITIGNORE_CONTENT = """
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
.env
|
||||||
|
.venv/
|
||||||
|
env/
|
||||||
|
"""
|
||||||
|
|
||||||
|
FLAT_MAIN_TEMPLATE = """
|
||||||
|
from argenta import Orchestrator, App
|
||||||
|
|
||||||
|
from handlers import router
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
app = App()
|
||||||
|
app.include_router(router)
|
||||||
|
|
||||||
|
orchestrator = Orchestrator()
|
||||||
|
orchestrator.start_polling(app)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
"""
|
||||||
|
|
||||||
|
FLAT_HANDLERS_TEMPLATE = """
|
||||||
|
from argenta import Router, Response
|
||||||
|
|
||||||
|
router = Router("Hello command")
|
||||||
|
|
||||||
|
@router.command("hello")
|
||||||
|
def start_handler(response: Response):
|
||||||
|
print("Hello world!")
|
||||||
|
"""
|
||||||
|
|
||||||
|
SRC_MAIN_TEMPLATE = """
|
||||||
|
from argenta import Orchestrator, App
|
||||||
|
|
||||||
|
from .routers import router
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
app = App()
|
||||||
|
app.include_router(router)
|
||||||
|
|
||||||
|
orchestrator = Orchestrator()
|
||||||
|
orchestrator.start_polling(app)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
"""
|
||||||
|
|
||||||
|
SRC_ROUTERS_TEMPLATE = """
|
||||||
|
from argenta import Router
|
||||||
|
from .handlers.hello_world_handler import hello_handler
|
||||||
|
|
||||||
|
router = Router()
|
||||||
|
|
||||||
|
router.command('hello')(hello_handler)
|
||||||
|
"""
|
||||||
|
|
||||||
|
SRC_HANDLER_TEMPLATE = """
|
||||||
|
from argenta import Response
|
||||||
|
|
||||||
|
|
||||||
|
def hello_handler(response: Response) -> None:
|
||||||
|
print("Hello world!")
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def create_file(path: Path, content: str) -> None:
|
||||||
|
if not path.exists():
|
||||||
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
path.write_text(content.strip(), encoding="utf-8")
|
||||||
|
else:
|
||||||
|
print(f"Skipped: {path} (already exists)")
|
||||||
|
|
||||||
|
|
||||||
|
def new_handler(project_name: str, with_arch: Literal["flat", "src"] = "flat") -> None:
|
||||||
|
base_dir = Path.cwd() / project_name
|
||||||
|
|
||||||
|
if base_dir.exists():
|
||||||
|
print(f"Error: Directory '{project_name}' already exists.")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
base_dir.mkdir(parents=True)
|
||||||
|
print(f"Initialized project directory: {base_dir}")
|
||||||
|
|
||||||
|
create_file(base_dir / ".gitignore", GITIGNORE_CONTENT)
|
||||||
|
|
||||||
|
if with_arch == "flat":
|
||||||
|
create_file(base_dir / "main.py", FLAT_MAIN_TEMPLATE)
|
||||||
|
create_file(base_dir / "handlers.py", FLAT_HANDLERS_TEMPLATE)
|
||||||
|
|
||||||
|
elif with_arch == "src":
|
||||||
|
pkg_name = project_name.lower().replace(" ", "_").replace("-", "_")
|
||||||
|
app_pkg = base_dir / "src" / pkg_name / "application"
|
||||||
|
|
||||||
|
create_file(app_pkg / "__main__.py", SRC_MAIN_TEMPLATE)
|
||||||
|
create_file(app_pkg / "routers.py", SRC_ROUTERS_TEMPLATE)
|
||||||
|
create_file(app_pkg / "handlers" / "hello_world_handler.py", SRC_HANDLER_TEMPLATE)
|
||||||
|
|
||||||
|
create_file(base_dir / "src" / "__init__.py", "")
|
||||||
|
create_file(base_dir / "src" / pkg_name / "__init__.py", "")
|
||||||
|
create_file(app_pkg / "__init__.py", "")
|
||||||
|
create_file(app_pkg / "handlers" / "__init__.py", "")
|
||||||
|
|
||||||
|
print(f"\nProject '{project_name}' created successfully! 🚀")
|
||||||
Reference in New Issue
Block a user