From 2e34677d31f136a085ea96aeaf49c531eaf6e2db Mon Sep 17 00:00:00 2001 From: evilchili Date: Mon, 25 Mar 2024 23:52:29 -0700 Subject: [PATCH] adding "init" command --- pyproject.toml | 21 +++++++++++++-------- src/poetry_slam/build_tool.py | 2 +- src/poetry_slam/cli.py | 26 ++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 491e694..38dc971 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "poetry-slam" -version = "0.1.0" +version = "1.0" description = "An opinionated build tool for python poetry projects" authors = ["evilchili "] readme = "README.md" @@ -20,6 +20,17 @@ pytest-cov = "^4.0.0" [tool.poetry.group.dev.dependencies] +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" + + +[tool.poetry.scripts] +slam = "poetry_slam.cli:app" + + +### SLAM + [tool.black] line-length = 120 target-version = ['py310'] @@ -39,10 +50,4 @@ remove-duplicate-keys = true # remove all duplicate keys in objects remove-unused-variables = true # remove unused variables -[build-system] -requires = ["poetry-core"] -build-backend = "poetry.core.masonry.api" - - -[tool.poetry.scripts] -slam = "poetry_slam.cli:app" +### ENDSLAM diff --git a/src/poetry_slam/build_tool.py b/src/poetry_slam/build_tool.py index 393390e..fec55ea 100644 --- a/src/poetry_slam/build_tool.py +++ b/src/poetry_slam/build_tool.py @@ -72,7 +72,7 @@ class BuildTool: def test(self, args) -> bool: old_v = self.verbose self.verbose = True - returncode = self._exec("pytest", *args) + returncode = self.run("pytest", *args) self.verbose = old_v return returncode diff --git a/src/poetry_slam/cli.py b/src/poetry_slam/cli.py index 38af4b8..a6dc7ca 100644 --- a/src/poetry_slam/cli.py +++ b/src/poetry_slam/cli.py @@ -34,6 +34,32 @@ def main( build() +@app.command() +def init(): + """ + Add opinionated defaults to your pyproject.toml. + """ + defaults = Path(__file__).parent / "defaults.toml" + target = app_state["build_tool"].project_root / "pyproject.toml" + if not target.exists(): + raise RuntimeError(f"Could not find pyproject.toml at '{target}'") + logger.debug(f"Checking for modifications to {target}") + existing = target.read_text() + if "### SLAM" in existing: + logging.info(f"Found what looks like poetry-slam modifications; stopping.") + print(f"Abort: It looks like poetry-slam has already modified {target}.") + else: + backup = Path(str(target) + ".slam-orig") + backup.write_text(existing) + logging.debug(f"Saved backup of {target} to {backup}") + new = Path(str(target) + ".slam-new") + new.write_text(existing + "\n\n" + defaults.read_text()) + logging.debug(f"Wrote temporary file {new}") + new.rename(target) + logging.debug(f"Renamed {new} to {target}") + print(f"Added poetry-slam defaults to {target}") + + @app.command() def format(): """