-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.py
97 lines (76 loc) · 3.75 KB
/
menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# import os
# import re
# from collections import defaultdict
# # Define the directory
# directory = '/home/sridhar/projects/bip-laravel/app/Nova'
# # Define regex patterns for extracting the group, label, and class name
# group_pattern = re.compile(r'public static \$group\s*=\s*[\'"](.*?)[\'"];')
# label_pattern = re.compile(r'public static function label\(\)\s*\{\s*return\s*[\'"](.*?)[\'"];\s*\}')
# class_pattern = re.compile(r'class\s+(\w+)')
# # Initialize a dictionary to store grouped menu data
# grouped_menus = defaultdict(list)
# # Loop through all PHP files in the directory
# for filename in os.listdir(directory):
# if filename.endswith('.php'):
# file_path = os.path.join(directory, filename)
# # Read the file content
# with open(file_path, 'r') as file:
# content = file.read()
# # Find matches for group and class
# group_match = group_pattern.search(content)
# class_match = class_pattern.search(content)
# # Check for label; if not found, use class name as submenu
# label_match = label_pattern.search(content)
# if group_match and class_match:
# group = group_match.group(1)
# class_name = class_match.group(1)
# label = label_match.group(1) if label_match else class_name
# # Append to grouped_menus under the group key
# grouped_menus[group].append({
# 'sub_menu': label,
# 'class': class_name
# })
# # Print the grouped menu structure
# for main_menu, items in grouped_menus.items():
# print(f"Main Menu: {main_menu}")
# for item in items:
# print(f" Sub Menu: {item['sub_menu']}, Class: {item['class']}")
import os
import re
from collections import defaultdict
# Define the directory
directory = '/home/sridhar/projects/bip-laravel/app/Nova'
# Define regex patterns for extracting the group, label, and class name
group_pattern = re.compile(r'public static \$group\s*=\s*[\'"](.*?)[\'"];')
label_pattern = re.compile(r'public static function label\(\)\s*\{\s*return\s*[\'"](.*?)[\'"];\s*\}')
class_pattern = re.compile(r'class\s+(\w+)')
# Initialize a dictionary to store grouped menu data
grouped_menus = defaultdict(list)
# Loop through all PHP files in the directory
for filename in os.listdir(directory):
if filename.endswith('.php'):
file_path = os.path.join(directory, filename)
# Read the file content
with open(file_path, 'r') as file:
content = file.read()
# Find matches for group, label, and class
group_match = group_pattern.search(content)
class_match = class_pattern.search(content)
label_match = label_pattern.search(content)
if group_match and class_match:
group = group_match.group(1)
class_name = class_match.group(1)
label = label_match.group(1) if label_match else class_name # Use class name as label if label function is missing
# Append to grouped_menus under the group key
grouped_menus[group].append({
'sub_menu': label,
'class': class_name
})
# Sort the grouped menu sections alphabetically by main menu
sorted_groups = sorted(grouped_menus.items())
# Print the grouped menu structure in MenuSection format
for main_menu, items in sorted_groups:
print(f"MenuSection::make('{main_menu}', [")
for item in items:
print(f" MenuItem::resource({item['class']}::class)->label('{item['sub_menu']}'),")
print("])->icon('academic-cap')->collapsable();\n")