aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
author苏向夜 <fu050409@163.com>2024-03-08 11:41:35 +0800
committer苏向夜 <fu050409@163.com>2024-03-08 11:41:35 +0800
commit7b6426059db1459dccc3030e1bff337209d8f0b2 (patch)
tree395d239136e1145180f294db77d97eb0a1eefbc4 /src
parent3c309527d92bae5ada36345b35666d18a7cba66d (diff)
downloadipm-7b6426059db1459dccc3030e1bff337209d8f0b2.tar.gz
ipm-7b6426059db1459dccc3030e1bff337209d8f0b2.zip
refactor(cli): refactor cli handlers
Diffstat (limited to 'src')
-rw-r--r--src/ipm/__init__.py6
-rw-r--r--src/ipm/__main__.py37
-rw-r--r--src/ipm/api.py114
-rw-r--r--src/ipm/const.py5
4 files changed, 87 insertions, 75 deletions
diff --git a/src/ipm/__init__.py b/src/ipm/__init__.py
index d72a4e9..ab811a6 100644
--- a/src/ipm/__init__.py
+++ b/src/ipm/__init__.py
@@ -1,8 +1,8 @@
-from .api import install, extract, build, uninstall, init, new
+# from .api import install, extract, build, uninstall, init, new
__author__ = "苏向夜 <fu050409@163.com>"
-__organization__ = (
+__organizations__ = (
"Noctisynth",
"HydroRoll-Team",
)
-__all__ = ["install", "extract", "build", "uninstall", "init", "new"]
+# __all__ = ["install", "extract", "build", "uninstall", "init", "new"]
diff --git a/src/ipm/__main__.py b/src/ipm/__main__.py
index 976d59c..18fd83b 100644
--- a/src/ipm/__main__.py
+++ b/src/ipm/__main__.py
@@ -1,3 +1,4 @@
+from pathlib import Path
from . import api
from .exceptions import IpmException
from .logging import status, error, tada
@@ -89,6 +90,40 @@ def build(package: str = typer.Argument(".", help="Infini 项目路径")):
status.stop()
+yggdrasil = typer.Typer(
+ name="yggdrasil", help="Infini 包世界树管理", no_args_is_help=True, add_completion=False
+)
+
+
+@yggdrasil.command()
+def add(
+ name: str = typer.Argument(help="世界树名称"),
+ index: str = typer.Argument(help="世界树地址"),
+):
+ """新增世界树地址"""
+ try:
+ if api.yggdrasil_add(Path.cwd(), name, index, echo=True):
+ tada()
+ except IpmException as err:
+ error(str(err), echo=True)
+ finally:
+ status.stop()
+
+
+# @yggdrasil.command()
+# def remove(
+# name: str = typer.Argument(help="世界树名称"),
+# ):
+# """移除世界树地址"""
+# try:
+# if api.yggdrasil_remove(Path.cwd(), name, echo=True):
+# tada()
+# except IpmException as err:
+# error(str(err), echo=True)
+# finally:
+# status.stop()
+
+
# @main.command()
# def uninstall(package: str = typer.Argument(help="Infini 项目路径")):
# """卸载 Infini 规则包"""
@@ -165,5 +200,7 @@ def collect(): ...
def update(): ...
+main.add_typer(yggdrasil)
+
if __name__ == "__main__":
main()
diff --git a/src/ipm/api.py b/src/ipm/api.py
index 54b9c76..322d051 100644
--- a/src/ipm/api.py
+++ b/src/ipm/api.py
@@ -1,5 +1,7 @@
from pathlib import Path
from typing import Optional
+from ipm.project.env import new_virtualenv
+from ipm.project.toml_file import add_yggdrasil, init_infini, init_pyproject
from ipm.typing import StrPath
from ipm.utils import freeze, urlparser, loader
from ipm.const import GITIGNORE, INDEX, INDEX_PATH, STORAGE, SRC_HOME
@@ -12,6 +14,7 @@ from ipm.exceptions import (
)
from ipm.utils.version import require_update
from ipm.models.ipk import InfiniProject, InfiniFrozenPackage
+
# from ipm.models.lock import PackageLock, ProjectLock
from ipm.models.index import Yggdrasil
@@ -80,78 +83,31 @@ def init(target_path: StrPath, force: bool = False, echo: bool = False) -> bool:
echo=echo,
)
+ status.update("构建环境中...")
status.start()
- toml_file = toml_path.open("w", encoding="utf-8")
- toml_data = tomlkit.document()
- project = tomlkit.table()
- project.add("name", name)
- project.add("version", version)
- project.add("description", description)
- author = tomlkit.array()
- author.add_line({"name": author_name, "email": author_email})
- author.multiline(True)
- project.add("author", author)
- project.add("license", license)
- toml_data.add("project", project)
- toml_data.add("requirements", tomlkit.table())
- toml_data.add("dependencies", tomlkit.table())
- tomlkit.dump(toml_data, toml_file)
- toml_file.close()
-
- source_path = target_path.joinpath("src")
- gitignore_filepath = target_path.joinpath(".gitignore")
- source_path.mkdir(parents=True, exist_ok=True)
- if not gitignore_filepath.exists():
- (target_path / ".gitignore").write_text(GITIGNORE)
-
- if entry_file == "0":
- init_filepath = source_path.joinpath("__init__.py")
- events_filepath = source_path.joinpath("events.py")
- handlers_filepath = source_path.joinpath("handlers.py")
- interceptors_filepath = source_path.joinpath("interceptors.py")
-
- if not init_filepath.exists():
- init_filepath.write_text(
- "# Initialized `events.py` generated by ipm.\n"
- "# Regists your text events and regist global variables here.\n"
- "# Documents at https://ipm.hydroroll.team/\n\n"
- "from infini.register import Register\n\n\n"
- "register = Register()\n"
- )
- if not events_filepath.exists():
- events_filepath.write_text(
- "# Initialized `events.py` generated by ipm.\n"
- "# Regists your text events and regist global variables here.\n"
- "# Documents at https://ipm.hydroroll.team/\n\n"
- "from infini.register import Register\n\n\n"
- "register = Register()\n"
- )
- if not handlers_filepath.exists():
- handlers_filepath.write_text(
- "# Initialized `handlers.py` generated by ipm.\n"
- "# Regists your handlers here.\n"
- "# Documents at https://ipm.hydroroll.team/\n\n"
- "from infini.register import Register\n\n\n"
- "register = Register()\n"
- )
- if not interceptors_filepath.exists():
- interceptors_filepath.write_text(
- "# Initialized `interceptors.py` generated by ipm.\n"
- "# Regists your pre-interceptors and interceptors here.\n"
- "# Documents at https://ipm.hydroroll.team/\n\n"
- "from infini.register import Register\n\n\n"
- "register = Register()\n"
- )
- else:
- entry_filepath = source_path.joinpath(default_entries[int(entry_file)])
- if not entry_filepath.exists():
- entry_filepath.write_text(
- f"# Initialized `{source_path.name}` generated by ipm.\n"
- "# Documents at https://ipm.hydroroll.team/\n\n"
- "from infini.register import Register\n\n\n"
- "register = Register()\n"
- )
+ init_infini(
+ toml_path,
+ target_path,
+ name,
+ version,
+ description,
+ author_name,
+ author_email,
+ license,
+ entry_file,
+ default_entries,
+ )
+ init_pyproject(
+ target_path,
+ name,
+ version,
+ description,
+ author_name,
+ author_email,
+ license,
+ )
+ new_virtualenv(target_path)
return True
@@ -201,6 +157,24 @@ def extract(
return freeze.extract_ipk(source_path, dist_path, echo)
+def yggdrasil_add(source_path: StrPath, name: str, index: str, echo: bool = False) -> bool:
+ info(f"新增世界树: [bold green]{name}[/]")
+ status.start()
+ status.update("检查环境中...")
+ if not (toml_path := Path(source_path).joinpath("infini.toml")).exists():
+ raise FileNotFoundError(
+ f"文件 [green]infini.toml[/green] 尚未被初始化, 你可以使用[bold green]`ipm init`[/bold green]来初始化项目."
+ )
+ success("环境检查完毕.", echo)
+ status.update("同步世界树中...")
+ yggdrasil = Yggdrasil(index)
+ yggdrasil.sync()
+
+ add_yggdrasil(toml_path, name, index)
+ success("更改均已写入文件.", echo)
+ return True
+
+
# def install(
# uri: str,
# index: str = "",
diff --git a/src/ipm/const.py b/src/ipm/const.py
index a94187f..753c3a3 100644
--- a/src/ipm/const.py
+++ b/src/ipm/const.py
@@ -11,8 +11,9 @@ STORAGE = IPM_PATH / "storage"
INDEX_PATH = IPM_PATH / "index"
# 文本参数
-ATTENSION = (
- "# This file is @generated by IPM.\n" "# It is not intended for manual editing.\n\n"
+ATTENTIONS = (
+ "This file is @generated by IPM.",
+ "It is not intended for manual editing.",
)
GITIGNORE = """# Initialized `.gitignores` @generated by IPM.
# Python