diff --git a/src/commands/form/codegen.ts b/src/commands/form/codegen.ts index 660c9c3..798d524 100644 --- a/src/commands/form/codegen.ts +++ b/src/commands/form/codegen.ts @@ -126,23 +126,47 @@ function validateCode(value: string, label: string): string | null { /** * snake_case → PascalCase 转换, 用于从 form.enCode 推导 Java 类名 * - * 例: - * "jxcj_announcement" → "JxcjAnnouncement" - * "jxcj_develop_project" → "JxcjDevelopProject" - * "jxcj" → "Jxcj" - * "jxcj_notification_record" → "JxcjNotificationRecord" + * 例 (appCode=jxcj): + * "jxcj_announcement" → "Announcement" (丢 appCode 前缀, 因包名已带) + * "jxcj_develop_project" → "DevelopProject" + * "jxcj" → "Jxcj" (无下划线保留) + * "jxcj_notification_record" → "NotificationRecord" + * "JxcjAnnouncement" (已 PascalCase) → 原样返回 * - * 规则: 按 `_` 切分, 每段首字母大写, 然后拼接; 若首段首字符是字母则强制大写 - * (e.g. "jxcj" → "Jxcj"). 已 PascalCase 风格的输入原样返回. + * 规则: + * - 含下划线 + 首段是 PascalCase 单段 (e.g. "Jxcj_xxx") → 丢首段, rest PascalCase + * - 含下划线 + 首段是小写 (e.g. "jxcj_xxx") → 丢首段, rest PascalCase + * - 不含下划线 + 首字母大写 → 原样返回 + * - 不含下划线 + 首字母小写 → 强制大写 + * + * 理由: 包名已带 appCode (ssw.jxcj.mapper), 类名不重复 */ -export function snakeToPascalCase(s: string): string { +export function snakeToPascalCase(s: string, appCode?: string): string { if (!s) return s - // 已是 PascalCase (无下划线, 首字母大写) → 原样返回, 避免破坏用户传入的 PascalCase + // 已是 PascalCase (无下划线, 首字母大写) → 原样返回 if (!s.includes('_') && /^[A-Z]/.test(s)) return s const parts = s.split('_').filter(p => p.length > 0) - return parts - .map(p => p.charAt(0).toUpperCase() + p.slice(1)) - .join('') + if (parts.length === 0) return s + // 丢首段 (appCode 前缀) + let rest = parts + const firstSegment = parts[0] + // 首段与 appCode 匹配 (大小写不敏感) → 丢 + if (appCode && firstSegment.toLowerCase() === appCode.toLowerCase()) { + rest = parts.slice(1) + } + // 首段是 PascalCase 单段 (Jxcj) + 后段存在 → 视为 appCode 丢 + else if (/^[A-Z][a-z0-9]*$/.test(firstSegment) && parts.length > 1) { + rest = parts.slice(1) + } + // 首段是小写 (jxcj) + 后段存在 → 视为 appCode 丢 + else if (/^[a-z][a-z0-9]*$/.test(firstSegment) && parts.length > 1) { + rest = parts.slice(1) + } + if (rest.length === 0) { + // 极端: enCode 就是 appCode (e.g. "jxcj") → 返回 appCode PascalCase + return appCode ? appCode.charAt(0).toUpperCase() + appCode.slice(1) : s + } + return rest.map(p => p.charAt(0).toUpperCase() + p.slice(1)).join('') } /** 分类文件: 前端 vs 后端, 后端再细分 */ @@ -176,10 +200,16 @@ function classifyFile(item: CodeTreeItem): 'web' | 'entity' | 'model' | 'service * 返回: 旧前缀字符串 (如 "Ais_aisprojectInfo") 或 null (无需修复) */ function detectOldPrefixFromDir(outDir: string, moduleArtifactId: string): string | null { - // aibridge 路径: {backend_path}/{module_name}-entity/src/main/java/{module_package}/entity - // dacli 简化: outDir 里 entity 类通常直接放根或 entity/ 子目录 + // 候选目录: aibridge 标准 (src/main/java) + dacli 简化 (entity/ mapper/ controller/ 直接在模块根) const candidateDirs = [ join(outDir, `${moduleArtifactId}-entity`, 'src', 'main', 'java', 'entity'), + join(outDir, `${moduleArtifactId}-entity`, 'entity'), // dacli 实际输出路径 + join(outDir, `${moduleArtifactId}-biz`, 'src', 'main', 'java', 'mapper'), + join(outDir, `${moduleArtifactId}-biz`, 'mapper'), // dacli 实际输出路径 + join(outDir, `${moduleArtifactId}-controller`, 'src', 'main', 'java', 'controller'), + join(outDir, `${moduleArtifactId}-controller`, 'controller'), // dacli 实际输出路径 + join(outDir, `${moduleArtifactId}-entity`, 'src', 'main', 'java', 'model'), + join(outDir, `${moduleArtifactId}-entity`, 'model'), join(outDir, 'entity'), ] const suffixes = [ @@ -199,7 +229,7 @@ function detectOldPrefixFromDir(outDir: string, moduleArtifactId: string): strin for (const suf of suffixes) { if (f.endsWith(suf)) { const prefix = f.slice(0, -suf.length) - // aibridge 规则: 含下划线 + 首字母大写 = 需要修复 + // 规则: 含下划线 + 首字母大写 = 需要修复 if (/_/.test(prefix) && /^[A-Z]/.test(prefix)) { return prefix } @@ -210,10 +240,12 @@ function detectOldPrefixFromDir(outDir: string, moduleArtifactId: string): strin return null } -/** 内容 regex 兜底: 形如 `public class Ais_aisprojectInfo` (aibridge 不依赖, dacli 兼容用) */ +/** 内容 regex 兜底: 形如 `public class Jxcj_develop_projectXxx` (aibridge 不依赖, dacli 兼容用) + * 捕获完整 snake_case 前缀 (含所有 _ 分段), 后续整个前缀替换为 mainClassName */ function detectOldPrefixFromContent(content: string): string | null { - const m = content.match(/public\s+class\s+([A-Z][a-z0-9]*)_([A-Z]?[a-z]+\w*)/) - if (m) return `${m[1]}_${m[2]}` + // 匹配 `public (class|interface) Xxx_yyy_zzz` 捕获 Xxx_yyy_zzz (PascalCase首段 + snake_case 全部分段) + const m = content.match(/(?:public|@Service)\s+(?:class|interface)\s+([A-Z][a-z0-9]*(?:_[a-z0-9]+)+)/) + if (m) return m[1] return null } @@ -542,7 +574,7 @@ export function buildFormCodegenCommand(): Command { log.error(moduleErr) process.exit(2) } - const mainClassName = opts.mainClassName || snakeToPascalCase(enCode) + const mainClassName = opts.mainClassName || snakeToPascalCase(enCode, module) // 1.5. 拿 form 完整配置 (含 tables) — 给 --auto-alias 用 + 拿主表名给 description 默认值 // 用 GET /Alias/Info 拿 table 列表 (比 /Config.formData 可靠, webType=2/3 都返回)