|
| 1 | +# core/bash.py for cookiecutter-cookiecutter |
| 2 | +# |
| 3 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026, Jared Cook |
| 4 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Affero General Public License as |
| 8 | +# published by the Free Software Foundation, either version 3 of the |
| 9 | +# License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Affero General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Affero General Public License |
| 17 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +import os |
| 20 | +import shutil |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +from core.config import ensure_config |
| 26 | +from core.logger import setup_logging |
| 27 | + |
| 28 | +cfg = ensure_config() # loads singleton config |
| 29 | +logger = setup_logging(cfg) # loads singleton logger |
| 30 | + |
| 31 | + |
| 32 | +def clean() -> None: |
| 33 | + """Remove _shared_hooks directory.""" |
| 34 | + _shared_hooks = Path.cwd() / "_shared_hooks" |
| 35 | + logger.info(f"hooks directory: {_shared_hooks}") |
| 36 | + if _shared_hooks.exists() and _shared_hooks.is_dir(): |
| 37 | + shutil.rmtree(_shared_hooks) |
| 38 | + logger.info(f"Removed {_shared_hooks} directory.") |
| 39 | + else: |
| 40 | + logger.info("_shared_hooks directory does not exist, nothing to remove.") |
| 41 | + |
| 42 | + |
| 43 | +def make(cmd: str, *, verbose: bool = False) -> None: |
| 44 | + """Run a make target inside post-gen, exiting on failure.""" |
| 45 | + logger.info(f"▶ Running: make {cmd}") |
| 46 | + try: |
| 47 | + result = subprocess.run( |
| 48 | + ["make", cmd], |
| 49 | + check=True, |
| 50 | + capture_output=True, |
| 51 | + text=True, |
| 52 | + ) |
| 53 | + if verbose and result.stdout: |
| 54 | + logger.info(result.stdout.rstrip()) |
| 55 | + logger.info(f"✅ Command succeeded: make {cmd}") |
| 56 | + except subprocess.CalledProcessError as e: |
| 57 | + logger.error(f"❌ Command failed: make {cmd}") |
| 58 | + # Optionally log stdout/stderr captured from the failed command |
| 59 | + if e.stdout: |
| 60 | + logger.error(f"STDOUT: {e.stdout}") |
| 61 | + if e.stderr: |
| 62 | + logger.error(f"STDERR: {e.stderr}") |
| 63 | + sys.exit(e.returncode) |
| 64 | + |
| 65 | + |
| 66 | +def tree() -> None: |
| 67 | + """Run tree cmd inside the post-gen.""" |
| 68 | + logger.info(f"Current working directory: {os.getcwd()}") |
| 69 | + subprocess.run(["tree", "-a", "."], check=False) |
0 commit comments