fix: update template_loader to handle both source and installed modes
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
"""Template loader for gitignore-generator."""
|
"""Template loader for gitignore-generator."""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, List, Optional, Set
|
from typing import Dict, List, Optional, Set
|
||||||
|
|
||||||
@@ -15,10 +16,22 @@ class TemplateLoader:
|
|||||||
self._templates_index: Optional[Dict] = None
|
self._templates_index: Optional[Dict] = None
|
||||||
self._custom_templates: Dict[str, str] = {}
|
self._custom_templates: Dict[str, str] = {}
|
||||||
|
|
||||||
|
def _get_templates_dir(self) -> Path:
|
||||||
|
"""Get the templates directory, handling both source and installed modes."""
|
||||||
|
templates_dir = config.template_dir
|
||||||
|
if templates_dir.exists() and (templates_dir / "templates.json").exists():
|
||||||
|
return templates_dir
|
||||||
|
module_dir = Path(__file__).parent
|
||||||
|
package_templates = module_dir / "templates"
|
||||||
|
if package_templates.exists() and (package_templates / "templates.json").exists():
|
||||||
|
return package_templates
|
||||||
|
return templates_dir
|
||||||
|
|
||||||
def _load_templates_index(self) -> Dict:
|
def _load_templates_index(self) -> Dict:
|
||||||
"""Load the templates index file."""
|
"""Load the templates index file."""
|
||||||
if self._templates_index is None:
|
if self._templates_index is None:
|
||||||
index_path = config.template_dir / "templates.json"
|
templates_dir = self._get_templates_dir()
|
||||||
|
index_path = templates_dir / "templates.json"
|
||||||
if index_path.exists():
|
if index_path.exists():
|
||||||
with open(index_path, "r") as f:
|
with open(index_path, "r") as f:
|
||||||
self._templates_index = json.load(f)
|
self._templates_index = json.load(f)
|
||||||
@@ -29,7 +42,8 @@ class TemplateLoader:
|
|||||||
|
|
||||||
def _save_templates_index(self) -> None:
|
def _save_templates_index(self) -> None:
|
||||||
"""Save the templates index file."""
|
"""Save the templates index file."""
|
||||||
index_path = config.template_dir / "templates.json"
|
templates_dir = self._get_templates_dir()
|
||||||
|
index_path = templates_dir / "templates.json"
|
||||||
with open(index_path, "w") as f:
|
with open(index_path, "w") as f:
|
||||||
json.dump(self._templates_index, f, indent=2)
|
json.dump(self._templates_index, f, indent=2)
|
||||||
|
|
||||||
@@ -72,6 +86,14 @@ class TemplateLoader:
|
|||||||
for template_file in custom_dir.glob("*.gitignore"):
|
for template_file in custom_dir.glob("*.gitignore"):
|
||||||
self._custom_templates[template_file.stem] = str(template_file)
|
self._custom_templates[template_file.stem] = str(template_file)
|
||||||
|
|
||||||
|
def _get_template_path(self, template_name: str, category: str) -> Optional[Path]:
|
||||||
|
"""Get the path to a template file."""
|
||||||
|
templates_dir = self._get_templates_dir()
|
||||||
|
template_path = templates_dir / category / f"{template_name}.gitignore"
|
||||||
|
if template_path.exists():
|
||||||
|
return template_path
|
||||||
|
return None
|
||||||
|
|
||||||
def load_template(self, template_name: str, category: Optional[str] = None) -> Optional[str]:
|
def load_template(self, template_name: str, category: Optional[str] = None) -> Optional[str]:
|
||||||
"""Load a template by name."""
|
"""Load a template by name."""
|
||||||
self._load_custom_templates()
|
self._load_custom_templates()
|
||||||
@@ -89,8 +111,8 @@ class TemplateLoader:
|
|||||||
categories = ["languages", "ides"]
|
categories = ["languages", "ides"]
|
||||||
|
|
||||||
for cat in categories:
|
for cat in categories:
|
||||||
template_path = config.template_dir / cat / f"{template_name}.gitignore"
|
template_path = self._get_template_path(template_name, cat)
|
||||||
if template_path.exists():
|
if template_path is not None:
|
||||||
with open(template_path, "r") as f:
|
with open(template_path, "r") as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
|
||||||
@@ -139,8 +161,8 @@ class TemplateLoader:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
for cat in ["languages", "ides"]:
|
for cat in ["languages", "ides"]:
|
||||||
template_path = config.template_dir / cat / f"{template_name}.gitignore"
|
template_path = self._get_template_path(template_name, cat)
|
||||||
if template_path.exists():
|
if template_path is not None:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|||||||
Reference in New Issue
Block a user