Skip to content

Commit

Permalink
feat: Set up pre-commit hooks and collect documentation files
Browse files Browse the repository at this point in the history
- Add GitHub Actions workflow for linting code
- Update .gitignore to exclude logs directory
- Configure pre-commit hooks for code formatting and linting
- Refactor main.py:
  - Extract codebase directory validation to separate function
  - Implement collect_documentation_files function to gather .md and .txt files
  - Skip files and directories specified in .gitignore
  - Add logging and setup logger
- Update main function to handle CLI arguments and call collect_documentation_files
  • Loading branch information
PriNova committed May 2, 2024
1 parent 6c0ff61 commit 2f62f69
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 36 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Lint code

on: [push, pull_request]

permissions:
contents: read

jobs:
lint:
runs-on: ubuntu-20.04
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Run pre-commit
uses: pre-commit/[email protected]
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.venv
**/__pycache__
/.venv
**/__pycache__
/logs
18 changes: 18 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 24.4.0
hooks:
- id: black

- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
107 changes: 73 additions & 34 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,85 @@
""" 1. The directory path of the codebase to be analyzed
2. The output directory for the generated reports
To implement this step, we'll break it down into smaller tasks:
1. Create a command-line interface (CLI) for the program
2. Prompt the user to enter the codebase directory path
3. Validate the provided codebase directory path
4. Prompt the user to enter the output directory path
5. Validate the provided output directory path
6. Store the user input for later use in the program """

import argparse
import os

from codypy import log_message, setup_logger
from pathspec import PathSpec


def validate_codebase_dir(codebase_dir):
if not os.path.exists(codebase_dir):
raise argparse.ArgumentTypeError(f"Codebase directory '{codebase_dir}' does not exist.")

git_dir = os.path.join(codebase_dir, '.git')
raise argparse.ArgumentTypeError(
f"Codebase directory '{codebase_dir}' does not exist."
)

git_dir = os.path.join(codebase_dir, ".git")
if not os.path.isdir(git_dir):
raise argparse.ArgumentTypeError(f"Codebase directory '{codebase_dir}' is not a local GitHub repository.")

raise argparse.ArgumentTypeError(
f"Codebase directory '{codebase_dir}' is not a local GitHub repository."
)

return codebase_dir

def main():
# Create a command-line interface (CLI) for the program
parser = argparse.ArgumentParser(description='CodyArchitect')
parser.add_argument('codebase_dir', type=validate_codebase_dir, help='Path to the codebase directory')
parser.add_argument('--output_dir', '-o', help='Path to the output directory for generated reports')

# Prompt the user to enter the codebase directory path
args = parser.parse_args()

# Store the user input for later use in the program
codebase_dir = args.codebase_dir
output_dir = args.output_dir


def collect_documentation_files(codebase_dir):
documentation_files = []
gitignore_path = os.path.join(codebase_dir, ".gitignore")
gitignore_spec = None

if os.path.exists(gitignore_path):
with open(gitignore_path, "r") as gitignore_file:
gitignore_spec = PathSpec.from_lines("gitwildmatch", gitignore_file)

for root, dirs, files in os.walk(codebase_dir):
if gitignore_spec:
dirs[:] = [
d for d in dirs if not gitignore_spec.match_file(os.path.join(root, d))
]

for file in files:
file_path = os.path.join(root, file)
if gitignore_spec and gitignore_spec.match_file(file_path):
continue

if file.endswith(".md") or file.endswith(".txt"):
documentation_files.append(file_path)

return documentation_files


def main(codebase_dir=None, output_dir=None):
if codebase_dir is None:
# Create a command-line interface (CLI) for the program
parser = argparse.ArgumentParser(description="CodyArchitect")
parser.add_argument(
"codebase_dir",
type=validate_codebase_dir,
help="Path to the codebase directory",
)
parser.add_argument(
"--output_dir",
"-o",
help="Path to the output directory for generated reports",
)

# Prompt the user to enter the codebase directory path
args = parser.parse_args()

# Store the user input for later use in the program
codebase_dir = args.codebase_dir
output_dir = args.output_dir

# If the user did not provide an output directory path, create one in the codebase directory
if not output_dir:
output_dir = os.path.join(codebase_dir, '.codyarchitect')
output_dir = os.path.join(codebase_dir, ".codyarchitect")
if not os.path.exists(output_dir):
os.makedirs(output_dir)

if __name__ == '__main__':
main()

documentation_files = collect_documentation_files(codebase_dir)
log_message("Main: ", f"{documentation_files}")


if __name__ == "__main__":

setup_logger("CodyArchitect", "logs")
codebase_dir = "."
main(codebase_dir)

0 comments on commit 2f62f69

Please sign in to comment.