diff --git a/nut/commands/setup.py b/nut/commands/setup.py new file mode 100644 index 0000000..930c86f --- /dev/null +++ b/nut/commands/setup.py @@ -0,0 +1,47 @@ +from nut.paths import get_templates_path +import shutil +import os + +def find_template_conflicts(dest_dir: str) -> list[str]: + templates_path = get_templates_path() / "project" + + if not os.path.exists(templates_path): + raise RuntimeError(f"Project template path does not exist: {templates_path}") + + conflicts = [] + + for root, _, files in os.walk(templates_path): + rel = os.path.relpath(root, templates_path) + target_root = os.path.join(dest_dir, rel) if rel != "." else dest_dir + + for file in files: + target_file = os.path.join(target_root, file) + if os.path.exists(target_file): + conflicts.append(target_file) + + return conflicts + +def copy_template_files(dest_dir: str, force: bool = False, checked: bool = False) -> None: + templates_path = get_templates_path() / "project" + + if not os.path.exists(templates_path): + raise RuntimeError(f"Project template path does not exist: {templates_path}") + + os.makedirs(dest_dir, exist_ok=True) + + if not checked: + conflicts = find_template_conflicts(dest_dir) + if conflicts and not force: + raise FileExistsError( + "Refusing to overwrite existing files:\n" + + "\n".join(conflicts) + ) + + shutil.copytree( + templates_path, + dest_dir, + dirs_exist_ok=True + ) + +def is_nut_workspace(path: str) -> bool: + return os.path.isfile(os.path.join(path, "Nutfile")) diff --git a/nut/paths.py b/nut/paths.py new file mode 100644 index 0000000..8dcc746 --- /dev/null +++ b/nut/paths.py @@ -0,0 +1,7 @@ +from pathlib import Path + +def get_root_path() -> Path: + return Path(__file__).resolve().parent + +def get_templates_path() -> Path: + return get_root_path() / "templates"