aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author苏向夜 <fu050409@163.com>2024-04-02 20:11:02 +0800
committer苏向夜 <fu050409@163.com>2024-04-02 20:11:02 +0800
commit43c5f931453f2b51c3d15498150ab158e156ae19 (patch)
treee97d085f27a0df92ecb60b8044ecf6b02576e545
parente0727eed9b1db5b709ecd7760712ab1f3ec6db6f (diff)
downloadipm-43c5f931453f2b51c3d15498150ab158e156ae19.tar.gz
ipm-43c5f931453f2b51c3d15498150ab158e156ae19.zip
feat(cli): add update api
-rw-r--r--src/ipm/__main__.py10
-rw-r--r--src/ipm/api.py36
2 files changed, 42 insertions, 4 deletions
diff --git a/src/ipm/__main__.py b/src/ipm/__main__.py
index 4c57221..36abb2b 100644
--- a/src/ipm/__main__.py
+++ b/src/ipm/__main__.py
@@ -237,8 +237,14 @@ def install():
@main.command()
def update():
- """更新 Infini 依赖"""
- raise NotImplementedError
+ """更新规则包依赖"""
+ try:
+ if api.update(Path.cwd(), echo=True):
+ tada()
+ except IPMException as err:
+ error(str(err), echo=True)
+ finally:
+ status.stop()
main.add_typer(yggdrasil)
diff --git a/src/ipm/api.py b/src/ipm/api.py
index 73f2692..317b2d8 100644
--- a/src/ipm/api.py
+++ b/src/ipm/api.py
@@ -1,5 +1,6 @@
from datetime import datetime
from pathlib import Path
+from distlib.version import SemanticVersion
from typing import Optional
from ipm.const import INDEX, VUE_CODE
@@ -23,7 +24,7 @@ from ipm.exceptions import (
NameError,
RuntimeError,
)
-from ipm.models.ipk import InfiniProject, InfiniFrozenPackage
+from ipm.models.ipk import InfiniProject
from ipm.models.index import Yggdrasil
from infini.loader import Loader
@@ -609,4 +610,35 @@ def doc(
return True
-def update(target_path: StrPath, echo: bool = False): ...
+def update(target_path: StrPath, echo: bool = False) -> bool:
+ info("更新依赖环境...", echo)
+ statusup("检查环境中...", echo)
+ if not (toml_path := Path(target_path).joinpath("infini.toml")).exists():
+ raise FileNotFoundError(
+ f"文件 [green]infini.toml[/green] 尚未被初始化, 你可以使用[bold green]`ipm init`[/bold green]来初始化项目."
+ )
+ project = InfiniProject(toml_path.parent)
+ if not shutil.which("pdm"):
+ raise EnvironmentError(
+ "IPM 未能在环境中找到 [bold green]PDM[/] 安装, 请确保 PDM 在环境中被正确安装. "
+ "你可以使用`[bold green]pipx install pdm[/]`来安装此包管理器."
+ )
+ success("环境检查完毕.", echo)
+
+ statusup("更新依赖中...", echo)
+ for requirement in project.requirements:
+ lastest_version = requirement.yggdrasil.get_lastest_version(requirement.name)
+ if not lastest_version:
+ raise ProjectError(f"包 [bold red]{requirement.name}[/] 被从世界树燃烧了。")
+ if SemanticVersion(lastest_version) > SemanticVersion(requirement.version):
+ project.require(requirement.name, version=lastest_version)
+ success(
+ f"将 [bold green]{requirement.version}[/] 升级到 [bold yellow]{lastest_version}[/].",
+ echo,
+ )
+
+ check(target_path, echo)
+ sync(target_path, echo)
+
+ install(target_path, echo)
+ return True