From 4ea9103f657c5b6507d4aa3630b50a6877a305c7 Mon Sep 17 00:00:00 2001 From: Cedrik Neumann <7921017+m1racoli@users.noreply.github.com> Date: Sat, 1 May 2021 13:06:30 +0200 Subject: [PATCH] feat(scripts): provide `diagrams` CLI This change addresses https://github.com/mingrammer/diagrams/issues/369 while providing a simple CLI entry point which can be used outside the virtual environment of the installed package. This works well with for example with [pipx](https://pipxproject.github.io/pipx/) or [uv](https://docs.astral.sh/uv/). --- diagrams/cli.py | 31 ++++++++++++++++++++++++++++ docs/getting-started/installation.md | 11 ++++++++++ pyproject.toml | 3 +++ 3 files changed, 45 insertions(+) create mode 100644 diagrams/cli.py diff --git a/diagrams/cli.py b/diagrams/cli.py new file mode 100644 index 00000000..22a73685 --- /dev/null +++ b/diagrams/cli.py @@ -0,0 +1,31 @@ +import argparse +import sys + + +def run() -> int: + parser = argparse.ArgumentParser( + description="Run diagrams code files in a diagrams environment.", + ) + parser.add_argument( + "paths", + metavar="path", + type=str, + nargs="+", + help="a Python file containing diagrams code", + ) + args = parser.parse_args() + + for path in args.paths: + print(path) + with open(path) as f: + exec(f.read()) + + return 0 + + +def main(): + sys.exit(run()) + + +if __name__ == "__main__": + main() diff --git a/docs/getting-started/installation.md b/docs/getting-started/installation.md index f38b5242..ad23387f 100644 --- a/docs/getting-started/installation.md +++ b/docs/getting-started/installation.md @@ -20,6 +20,9 @@ $ pipenv install diagrams # using poetry $ poetry add diagrams + +# using uv +$ uv tool install diagrams ``` ## Quick Start @@ -47,6 +50,14 @@ This generates the diagram below: It will be saved as `web_service.png` in your working directory. +### CLI + +With the `diagrams` CLI you can process one or more diagram files at once. + +```shell +$ diagrams diagram1.py diagram2.py +``` + ## Next See more [Examples](/docs/getting-started/examples) or see the [Guides](/docs/guides/diagram) page for more details. diff --git a/pyproject.toml b/pyproject.toml index 7e9b68b5..3e169152 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,9 @@ homepage = "https://diagrams.mingrammer.com" repository = "https://github.com/mingrammer/diagrams" include = ["resources/**/*"] +[tool.poetry.scripts] +diagrams="diagrams.cli:main" + [tool.poetry.dependencies] python = "^3.9" graphviz = ">=0.13.2,<0.21.0"