From a7256bac54ade7271e7ffb9d9dd61c84bf76fa6e Mon Sep 17 00:00:00 2001 From: hejl Date: Sun, 7 Sep 2025 11:31:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AD=A5=E6=94=B6=E9=9B=86?= =?UTF-8?q?=E5=AE=8C=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/plugins/modules/file-generator.js | 23 +++++++++ .../app/plugins/modules/route-analyzer.js | 49 +++---------------- gofaster/app/plugins/route-mapping-plugin.js | 10 ++-- .../modules/route-sync/RouteCollector.js | 2 +- .../route-sync/direct-route-mappings.js | 29 ++++++++++- gofaster/app/src/renderer/router/index.js | 15 ++++-- gofaster/app/test-optimized-collection.js | 11 +++++ 7 files changed, 87 insertions(+), 52 deletions(-) create mode 100644 gofaster/app/test-optimized-collection.js diff --git a/gofaster/app/plugins/modules/file-generator.js b/gofaster/app/plugins/modules/file-generator.js index fae210a..c039758 100644 --- a/gofaster/app/plugins/modules/file-generator.js +++ b/gofaster/app/plugins/modules/file-generator.js @@ -49,6 +49,29 @@ export default { writeFileSync(this.outputPath, content, 'utf-8') } + /** + * 生成路由映射文件 - 第二步:包含路由信息 + * @param {Array} routes - 路由数组 + */ + generateRouteMappingFile(routes) { + const content = `// 路由映射数据 - 构建时生成 +export default { + // 路由配置 + routes: ${JSON.stringify(routes, null, 2)}, + + // API映射配置 + apiMappings: [] +}` + + // 如果文件已存在,先清空内容 + if (existsSync(this.outputPath)) { + writeFileSync(this.outputPath, '', 'utf-8') + } + + // 写入新内容 + writeFileSync(this.outputPath, content, 'utf-8') + } + /** * 生成文件内容 * @param {Array} routes - 路由数组 diff --git a/gofaster/app/plugins/modules/route-analyzer.js b/gofaster/app/plugins/modules/route-analyzer.js index 1963a65..7f0ee0a 100644 --- a/gofaster/app/plugins/modules/route-analyzer.js +++ b/gofaster/app/plugins/modules/route-analyzer.js @@ -31,7 +31,7 @@ class RouteAnalyzer { } /** - * 解析路由配置 + * 解析路由配置 - 优化版本:直接从路由配置中收集信息 * @param {string} routeContent - 路由配置内容 * @returns {Array} 路由数组 */ @@ -47,6 +47,7 @@ class RouteAnalyzer { const pathMatch = match.match(/path:\s*['"]([^'"]+)['"]/) const componentMatch = match.match(/component:\s*([A-Za-z][A-Za-z0-9]*)/) const nameMatch = match.match(/name:\s*['"]([^'"]+)['"]/) + const descriptionMatch = match.match(/description:\s*['"]([^'"]+)['"]/) if (pathMatch && componentMatch) { const route = { @@ -54,12 +55,15 @@ class RouteAnalyzer { component: componentMatch[1] } - if (nameMatch) { + // 只有当name与component不同时才保留name字段 + if (nameMatch && nameMatch[1] !== componentMatch[1]) { route.name = nameMatch[1] } - // 添加中文描述 - route.description = this._generateRouteDescription(route) + // 优先使用路由配置中的description,如果没有则生成 + if (descriptionMatch) { + route.description = descriptionMatch[1] + } routes.push(route) } @@ -69,43 +73,6 @@ class RouteAnalyzer { return routes } - /** - * 生成路由的中文描述 - * @param {Object} route - 路由对象 - * @returns {string} 中文描述 - */ - _generateRouteDescription(route) { - const path = route.path - const component = route.component - - // 根据路径和组件名称生成中文描述 - if (path === '/' || path === '') { - return '首页' - } else if (path.includes('user-management')) { - return '用户管理' - } else if (path.includes('role-management')) { - return '角色管理' - } else if (path.includes('user-profile')) { - return '个人资料' - } else if (path.includes('settings')) { - return '系统设置' - } else if (component === 'MainLayout') { - return '主布局' - } else if (component === 'Home') { - return '首页' - } else if (component === 'UserManagement') { - return '用户管理' - } else if (component === 'RoleManagement') { - return '角色管理' - } else if (component === 'UserProfile') { - return '个人资料' - } else if (component === 'Settings') { - return '系统设置' - } else { - return `${component}页面` - } - } - /** * 获取模块列表 * @returns {Array} 模块目录列表 diff --git a/gofaster/app/plugins/route-mapping-plugin.js b/gofaster/app/plugins/route-mapping-plugin.js index 288c1f9..8f6be8f 100644 --- a/gofaster/app/plugins/route-mapping-plugin.js +++ b/gofaster/app/plugins/route-mapping-plugin.js @@ -130,8 +130,12 @@ function routeMappingPlugin() { return } - // 简化版本:只创建基础文件结构 - this._fileGenerator.generateBasicMappingFile() + // 第二步:启用路由分析功能 + // 1. 分析路由配置 + const routes = this._routeAnalyzer.analyzeRoutes() + + // 生成包含路由信息的文件 + this._fileGenerator.generateRouteMappingFile(routes) // 重置生成中标志并更新时间戳 this._generationInProgress = false @@ -143,8 +147,6 @@ function routeMappingPlugin() { } // TODO: 后续步骤将在优化过程中逐步添加 - // 1. 分析路由配置 - // const routes = this._routeAnalyzer.analyzeRoutes() // 2. 收集API信息(第一层) // const moduleDirs = this._routeAnalyzer.getModuleDirs() diff --git a/gofaster/app/src/renderer/modules/route-sync/RouteCollector.js b/gofaster/app/src/renderer/modules/route-sync/RouteCollector.js index 18aa5fc..99958c7 100644 --- a/gofaster/app/src/renderer/modules/route-sync/RouteCollector.js +++ b/gofaster/app/src/renderer/modules/route-sync/RouteCollector.js @@ -37,7 +37,7 @@ export class RouteCollector { return { path: route.path, - name: route.name, + name: route.name || route.component, // 如果有name就取name,没有就取component module: route.module, description: route.description || `${route.name || route.component}页面`, type: 'list', diff --git a/gofaster/app/src/renderer/modules/route-sync/direct-route-mappings.js b/gofaster/app/src/renderer/modules/route-sync/direct-route-mappings.js index 94ccb1c..b067131 100644 --- a/gofaster/app/src/renderer/modules/route-sync/direct-route-mappings.js +++ b/gofaster/app/src/renderer/modules/route-sync/direct-route-mappings.js @@ -1,7 +1,34 @@ // 路由映射数据 - 构建时生成 export default { // 路由配置 - routes: [], + routes: [ + { + "path": "/", + "component": "MainLayout", + "name": "Home", + "description": "首页" + }, + { + "path": "/user-management", + "component": "UserManagement", + "description": "用户管理" + }, + { + "path": "/settings", + "component": "Settings", + "description": "系统设置" + }, + { + "path": "/user-profile", + "component": "UserProfile", + "description": "个人资料" + }, + { + "path": "/role-management", + "component": "RoleManagement", + "description": "角色管理" + } +], // API映射配置 apiMappings: [] diff --git a/gofaster/app/src/renderer/router/index.js b/gofaster/app/src/renderer/router/index.js index bcde758..f02cc6e 100644 --- a/gofaster/app/src/renderer/router/index.js +++ b/gofaster/app/src/renderer/router/index.js @@ -14,27 +14,32 @@ const routes = [ { path: '', name: 'Home', - component: Home + component: Home, + description: '首页' }, { path: '/user-management', name: 'UserManagement', - component: UserManagement + component: UserManagement, + description: '用户管理' }, { path: '/settings', name: 'Settings', - component: Settings + component: Settings, + description: '系统设置' }, { path: '/user-profile', name: 'UserProfile', - component: UserProfile + component: UserProfile, + description: '个人资料' }, { path: '/role-management', name: 'RoleManagement', - component: RoleManagement + component: RoleManagement, + description: '角色管理' } ] } diff --git a/gofaster/app/test-optimized-collection.js b/gofaster/app/test-optimized-collection.js new file mode 100644 index 0000000..ce1fc5f --- /dev/null +++ b/gofaster/app/test-optimized-collection.js @@ -0,0 +1,11 @@ +import { RouteCollector } from './src/renderer/modules/route-sync/RouteCollector.js'; + +const collector = new RouteCollector(); +const routes = collector.collectRoutes(); + +console.log('优化后的路由收集结果:'); +console.log('路由数量:', routes.length); +console.log('路由详情:'); +routes.forEach((route, index) => { + console.log(`${index + 1}. ${route.path} - name: ${route.name} - component: ${route.component} - description: ${route.description}`); +});