Add setup and paths modules for template management and file operations
This commit is contained in:
parent
e49a865008
commit
d9dd069d50
47
nut/commands/setup.py
Normal file
47
nut/commands/setup.py
Normal file
@ -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"))
|
||||
7
nut/paths.py
Normal file
7
nut/paths.py
Normal file
@ -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"
|
||||
Loading…
x
Reference in New Issue
Block a user