55 lines
2.0 KiB
Python
55 lines
2.0 KiB
Python
# .agent/tools/map_project.py
|
|
import os
|
|
import pathspec
|
|
import sys
|
|
|
|
# Ensure stdout uses utf-8
|
|
if sys.platform == 'win32':
|
|
import io
|
|
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', line_buffering=True)
|
|
|
|
def generate_tree(start_path='.', gitignore_file='.gitignore'):
|
|
"""Gera árvore de arquivos ignorando lixo, compatível com Windows."""
|
|
|
|
# 1. Carrega regras do .gitignore
|
|
patterns = ['.git', '.agent', '__pycache__', 'node_modules', 'venv', '.idea', '.vscode', '*.tmp', '*.log']
|
|
if os.path.exists(gitignore_file):
|
|
with open(gitignore_file, 'r', encoding='utf-8') as f:
|
|
patterns.extend(f.read().splitlines())
|
|
|
|
spec = pathspec.PathSpec.from_lines('gitwildmatch', patterns)
|
|
tree_output = ["# 🗺️ MAPA DO PROJETO - ESTRUTURA REAL", ""]
|
|
|
|
for root, dirs, files in os.walk(start_path):
|
|
# Normaliza o caminho para comparison com o spec
|
|
rel_root = os.path.relpath(root, start_path)
|
|
if rel_root == '.':
|
|
rel_root = ''
|
|
|
|
# Remove diretórios ignorados "in-place"
|
|
dirs[:] = [d for d in dirs if not spec.match_file(os.path.join(rel_root, d))]
|
|
|
|
level = rel_root.count(os.sep) if rel_root else 0
|
|
indent = ' ' * level
|
|
folder_name = os.path.basename(root)
|
|
|
|
if rel_root == '':
|
|
tree_output.append(f'📂 {os.path.basename(os.path.abspath(start_path))}/')
|
|
else:
|
|
tree_output.append(f'{indent}📂 {folder_name}/')
|
|
|
|
subindent = ' ' * (level + 1)
|
|
for f in files:
|
|
file_path = os.path.join(rel_root, f)
|
|
if not spec.match_file(file_path):
|
|
tree_output.append(f'{subindent}📄 {f}')
|
|
|
|
return '\n'.join(tree_output)
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
print(generate_tree())
|
|
except Exception as e:
|
|
import traceback
|
|
print(f"Erro ao mapear: {e}", file=sys.stderr)
|
|
traceback.print_exc() |