forked from RenjiYuusei/CursorFocus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules_analyzer.py
More file actions
260 lines (230 loc) · 9.57 KB
/
rules_analyzer.py
File metadata and controls
260 lines (230 loc) · 9.57 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import os
import json
from typing import Dict, Any
class RulesAnalyzer:
def __init__(self, project_path: str):
self.project_path = project_path
def analyze_project_for_rules(self) -> Dict[str, Any]:
"""Analyze the project and return project information for rules generation."""
project_info = {
'name': self._detect_project_name(),
'version': '1.0.0',
'language': self._detect_main_language(),
'framework': self._detect_framework(),
'type': self._detect_project_type()
}
return project_info
def _detect_project_name(self) -> str:
"""Detect the project name from package files or directory name."""
# Try package.json
package_json_path = os.path.join(self.project_path, 'package.json')
if os.path.exists(package_json_path):
try:
with open(package_json_path, 'r') as f:
data = json.load(f)
if data.get('name'):
return data['name']
except:
pass
# Try setup.py
setup_py_path = os.path.join(self.project_path, 'setup.py')
if os.path.exists(setup_py_path):
try:
with open(setup_py_path, 'r') as f:
content = f.read()
if 'name=' in content:
# Simple extraction, could be improved
name = content.split('name=')[1].split(',')[0].strip("'\"")
if name:
return name
except:
pass
# Default to directory name
return os.path.basename(os.path.abspath(self.project_path))
def _detect_main_language(self) -> str:
"""Detect the main programming language used in the project."""
extensions = {}
for root, _, files in os.walk(self.project_path):
if 'node_modules' in root or 'venv' in root or '.git' in root:
continue
for file in files:
ext = os.path.splitext(file)[1].lower()
if ext:
extensions[ext] = extensions.get(ext, 0) + 1
# Map extensions to languages
language_map = {
'.py': 'python',
'.pyi': 'python',
'.js': 'javascript',
'.ts': 'typescript',
'.jsx': 'react',
'.tsx': 'typescript-react',
'.vue': 'vue',
'.go': 'golang',
'.rs': 'rust',
'.java': 'java',
'.kt': 'kotlin',
'.kts': 'kotlin',
'.swift': 'swift',
'.c': 'c',
'.h': 'c',
'.cpp': 'cpp',
'.cc': 'cpp',
'.cxx': 'cpp',
'.hpp': 'cpp',
'.hxx': 'cpp',
'.cs': 'csharp',
'.cshtml': 'csharp',
'.rb': 'ruby',
'.php': 'php',
'.lua': 'lua',
}
# Find the most common language
max_count = 0
main_language = 'javascript' # default
for ext, count in extensions.items():
if ext in language_map and count > max_count:
max_count = count
main_language = language_map[ext]
return main_language
def _detect_framework(self) -> str:
"""Detect the framework used in the project."""
# Check package.json for JS/TS frameworks
package_json_path = os.path.join(self.project_path, 'package.json')
if os.path.exists(package_json_path):
try:
with open(package_json_path, 'r') as f:
data = json.load(f)
deps = {**data.get('dependencies', {}), **data.get('devDependencies', {})}
if 'react' in deps:
return 'react'
if 'vue' in deps:
return 'vue'
if '@angular/core' in deps:
return 'angular'
if 'next' in deps:
return 'next.js'
if 'express' in deps:
return 'express'
except:
pass
# Check requirements.txt for Python frameworks
requirements_path = os.path.join(self.project_path, 'requirements.txt')
if os.path.exists(requirements_path):
try:
with open(requirements_path, 'r') as f:
content = f.read().lower()
if 'django' in content:
return 'django'
if 'flask' in content:
return 'flask'
if 'fastapi' in content:
return 'fastapi'
except:
pass
# Check composer.json for PHP frameworks
composer_path = os.path.join(self.project_path, 'composer.json')
if os.path.exists(composer_path):
try:
with open(composer_path, 'r') as f:
data = json.load(f)
deps = {**data.get('require', {}), **data.get('require-dev', {})}
if 'laravel/framework' in deps:
return 'laravel'
if 'symfony/symfony' in deps:
return 'symfony'
if 'cakephp/cakephp' in deps:
return 'cakephp'
if 'codeigniter/framework' in deps:
return 'codeigniter'
if 'yiisoft/yii2' in deps:
return 'yii2'
except:
pass
# Check for WordPress
if os.path.exists(os.path.join(self.project_path, 'wp-config.php')):
return 'wordpress'
# Check for C++ frameworks
cmake_path = os.path.join(self.project_path, 'CMakeLists.txt')
if os.path.exists(cmake_path):
try:
with open(cmake_path, 'r') as f:
content = f.read().lower()
if 'qt' in content:
return 'qt'
if 'boost' in content:
return 'boost'
if 'opencv' in content:
return 'opencv'
except:
pass
# Check for C# frameworks
csproj_files = [f for f in os.listdir(self.project_path) if f.endswith('.csproj')]
for csproj in csproj_files:
try:
with open(os.path.join(self.project_path, csproj), 'r') as f:
content = f.read().lower()
if 'microsoft.aspnetcore' in content:
return 'asp.net core'
if 'microsoft.net.sdk.web' in content:
return 'asp.net core'
if 'xamarin' in content:
return 'xamarin'
if 'microsoft.maui' in content:
return 'maui'
except:
pass
# Check for Swift frameworks
podfile_path = os.path.join(self.project_path, 'Podfile')
if os.path.exists(podfile_path):
try:
with open(podfile_path, 'r') as f:
content = f.read().lower()
if 'swiftui' in content:
return 'swiftui'
if 'combine' in content:
return 'combine'
if 'vapor' in content:
return 'vapor'
except:
pass
# Check for Kotlin frameworks
build_gradle_path = os.path.join(self.project_path, 'build.gradle')
if os.path.exists(build_gradle_path):
try:
with open(build_gradle_path, 'r') as f:
content = f.read().lower()
if 'org.jetbrains.compose' in content:
return 'jetpack compose'
if 'org.springframework.boot' in content:
return 'spring boot'
if 'ktor' in content:
return 'ktor'
except:
pass
return 'none'
def _detect_project_type(self) -> str:
"""Detect the type of project (web, mobile, library, etc.)."""
package_json_path = os.path.join(self.project_path, 'package.json')
if os.path.exists(package_json_path):
try:
with open(package_json_path, 'r') as f:
data = json.load(f)
deps = {**data.get('dependencies', {}), **data.get('devDependencies', {})}
# Check for mobile frameworks
if 'react-native' in deps or '@ionic/core' in deps:
return 'mobile application'
# Check for desktop frameworks
if 'electron' in deps:
return 'desktop application'
# Check if it's a library
if data.get('name', '').startswith('@') or '-lib' in data.get('name', ''):
return 'library'
except:
pass
# Look for common web project indicators
web_indicators = ['index.html', 'public/index.html', 'src/index.html']
for indicator in web_indicators:
if os.path.exists(os.path.join(self.project_path, indicator)):
return 'web application'
return 'application'