Implements jeaddons CLI tool
Adds a CLI tool to copy files or folders from a git repository into the current project based on a registry. The tool provides commands to: - `add`: Copies files/folders based on registry entries. - `list`: Lists available registry entries. Registry entries are defined in `jeAddons/registry.py`. The tool supports overriding the registry's ref and destination.
This commit is contained in:
+26
-7
@@ -4,13 +4,32 @@ import shutil
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def fetch_file(repo_url: str, file_path: str, dest: Path):
|
||||
tmp = tempfile.mkdtemp()
|
||||
def fetch_path(repo_url: str, repo_path: str, dest: Path, ref: str = "HEAD", force: bool = False):
|
||||
with tempfile.TemporaryDirectory(prefix="jeaddons-") as tmp:
|
||||
tmp_path = Path(tmp)
|
||||
subprocess.check_call(
|
||||
["git", "clone", "--filter=blob:none", "--no-checkout", "--depth", "1", repo_url, str(tmp_path)]
|
||||
)
|
||||
subprocess.check_call(["git", "-C", str(tmp_path), "sparse-checkout", "init", "--cone"])
|
||||
subprocess.check_call(["git", "-C", str(tmp_path), "sparse-checkout", "set", repo_path])
|
||||
subprocess.check_call(["git", "-C", str(tmp_path), "checkout", ref])
|
||||
|
||||
subprocess.check_call(["git", "clone", "--depth", "1", repo_url, tmp])
|
||||
src = tmp_path / repo_path
|
||||
if not src.exists():
|
||||
raise FileNotFoundError(repo_path)
|
||||
|
||||
src = Path(tmp) / file_path
|
||||
if not src.exists():
|
||||
raise FileNotFoundError(file_path)
|
||||
target = dest / src.name
|
||||
if target.exists():
|
||||
if not force:
|
||||
raise FileExistsError(target)
|
||||
if target.is_dir():
|
||||
shutil.rmtree(target)
|
||||
else:
|
||||
target.unlink()
|
||||
|
||||
shutil.copy2(src, dest / src.name)
|
||||
if src.is_dir():
|
||||
shutil.copytree(src, target)
|
||||
return target
|
||||
|
||||
shutil.copy2(src, target)
|
||||
return target
|
||||
|
||||
Reference in New Issue
Block a user