aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author苏向夜 <fu050409@163.com>2024-03-08 15:51:56 +0800
committer苏向夜 <fu050409@163.com>2024-03-08 15:51:56 +0800
commit26ef62510165ee431b43c34f7cdab3cb78f5dbc8 (patch)
tree80528d0b98acef8aca5784316d346278b1025450
parent14af33a6b87f67264cfdeef4567ae15211b9d475 (diff)
downloadipm-26ef62510165ee431b43c34f7cdab3cb78f5dbc8.tar.gz
ipm-26ef62510165ee431b43c34f7cdab3cb78f5dbc8.zip
feat(api): add tag cli
-rw-r--r--src/ipm/__main__.py25
-rw-r--r--src/ipm/api.py34
2 files changed, 44 insertions, 15 deletions
diff --git a/src/ipm/__main__.py b/src/ipm/__main__.py
index a74311d..1a016d4 100644
--- a/src/ipm/__main__.py
+++ b/src/ipm/__main__.py
@@ -23,21 +23,16 @@ def check():
status.stop()
-# @main.command()
-# def install(
-# uri: str = typer.Argument(help="Infini 包的统一资源标识符"),
-# index: str = typer.Option(None, help="世界树服务器地址"),
-# upgrade: bool = typer.Option(False, "--upgrade", "-u", help="更新 Infini 包"),
-# force: bool = typer.Option(False, "--force", "-f", help="强制安装"),
-# ):
-# """安装一个 Infini 规则包到此计算机"""
-# try:
-# if api.install(uri, index, upgrade=upgrade, force=force, echo=True):
-# tada()
-# except IpmException as err:
-# error(str(err), echo=True)
-# finally:
-# status.stop()
+@main.command()
+def tag(tag: str = typer.Argument(help="版本号标签")):
+ """设置规则包版本号"""
+ try:
+ if api.tag(Path.cwd(), tag, echo=True):
+ tada()
+ except IPMException as err:
+ error(str(err), echo=True)
+ finally:
+ status.stop()
@main.command()
diff --git a/src/ipm/api.py b/src/ipm/api.py
index bf2e740..2d7f277 100644
--- a/src/ipm/api.py
+++ b/src/ipm/api.py
@@ -1,5 +1,6 @@
from pathlib import Path
from typing import Optional
+
from ipm.models.lock import ProjectLock
from ipm.project.env import new_virtualenv
from ipm.project.toml_file import add_yggdrasil, init_infini, init_pyproject
@@ -20,6 +21,7 @@ import shutil
import os
import re
import subprocess
+import tomlkit
import configparser
@@ -42,6 +44,38 @@ def check(source_path: StrPath, echo: bool = False) -> bool:
return True
+def tag(target_path: StrPath, tag: str, echo: bool = False):
+ info(f"更新规则包版本号为: [bold green]{tag}[/]", echo)
+ tag = tag.lstrip("v")
+
+ status.start()
+ status.update("检查环境中...")
+ if not (toml_path := Path(target_path).joinpath("infini.toml")).exists():
+ raise FileNotFoundError(
+ f"文件 [green]infini.toml[/green] 尚未被初始化, 你可以使用[bold green]`ipm init`[/bold green]来初始化项目."
+ )
+ if not (project_path := Path(target_path).joinpath("pyproject.toml")).exists():
+ raise FileNotFoundError(
+ f"文件 [green]pyproject.toml[/green] 尚未被初始化, 你可以使用[bold green]`ipm init`[/bold green]来初始化项目."
+ )
+ success("环境检查完毕.", echo)
+
+ infini_project = tomlkit.load(toml_path.open("r", encoding="utf-8"))
+ py_project = tomlkit.load(project_path.open("r", encoding="utf-8"))
+ infini_project["project"]["version"] = tag # type: ignore
+ py_project["project"]["version"] = tag # type: ignore
+
+ toml_file = toml_path.open("w", encoding="utf-8")
+ project_file = project_path.open("w", encoding="utf-8")
+ tomlkit.dump(infini_project, toml_file)
+ tomlkit.dump(py_project, project_file)
+ toml_file.close()
+ project_file.close()
+
+ success("项目文件写入完成.", echo)
+ return True
+
+
def init(target_path: StrPath, force: bool = False, echo: bool = False) -> bool:
info("初始化规则包...", echo)
update("检查环境...", echo)