From 4e053ae23fac2102c8bbf81ca9d1a1742bba52ba Mon Sep 17 00:00:00 2001 From: 苏向夜 Date: Fri, 29 Mar 2024 19:46:18 +0800 Subject: feat(api): support data keep when force reinit --- src/ipm/project/toml_file.py | 56 +++++++++++++++++++++++++------------------- src/ipm/utils/freeze.py | 5 ++-- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/ipm/project/toml_file.py b/src/ipm/project/toml_file.py index d19998b..e28eb1f 100644 --- a/src/ipm/project/toml_file.py +++ b/src/ipm/project/toml_file.py @@ -19,21 +19,25 @@ def init_infini( entry_file: str, default_entries: List[str], ) -> None: + toml_path = target_path.joinpath("pyproject.toml") + if toml_path.exists(): + toml_data = tomlkit.loads(toml_path.read_text(encoding="utf-8")) + else: + toml_data = tomlkit.document() 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) + project = toml_data.get("project", tomlkit.table()) + project["name"] = name + project["version"] = version + project["description"] = description author = tomlkit.array() author.add_line({"name": author_name, "email": author_email}) author.multiline(True) - project.add("authors", author) - project.add("license", license) - project.add("readme", "README.md") - toml_data.add("project", project) - toml_data.add("requirements", tomlkit.table()) - toml_data.add("dependencies", tomlkit.table()) + project["authors"] = author + project["license"] = license + project["readme"] = "README.md" + toml_data["project"] = project + toml_data["requirements"] = tomlkit.table() + toml_data["dependencies"] = tomlkit.table() tomlkit.dump(toml_data, toml_file) toml_file.close() @@ -130,22 +134,26 @@ def init_pyproject( license: str, standalone: bool, ): - toml_file = target_path.joinpath("pyproject.toml").open("w", encoding="utf-8") - toml_data = tomlkit.document() - project = tomlkit.table() - project.add("name", name) - project.add("version", version) - project.add("description", description) + toml_path = target_path.joinpath("pyproject.toml") + if toml_path.exists(): + toml_data = tomlkit.loads(toml_path.read_text(encoding="utf-8")) + else: + toml_data = tomlkit.document() + toml_file = toml_path.open("w", encoding="utf-8") + project = toml_data.get("project", tomlkit.table()) + project["name"] = name + project["version"] = version + project["description"] = description author = tomlkit.array() author.add_line({"name": author_name, "email": author_email}) author.multiline(True) - project.add("authors", author) + project["authors"] = author license_table = tomlkit.inline_table() license_table.update({"text": license}) - project.add("license", license_table) - project.add("dependencies", tomlkit.array('["infini>2.1.0"]')) - project.add("requires-python", ">=3.8") - project.add("readme", "README.md") + project["license"] = license_table + project["dependencies"] = tomlkit.array('["infini>2.1.0"]') + project["requires-python"] = ">=3.8" + project["readme"] = "README.md" tool = tomlkit.table(True) pdm = tomlkit.table(True) @@ -156,7 +164,7 @@ def init_pyproject( pdm.append("dev-dependencies", dev_dependencies) tool.append("pdm", pdm) - toml_data.add("project", project) - toml_data.add("tool", tool) + toml_data["project"] = project + toml_data["tool"] = tool tomlkit.dump(toml_data, toml_file) toml_file.close() diff --git a/src/ipm/utils/freeze.py b/src/ipm/utils/freeze.py index 846c6ec..58314df 100644 --- a/src/ipm/utils/freeze.py +++ b/src/ipm/utils/freeze.py @@ -13,9 +13,8 @@ import shutil def build_ipk(ipk: InfiniProject, echo: bool = False) -> InfiniFrozenPackage: update("构建开发环境...", echo) - project = InfiniProject() - arcname = f"{project.name}-{project.version}" + arcname = f"{ipk.name}-{ipk.version}" build_dir = ipk._source_path.joinpath(".ipm-build") arc_dir = build_dir.joinpath(arcname) src_path = ipk._source_path / "src" @@ -58,7 +57,7 @@ def build_ipk(ipk: InfiniProject, echo: bool = False) -> InfiniFrozenPackage: echo, ) - return InfiniFrozenPackage(ifp_path, ipk.name, version=project.version) + return InfiniFrozenPackage(ifp_path, ipk.name, version=ipk.version) def extract_ipk( -- cgit v1.2.3-70-g09d2