Browse Source

直接方法已搜索完成

master
hejl 1 day ago
parent
commit
09e2c514a2
  1. 450
      gofaster/app/plugins/modules/trigger-analyzer.js
  2. 12
      gofaster/app/plugins/route-mapping-plugin.js
  3. 255
      gofaster/app/src/renderer/modules/route-sync/direct-route-mappings.js

450
gofaster/app/plugins/modules/trigger-analyzer.js

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
const { readFileSync, existsSync, readdirSync } = require('fs')
const { resolve } = require('path')
const parser = require('@babel/parser')
const traverse = require('@babel/traverse').default
const AstAnalyzer = require('./ast-analyzer')
/**
@ -12,7 +14,7 @@ class TriggerAnalyzer { @@ -12,7 +14,7 @@ class TriggerAnalyzer {
}
/**
* 查找API方法的触发器源
* 查找API方法的触发器源 - 第一步第一层触发源分析
* @param {Array} apiMappings - API映射数组
* @returns {Array} 增强的API映射数组
*/
@ -26,30 +28,72 @@ class TriggerAnalyzer { @@ -26,30 +28,72 @@ class TriggerAnalyzer {
}
moduleMapping.apiMappings.forEach(apiMapping => {
const triggerSources = this.findTriggerSourcesForApiMethod(
apiMapping.serviceName,
// 第一步:查找第一层触发源
const triggerSources = this.findFirstLevelTriggerSources(
moduleMapping.serviceName, // 使用第一层的serviceName
apiMapping.apiMethodName,
moduleMapping.module
)
if (triggerSources.length > 0) {
enhancedModuleMapping.apiMappings.push({
...apiMapping,
triggerSources: triggerSources
})
}
// 为所有API映射添加triggerSources字段(即使为空)
enhancedModuleMapping.apiMappings.push({
...apiMapping,
triggerSources: triggerSources || []
})
})
if (enhancedModuleMapping.apiMappings.length > 0) {
enhancedApiMappings.push(enhancedModuleMapping)
}
// 返回所有模块映射(包括没有触发源的)
enhancedApiMappings.push(enhancedModuleMapping)
})
return enhancedApiMappings
}
/**
* 查找API方法的触发器源
* 查找第一层触发源 - 使用Babel分析真正的调用关系
* @param {string} serviceName - 服务名称
* @param {string} apiMethodName - API方法名称
* @param {string} moduleName - 模块名称
* @returns {Array} 触发器源数组
*/
findFirstLevelTriggerSources(serviceName, apiMethodName, moduleName) {
const triggerSources = []
try {
// 1. 遍历模块中的所有Vue组件文件
const modulePath = resolve(__dirname, `../../src/renderer/modules/${moduleName}`)
if (existsSync(modulePath)) {
const files = readdirSync(modulePath, { recursive: true })
files.forEach(file => {
if (typeof file === 'string' && file.endsWith('.vue')) {
const filePath = resolve(modulePath, file)
const componentName = this.extractComponentNameFromPath(file)
// 2. 使用Babel分析Vue组件中的API调用
const componentTriggerSources = this.analyzeComponentForApiCalls(
filePath,
serviceName,
apiMethodName,
componentName
)
triggerSources.push(...componentTriggerSources)
}
})
}
} catch (error) {
console.warn(`分析模块 ${moduleName} 时出错:`, error.message)
}
// 去重
const uniqueTriggerSources = this.deduplicateTriggerSources(triggerSources)
return uniqueTriggerSources
}
/**
* 查找API方法的触发器源保留原方法用于后续扩展
* @param {string} serviceName - 服务名称
* @param {string} apiMethodName - API方法名称
* @param {string} moduleName - 模块名称
@ -105,17 +149,23 @@ class TriggerAnalyzer { @@ -105,17 +149,23 @@ class TriggerAnalyzer {
if (existsSync(routeConfigPath)) {
const routeContent = readFileSync(routeConfigPath, 'utf-8')
// 简单的路由解析,查找模块相关的路由
const routeMatches = routeContent.match(new RegExp(`component:\\s*['"]${moduleName}[^'"]*['"]`, 'g'))
if (routeMatches) {
routeMatches.forEach(match => {
const componentMatch = match.match(/component:\s*['"]([^'"]+)['"]/)
if (componentMatch) {
routeComponents.push({
component: componentMatch[1]
})
}
})
// 根据模块名称映射到对应的组件名称
const moduleToComponentMap = {
'role-management': 'RoleManagement',
'user-management': 'UserManagement',
'system-settings': 'Settings',
'user-profile': 'UserProfile'
}
const componentName = moduleToComponentMap[moduleName]
if (componentName) {
// 查找包含该组件的路由
const routeMatches = routeContent.match(new RegExp(`component:\\s*${componentName}`, 'g'))
if (routeMatches) {
routeComponents.push({
component: componentName
})
}
}
}
} catch (error) {
@ -402,6 +452,358 @@ class TriggerAnalyzer { @@ -402,6 +452,358 @@ class TriggerAnalyzer {
return true
})
}
/**
* 从文件路径提取组件名称
* @param {string} filePath - 文件路径
* @returns {string} 组件名称
*/
extractComponentNameFromPath(filePath) {
const fileName = filePath.split('/').pop().split('\\').pop()
return fileName.replace('.vue', '')
}
/**
* 分析Vue组件中的API调用
* @param {string} filePath - 组件文件路径
* @param {string} serviceName - 服务名称
* @param {string} apiMethodName - API方法名称
* @param {string} componentName - 组件名称
* @returns {Array} 触发源数组
*/
analyzeComponentForApiCalls(filePath, serviceName, apiMethodName, componentName) {
const triggerSources = []
try {
const content = readFileSync(filePath, 'utf-8')
// 使用Babel解析Vue组件
const ast = this.parseVueComponent(content)
if (!ast) return triggerSources
// 查找API调用和函数调用关系
const apiCalls = this.findApiCallsWithContext(ast, serviceName, apiMethodName)
// 为每个API调用创建触发源
apiCalls.forEach(call => {
triggerSources.push({
component: componentName,
triggerName: call.triggerName || '',
triggerType: call.triggerType || 'function'
})
})
} catch (error) {
console.warn(`分析组件 ${filePath} 时出错:`, error.message)
}
return triggerSources
}
/**
* 解析Vue组件内容
* @param {string} content - 组件内容
* @returns {Object|null} AST对象
*/
parseVueComponent(content) {
try {
// 提取<script>部分
const scriptMatch = content.match(/<script[^>]*>([\s\S]*?)<\/script>/)
if (!scriptMatch) return null
const scriptContent = scriptMatch[1]
// 使用Babel解析JavaScript代码
const ast = parser.parse(scriptContent, {
sourceType: 'module',
plugins: ['jsx', 'decorators-legacy', 'classProperties', 'objectRestSpread']
})
return ast
} catch (error) {
console.warn('解析Vue组件脚本时出错:', error.message)
return null
}
}
/**
* 在AST中查找API调用和函数调用关系
* @param {Object} ast - AST对象
* @param {string} serviceName - 服务名称
* @param {string} apiMethodName - API方法名称
* @returns {Array} API调用信息数组
*/
findApiCallsWithContext(ast, serviceName, apiMethodName) {
const apiCalls = []
const self = this
const functionCallMap = new Map() // 存储函数调用关系
// 第一遍:收集所有函数定义和调用关系
traverse(ast, {
// 收集函数定义
FunctionDeclaration(path) {
if (path.node.id && path.node.id.name) {
functionCallMap.set(path.node.id.name, {
type: 'function',
path: path
})
}
},
// 收集箭头函数和const函数
VariableDeclarator(path) {
if (path.node.id && path.node.id.type === 'Identifier' &&
path.node.init && path.node.init.type === 'ArrowFunctionExpression') {
functionCallMap.set(path.node.id.name, {
type: 'arrowFunction',
path: path
})
}
},
// 收集函数调用关系
CallExpression(path) {
const { node } = path
// 记录函数调用
if (node.callee.type === 'Identifier') {
const functionName = node.callee.name
const parentFunction = self.findParentFunction(path)
if (parentFunction) {
if (!functionCallMap.has(functionName)) {
functionCallMap.set(functionName, { callers: [] })
}
const funcInfo = functionCallMap.get(functionName)
if (!funcInfo.callers) funcInfo.callers = []
funcInfo.callers.push(parentFunction)
}
}
}
})
// 第二遍:查找API调用并分析调用链
traverse(ast, {
CallExpression(path) {
const { node } = path
// 查找 serviceName.apiMethodName() 调用
if (node.callee.type === 'MemberExpression' &&
node.callee.object.name === serviceName &&
node.callee.property.name === apiMethodName) {
// 查找调用上下文
const context = self.findCallContextWithChain(path, functionCallMap)
apiCalls.push({
triggerName: context.triggerName,
triggerType: context.triggerType
})
}
}
})
return apiCalls
}
/**
* 查找父级函数
* @param {Object} path - Babel路径对象
* @returns {string|null} 父级函数名
*/
findParentFunction(path) {
let currentPath = path.parentPath
while (currentPath) {
const { node } = currentPath
if (node.type === 'FunctionDeclaration' && node.id) {
return node.id.name
}
if (node.type === 'VariableDeclarator' &&
node.id && node.id.type === 'Identifier' &&
node.init && node.init.type === 'ArrowFunctionExpression') {
return node.id.name
}
if (node.type === 'ObjectMethod' && node.key) {
return node.key.name
}
currentPath = currentPath.parentPath
}
return null
}
/**
* 查找API调用的上下文包含调用链分析
* @param {Object} path - Babel路径对象
* @param {Map} functionCallMap - 函数调用关系映射
* @returns {Object} 上下文信息
*/
findCallContextWithChain(path, functionCallMap) {
let currentPath = path
let triggerName = ''
let triggerType = 'function'
// 向上遍历AST,查找触发源
while (currentPath) {
const { node } = currentPath
// 检查是否在Vue 3 Composition API生命周期钩子中
if (node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
['onMounted', 'onCreated', 'onBeforeMount', 'onBeforeCreate', 'onUpdated', 'onBeforeUpdate', 'onUnmounted', 'onBeforeUnmount'].includes(node.callee.name)) {
triggerName = node.callee.name
triggerType = 'lifecycle'
break
}
// 检查是否在Vue 3 Composition API的setup方法中
if (node.type === 'ObjectMethod' && node.key && node.key.name === 'setup') {
triggerName = 'setup'
triggerType = 'lifecycle' // setup是Vue 3的生命周期钩子
break
}
// 检查是否在方法定义中
if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') {
if (node.id && node.id.name) {
triggerName = node.id.name
triggerType = 'function'
break
}
}
// 检查是否在对象方法中(非setup)
if (node.type === 'ObjectMethod') {
if (node.key && node.key.name && node.key.name !== 'setup') {
triggerName = node.key.name
triggerType = 'method'
break
}
}
// 检查是否在箭头函数中
if (node.type === 'ArrowFunctionExpression') {
// 查找父级的属性名
const parent = currentPath.parent
if (parent && parent.type === 'ObjectProperty' && parent.key) {
const methodName = parent.key.name || ''
if (methodName === 'setup') {
triggerName = 'setup'
triggerType = 'lifecycle'
} else {
triggerName = methodName
triggerType = 'method'
}
break
}
// 检查是否在const声明的箭头函数中
if (parent && parent.type === 'VariableDeclarator' && parent.id) {
const functionName = parent.id.name || ''
triggerName = functionName
triggerType = 'method'
break
}
}
// 检查是否在Vue 2生命周期钩子中
if (node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.name === 'this' &&
['mounted', 'created', 'beforeMount', 'beforeCreate'].includes(node.callee.property.name)) {
triggerName = node.callee.property.name
triggerType = 'lifecycle'
break
}
currentPath = currentPath.parentPath
}
return { triggerName, triggerType }
}
/**
* 查找API调用的上下文
* @param {Object} path - Babel路径对象
* @returns {Object} 上下文信息
*/
findCallContext(path) {
let currentPath = path
let triggerName = ''
let triggerType = 'function'
// 向上遍历AST,查找触发源
while (currentPath) {
const { node } = currentPath
// 检查是否在Vue 3 Composition API生命周期钩子中
if (node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
['onMounted', 'onCreated', 'onBeforeMount', 'onBeforeCreate', 'onUpdated', 'onBeforeUpdate', 'onUnmounted', 'onBeforeUnmount'].includes(node.callee.name)) {
triggerName = node.callee.name
triggerType = 'lifecycle'
break
}
// 检查是否在Vue 3 Composition API的setup方法中
if (node.type === 'ObjectMethod' && node.key && node.key.name === 'setup') {
triggerName = 'setup'
triggerType = 'lifecycle' // setup是Vue 3的生命周期钩子
break
}
// 检查是否在方法定义中
if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression') {
if (node.id && node.id.name) {
triggerName = node.id.name
triggerType = 'function'
break
}
}
// 检查是否在对象方法中(非setup)
if (node.type === 'ObjectMethod') {
if (node.key && node.key.name && node.key.name !== 'setup') {
triggerName = node.key.name
triggerType = 'method'
break
}
}
// 检查是否在箭头函数中
if (node.type === 'ArrowFunctionExpression') {
// 查找父级的属性名
const parent = currentPath.parent
if (parent && parent.type === 'ObjectProperty' && parent.key) {
const methodName = parent.key.name || ''
if (methodName === 'setup') {
triggerName = 'setup'
triggerType = 'lifecycle'
} else {
triggerName = methodName
triggerType = 'method'
}
break
}
}
// 检查是否在Vue 2生命周期钩子中
if (node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.name === 'this' &&
['mounted', 'created', 'beforeMount', 'beforeCreate'].includes(node.callee.property.name)) {
triggerName = node.callee.property.name
triggerType = 'lifecycle'
break
}
currentPath = currentPath.parentPath
}
return { triggerName, triggerType }
}
}
module.exports = TriggerAnalyzer

