import os
import sys
def generate_project_structure(root_path, output_file="project_structure.md", ignore_dirs=None):
"""
Generate a markdown file with visually indented folder tree structure.
Output is written to the same directory as the script.
Args:
root_path: المسار الجذر للمشروع
output_file: اسم ملف الإخراج
ignore_dirs: قائمة المجلدات المراد تجاهلها (مثل node_modules, __pycache__)
"""
if ignore_dirs is None:
ignore_dirs = ['node_modules', '__pycache__', '.git', 'old_data', 'dist', 'build', 'plugins', 'lib']
try:
root_path = os.path.abspath(root_path)
output_path = os.path.join(root_path, output_file)
md_content = f"# Project Structure\n\n**المسار:** `{root_path}`\n\n"
md_content += "```yaml\n"
# اسم المجلد الجذر
root_name = os.path.basename(root_path)
md_content += f"{root_name}/\n"
# Walk through directory tree with indentation
stack = []
prefix = "├── "
last_prefix = "└── "
# استثناء المجلدات المراد تجاهلها
for root, dirs, files in os.walk(root_path):
# تصفية المجلدات المستثناة
dirs[:] = [d for d in dirs if d not in ignore_dirs]
# تحديد مستوى العمق
rel_path = os.path.relpath(root, root_path)
if rel_path == '.':
level = 0
else:
level = rel_path.count(os.sep) + 1
indent = " " * level
# عرض المجلد الحالي (إذا لم يكن الجذر)
if level > 0:
folder_name = os.path.basename(root)
# تحديد ما إذا كان هذا آخر مجلد في المستوى
is_last = (root == root_path) or (len(os.listdir(os.path.dirname(root))) == 0)
connector = last_prefix if is_last else prefix
md_content += f"{indent}{connector}{folder_name}/\n"
# عرض الملفات
files_in_dir = sorted([f for f in files if not f.startswith('.')])
for i, file in enumerate(files_in_dir):
is_last_file = (i == len(files_in_dir) - 1) and (len(dirs) == 0)
file_connector = last_prefix if is_last_file else prefix
file_ext = os.path.splitext(file)[1] or '(no ext)'
md_content += f"{indent} {file_connector}{file} [{file_ext}]\n"
md_content += "```\n"
# إضافة إحصائيات
md_content += f"\n## 📊 الإحصائيات\n\n"
md_content += f"- **تاريخ الإنشاء:** {__import__('datetime').datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
md_content += f"- **الأداة:** `get_structure.py`\n"
md_content += f"- **المجلدات المستثناة:** {', '.join(ignore_dirs)}\n"
# كتابة الملف
with open(output_path, 'w', encoding='utf-8') as f:
f.write(md_content)
print(f"✅ تم إنشاء {output_file} بنجاح في {root_path}")
return True
except Exception as e:
print(f"❌ خطأ: {e}")
return False
# التنفيذ الرئيسي
if __name__ == "__main__":
# استخدم المجلد الحالي أو المسار المحدد كـ argument
if len(sys.argv) > 1:
target_path = sys.argv[1]
else:
target_path = os.getcwd()
generate_project_structure(target_path)