From ec1b692365741e1594a45f8f1bbd1687428ab08f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=90=91=E5=A4=9C?= Date: Sat, 30 Mar 2024 13:22:04 +0800 Subject: [PATCH] feat(sync): add lock file check for sync cli and add abort when no dependency --- src/ipm/api.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/ipm/api.py b/src/ipm/api.py index 12028a6..ab1ec0a 100644 --- a/src/ipm/api.py +++ b/src/ipm/api.py @@ -437,6 +437,10 @@ def sync(target_path: StrPath, echo: bool = False) -> bool: ) project = InfiniProject(toml_path.parent) lock = ProjectLock(target_path) + if not lock._lock_path.exists(): + raise FileNotFoundError( + "文件[red]infini.lock[/]不存在!请先执行[bold red]`.ipm lock`[/]生成锁文件!" + ) global_lock = PackageLock() if not shutil.which("pdm"): raise EnvironmentError( @@ -477,16 +481,28 @@ def sync(target_path: StrPath, echo: bool = False) -> bool: dependencies.append(name) else: dependencies.append(f"{name}{version}") - if ( - result := subprocess.run( - ["pdm", "add", *dependencies], - cwd=target_path, - capture_output=True, - text=True, + if dependencies: + update( + "安装依赖: " + + ", ".join( + ["[bold green]" + dependency + "[/]" for dependency in dependencies] + ) + + "...", + echo, ) - ).returncode != 0: - error(result.stderr.strip("\n"), echo) - raise RuntimeError("PDM 异常退出, 指令忽略.") + if ( + result := subprocess.run( + ["pdm", "add", *dependencies], + cwd=target_path, + capture_output=True, + text=True, + ) + ).returncode != 0: + error(result.stderr.strip("\n"), echo) + raise RuntimeError("PDM 异常退出, 指令忽略.") + success("依赖安装完成!", echo) + else: + success("未检测到任何依赖,忽略任务。", echo) success("依赖环境同步完毕!", echo) return True