12
gofaster/app/plugins/route-mapping-plugin.js

@ -130,7 +130,7 @@ function routeMappingPlugin() { @@ -130,7 +130,7 @@ function routeMappingPlugin() {
return
}
// 第三步第一小步:启用API收集功能
// 第三步第二小步:启用触发源关联功能
// 1. 分析路由配置
const routes = this._routeAnalyzer.analyzeRoutes()
@ -138,8 +138,11 @@ function routeMappingPlugin() { @@ -138,8 +138,11 @@ function routeMappingPlugin() {
const moduleDirs = this._routeAnalyzer.getModuleDirs()
const apiMappings = this._apiCollector.collectApiMappings(moduleDirs)
// 生成包含路由和API信息的文件
this._fileGenerator.generateRouteApiMappingFile(routes, apiMappings)
// 3. 关联页面与API(第三层)
const enhancedApiMappings = this._triggerAnalyzer.findTriggerSourcesForApiMappings(apiMappings)
// 生成包含路由和API关联信息的文件
this._fileGenerator.generateRouteApiMappingFile(routes, enhancedApiMappings)
// 重置生成中标志并更新时间戳
this._generationInProgress = false
@ -152,9 +155,6 @@ function routeMappingPlugin() { @@ -152,9 +155,6 @@ function routeMappingPlugin() {
// TODO: 后续步骤将在优化过程中逐步添加
// 3. 关联页面与API(第三层)
// const enhancedApiMappings = this._triggerAnalyzer.findTriggerSourcesForApiMappings(apiMappings)
// 4. 优化API映射
// const optimizedApiMappings = this._fileGenerator.optimizeApiMappings(enhancedApiMappings)

255
gofaster/app/src/renderer/modules/route-sync/direct-route-mappings.js

@ -39,62 +39,171 @@ export default { @@ -39,62 +39,171 @@ export default {
{
"apiMethodName": "getRoles",
"method": "GET",
"path": "/api/auth/roles"
"path": "/api/auth/roles",
"triggerSources": [
{
"component": "UserRoleAssignment",
"triggerName": "loadRoles",
"triggerType": "method"
},
{
"component": "RoleManagement",
"triggerName": "loadRoles",
"triggerType": "method"
}
]
},
{
"apiMethodName": "getRole",
"method": "GET",
"path": "/api/auth/roles/{id}"
"path": "/api/auth/roles/{id}",
"triggerSources": [
{
"component": "PermissionManager",
"triggerName": "loadRolePermissions",
"triggerType": "method"
}
]
},
{
"apiMethodName": "createRole",
"method": "POST",
"path": "/api/auth/roles"
"path": "/api/auth/roles",
"triggerSources": [
{
"component": "RoleManagement",
"triggerName": "saveRole",
"triggerType": "method"
}
]
},
{
"apiMethodName": "updateRole",
"method": "PUT",
"path": "/api/auth/roles/{id}"
"path": "/api/auth/roles/{id}",
"triggerSources": [
{
"component": "PermissionManager",
"triggerName": "savePermissions",
"triggerType": "method"
},
{
"component": "RoleManagement",
"triggerName": "saveRole",
"triggerType": "method"
}
]
},
{
"apiMethodName": "deleteRole",
"method": "DELETE",
"path": "/api/auth/roles/{id}"
"path": "/api/auth/roles/{id}",
"triggerSources": [
{
"component": "RoleManagement",
"triggerName": "deleteRole",
"triggerType": "method"
}
]
},
{
"apiMethodName": "getPermissions",
"method": "GET",
"path": "/api/auth/permissions"
"path": "/api/auth/permissions",
"triggerSources": [
{
"component": "PermissionManager",
"triggerName": "loadPermissions",
"triggerType": "method"
},
{
"component": "RolePermissionAssignment",
"triggerName": "loadAllPermissions",
"triggerType": "method"
}
]
},
{
"apiMethodName": "assignRolesToUser",
"method": "POST",
"path": "/api/auth/roles/users/{userId}/assign"
"path": "/api/auth/roles/users/{userId}/assign",
"triggerSources": [
{
"component": "UserRoleAssignment",
"triggerName": "saveRoleAssignment",
"triggerType": "method"
}
]
},
{
"apiMethodName": "getUserRoles",
"method": "GET",
"path": "/api/auth/roles/users/{userId}"
"path": "/api/auth/roles/users/{userId}",
"triggerSources": [
{
"component": "UserRoleAssignment",
"triggerName": "loadUserRoles",
"triggerType": "method"
}
]
},
{
"apiMethodName": "removeRolesFromUser",
"method": "DELETE",
"path": "/api/auth/roles/users/{userId}/remove"
"path": "/api/auth/roles/users/{userId}/remove",
"triggerSources": [
{
"component": "UserRoleAssignment",
"triggerName": "saveRoleAssignment",
"triggerType": "method"
}
]
},
{
"apiMethodName": "getRolePermissions",
"method": "GET",
"path": "/api/auth/permissions/roles/{roleId}"
"path": "/api/auth/permissions/roles/{roleId}",
"triggerSources": [
{
"component": "RolePermissionAssignment",
"triggerName": "loadRolePermissions",
"triggerType": "method"
}
]
},
{
"apiMethodName": "assignPermissionsToRole",
"method": "POST",
"path": "/api/auth/permissions/roles/{roleId}/assign"
"path": "/api/auth/permissions/roles/{roleId}/assign",
"triggerSources": [
{
"component": "RolePermissionAssignment",
"triggerName": "assignPermission",
"triggerType": "method"
},
{
"component": "RolePermissionAssignment",
"triggerName": "assignSelectedPermissions",
"triggerType": "method"
}
]
},
{
"apiMethodName": "removePermissionsFromRole",
"method": "DELETE",
"path": "/api/auth/permissions/roles/{roleId}/remove"
"path": "/api/auth/permissions/roles/{roleId}/remove",
"triggerSources": [
{
"component": "RolePermissionAssignment",
"triggerName": "removePermission",
"triggerType": "method"
},
{
"component": "RolePermissionAssignment",
"triggerName": "removeSelectedPermissions",
"triggerType": "method"
}
]
}
]
},
@ -105,87 +214,175 @@ export default { @@ -105,87 +214,175 @@ export default {
{
"apiMethodName": "getUsers",
"method": "GET",
"path": "/api/auth/admin/users"
"path": "/api/auth/admin/users",
"triggerSources": [
{
"component": "UserManagement",
"triggerName": "loadUsers",
"triggerType": "method"
}
]
},
{
"apiMethodName": "getUser",
"method": "GET",
"path": "/api/auth/admin/users/{id}"
"path": "/api/auth/admin/users/{id}",
"triggerSources": []
},
{
"apiMethodName": "createUser",
"method": "POST",
"path": "/api/auth/admin/users"
"path": "/api/auth/admin/users",
"triggerSources": [
{
"component": "UserManagement",
"triggerName": "submitUser",
"triggerType": "method"
}
]
},
{
"apiMethodName": "updateUser",
"method": "PUT",
"path": "/api/auth/admin/users/{id}"
"path": "/api/auth/admin/users/{id}",
"triggerSources": [
{
"component": "UserManagement",
"triggerName": "submitUser",
"triggerType": "method"
}
]
},
{
"apiMethodName": "deleteUser",
"method": "DELETE",
"path": "/api/auth/admin/users/{id}"
"path": "/api/auth/admin/users/{id}",
"triggerSources": [
{
"component": "UserManagement",
"triggerName": "deleteUser",
"triggerType": "method"
}
]
},
{
"apiMethodName": "getRoles",
"method": "GET",
"path": "/api/auth/admin/roles"
"path": "/api/auth/admin/roles",
"triggerSources": []
},
{
"apiMethodName": "changePassword",
"method": "POST",
"path": "/api/auth/change-password"
"path": "/api/auth/change-password",
"triggerSources": [
{
"component": "PasswordChangeModal",
"triggerName": "handleSubmit",
"triggerType": "method"
}
]
},
{
"apiMethodName": "getPasswordPolicy",
"method": "GET",
"path": "/api/auth/password-policy"
"path": "/api/auth/password-policy",
"triggerSources": [
{
"component": "PasswordChangeModal",
"triggerName": "loadPasswordPolicy",
"triggerType": "method"
}
]
},
{
"apiMethodName": "validatePassword",
"method": "POST",
"path": "/api/auth/validate-password"
"path": "/api/auth/validate-password",
"triggerSources": [
{
"component": "PasswordChangeModal",
"triggerName": "validatePassword",
"triggerType": "method"
}
]
},
{
"apiMethodName": "checkPasswordStatus",
"method": "GET",
"path": "/api/auth/password-status"
"path": "/api/auth/password-status",
"triggerSources": []
},
{
"apiMethodName": "getPermissions",
"method": "GET",
"path": "/api/permissions"
"path": "/api/permissions",
"triggerSources": []
},
{
"apiMethodName": "getCaptcha",
"method": "GET",
"path": "/api/auth/captcha"
"path": "/api/auth/captcha",
"triggerSources": [
{
"component": "LoginModal",
"triggerName": "refreshCaptcha",
"triggerType": "method"
},
{
"component": "LoginModal",
"triggerName": "refreshCaptchaWithoutClearError",
"triggerType": "method"
}
]
},
{
"apiMethodName": "login",
"method": "POST",
"path": "/api/auth/login"
"path": "/api/auth/login",
"triggerSources": [
{
"component": "LoginModal",
"triggerName": "handleLogin",
"triggerType": "method"
}
]
},
{
"apiMethodName": "logout",
"method": "POST",
"path": "/api/auth/logout"
"path": "/api/auth/logout",
"triggerSources": []
},
{
"apiMethodName": "getCurrentUser",
"method": "GET",
"path": "/api/auth/me"
"path": "/api/auth/me",
"triggerSources": [
{
"component": "UserProfile",
"triggerName": "loadUserProfile",
"triggerType": "method"
}
]
},
{
"apiMethodName": "changePassword",
"method": "POST",
"path": "/api/auth/change-password"
"path": "/api/auth/change-password",
"triggerSources": [
{
"component": "PasswordChangeModal",
"triggerName": "handleSubmit",
"triggerType": "method"
}
]
},
{
"apiMethodName": "resetPassword",
"method": "POST",
"path": "/api/auth/reset-password"
"path": "/api/auth/reset-password",
"triggerSources": []
}
]
}

Loading…
Cancel
Save