From 3635887a85c418d5bc3b0cbbf83e6b961a782932 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Wed, 24 Dec 2025 23:34:32 +0545 Subject: [PATCH] feat(devgen/modules): enhance changelog and license generation with improved file handling This update introduces improvements to the changelog generator's file handling logic, including proper prepending of new entries and automatic header management. It also refines the license generator's template directory structure and cleans up minor formatting issues in the commit generator. **New Features** - Added intelligent changelog prepending logic that preserves existing headers and maintains proper formatting when updating files - Implemented automatic `# CHANGELOG` header insertion for new changelog files **Enhancements & Refinements** - Changed changelog version headers from `#` to `##` for better markdown hierarchy - Updated license generator to use `templates/licenses` directory instead of `prompt_templates/licenses` - Removed unnecessary whitespace and improved code formatting in commit generator - Enhanced changelog file writing logic to handle both new and existing files more robustly ``` --- devgen/modules/changelog_generator.py | 15 ++++++++++----- devgen/modules/commit_generator.py | 5 ++--- devgen/modules/license_generator.py | 4 +--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/devgen/modules/changelog_generator.py b/devgen/modules/changelog_generator.py index c39216f..6c3634a 100644 --- a/devgen/modules/changelog_generator.py +++ b/devgen/modules/changelog_generator.py @@ -120,7 +120,7 @@ class ChangelogGenerator: ) -> str: """Generates markdown changelog from parsed commits.""" date_str = datetime.now().strftime("%Y-%m-%d") - md = [f"# {version} ({date_str})\n"] + md = [f"## {version} ({date_str})\n"] # Order: Breaking, Features, Fixes, Docs, Others order = [ @@ -163,14 +163,19 @@ class ChangelogGenerator: if output_file: path = Path(output_file) - # If file exists, prepend? For now, just overwrite or append logic could be complex. - # Let's implement prepend logic if file exists. if path.exists(): old_content = path.read_text(encoding="utf-8") - new_content = old_content + "\n\n" + md_content + # Prepend the new content, assume # CHANGELOG is at the top or needs to be + if old_content.strip().startswith("# CHANGELOG"): + lines = old_content.split("\n", 1) + header = lines[0] + rest = lines[1] if len(lines) > 1 else "" + new_content = f"{header}\n\n{md_content}\n{rest.lstrip()}" + else: + new_content = f"# CHANGELOG\n\n{md_content}\n\n{old_content}" path.write_text(new_content, encoding="utf-8") else: - path.write_text(md_content, encoding="utf-8") + path.write_text(f"# CHANGELOG\n\n{md_content}", encoding="utf-8") self.logger.info(f"Changelog written to {output_file}") print(f" Changelog updated: {output_file}") diff --git a/devgen/modules/commit_generator.py b/devgen/modules/commit_generator.py index 2f97b5b..9f30950 100644 --- a/devgen/modules/commit_generator.py +++ b/devgen/modules/commit_generator.py @@ -39,7 +39,6 @@ IGNORE_PATTERNS = [ ] - class CommitEngine: """ Engine for generating AI-powered commit messages. @@ -98,11 +97,11 @@ class CommitEngine: ] ) files = [f.strip() for f in out.split("\n") if f.strip()] - + # Also get staged files (in case of a previous failed run) staged_out = run_git_command(["git", "diff", "--name-only", "--cached"]) staged_files = [f.strip() for f in staged_out.split("\n") if f.strip()] - + # Combine and deduplicate all_files = list(set(files + staged_files)) return all_files diff --git a/devgen/modules/license_generator.py b/devgen/modules/license_generator.py index 096ba0f..83feafd 100644 --- a/devgen/modules/license_generator.py +++ b/devgen/modules/license_generator.py @@ -8,9 +8,7 @@ class LicenseGenerator: """Generates license files from templates.""" def __init__(self): - self.templates_dir = ( - Path(__file__).parent.parent / "prompt_templates" / "licenses" - ) + self.templates_dir = Path(__file__).parent.parent / "templates" / "licenses" def list_licenses(self) -> List[Dict[str, str]]: """Lists available license templates."""